lib: fixed DeviceParser, to inherit from Parser (oops)
[barry.git] / tools / brecsum.cc
blob718d8d689944a8e22a8e6fccd618008b285ab370
1 ///
2 /// \file brecsum.cc
3 /// Generate SHA1 sums of raw Blackberry database records.
4 /// This is mostly useful for data verification during testing.
5 ///
7 /*
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>
24 #include <iomanip>
25 #include <iostream>
26 #include <vector>
27 #include <string>
28 #include <getopt.h>
29 #include "i18n.h"
31 using namespace std;
32 using namespace Barry;
34 void Usage()
36 int major, minor;
37 const char *Version = Barry::Version(major, minor);
39 cerr
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"
43 << "\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"
46 << " -h This help\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"
52 << endl;
55 class ChecksumParser : public Barry::Parser
57 bool m_IncludeIds;
58 SHA_CTX m_ctx;
60 public:
61 explicit ChecksumParser(bool IncludeIds)
62 : m_IncludeIds(IncludeIds)
65 virtual void ParseRecord(const Barry::DBData &data,
66 const Barry::IConverter *ic)
68 SHA1_Init(&m_ctx);
70 if( m_IncludeIds ) {
71 SHA1_Update(&m_ctx, data.GetDBName().c_str(),
72 data.GetDBName().size());
74 uint8_t recType = data.GetRecType();
75 SHA1_Update(&m_ctx, &recType, sizeof(recType));
77 uint32_t uniqueId = data.GetUniqueId();
78 SHA1_Update(&m_ctx, &uniqueId, sizeof(uniqueId));
81 int len = data.GetData().GetSize() - data.GetOffset();
82 SHA1_Update(&m_ctx,
83 data.GetData().GetData() + data.GetOffset(), len);
85 unsigned char sha1[SHA_DIGEST_LENGTH];
86 SHA1_Final(sha1, &m_ctx);
88 for( int i = 0; i < SHA_DIGEST_LENGTH; i++ ) {
89 cout << hex << setfill('0') << setw(2)
90 << (unsigned int) sha1[i];
92 cout << endl;
96 int main(int argc, char *argv[])
98 INIT_I18N(PACKAGE);
100 cout.sync_with_stdio(true); // leave this on, since libusb uses
101 // stdio for debug messages
103 try {
105 uint32_t pin = 0;
106 bool
107 data_dump = false,
108 include_ids = false;
109 string password;
110 vector<string> dbNames;
112 // process command line options
113 for(;;) {
114 int cmd = getopt(argc, argv, "d:hip:P:v");
115 if( cmd == -1 )
116 break;
118 switch( cmd )
120 case 'd': // show dbname
121 dbNames.push_back(string(optarg));
122 break;
124 case 'i': // Include IDs
125 include_ids = true;
126 break;
128 case 'p': // Blackberry PIN
129 pin = strtoul(optarg, NULL, 16);
130 break;
132 case 'P': // Device password
133 password = optarg;
134 break;
136 case 'v': // data dump on
137 data_dump = true;
138 break;
140 case 'h': // help
141 default:
142 Usage();
143 return 0;
147 // Display usage info if user appears confused
148 if( !dbNames.size() ) {
149 Usage();
150 return 0;
153 // Initialize the barry library. Must be called before
154 // anything else.
155 Barry::Init(data_dump);
157 // Probe the USB bus for Blackberry devices and display.
158 Barry::Probe probe;
159 int activeDevice = probe.FindActive(pin);
160 if( activeDevice == -1 ) {
161 cerr << "No device selected, or PIN not found" << endl;
162 return 1;
165 // Create our controller object
166 Barry::Controller con(probe.Get(activeDevice));
167 Barry::Mode::Desktop desktop(con);
169 // Sum all specified databases
170 if( dbNames.size() ) {
171 vector<string>::iterator b = dbNames.begin();
172 ChecksumParser parser(include_ids);
174 desktop.Open(password.c_str());
175 for( ; b != dbNames.end(); b++ ) {
176 unsigned int id = desktop.GetDBID(*b);
177 desktop.LoadDatabase(id, parser);
182 catch( std::exception &e ) {
183 std::cerr << e.what() << endl;
184 return 1;
187 return 0;