doc: added link to Nicolas Vivien's development blog
[barry.git] / tools / brecsum.cc
blob8d21c47bedf8c8c98747447e48782cb7936ad9b8
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"
30 #include "brecsum.h"
32 using namespace std;
33 using namespace Barry;
35 void Usage()
37 int major, minor;
38 const char *Version = Barry::Version(major, minor);
40 cerr
41 << "brecsum - Generate SHA1 sums of raw Blackberry database records.\n"
42 << " Copyright 2008-2010, Net Direct Inc. (http://www.netdirect.ca/)\n"
43 << " Using: " << Version << "\n"
44 << "\n"
45 << " -d db Read database 'db' and sum all its records.\n"
46 << " Can be used multiple times to fetch more than one DB\n"
47 << " -h This help\n"
48 << " -i Include DB Name, Type, and Unique record IDs in the checksums\n"
49 << " -p pin PIN of device to talk with\n"
50 << " If only one device is plugged in, this flag is optional\n"
51 << " -P pass Simplistic method to specify device password\n"
52 << " -v Dump protocol data during operation\n"
53 << endl;
56 int main(int argc, char *argv[])
58 INIT_I18N(PACKAGE);
60 cout.sync_with_stdio(true); // leave this on, since libusb uses
61 // stdio for debug messages
63 try {
65 uint32_t pin = 0;
66 bool
67 data_dump = false,
68 include_ids = false;
69 string password;
70 vector<string> dbNames;
72 // process command line options
73 for(;;) {
74 int cmd = getopt(argc, argv, "d:hip:P:v");
75 if( cmd == -1 )
76 break;
78 switch( cmd )
80 case 'd': // show dbname
81 dbNames.push_back(string(optarg));
82 break;
84 case 'i': // Include IDs
85 include_ids = true;
86 break;
88 case 'p': // Blackberry PIN
89 pin = strtoul(optarg, NULL, 16);
90 break;
92 case 'P': // Device password
93 password = optarg;
94 break;
96 case 'v': // data dump on
97 data_dump = true;
98 break;
100 case 'h': // help
101 default:
102 Usage();
103 return 0;
107 // Display usage info if user appears confused
108 if( !dbNames.size() ) {
109 Usage();
110 return 0;
113 // Initialize the barry library. Must be called before
114 // anything else.
115 Barry::Init(data_dump);
117 // Probe the USB bus for Blackberry devices and display.
118 Barry::Probe probe;
119 int activeDevice = probe.FindActive(pin);
120 if( activeDevice == -1 ) {
121 cerr << "No device selected, or PIN not found" << endl;
122 return 1;
125 // Create our controller object
126 Barry::Controller con(probe.Get(activeDevice));
127 Barry::Mode::Desktop desktop(con);
129 // Sum all specified databases
130 if( dbNames.size() ) {
131 vector<string>::iterator b = dbNames.begin();
132 ChecksumParser parser(include_ids);
134 desktop.Open(password.c_str());
135 for( ; b != dbNames.end(); b++ ) {
136 unsigned int id = desktop.GetDBID(*b);
137 desktop.LoadDatabase(id, parser);
142 catch( std::exception &e ) {
143 std::cerr << e.what() << endl;
144 return 1;
147 return 0;