Updating text, names and comments to be consistent and generally make a bit more...
[barry.git] / src / r_servicebook.cc
blob985bd6d1f6e3434fdc5ce6137e378e75094c0c71
1 ///
2 /// \file r_servicebook.cc
3 /// Blackberry database record parser class for
4 /// Service Book records.
5 ///
7 /*
8 Copyright (C) 2005-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 "r_servicebook.h"
24 #include "record-internal.h"
25 #include "protocol.h"
26 #include "protostructs.h"
27 #include "data.h"
28 #include "time.h"
29 #include "error.h"
30 #include "endian.h"
31 #include "iconv.h"
32 #include <ostream>
33 #include <iomanip>
34 #include <time.h>
35 #include <stdexcept>
37 #define __DEBUG_MODE__
38 #include "debug.h"
40 using namespace std;
41 using namespace Barry::Protocol;
43 namespace Barry {
45 ///////////////////////////////////////////////////////////////////////////////
46 // ServiceBookConfig class
48 // service book packed field codes
49 #define SBFCC_END 0xffff
51 static FieldLink<ServiceBookConfig> ServiceBookConfigFieldLinks[] = {
52 // { SBFC_DSID, "DSID", 0, 0, &ServiceBook::DSID, 0, 0 },
53 { SBFCC_END, "End of List",0, 0, 0, 0, 0 }
56 ServiceBookConfig::ServiceBookConfig()
57 : Format(0)
59 Clear();
62 ServiceBookConfig::~ServiceBookConfig()
66 const unsigned char* ServiceBookConfig::ParseField(const unsigned char *begin,
67 const unsigned char *end,
68 const IConverter *ic)
70 const void *raw;
71 uint16_t size, type;
73 switch( Format )
75 case 0x01:
76 case 0x02:
78 const PackedField_02 *field = (const PackedField_02 *) begin;
79 raw = field->raw;
80 size = field->size;
81 type = field->type;
82 begin += PACKED_FIELD_02_HEADER_SIZE + size;
84 break;
86 case 0x10:
88 const PackedField_10 *field = (const PackedField_10 *) begin;
89 raw = field->raw;
90 size = field->size;
91 type = field->type;
92 begin += PACKED_FIELD_10_HEADER_SIZE + size;
94 break;
96 default:
97 eout("------> Unknown packed field format: 0x" << std::hex <<
98 (unsigned int) Format);
99 throw BadPackedFormat(Format);
100 return begin + 1;
104 // check size
105 if( begin > end ) // if begin==end, we are ok
106 return begin;
108 if( !size ) // if field has no size, something's up
109 return begin;
111 // cycle through the type table
112 for( FieldLink<ServiceBookConfig> *b = ServiceBookConfigFieldLinks;
113 b->type != SBFCC_END;
114 b++ )
116 if( b->type == type ) {
117 if( b->strMember ) {
118 std::string &s = this->*(b->strMember);
119 s = ParseFieldString(raw, size-1);
120 return begin; // done!
126 // handle special cases
127 switch( type )
132 // if still not handled, add to the Unknowns list
133 UnknownField uf;
134 uf.type = type;
135 uf.data.assign((const char*)raw, size);
136 Unknowns.push_back(uf);
138 // return new pointer for next field
139 return begin;
142 void ServiceBookConfig::ParseHeader(const Data &data, size_t &offset)
144 MAKE_RECORD(const Barry::Protocol::ServiceBookConfigField, sbc, data, offset);
145 offset += SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE;
146 if( data.GetSize() >= offset ) { // size check!
147 Format = sbc->format;
151 void ServiceBookConfig::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
153 const unsigned char *finish = ParseCommonFields(*this,
154 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
155 offset += finish - (data.GetData() + offset);
158 void ServiceBookConfig::BuildHeader(Data &data, size_t &offset) const
160 // make sure there is enough space
161 data.GetBuffer(offset + SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE);
163 MAKE_RECORD(Barry::Protocol::ServiceBookConfigField, sbc, data, offset);
164 sbc->format = Format;
166 offset += SERVICE_BOOK_CONFIG_FIELD_HEADER_SIZE;
170 // BuildFields
172 /// Build fields part of record
174 void ServiceBookConfig::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
176 throw std::logic_error("ServiceBookConfig::Build not yet implemented");
179 void ServiceBookConfig::Clear()
181 Unknowns.clear();
184 void ServiceBookConfig::Dump(std::ostream &os) const
186 os << " ServiceBookConfig Format: " << setbase(16) << (uint16_t)Format << "\n";
188 // cycle through the type table
189 for( const FieldLink<ServiceBookConfig> *b = ServiceBookConfigFieldLinks;
190 b->type != SBFCC_END;
191 b++ )
193 if( b->strMember ) {
194 const std::string &s = this->*(b->strMember);
195 if( s.size() )
196 os << " " << b->name << ": " << s << "\n";
198 else if( b->timeMember ) {
199 time_t t = this->*(b->timeMember);
200 if( t > 0 )
201 os << " " << b->name << ": " << ctime(&t);
205 // print any unknowns
206 os << Unknowns;
207 os << " ------------------- End of Config Field\n";
211 ///////////////////////////////////////////////////////////////////////////////
212 // ServiceBook class
214 // service book field codes
215 #define SBFC_OLD_NAME 0x01
216 #define SBFC_HIDDEN_NAME 0x02
217 #define SBFC_NAME 0x03
218 #define SBFC_OLD_UNIQUE_ID 0x06
219 #define SBFC_UNIQUE_ID 0x07
220 #define SBFC_CONTENT_ID 0x08
221 #define SBFC_CONFIG 0x09
222 #define SBFC_OLD_DESC 0x32
223 #define SBFC_DESCRIPTION 0x0f
224 #define SBFC_DSID 0xa1
225 #define SBFC_BES_DOMAIN 0xa2
226 #define SBFC_USER_ID 0xa3
227 #define SBFC_END 0xffff
229 // private data class, containing internal structures
230 class ServiceBookData
232 public:
233 FieldLink<ServiceBook> *m_typeSet;
234 ServiceBookData(FieldLink<ServiceBook> *typeSet) : m_typeSet(typeSet) {}
237 // The Old/New tables contain the same fields, but use different
238 // type codes. Keeping them separate yet linked makes it possible
239 // to convert between old and new type codes, while hopefully keeping
240 // things generic.
241 static FieldLink<ServiceBook> ServiceBookOldFieldLinks[] = {
242 { SBFC_OLD_NAME, "Old Name", 0, 0, &ServiceBook::Name, 0, 0, 0, 0, true },
243 { SBFC_OLD_DESC, "Old Desc", 0, 0, &ServiceBook::Description, 0, 0, 0, 0, true },
244 { SBFC_OLD_UNIQUE_ID, "Old UniqueId", 0, 0, &ServiceBook::UniqueId, 0, 0, 0, 0, false },
245 { SBFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
248 static FieldLink<ServiceBook> ServiceBookNewFieldLinks[] = {
249 { SBFC_NAME, "Name", 0, 0, &ServiceBook::Name, 0, 0, 0, 0, true },
250 { SBFC_DESCRIPTION, "Description", 0, 0, &ServiceBook::Description, 0, 0, 0, 0, true },
251 { SBFC_UNIQUE_ID, "UniqueId", 0, 0, &ServiceBook::UniqueId, 0, 0, 0, 0, false },
252 { SBFC_END, "End of List", 0, 0, 0, 0, 0, 0, 0, false }
255 // This table holds all
256 static FieldLink<ServiceBook> ServiceBookFieldLinks[] = {
257 { SBFC_HIDDEN_NAME, "Hidden Name",0, 0, &ServiceBook::HiddenName, 0, 0, 0, 0, true },
258 { SBFC_DSID, "DSID", 0, 0, &ServiceBook::DSID, 0, 0, 0, 0, false },
259 { SBFC_CONTENT_ID, "ContentId", 0, 0, &ServiceBook::ContentId, 0, 0, 0, 0, false },
260 { SBFC_BES_DOMAIN, "BES Domain", 0, 0, &ServiceBook::BesDomain, 0, 0, 0, 0, false },
261 { SBFC_END, "End of List",0, 0, 0, 0, 0, 0, 0, false }
264 // Array of conflicting tables only
265 static FieldLink<ServiceBook> *ServiceBookLinkTable[] = {
266 ServiceBookOldFieldLinks,
267 ServiceBookNewFieldLinks,
271 #define FIELDLINK_END 0xffff
273 template <class RecordT>
274 FieldLink<RecordT>* ParseFieldByTable(RecordT *rec,
275 const CommonField *field,
276 const IConverter *ic,
277 FieldLink<RecordT> *links)
279 // cycle through the type table
280 for( FieldLink<RecordT> *b = links; b->type != FIELDLINK_END; b++ ) {
281 if( b->type == field->type ) {
282 if( b->strMember ) {
283 std::string &s = rec->*(b->strMember);
284 if( s.size() ) {
285 dout(RecordT::GetDBName() << ": field '" << b->name << "' already has data (" << s << "). Overwriting.");
287 s = ParseFieldString(field);
288 if( b->iconvNeeded && ic )
289 s = ic->FromBB(s);
290 return links;
292 else if( b->timeMember && btohs(field->size) == 4 ) {
293 time_t &t = rec->*(b->timeMember);
294 t = min2time(field->u.min1900);
295 return links;
299 return 0;
302 template <class RecordT>
303 FieldLink<RecordT>* ParseFieldByTable(RecordT *rec,
304 const CommonField *field,
305 const IConverter *ic,
306 FieldLink<RecordT> **b)
308 for( ; *b; b++ ) {
309 FieldLink<RecordT> *link =
310 ParseFieldByTable<RecordT>(rec, field, ic, *b);
311 if( link )
312 return link;
314 return 0;
317 ServiceBook::ServiceBook()
318 : m_data( new ServiceBookData(ServiceBookOldFieldLinks) )
319 , RecordId(0)
321 Clear();
324 ServiceBook::~ServiceBook()
328 const unsigned char* ServiceBook::ParseField(const unsigned char *begin,
329 const unsigned char *end,
330 const IConverter *ic)
332 const CommonField *field = (const CommonField *) begin;
334 // advance and check size
335 begin += COMMON_FIELD_HEADER_SIZE + btohs(field->size);
336 if( begin > end ) // if begin==end, we are ok
337 return begin;
339 if( !btohs(field->size) ) // if field has no size, something's up
340 return begin;
342 // cycle through the type tables
343 FieldLink<ServiceBook> *typeSet =
344 ParseFieldByTable(this, field, ic, ServiceBookLinkTable);
345 if( typeSet ) {
346 if( m_data->m_typeSet && m_data->m_typeSet != typeSet ) {
347 dout("ServiceBook record has a mix of old and new field types.");
349 m_data->m_typeSet = typeSet;
350 return begin;
352 else {
353 if( ParseFieldByTable(this, field, ic, ServiceBookFieldLinks) )
354 return begin; // done!
357 // handle special cases
358 switch( field->type )
360 case SBFC_CONFIG:
361 try {
362 Data config((const void *)field->u.raw, btohs(field->size));
363 size_t offset = 0;
364 Config.ParseHeader(config, offset);
365 Config.ParseFields(config, offset);
366 return begin; // success
368 catch( BadPackedFormat & ) {
369 // break here so unprocessed raw packet is still
370 // visible in dump
371 break;
375 // if still not handled, add to the Unknowns list
376 UnknownField uf;
377 uf.type = field->type;
378 uf.data.assign((const char*)field->u.raw, btohs(field->size));
379 Unknowns.push_back(uf);
381 // return new pointer for next field
382 return begin;
385 void ServiceBook::ParseHeader(const Data &data, size_t &offset)
387 // no header in this record (?)
390 void ServiceBook::ParseFields(const Data &data, size_t &offset, const IConverter *ic)
392 const unsigned char *finish = ParseCommonFields(*this,
393 data.GetData() + offset, data.GetData() + data.GetSize(), ic);
394 offset += finish - (data.GetData() + offset);
397 void ServiceBook::BuildHeader(Data &data, size_t &offset) const
399 // no header in this record (?)
403 // BuildFields
405 /// Build fields part of record
407 void ServiceBook::BuildFields(Data &data, size_t &offset, const IConverter *ic) const
409 throw std::logic_error("ServiceBook::BuildFields not yet implemented");
412 void ServiceBook::Clear()
414 m_data->m_typeSet = ServiceBookOldFieldLinks;
415 Unknowns.clear();
416 Config.Clear();
419 inline void FormatStr(std::ostream &os, const char *name, const std::string &str)
421 if( str.size() ) {
422 os << " " << setw(20) << name;
423 os << ": " << str << "\n";
427 void ServiceBook::Dump(std::ostream &os) const
429 ios::fmtflags oldflags = os.setf(ios::left);
430 char fill = os.fill(' ');
432 os << "ServiceBook entry: 0x" << setbase(16) << RecordId
433 << " (" << (unsigned int)RecType << ")\n";
435 FormatStr(os, "Name", Name);
436 FormatStr(os, "Hidden Name", HiddenName);
437 FormatStr(os, "Description", Description);
438 FormatStr(os, "DSID", DSID);
439 FormatStr(os, "Unique ID", UniqueId);
440 FormatStr(os, "Content ID", ContentId);
441 FormatStr(os, "(BES) Domain", BesDomain);
443 os << Config;
445 // print any unknowns
446 os << Unknowns;
448 // cleanup the stream
449 os.flags(oldflags);
450 os.fill(fill);
453 bool ServiceBook::operator<(const ServiceBook &other) const
455 int cmp = BesDomain.compare(other.BesDomain);
456 if( cmp == 0 )
457 cmp = DSID.compare(other.DSID);
458 if( cmp == 0 )
459 cmp = Name.compare(other.Name);
460 if( cmp == 0 )
461 cmp = UniqueId.compare(other.UniqueId);
462 return cmp < 0;
465 } // namespace Barry