3 /// Record parsing class for the SMS database.
7 Copyright (C) 2005-2011, Net Direct Inc. (http://www.netdirect.ca/)
8 Copyright (C) 2009, Ryan Li(ryan@ryanium.com)
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.
24 #include "record-internal.h"
25 #include "protostructs.h"
34 #include "ios_state.h"
37 using namespace Barry::Protocol
;
41 ///////////////////////////////////////////////////////////////////////////////
45 #define SMSFC_METADATA 0x01
46 #define SMSFC_ADDRESS 0x02
47 #define SMSFC_BODY 0x04
49 // SMS Field Sizes and Header Sizes
50 #define SMS_ADDRESS_HEADER_SIZE 0x04
52 #define MILLISECONDS_IN_A_SECOND 1000
54 time_t Sms::GetTime() const
56 return (time_t)(Timestamp
/ MILLISECONDS_IN_A_SECOND
);
59 time_t Sms::GetServiceCenterTime() const
61 return (time_t)(ServiceCenterTimestamp
/ MILLISECONDS_IN_A_SECOND
);
64 void Sms::SetTime(const time_t timestamp
, const unsigned milliseconds
)
66 Timestamp
= (uint64_t)timestamp
* MILLISECONDS_IN_A_SECOND
+ milliseconds
;
69 void Sms::SetServiceCenterTime(const time_t timestamp
, const unsigned milliseconds
)
71 ServiceCenterTimestamp
= (uint64_t)timestamp
* MILLISECONDS_IN_A_SECOND
+ milliseconds
;
83 const unsigned char* Sms::ParseField(const unsigned char *begin
,
84 const unsigned char *end
,
87 const CommonField
*field
= (const CommonField
*)begin
;
89 // advance and check size
90 begin
+= COMMON_FIELD_HEADER_SIZE
+ btohs(field
->size
);
91 if (begin
> end
) // if begin==end, we are ok
94 if (!btohs(field
->size
)) // if field has no size, something's up
101 if (btohs(field
->size
) < SMS_METADATA_SIZE
)
102 break; // size not match
104 const SMSMetaData
&metadata
= field
->u
.sms_metadata
;
105 NewConversation
= metadata
.flags
& SMS_FLG_NEW_CONVERSATION
;
106 Saved
= metadata
.flags
& SMS_FLG_SAVED
;
107 Deleted
= metadata
.flags
& SMS_FLG_DELETED
;
108 Opened
= metadata
.flags
& SMS_FLG_OPENED
;
110 IsNew
= metadata
.new_flag
;
112 uint32_t status
= btohl(metadata
.status
);
116 case SMS_STA_RECEIVED
:
117 MessageStatus
= Received
;
120 MessageStatus
= Draft
;
123 MessageStatus
= Sent
; // consider all others as sent
126 ErrorId
= btohl(metadata
.error_id
);
128 Timestamp
= btohll(metadata
.timestamp
);
129 ServiceCenterTimestamp
= btohll(metadata
.service_center_timestamp
);
131 switch (metadata
.dcs
)
134 DataCodingScheme
= SevenBit
;
137 DataCodingScheme
= EightBit
;
140 DataCodingScheme
= UCS2
;
143 DataCodingScheme
= SevenBit
; // consider all unknowns as 7bit
151 uint16_t length
= btohs(field
->size
);
152 if (length
< SMS_ADDRESS_HEADER_SIZE
+ 1) // trailing '\0'
155 length
-= SMS_ADDRESS_HEADER_SIZE
;
156 const char *address
= (const char *)field
->u
.raw
+ SMS_ADDRESS_HEADER_SIZE
;
157 Addresses
.push_back(std::string(address
, strnlen(address
, length
)));
164 // Some SMS bodies contain a null terminator
165 // in the middle, and it is unknown at the moment
166 // why this is. For regular 8bit char strings,
167 // we just strip out the nulls. For UCS2
168 // 16bit char strings, we strip out the
171 // Any further information on why these null
172 // terminators appear is welcome.
174 const char *str
= (const char *)field
->u
.raw
;
175 uint16_t maxlen
= btohs(field
->size
);
176 if (DataCodingScheme
!= UCS2
) {
177 for (uint16_t i
= 0; i
< maxlen
; ++i
) {
178 if (str
[i
]) // if not null, push it
183 for (uint16_t i
= 0; maxlen
&& i
< (maxlen
-1); i
+= 2) {
184 if (str
[i
] || str
[i
+ 1]) // if not null, push it
185 Body
+= std::string(str
+ i
, 2);
189 if (DataCodingScheme
== SevenBit
) {
190 // SevenBit -> UTF-8 -> ic's tocode
191 IConvHandle
utf8("UTF-8", *ic
);
192 Body
= ic
->Convert(utf8
, ConvertGsmToUtf8(Body
)); // convert the Body string from GSM 03.38 defined to UTF-8
194 else if (DataCodingScheme
== EightBit
)
195 Body
= ic
->FromBB(Body
);
197 IConvHandle
ucs2("UCS-2BE", *ic
);
198 Body
= ic
->Convert(ucs2
, Body
);
205 // if still not handled, add to the Unknowns list
207 uf
.type
= field
->type
;
208 uf
.data
.assign((const char*)field
->u
.raw
, btohs(field
->size
));
209 Unknowns
.push_back(uf
);
211 // return new pointer for next field
215 void Sms::ParseHeader(const Data
&data
, size_t &offset
)
217 // no header in SMS records
220 void Sms::ParseFields(const Data
&data
, size_t &offset
, const IConverter
*ic
)
222 const unsigned char *finish
= ParseCommonFields(*this,
223 data
.GetData() + offset
, data
.GetData() + data
.GetSize(), ic
);
224 offset
+= finish
- (data
.GetData() + offset
);
227 void Sms::BuildHeader(Data
&data
, size_t &offset
) const
229 // not yet implemented
232 void Sms::BuildFields(Data
&data
, size_t &offset
, const IConverter
*ic
) const
234 // not yet implemented
239 RecType
= GetDefaultRecType();
242 MessageStatus
= Unknown
;
243 DeliveryStatus
= NoReport
;
245 IsNew
= NewConversation
= Saved
= Deleted
= Opened
= false;
247 Timestamp
= ServiceCenterTimestamp
= 0;
249 DataCodingScheme
= SevenBit
;
259 std::string
Sms::GetDescription() const
261 if( Addresses
.size() )
264 return "Unknown destination";
267 void Sms::Dump(std::ostream
&os
) const
269 ios_format_state
state(os
);
271 os
<< "SMS record: 0x" << setbase(16) << RecordId
272 << " (" << (unsigned int)RecType
<< ")\n";
273 time_t t
= GetTime();
274 os
<< "\tTimestamp: " << ctime(&t
);
276 if (MessageStatus
== Received
) {
277 t
= GetServiceCenterTime();
278 os
<< "\tService Center Timestamp: " << ctime(&t
);
282 os
<< "\tSend Error: 0x" << setbase(16) << ErrorId
<< "\n";
284 switch (MessageStatus
)
287 os
<< "\tReceived From:\n";
290 os
<< "\tSent to:\n";
293 os
<< "\tDraft for:\n";
296 os
<< "\tUnknown status for:\n";
301 for (std::vector
<std::string
>::const_iterator i
= Addresses
.begin(); i
< Addresses
.end(); ++i
) {
302 if (i
!= Addresses
.begin())
308 if (IsNew
|| Opened
|| Saved
|| Deleted
|| NewConversation
) {
318 os
<< "Message" << (NewConversation
? " that starts a new conversation" : "") << "\n";
320 os
<< "\tContent: " << Body
<< "\n";
325 // This function helps to convert GSM 03.38 defined 7-bit
327 // Detailed information can be found in:
328 // ftp://ftp.3gpp.org/Specs/html-info/0338.htm (Official)
329 // http://en.wikipedia.org/wiki/SMS#GSM
332 std::string
Sms::ConvertGsmToUtf8(const std::string
&s
)
335 // This array stores the GSM 03.38 defined encoding's
336 // corresponding UTF-8 values.
337 // For example: GsmTable[0] = "\x40", which refers to
338 // a "@" in UTF-8 encoding.
339 // The 0x1b item, leads to the extension table, using
340 // the char right next to it as the index.
341 // According to the official specification, when not
342 // able to handle it, it should be treated simply as
343 // a space, which is denoted in UTF-8 as "\x20".
345 static const std::string GsmTable
[0x80] = {
346 // 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
347 "\x40", "\xc2\xa3", "\x24", "\xc2\xa5", "\xc3\xa8", "\xc3\xa9", "\xc3\xb9", "\xc3\xac", // 0x00
348 "\xc3\xb2", "\xc3\x87", "\x0a", "\xc3\x98", "\xc3\xb8", "\x0d", "\xc3\x85", "\xc3\xa5", // 0x08
349 "\xce\x94", "\x5f", "\xce\xa6", "\xce\x93", "\xce\x9b", "\xce\xa9", "\xce\xa0", "\xce\xa8", // 0x10
350 "\xce\xa3", "\xce\x98", "\xce\x9e", "\x20", "\xc3\x86", "\xc3\xa6", "\xc3\x9f", "\xc3\x89", // 0x18
351 "\x20", "\x21", "\x22", "\x23", "\xc2\xa4", "\x25", "\x26", "\x27", // 0x20
352 "\x28", "\x29", "\x2a", "\x2b", "\x2c", "\x2d", "\x2e", "\x2f", // 0x28
353 "\x30", "\x31", "\x32", "\x33", "\x34", "\x35", "\x36", "\x37", // 0x30
354 "\x38", "\x39", "\x3a", "\x3b", "\x3c", "\x3d", "\x3e", "\x3f", // 0x38
355 "\xc2\xa1", "\x41", "\x42", "\x43", "\x44", "\x45", "\x46", "\x47", // 0x40
356 "\x48", "\x49", "\x4a", "\x4b", "\x4c", "\x4d", "\x4e", "\x4f", // 0x48
357 "\x50", "\x51", "\x52", "\x53", "\x54", "\x55", "\x56", "\x57", // 0x50
358 "\x58", "\x59", "\x5a", "\xc3\x84", "\xc3\x96", "\xc3\x91", "\xc3\x9c", "\xc2\xa7", // 0x58
359 "\xc2\xbf", "\x61", "\x62", "\x63", "\x64", "\x65", "\x66", "\x67", // 0x60
360 "\x68", "\x69", "\x6a", "\x6b", "\x6c", "\x6d", "\x6e", "\x6f", // 0x68
361 "\x70", "\x71", "\x72", "\x73", "\x74", "\x75", "\x76", "\x77", // 0x70
362 "\x78", "\x79", "\x7a", "\xc3\xa4", "\xc3\xb6", "\xc3\xb1", "\xc3\xbc", "\xc3\xa0" // 0x78
365 // This sparse array stores the GSM 03.38 defined
366 // encoding extension table.
367 // The \x1b item is also preserved, for possibly
368 // another extension table.
370 static const std::string GsmExtensionTable
[0x80] = {
371 // 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7
372 "", "", "", "", "", "", "", "", // 0x00
373 "", "", "\x0c", "", "", "", "", "", // 0x08
374 "", "", "", "", "\x5e", "", "", "", // 0x10
375 "", "", "", " ", "", "", "", "", // 0x18
376 "", "", "", "", "", "", "", "", // 0x20
377 "\x7b", "\x7d", "", "", "", "", "", "\x5c", // 0x28
378 "", "", "", "", "", "", "", "", // 0x30
379 "", "", "", "", "\x5b", "\x7e", "\x5d", "", // 0x38
380 "\x7c", "", "", "", "", "", "", "", // 0x40
381 "", "", "", "", "", "", "", "", // 0x48
382 "", "", "", "", "", "", "", "", // 0x50
383 "", "", "", "", "", "", "", "", // 0x58
384 "", "", "", "", "", "\xe2\x82\xac", "", "", // 0x60
385 "", "", "", "", "", "", "", "", // 0x68
386 "", "", "", "", "", "", "", "", // 0x70
387 "", "", "", "", "", "", "", "" // 0x78
390 unsigned len
= s
.length();
391 for (unsigned i
= 0; i
< len
; ++i
) {
392 unsigned char c
= (unsigned char) s
[i
];
393 if (c
> 0x7f) // prevent from illegal index
395 else if (c
== 0x1b) { // go to extension table
397 c
= (unsigned char) s
[++i
];
398 if (c
<= 0x7f) // prevent from illegal index
399 ret
+= GsmExtensionTable
[c
];