Reorganized sample ppp options files
[barry/pauldeden.git] / tools / brecsum.cc
blobd2ee0f02a362e60331b3b3f9162b70b7b9cc3d2d
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 <openssl/sha.h>
25 #include <iomanip>
26 #include <iostream>
27 #include <vector>
28 #include <string>
29 #include <getopt.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, 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 << " -p pin PIN of device to talk with\n"
48 << " If only one device is plugged in, this flag is optional\n"
49 << " -P pass Simplistic method to specify device password\n"
50 << " -v Dump protocol data during operation\n"
51 << endl;
54 class ChecksumParser : public Barry::Parser
56 public:
57 virtual void ParseFields(const Barry::Data &data, size_t &offset)
59 unsigned char sha1[SHA_DIGEST_LENGTH];
60 SHA1(data.GetData() + offset, data.GetSize() - offset, sha1);
62 for( int i = 0; i < SHA_DIGEST_LENGTH; i++ ) {
63 cout << hex << setfill('0') << setw(2)
64 << (unsigned int) sha1[i];
66 cout << endl;
70 int main(int argc, char *argv[])
72 cout.sync_with_stdio(true); // leave this on, since libusb uses
73 // stdio for debug messages
75 try {
77 uint32_t pin = 0;
78 bool
79 data_dump = false;
80 string password;
81 vector<string> dbNames;
83 // process command line options
84 for(;;) {
85 int cmd = getopt(argc, argv, "d:hp:P:v");
86 if( cmd == -1 )
87 break;
89 switch( cmd )
91 case 'd': // show dbname
92 dbNames.push_back(string(optarg));
93 break;
95 case 'p': // Blackberry PIN
96 pin = strtoul(optarg, NULL, 16);
97 break;
99 case 'P': // Device password
100 password = optarg;
101 break;
103 case 'v': // data dump on
104 data_dump = true;
105 break;
107 case 'h': // help
108 default:
109 Usage();
110 return 0;
114 // Display usage info if user appears confused
115 if( !dbNames.size() ) {
116 Usage();
117 return 0;
120 // Initialize the barry library. Must be called before
121 // anything else.
122 Barry::Init(data_dump);
124 // Probe the USB bus for Blackberry devices and display.
125 Barry::Probe probe;
126 int activeDevice = probe.FindActive(pin);
127 if( activeDevice == -1 ) {
128 cerr << "No device selected, or PIN not found" << endl;
129 return 1;
132 // Create our controller object
133 Barry::Controller con(probe.Get(activeDevice));
134 Barry::Mode::Desktop desktop(con);
136 // Sum all specified databases
137 if( dbNames.size() ) {
138 vector<string>::iterator b = dbNames.begin();
139 ChecksumParser parser;
141 desktop.Open(password.c_str());
142 for( ; b != dbNames.end(); b++ ) {
143 unsigned int id = desktop.GetDBID(*b);
144 desktop.LoadDatabase(id, parser);
149 catch( std::exception &e ) {
150 std::cerr << e.what() << endl;
151 return 1;
154 return 0;