rpm: removed .la files, and fixed library packaging
[barry/progweb.git] / src / r_hhagent.cc
blobb613cb59622264195ebda49a0f175eb17b4d7b7e
1 ///
2 /// \file r_hhagent.cc
3 /// Blackberry database record parser class for Handheld Agent records
4 ///
6 /*
7 Copyright (C) 2011, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #include "r_hhagent.h"
23 #include "record-internal.h"
24 #include "protostructs.h"
25 #include "iconv.h"
26 #include "time.h"
27 #include <iostream>
28 #include <sstream>
29 #include <iomanip>
30 #include <string>
31 #include "ios_state.h"
33 #define __DEBUG_MODE__
34 #include "debug.h"
36 using namespace std;
37 using namespace Barry::Protocol;
39 namespace Barry {
42 ///////////////////////////////////////////////////////////////////////////////
43 // HandheldAgent class
45 // Common field codes
46 #define HHAFC_END 0xffff
48 // Field codes for record 3000000
49 #define HHAFC3_MODEL 0x03
50 #define HHAFC3_BANDS 0x07
51 #define HHAFC3_VERSION 0x08
52 #define HHAFC3_NETWORK 0x0f
53 #define HHAFC3_PIN 0x10
54 #define HHAFC3_MEID 0x11
56 // Field codes for record 7000000
57 #define HHAFC7_FIRMWARE 0x13
58 #define HHAFC7_MANUFACTURER 0x14
59 #define HHAFC7_MODEL 0x15
60 #define HHAFC7_PLATFORM 0x17
62 // These fields are only valid for RecordId 0x3000000
63 static FieldLink<HandheldAgent> HandheldAgentFieldLinks_3000000[] = {
64 { HHAFC3_MODEL, "Model", 0, 0, &HandheldAgent::Model, 0, 0, 0, 0, true },
65 { HHAFC3_NETWORK, "Network", 0, 0, &HandheldAgent::Network, 0, 0, 0, 0, true },
66 { HHAFC3_BANDS, "Bands", 0, 0, &HandheldAgent::Bands, 0, 0, 0, 0, true },
67 { HHAFC3_MEID, "MEID/ESN", 0, 0, &HandheldAgent::MEID, 0, 0, 0, 0, true },
68 { HHAFC3_PIN, "PIN", 0, 0, &HandheldAgent::Pin, 0, 0, 0, 0, true },
69 { HHAFC3_VERSION, "Version",0, 0, &HandheldAgent::Version, 0, 0, 0, 0, true },
70 { HHAFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
73 // These fields are only for RecordId 0x4000000
74 static FieldLink<HandheldAgent> HandheldAgentFieldLinks_4000000[] = {
75 { HHAFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
78 // These fields are only for RecordId 0x5000000
79 static FieldLink<HandheldAgent> HandheldAgentFieldLinks_5000000[] = {
80 { HHAFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
83 // These fields are only for RecordId 0x7000000
84 static FieldLink<HandheldAgent> HandheldAgentFieldLinks_7000000[] = {
85 { HHAFC7_MODEL, "Model", 0, 0, &HandheldAgent::Model, 0, 0, 0, 0, true },
86 { HHAFC7_MANUFACTURER,"Manufacturer",0,0,&HandheldAgent::Manufacturer,0, 0, 0, 0, true },
87 { HHAFC7_FIRMWARE, "Firmware", 0, 0, &HandheldAgent::Version, 0, 0, 0, 0, true },
88 { HHAFC7_PLATFORM, "Platform", 0, 0, &HandheldAgent::PlatformVersion, 0, 0, 0, 0, true },
89 { HHAFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
92 // Use this table for default application style records
93 static FieldLink<HandheldAgent> HandheldAgentFieldLinks_Default[] = {
94 { HHAFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
97 // Use this for display / Dump() etc... includes all fields
98 static FieldLink<HandheldAgent> HandheldAgentFieldLinks_All[] = {
99 { 0, "Model", 0, 0, &HandheldAgent::Model, 0, 0, 0, 0, true },
100 { 0, "Network", 0, 0, &HandheldAgent::Network, 0, 0, 0, 0, true },
101 { 0, "Manufacturer",0,0, &HandheldAgent::Manufacturer,0, 0, 0, 0, true },
102 { 0, "Bands", 0, 0, &HandheldAgent::Bands, 0, 0, 0, 0, true },
103 { 0, "MEID/ESN", 0, 0, &HandheldAgent::MEID, 0, 0, 0, 0, true },
104 { 0, "PIN", 0, 0, &HandheldAgent::Pin, 0, 0, 0, 0, true },
105 { 0, "Version", 0, 0, &HandheldAgent::Version, 0, 0, 0, 0, true },
106 { 0, "Platform", 0, 0, &HandheldAgent::PlatformVersion, 0, 0, 0, 0, true },
107 { HHAFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
110 HandheldAgent::HandheldAgent()
112 Clear();
115 HandheldAgent::~HandheldAgent()
119 const unsigned char* HandheldAgent::ParseField(const unsigned char *begin,
120 const unsigned char *end,
121 const IConverter *ic)
123 const CommonField *field = (const CommonField *) begin;
125 // advance and check size
126 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
127 if( begin > end ) // if begin==end, we are ok
128 return begin;
130 if( !btohs(field->size) ) // if field has no size, something's up
131 return begin;
133 // cycle through the type table
134 FieldLink<HandheldAgent> *b = HandheldAgentFieldLinks_Default;
135 if( RecordId == 0 ) {
136 // internal consistency check... all parsing code should
137 // call SetIds() first, and HandheldAgent relies on this,
138 // so double check, and throw if not
139 throw std::logic_error("HandheldAgent requires SetIds() to be called before ParseField()");
141 else if( RecordId == GetMEIDRecordId() ) {
142 b = HandheldAgentFieldLinks_3000000;
144 else if( RecordId == GetUnknown1RecordId() ) {
145 b = HandheldAgentFieldLinks_4000000;
147 else if( RecordId == GetUnknown2RecordId() ) {
148 b = HandheldAgentFieldLinks_5000000;
150 else if( RecordId == GetUnknown3RecordId() ) {
151 b = HandheldAgentFieldLinks_7000000;
154 for( ; b->type != HHAFC_END; b++ ) {
155 if( b->type == field->type ) {
156 if( b->strMember ) {
157 std::string &s = this->*(b->strMember);
158 s = ParseFieldString(field);
159 if( b->iconvNeeded && ic )
160 s = ic->FromBB(s);
161 return begin; // done!
163 else if( b->timeMember && btohs(field->size) == 4 ) {
164 time_t &t = this->*(b->timeMember);
165 dout("min1900: " << field->u.min1900);
166 t = min2time(field->u.min1900);
167 return begin;
169 else if( b->addrMember ) {
171 // parse email address
172 // get dual addr+name string first
173 // Note: this is a different format than
174 // used in r_message*.cc
176 std::string dual((const char*)field->u.raw, btohs(field->size));
178 EmailAddress a;
180 // assign first string, using null terminator
181 // letting std::string add it for us if it
182 // doesn't exist
183 a.Email = dual.c_str();
185 // assign second string, using first size
186 // as starting point
187 a.Name = dual.c_str() + a.Email.size() + 1;
189 // if the address is non-empty, add to list
190 if( a.size() ) {
191 // i18n convert if needed
192 if( b->iconvNeeded && ic ) {
193 a.Name = ic->FromBB(a.Name);
194 a.Email = ic->FromBB(a.Email);
197 EmailAddressList &al = this->*(b->addrMember);
198 al.push_back(a);
201 return begin;
206 // handle special cases
207 // switch( field->type )
208 // {
209 // }
211 // if still not handled, add to the Unknowns list
212 UnknownField uf;
213 uf.type = field->type;
214 uf.data.assign((const char*)field->u.raw, btohs(field->size));
215 Unknowns.push_back(uf);
217 // return new pointer for next field
218 return begin;
221 void HandheldAgent::ParseHeader(const Data &data, size_t &offset)
223 // no header in HandheldAgent records
226 void HandheldAgent::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
228 const unsigned char *finish = ParseCommonFields(*this,
229 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
230 offset += finish - (data.GetData() + offset);
233 void HandheldAgent::BuildHeader(Data &data, size_t &offset) const
235 // no header in HandheldAgent records
239 // Build
241 /// Build fields part of record.
243 void HandheldAgent::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
247 void HandheldAgent::Clear()
249 // clear our fields
250 RecType = GetDefaultRecType();
251 RecordId = 0;
253 MEID.clear();
254 Model.clear();
255 Bands.clear();
256 Pin.clear();
257 Version.clear();
258 PlatformVersion.clear();
259 Manufacturer.clear();
260 Network.clear();
262 Unknowns.clear();
265 std::string HandheldAgent::GetDescription() const
267 ostringstream oss;
268 oss << "Handheld Agent: 0x" << hex << RecordId;
269 return oss.str();
272 void HandheldAgent::Dump(std::ostream &os) const
274 ios_format_state state(os);
276 os << "HandheldAgent entry: 0x" << hex << RecordId
277 << " (" << (unsigned int)RecType << ")\n";
279 // cycle through the type table
280 for( const FieldLink<HandheldAgent> *b = HandheldAgentFieldLinks_All;
281 b->type != HHAFC_END;
282 b++ )
284 if( b->strMember ) {
285 const std::string &s = this->*(b->strMember);
286 if( s.size() )
287 os << " " << b->name << ": " << s << "\n";
289 else if( b->timeMember ) {
290 time_t t = this->*(b->timeMember);
291 if( t > 0 )
292 os << " " << b->name << ": " << ctime(&t);
293 else
294 os << " " << b->name << ": disabled\n";
296 else if( b->addrMember ) {
297 const EmailAddressList &al = this->*(b->addrMember);
298 EmailAddressList::const_iterator lb = al.begin(), le = al.end();
300 for( ; lb != le; ++lb ) {
301 if( !lb->size() )
302 continue;
304 os << " " << b->name << ": " << *lb << "\n";
309 // print any unknowns
310 os << Unknowns;
313 bool HandheldAgent::operator<(const HandheldAgent &other) const
315 return RecordId < other.RecordId;
318 bool HandheldAgent::IsSpecial(uint32_t record_id)
320 return
321 record_id == GetMEIDRecordId() ||
322 record_id == GetUnknown1RecordId() ||
323 record_id == GetUnknown2RecordId() ||
324 record_id == GetUnknown3RecordId();
328 // The ESN number is in two parts. When in decimal, the first 3
329 // characters are one number, then the rest. In hex, the first 2
330 // digits are the same number, then the rest. Both are padded
331 // with zeros.
333 // For example, hex: 4c070068 dec: 07600458856
334 // hex: [4c]070068 dec: [076]00458856
336 // Returns an empty string on error.
339 bool HandheldAgent::IsESNHex(const std::string &esn)
341 const char *hex = "0123456789ABCDEFabcdef";
342 size_t npos = string::npos;
344 if( esn.size() == 8 && esn.find_first_not_of(hex) == npos ) {
345 return true;
348 return false;
351 bool HandheldAgent::IsESNDec(const std::string &esn)
353 const char *dec = "0123456789";
354 size_t npos = string::npos;
356 if( esn.size() == 11 && esn.find_first_not_of(dec) == npos ) {
357 return true;
360 return false;
363 std::string HandheldAgent::ESNDec2Hex(const std::string &esn)
365 string empty;
367 if( esn.size() != 11 )
368 return empty;
370 unsigned int part1, part2;
371 istringstream iss(esn.substr(0, 3));
372 iss >> dec >> part1;
373 if( !iss )
374 return empty;
375 iss.str(esn.substr(3));
376 iss.clear();
377 iss >> dec >> part2;
378 if( !iss )
379 return empty;
381 ostringstream oss;
382 oss << setfill('0') << setw(2) << hex << part1;
383 oss << setfill('0') << setw(6) << hex << part2;
384 if( !oss )
385 return empty;
386 return oss.str();
389 std::string HandheldAgent::ESNHex2Dec(const std::string &esn)
391 string empty;
393 if( esn.size() != 8 )
394 return empty;
396 unsigned int part1, part2;
397 istringstream iss(esn.substr(0, 2));
398 iss >> hex >> part1;
399 if( !iss )
400 return empty;
401 iss.str(esn.substr(2));
402 iss.clear();
403 iss >> hex >> part2;
404 if( !iss )
405 return empty;
407 ostringstream oss;
408 oss << setfill('0') << setw(3) << dec << part1;
409 oss << setfill('0') << setw(8) << dec << part2;
410 if( !oss )
411 return empty;
412 return oss.str();
415 } // namespace Barry