Fixed virtual function mis-name bug in btool's null parser
[barry/pauldeden.git] / tools / brecsum.cc
blob286543224005e6d1c3f023b08aa12a892901b836
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, 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>
30 using namespace std;
31 using namespace Barry;
33 void Usage()
35 int major, minor;
36 const char *Version = Barry::Version(major, minor);
38 cerr
39 << "brecsum - Generate SHA1 sums of raw Blackberry database records.\n"
40 << " Copyright 2008, Net Direct Inc. (http://www.netdirect.ca/)\n"
41 << " Using: " << Version << "\n"
42 << "\n"
43 << " -d db Read database 'db' and sum all its records.\n"
44 << " Can be used multiple times to fetch more than one DB\n"
45 << " -h This help\n"
46 << " -p pin PIN of device to talk with\n"
47 << " If only one device is plugged in, this flag is optional\n"
48 << " -P pass Simplistic method to specify device password\n"
49 << " -v Dump protocol data during operation\n"
50 << endl;
53 class ChecksumParser : public Barry::Parser
55 public:
56 virtual void ParseFields(const Barry::Data &data, size_t &offset)
58 unsigned char sha1[SHA_DIGEST_LENGTH];
59 SHA1(data.GetData() + offset, data.GetSize() - offset, sha1);
61 for( int i = 0; i < SHA_DIGEST_LENGTH; i++ ) {
62 cout << hex << setfill('0') << setw(2)
63 << (unsigned int) sha1[i];
65 cout << endl;
69 int main(int argc, char *argv[])
71 cout.sync_with_stdio(true); // leave this on, since libusb uses
72 // stdio for debug messages
74 try {
76 uint32_t pin = 0;
77 bool
78 data_dump = false;
79 string password;
80 vector<string> dbNames;
82 // process command line options
83 for(;;) {
84 int cmd = getopt(argc, argv, "d:hp:P:v");
85 if( cmd == -1 )
86 break;
88 switch( cmd )
90 case 'd': // show dbname
91 dbNames.push_back(string(optarg));
92 break;
94 case 'p': // Blackberry PIN
95 pin = strtoul(optarg, NULL, 16);
96 break;
98 case 'P': // Device password
99 password = optarg;
100 break;
102 case 'v': // data dump on
103 data_dump = true;
104 break;
106 case 'h': // help
107 default:
108 Usage();
109 return 0;
113 // Display usage info if user appears confused
114 if( !dbNames.size() ) {
115 Usage();
116 return 0;
119 // Initialize the barry library. Must be called before
120 // anything else.
121 Barry::Init(data_dump);
123 // Probe the USB bus for Blackberry devices and display.
124 Barry::Probe probe;
125 int activeDevice = probe.FindActive(pin);
126 if( activeDevice == -1 ) {
127 cerr << "No device selected, or PIN not found" << endl;
128 return 1;
131 // Create our controller object
132 Barry::Controller con(probe.Get(activeDevice));
133 Barry::Mode::Desktop desktop(con);
135 // Sum all specified databases
136 if( dbNames.size() ) {
137 vector<string>::iterator b = dbNames.begin();
138 ChecksumParser parser;
140 desktop.Open(password.c_str());
141 for( ; b != dbNames.end(); b++ ) {
142 unsigned int id = desktop.GetDBID(*b);
143 desktop.LoadDatabase(id, parser);
148 catch( std::exception &e ) {
149 std::cerr << e.what() << endl;
150 return 1;
153 return 0;