updated change log to reflect reality
[barry.git] / src / record.cc
blob525df8cf6d8f0b18643d1f98d513eff6405b9c3d
1 ///
2 /// \file record.cc
3 /// Blackberry database record classes. Help translate data
4 /// from data packets to useful structurs, and back.
5 /// This header provides the common types and classes
6 /// used by the general record parser classes in the
7 /// r_*.h files. Only application-safe API stuff goes in
8 /// here. Internal library types go in record-internal.h
9 ///
12 Copyright (C) 2005-2007, Net Direct Inc. (http://www.netdirect.ca/)
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 2 of the License, or
17 (at your option) any later version.
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
23 See the GNU General Public License in the COPYING file at the
24 root directory of this project for more details.
27 #include "record.h"
28 #include "record-internal.h"
29 #include "protocol.h"
30 #include "protostructs.h"
31 #include "data.h"
32 #include "time.h"
33 #include "error.h"
34 #include "endian.h"
35 #include <ostream>
36 #include <iomanip>
37 #include <time.h>
38 #include <stdexcept>
40 #define __DEBUG_MODE__
41 #include "debug.h"
43 using namespace std;
44 using namespace Barry::Protocol;
46 namespace Barry {
48 //////////////////////////////////////////////////////////////////////////////
49 // Field builder helper functions
51 void BuildField1900(Data &data, size_t &size, uint8_t type, time_t t)
53 size_t timesize = COMMON_FIELD_MIN1900_SIZE;
54 size_t fieldsize = COMMON_FIELD_HEADER_SIZE + timesize;
55 unsigned char *pd = data.GetBuffer(size + fieldsize) + size;
56 CommonField *field = (CommonField *) pd;
58 field->size = htobs(timesize);
59 field->type = type;
60 field->u.min1900 = time2min(t);
62 size += fieldsize;
65 void BuildField(Data &data, size_t &size, uint8_t type, char c)
67 size_t strsize = 1;
68 size_t fieldsize = COMMON_FIELD_HEADER_SIZE + strsize;
69 unsigned char *pd = data.GetBuffer(size + fieldsize) + size;
70 CommonField *field = (CommonField *) pd;
72 field->size = htobs(strsize);
73 field->type = type;
74 memcpy(field->u.raw, &c, strsize);
76 size += fieldsize;
79 void BuildField(Data &data, size_t &size, uint8_t type, const std::string &str)
81 // include null terminator
82 BuildField(data, size, type, str.c_str(), str.size() + 1);
85 void BuildField(Data &data, size_t &size, uint8_t type,
86 const void *buf, size_t bufsize)
88 // include null terminator
89 size_t fieldsize = COMMON_FIELD_HEADER_SIZE + bufsize;
90 unsigned char *pd = data.GetBuffer(size + fieldsize) + size;
91 CommonField *field = (CommonField *) pd;
93 field->size = htobs(bufsize);
94 field->type = type;
95 memcpy(field->u.raw, buf, bufsize);
97 size += fieldsize;
100 void BuildField(Data &data, size_t &size, uint8_t type, const Barry::Protocol::GroupLink &link)
102 size_t linksize = sizeof(Barry::Protocol::GroupLink);
103 size_t fieldsize = COMMON_FIELD_HEADER_SIZE + linksize;
104 unsigned char *pd = data.GetBuffer(size + fieldsize) + size;
105 CommonField *field = (CommonField *) pd;
107 field->size = htobs(linksize);
108 field->type = type;
109 field->u.link = link;
111 size += fieldsize;
114 std::string ParseFieldString(const Barry::Protocol::CommonField *field)
116 // make no assumptions here, and pass the full size in as
117 // the maxlen, even though 99% of the time, it will be a null...
118 // this function can be used by non-null terminated strings as well
119 return ParseFieldString(field->u.raw, btohs(field->size));
122 std::string ParseFieldString(const void *data, uint16_t maxlen)
124 const char *str = (const char *)data;
126 // find last non-null character, since some fields
127 // can have multiple null terminators
128 while( maxlen && str[maxlen-1] == 0 )
129 maxlen--;
131 return std::string(str, maxlen);
135 ///////////////////////////////////////////////////////////////////////////////
136 // CommandTable class
138 CommandTable::CommandTable()
142 CommandTable::~CommandTable()
146 const unsigned char* CommandTable::ParseField(const unsigned char *begin,
147 const unsigned char *end)
149 // check if there is enough data for a header
150 const unsigned char *headend = begin + sizeof(CommandTableField);
151 if( headend > end )
152 return headend;
154 const CommandTableField *field = (const CommandTableField *) begin;
156 // advance and check size
157 begin += COMMAND_FIELD_HEADER_SIZE + field->size; // size is byte
158 if( begin > end ) // if begin==end, we are ok
159 return begin;
161 if( !field->size ) // if field has no size, something's up
162 return begin;
164 Command command;
165 command.Code = field->code;
166 command.Name.assign((const char *)field->name, field->size);
167 Commands.push_back(command);
168 return begin;
171 void CommandTable::Parse(const Data &data, size_t offset)
173 if( offset >= data.GetSize() )
174 return;
176 const unsigned char *begin = data.GetData() + offset;
177 const unsigned char *end = data.GetData() + data.GetSize();
179 while( begin < end )
180 begin = ParseField(begin, end);
183 void CommandTable::Clear()
185 Commands.clear();
188 unsigned int CommandTable::GetCommand(const std::string &name) const
190 CommandArrayType::const_iterator b = Commands.begin();
191 for( ; b != Commands.end(); b++ )
192 if( b->Name == name )
193 return b->Code;
194 return 0;
197 void CommandTable::Dump(std::ostream &os) const
199 CommandArrayType::const_iterator b = Commands.begin();
200 os << "Command table:\n";
201 for( ; b != Commands.end(); b++ ) {
202 os << " Command: 0x" << setbase(16) << b->Code
203 << " '" << b->Name << "'\n";
209 ///////////////////////////////////////////////////////////////////////////////
210 // RecordStateTable class
212 RecordStateTable::RecordStateTable()
213 : m_LastNewRecordId(1)
217 RecordStateTable::~RecordStateTable()
221 const unsigned char* RecordStateTable::ParseField(const unsigned char *begin,
222 const unsigned char *end)
224 const RecordStateTableField *field = (const RecordStateTableField *) begin;
226 // advance and check size
227 begin += sizeof(RecordStateTableField);
228 if( begin > end ) // if begin==end, we are ok
229 return begin;
231 State state;
232 state.Index = btohs(field->index);
233 state.RecordId = btohl(field->uniqueId);
234 state.Dirty = (field->flags & BARRY_RSTF_DIRTY) != 0;
235 state.RecType = field->rectype;
236 state.Unknown2.assign((const char*)field->unknown2, sizeof(field->unknown2));
237 StateMap[state.Index] = state;
239 return begin;
242 void RecordStateTable::Parse(const Data &data)
244 size_t offset = 12; // skipping the unknown 2 bytes at start
246 if( offset >= data.GetSize() )
247 return;
249 const unsigned char *begin = data.GetData() + offset;
250 const unsigned char *end = data.GetData() + data.GetSize();
252 while( begin < end )
253 begin = ParseField(begin, end);
256 void RecordStateTable::Clear()
258 StateMap.clear();
259 m_LastNewRecordId = 1;
262 // Searches the StateMap table for RecordId, and returns the "index"
263 // in the map if found. Returns true if found, false if not.
264 // pFoundIndex can be null if only the existence of the index is desired
265 bool RecordStateTable::GetIndex(uint32_t RecordId, IndexType *pFoundIndex) const
267 StateMapType::const_iterator i = StateMap.begin();
268 for( ; i != StateMap.end(); ++i ) {
269 if( i->second.RecordId == RecordId ) {
270 if( pFoundIndex )
271 *pFoundIndex = i->first;
272 return true;
275 return false;
278 // Generate a new RecordId that is not in the state table.
279 // Starts at 1 and keeps incrementing until a free one is found.
280 uint32_t RecordStateTable::MakeNewRecordId() const
282 // start with next Id
283 m_LastNewRecordId++;
285 // make sure it doesn't already exist
286 StateMapType::const_iterator i = StateMap.begin();
287 while( i != StateMap.end() ) {
288 if( m_LastNewRecordId == i->second.RecordId ) {
289 m_LastNewRecordId++; // try again
290 i = StateMap.begin(); // start over
292 else {
293 ++i; // next State
296 return m_LastNewRecordId;
299 void RecordStateTable::Dump(std::ostream &os) const
301 ios::fmtflags oldflags = os.setf(ios::right);
302 char fill = os.fill(' ');
303 bool bPrintAscii = Data::PrintAscii();
304 Data::PrintAscii(false);
306 os << " Index RecordId Dirty RecType" << endl;
307 os << "------- ---------- ----- -------" << endl;
309 StateMapType::const_iterator b, e = StateMap.end();
310 for( b = StateMap.begin(); b != e ; ++b ) {
311 const State &state = b->second;
313 os.fill(' ');
314 os << setbase(10) << setw(7) << state.Index;
315 os << " 0x" << setbase(16) << setfill('0') << setw(8) << state.RecordId;
316 os << " " << setfill(' ') << setw(5) << (state.Dirty ? "yes" : "no");
317 os << " 0x" << setbase(16) << setfill('0') << setw(2) << state.RecType;
318 os << " " << Data(state.Unknown2.data(), state.Unknown2.size());
321 // cleanup the stream
322 os.flags(oldflags);
323 os.fill(fill);
324 Data::PrintAscii(bPrintAscii);
329 ///////////////////////////////////////////////////////////////////////////////
330 // DatabaseDatabase class
332 DatabaseDatabase::DatabaseDatabase()
336 DatabaseDatabase::~DatabaseDatabase()
340 template <class RecordType, class FieldType>
341 void DatabaseDatabase::ParseRec(const RecordType &rec, const unsigned char *end)
345 template <class FieldType>
346 const unsigned char* DatabaseDatabase::ParseField(const unsigned char *begin,
347 const unsigned char *end)
349 // check if there is enough data for a header
350 const unsigned char *headend = begin + sizeof(FieldType);
351 if( headend > end )
352 return headend;
354 // get our header
355 const FieldType *field = (const FieldType *) begin;
357 // advance and check size
358 begin += sizeof(FieldType) - sizeof(field->name) + ConvertHtoB(field->nameSize);
359 if( begin > end ) // if begin==end, we are ok
360 return begin;
362 if( !ConvertHtoB(field->nameSize) ) // if field has no size, something's up
363 return begin;
365 Database db;
366 db.Number = ConvertHtoB(field->dbNumber);
367 db.RecordCount = ConvertHtoB(field->dbRecordCount);
368 db.Name.assign((const char *)field->name, ConvertHtoB(field->nameSize) - 1);
369 Databases.push_back(db);
370 return begin;
373 void DatabaseDatabase::Parse(const Data &data)
375 // check size to make sure we have up to the DBAccess operation byte
376 if( data.GetSize() < (SB_PACKET_DBACCESS_HEADER_SIZE + 1) )
377 return;
379 MAKE_PACKET(pack, data);
380 const unsigned char *begin = 0;
381 const unsigned char *end = data.GetData() + data.GetSize();
383 switch( pack->u.db.u.response.operation )
385 case SB_DBOP_GET_DBDB:
386 // using the new protocol
387 if( data.GetSize() > SB_PACKET_DBDB_HEADER_SIZE ) {
388 begin = (const unsigned char *)
389 &pack->u.db.u.response.u.dbdb.field[0];
391 // this while check is ok, since ParseField checks
392 // for header size
393 while( begin < end )
394 begin = ParseField<DBDBField>(begin, end);
396 else
397 dout("DatabaseDatabase: not enough data for parsing");
398 break;
400 case SB_DBOP_OLD_GET_DBDB:
401 // using the old protocol
402 if( data.GetSize() > SB_PACKET_OLD_DBDB_HEADER_SIZE ) {
403 begin = (const unsigned char *)
404 &pack->u.db.u.response.u.old_dbdb.field[0];
406 // this while check is ok, since ParseField checks
407 // for header size
408 while( begin < end )
409 begin = ParseField<OldDBDBField>(begin, end);
411 else
412 dout("DatabaseDatabase: not enough data for parsing");
413 break;
415 default:
416 // unknown protocol
417 dout("Unknown protocol");
418 break;
424 void DatabaseDatabase::Clear()
426 Databases.clear();
429 bool DatabaseDatabase::GetDBNumber(const std::string &name,
430 unsigned int &number) const
432 DatabaseArrayType::const_iterator b = Databases.begin();
433 for( ; b != Databases.end(); b++ )
434 if( b->Name == name ) {
435 number = b->Number;
436 return true;
438 return false;
441 bool DatabaseDatabase::GetDBName(unsigned int number,
442 std::string &name) const
444 DatabaseArrayType::const_iterator b = Databases.begin();
445 for( ; b != Databases.end(); b++ )
446 if( b->Number == number ) {
447 name = b->Name;
448 return true;
450 return false;
453 void DatabaseDatabase::Dump(std::ostream &os) const
455 DatabaseArrayType::const_iterator b = Databases.begin();
456 os << "Database database:\n";
457 for( ; b != Databases.end(); b++ ) {
458 os << " Database: 0x" << setbase(16) << b->Number
459 << " '" << b->Name << "' (records: "
460 << setbase(10) << b->RecordCount << ")\n";
465 std::ostream& operator<< (std::ostream &os, const std::vector<UnknownField> &unknowns)
467 std::vector<UnknownField>::const_iterator
468 ub = unknowns.begin(), ue = unknowns.end();
469 if( ub != ue )
470 os << " Unknowns:\n";
471 for( ; ub != ue; ub++ ) {
472 os << " Type: 0x" << setbase(16)
473 << (unsigned int) ub->type
474 << " Data:\n" << Data(ub->data.data(), ub->data.size());
476 return os;
481 ///////////////////////////////////////////////////////////////////////////////
482 // EmailAddress class
484 std::ostream& operator<<(std::ostream &os, const EmailAddress &msga) {
485 os << msga.Name.c_str() << " <" << msga.Email.c_str() << ">";
486 return os;
490 ///////////////////////////////////////////////////////////////////////////////
491 // PostalAddress class
494 // GetLabel
496 /// Format a mailing address into a single string, handling missing fields.
498 std::string PostalAddress::GetLabel() const
500 std::string address = Address1;
501 if( Address2.size() ) {
502 if( address.size() )
503 address += "\n";
504 address += Address2;
506 if( Address3.size() ) {
507 if( address.size() )
508 address += "\n";
509 address += Address3;
511 if( address.size() )
512 address += "\n";
513 if( City.size() )
514 address += City + " ";
515 if( Province.size() )
516 address += Province + " ";
517 if( Country.size() )
518 address += Country;
519 if( address.size() )
520 address += "\n";
521 if( PostalCode.size() )
522 address += PostalCode;
524 return address;
527 void PostalAddress::Clear()
529 Address1.clear();
530 Address2.clear();
531 Address3.clear();
532 City.clear();
533 Province.clear();
534 PostalCode.clear();
535 Country.clear();
538 std::ostream& operator<<(std::ostream &os, const PostalAddress &post) {
539 os << post.GetLabel();
540 return os;
545 } // namespace Barry
548 #ifdef __TEST_MODE__
550 #include <iostream>
552 int main(int argc, char *argv[])
554 if( argc < 2 ) {
555 cerr << "Usage: test <datafile>" << endl;
556 return 1;
559 std::vector<Data> array;
560 if( !LoadDataArray(argv[1], array) ) {
561 cerr << "Unable to load file: " << argv[1] << endl;
562 return 1;
565 cout << "Loaded " << array.size() << " items" << endl;
567 for( std::vector<Data>::iterator b = array.begin(), e = array.end();
568 b != e; b++ )
570 Data &d = *b;
571 // cout << d << endl;
572 if( d.GetSize() > 13 && d.GetData()[6] == 0x4f ) {
573 Barry::Contact contact;
574 size_t size = 13;
575 contact.ParseFields(d, size);
576 cout << contact << endl;
577 contact.DumpLdif(cout, "ou=People,dc=example,dc=com");
579 else if( d.GetSize() > 13 && d.GetData()[6] == 0x44 ) {
580 Barry::Calendar cal;
581 size_t size = 13;
582 cal.ParseFields(d, size);
583 cout << cal << endl;
588 #endif