3 /// Generate SHA1 sums of raw Blackberry database records.
4 /// This is mostly useful for data verification during testing.
8 Copyright (C) 2008-2010, Net Direct Inc. (http://www.netdirect.ca/)
10 This program is free software; you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation; either version 2 of the License, or
13 (at your option) any later version.
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 See the GNU General Public License in the COPYING file at the
20 root directory of this project for more details.
23 #include <barry/barry.h>
32 using namespace Barry
;
37 const char *Version
= Barry::Version(major
, minor
);
40 << "brecsum - Generate SHA1 sums of raw Blackberry database records.\n"
41 << " Copyright 2008-2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
42 << " Using: " << Version
<< "\n"
44 << " -d db Read database 'db' and sum all its records.\n"
45 << " Can be used multiple times to fetch more than one DB\n"
47 << " -i Include DB Name, Type, and Unique record IDs in the checksums\n"
48 << " -p pin PIN of device to talk with\n"
49 << " If only one device is plugged in, this flag is optional\n"
50 << " -P pass Simplistic method to specify device password\n"
51 << " -v Dump protocol data during operation\n"
55 class ChecksumParser
: public Barry::Parser
61 explicit ChecksumParser(bool IncludeIds
)
62 : m_IncludeIds(IncludeIds
)
70 virtual void SetIds(const std::string
&DbName
,
71 uint8_t RecType
, uint32_t UniqueId
)
74 SHA1_Update(&m_ctx
, DbName
.c_str(), DbName
.size());
75 SHA1_Update(&m_ctx
, &RecType
, sizeof(RecType
));
76 SHA1_Update(&m_ctx
, &UniqueId
, sizeof(UniqueId
));
80 virtual void ParseHeader(const Barry::Data
&, size_t &)
82 // do nothing here, parse it all at once in ParseFields
85 virtual void ParseFields(const Barry::Data
&data
, size_t &offset
,
86 const Barry::IConverter
*ic
)
88 int len
= data
.GetSize() - offset
;
89 SHA1_Update(&m_ctx
, data
.GetData() + offset
, len
);
95 unsigned char sha1
[SHA_DIGEST_LENGTH
];
96 SHA1_Final(sha1
, &m_ctx
);
98 for( int i
= 0; i
< SHA_DIGEST_LENGTH
; i
++ ) {
99 cout
<< hex
<< setfill('0') << setw(2)
100 << (unsigned int) sha1
[i
];
106 int main(int argc
, char *argv
[])
110 cout
.sync_with_stdio(true); // leave this on, since libusb uses
111 // stdio for debug messages
120 vector
<string
> dbNames
;
122 // process command line options
124 int cmd
= getopt(argc
, argv
, "d:hip:P:v");
130 case 'd': // show dbname
131 dbNames
.push_back(string(optarg
));
134 case 'i': // Include IDs
138 case 'p': // Blackberry PIN
139 pin
= strtoul(optarg
, NULL
, 16);
142 case 'P': // Device password
146 case 'v': // data dump on
157 // Display usage info if user appears confused
158 if( !dbNames
.size() ) {
163 // Initialize the barry library. Must be called before
165 Barry::Init(data_dump
);
167 // Probe the USB bus for Blackberry devices and display.
169 int activeDevice
= probe
.FindActive(pin
);
170 if( activeDevice
== -1 ) {
171 cerr
<< "No device selected, or PIN not found" << endl
;
175 // Create our controller object
176 Barry::Controller
con(probe
.Get(activeDevice
));
177 Barry::Mode::Desktop
desktop(con
);
179 // Sum all specified databases
180 if( dbNames
.size() ) {
181 vector
<string
>::iterator b
= dbNames
.begin();
182 ChecksumParser
parser(include_ids
);
184 desktop
.Open(password
.c_str());
185 for( ; b
!= dbNames
.end(); b
++ ) {
186 unsigned int id
= desktop
.GetDBID(*b
);
187 desktop
.LoadDatabase(id
, parser
);
192 catch( std::exception
&e
) {
193 std::cerr
<< e
.what() << endl
;