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
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.
28 #include "record-internal.h"
30 #include "protostructs.h"
40 #define __DEBUG_MODE__
44 using namespace Barry::Protocol
;
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
);
60 field
->u
.min1900
= time2min(t
);
65 void BuildField(Data
&data
, size_t &size
, uint8_t type
, char c
)
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
);
74 memcpy(field
->u
.raw
, &c
, strsize
);
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
);
95 memcpy(field
->u
.raw
, buf
, bufsize
);
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
);
109 field
->u
.link
= link
;
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 )
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
);
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
161 if( !field
->size
) // if field has no size, something's up
165 command
.Code
= field
->code
;
166 command
.Name
.assign((const char *)field
->name
, field
->size
);
167 Commands
.push_back(command
);
171 void CommandTable::Parse(const Data
&data
, size_t offset
)
173 if( offset
>= data
.GetSize() )
176 const unsigned char *begin
= data
.GetData() + offset
;
177 const unsigned char *end
= data
.GetData() + data
.GetSize();
180 begin
= ParseField(begin
, end
);
183 void CommandTable::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
)
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
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
;
242 void RecordStateTable::Parse(const Data
&data
)
244 size_t offset
= 12; // skipping the unknown 2 bytes at start
246 if( offset
>= data
.GetSize() )
249 const unsigned char *begin
= data
.GetData() + offset
;
250 const unsigned char *end
= data
.GetData() + data
.GetSize();
253 begin
= ParseField(begin
, end
);
256 void RecordStateTable::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
) {
271 *pFoundIndex
= i
->first
;
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
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
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
;
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
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
);
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
362 if( !ConvertHtoB(field
->nameSize
) ) // if field has no size, something's up
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
);
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) )
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
394 begin
= ParseField
<DBDBField
>(begin
, end
);
397 dout("DatabaseDatabase: not enough data for parsing");
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
409 begin
= ParseField
<OldDBDBField
>(begin
, end
);
412 dout("DatabaseDatabase: not enough data for parsing");
417 dout("Unknown protocol");
424 void DatabaseDatabase::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
) {
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
) {
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();
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());
481 ///////////////////////////////////////////////////////////////////////////////
482 // EmailAddress class
484 std::ostream
& operator<<(std::ostream
&os
, const EmailAddress
&msga
) {
485 os
<< msga
.Name
.c_str() << " <" << msga
.Email
.c_str() << ">";
490 ///////////////////////////////////////////////////////////////////////////////
491 // PostalAddress class
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() ) {
506 if( Address3
.size() ) {
514 address
+= City
+ " ";
515 if( Province
.size() )
516 address
+= Province
+ " ";
521 if( PostalCode
.size() )
522 address
+= PostalCode
;
527 void PostalAddress::Clear()
538 std::ostream
& operator<<(std::ostream
&os
, const PostalAddress
&post
) {
539 os
<< post
.GetLabel();
552 int main(int argc
, char *argv
[])
555 cerr
<< "Usage: test <datafile>" << endl
;
559 std::vector
<Data
> array
;
560 if( !LoadDataArray(argv
[1], array
) ) {
561 cerr
<< "Unable to load file: " << argv
[1] << endl
;
565 cout
<< "Loaded " << array
.size() << " items" << endl
;
567 for( std::vector
<Data
>::iterator b
= array
.begin(), e
= array
.end();
571 // cout << d << endl;
572 if( d
.GetSize() > 13 && d
.GetData()[6] == 0x4f ) {
573 Barry::Contact contact
;
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 ) {
582 cal
.ParseFields(d
, size
);