-
-
Notifications
You must be signed in to change notification settings - Fork 87
/
pdu.c
1848 lines (1735 loc) · 72.4 KB
/
pdu.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "pdu.h"
#include <ctype.h>
/**
* Utilities to convert between UTF-8 and UCS-2
* ANSII-C can be used anywhere
*/
//#define BITS7654ON 0B11110000
//#define BITS765ON 0B11100000
//#define BITS76ON 0B11000000
//#define BIT7ON6OFF 0B10000000
//#define BITS0TO5ON 0B00111111
#define BITS7654ON (240)
#define BITS765ON (224)
#define BITS76ON (192)
#define BIT7ON6OFF (128)
#define BITS0TO5ON (63)
/** @brief int The length of the message, need for the GSM command <b>AT+CSMG=nn</b> */
static int message_length;
static int bufferHead;
PDU_DeliverHeader_t deliverHeader = {0};
static char *pduBuffer;
static int pduBufferLength;
static int concatInfo[3];
// array must be defined before its use in sizeof statement
const
#ifdef PM
PROGMEM
#endif
sRange gsm7_legal[] = {
{10, 10}, {13, 13}, {32, 95}, {97, 126}, {161, 161}, {163, 167}, {191, 191}, {196, 201}, {209, 209}, {214, 214}, {216, 216}, {220, 220}, {223, 224}, {228, 230}, {232, 233}, {236, 236}, {241, 242}, {246, 246}, {248, 249}, {252, 252}, {915, 916}, {920, 920}, {923, 923}, {926, 926}, {928, 928}, {931, 931}, {934, 934}, {936, 937}, // greek
{8364, 8364} // euro
};
/****************** Private function prototype ******************/
void stringToBCD(const char *number, int addressLength, char *pdu);
void BCDtoString(char *number, const char *pdu,int length);
int utf8_to_packed7bit(const char *utf8, char *pdu, int *septets, int udhSize, int availableSpace);
int pduGsm7_to_unicode(const char *pdu, int pduLength, char *ascii,char firstChar);
int convert_utf8_to_gsm7bit(const char *ascii, char *a7bit, int udhSize, int availableSpace);
int convert_7bit_to_unicode(unsigned char *a7bit, int length, char *ascii);
int pdu_to_ucs2(const char *pdu, int length, unsigned short *ucs2);
int ucs2_to_utf8(unsigned short ucs2, char *utf8);
int utf8_to_ucs2_single(const char *utf8, unsigned short *ucs2);
int utf8_to_ucs2(const char *utf8, char *ucs2);
int utf8Length(const char *utf8);
unsigned char getHex(const char *pc);
void putHex(unsigned char b, char *target);
bool isPhoneNumberLegal(const char *number);
bool setAddress(const char *address, PDU_NumberLengthType_t numberLengthType, char *buffer);
int decodeAddress(const char *pdu, char *output, PDU_NumberLengthType_t et);
int buildUDH(char *udhBuffer, int items, va_list args);
bool isGSM7(unsigned short *pucs);
void digitSwap(const char *number, char *pdu);
static char* swapchars(char* string);
static void pop(int length, int *value, char *str);
void PDU_Init(int workSize)
{
pduBufferLength = workSize;
pduBuffer = malloc(sizeof(pduBuffer) * (pduBufferLength + 2));
//malloc(sizeof(deliverHeader.UD) * MAX_SMS_LENGTH_7BIT);
}
void PDU_Free()
{
free(pduBuffer);
//free(deliverHeader.UD);
}
char *PDU_getPDUBuffer()
{
return pduBuffer;
}
/**
* @brief Sanity check on phone number, only numeric or '+'
* @param number
* @return true If the phone number is correct
* @return false If the phone number is incorrect
*/
bool isPhoneNumberLegal(const char *number)
{
bool result = true;
int size = strlen(number);
int i = 0;
while(i++ < size)
{
char c = *number++;
if(!(isdigit(c) || c == '+'))
{
result = false;
break;
}
}
return result;
}
/**
* @brief Convert 2 printable digits to 1 BCD byte, This method also swap the digits
*
* @param number the phone number
* @param pdu In fact, it is the same buffer
* @return none
*/
void stringToBCD(const char *number, int addressLength, char *pdu)
{
int i, tIndex = 0;
/* Ignore leading '+' */
if (*number == '+')
number++;
for (i = 0; i < addressLength; i++)
{
if ((i & 1) == 1) // odd, upper
{
pdu[tIndex] &= 0x0f;
pdu[tIndex] += (*number++ - '0') << 4;
tIndex++;
}
else
{
// prime in case this is the last byte
pdu[tIndex] = 0xf0;
pdu[tIndex] += *number++ - '0';
}
}
}
/**
* @brief Convert 1 BCD byte to 2 printable digits
*
* @param output
* @param input
* @param length
* @return none
*/
void BCDtoString(char *output, const char *input, int length)
{
unsigned char X;
for (int i = 0; i < length; i += 2)
{
X = getHex(input);
input += 2;
*output++ = (X & 0xf) + 0x30;
if ((X & 0xf0) == 0xf0) // end filler
break;
*output++ = (X >> 4) + 0x30;
}
*output = 0; // add end of string
}
/**
* @brief Save recipient phone number, check that it is numeric
* @return true if valid
*/
bool setAddress(const char *address, PDU_NumberLengthType_t numberLengthType, char *buffer)
{
int addressLength;
/* If it is SMSC number and the service number is NULL */
if((numberLengthType == OCTET) && (address == NULL))
{
/* set zero, use default service center number */
buffer[bufferHead++] = 0;
return true;
}
/* sanity check on number format numeric and optional '+' */
bool result = isPhoneNumberLegal(address);
if(result)
{
PDU_NumberFormat_t numberFormat = NATIONAL_NUMBER;
if(*address == '+')
{
/* Ignore leading '+' */
address++;
numberFormat = INTERNATIONAL_NUMBER;
}
addressLength = strlen(address);
if(addressLength < MAX_NUMBER_LENGTH)
{
if(numberLengthType == NIBBLES)
buffer[bufferHead++] = addressLength;
else
buffer[bufferHead++] =((addressLength +1 ) / 2) + 1;
buffer[bufferHead++] = numberFormat;
stringToBCD(address, addressLength, &buffer[bufferHead]);
bufferHead += (strlen(address) + 1) / 2;
result = true;
}
else
result = false;
}
return result;
}
/**
* @brief pdu to readable starts with length octet
*
* @param pdu
* @param output
* @param et
* @return returns number of characters to occupied by number part (after length and atn)
* @returns 0 if number cannot be decoded
*/
int decodeAddress(const char *pdu, char *output, PDU_NumberLengthType_t et)
{
int addressLength = 0;
// pdu to readable starts with length octet
int length = getHex(pdu); // could be nibbles or octets
// if octets, length include TON so reduce by 1
// if nibbles length is just the number
if (et == NIBBLES)
addressLength = length;
else
addressLength = --length * 2;
pdu += 2; // getHex reads 2 bytes
// now analyse address type
int adt = getHex(pdu);
pdu += 2;
if ((adt & EXT_MASK) != 0)
{
switch ((adt & TON_MASK) >> TON_OFFSET)
{
case 1: // international number
*output++ = '+'; // add prefix and fall through
case 2: // national number
//case 3: // network specific number, Issue #26
BCDtoString(output, pdu, addressLength);
if ((addressLength & 1) == 1) // if odd, bump 1
addressLength++; // we could do this before calling BCDtoString
break;
case 5: // alphabetic, convert nibble length to septets
pduGsm7_to_unicode(pdu, (addressLength * 4) / 7, output,0);
if ((addressLength & 1) == 1) // if odd, bump 1
addressLength++; // we could do NOT this before calling pduGsm7_to_unicode
break;
default:
addressLength = 0;
break;
}
}
else
{
addressLength = 0; // don't know how to handle EXT
}
return addressLength;
}
/**
* @brief Make user data header
* @param items
* @param va_list first param should be IEI Predefined sound
* second param and next contains data
* @return
*/
int buildUDH(char *udhBuffer, int items, va_list tag)
{
int udhBufferPointer = 0, tmp;
//va_list tag;
//va_start(tag, items);
/* IEI: Predefined sound */
int val = va_arg(tag, int);
switch (val)
{
case 0: /* Concatenated short messages, 8-bit reference number */
/* length = 3 */
if(items == 3+1)
{
udhBuffer[udhBufferPointer++] = 5; // UDH Length
udhBuffer[udhBufferPointer++] = 0; // IEI
udhBuffer[udhBufferPointer++] = 3; // IEI Length
udhBuffer[udhBufferPointer++] = va_arg(tag, int); // CSMS
udhBuffer[udhBufferPointer++] = va_arg(tag, int); // num parts
udhBuffer[udhBufferPointer++] = va_arg(tag, int); // part number
}
break;
case 1: /* Special SMS Message Indication */
/* length = 2 */
break;
case 4: /* Application port addressing scheme, 8-bit address */
/* length = 2 */
if(items == 2+1)
{
udhBuffer[udhBufferPointer++] = 6; // UDH Length
udhBuffer[udhBufferPointer++] = 4; // IEI
udhBuffer[udhBufferPointer++] = 2; // IEI Length
udhBuffer[udhBufferPointer++] = va_arg(tag, int); // Source Port
udhBuffer[udhBufferPointer++] = va_arg(tag, int); // Destination Port
}
break;
case 5: /* Application port addressing scheme, 16-bit address */
/* length = 4 */
if(items == 2+1)
{
udhBuffer[udhBufferPointer++] = 6; // UDH Length
udhBuffer[udhBufferPointer++] = 5; // IEI
udhBuffer[udhBufferPointer++] = 4; // IEI Length
tmp = va_arg(tag, int);
udhBuffer[udhBufferPointer++] = tmp >> 8; // Source Port high byte
udhBuffer[udhBufferPointer++] = tmp & 0xff; // Source Port low byte
tmp = va_arg(tag, int);
udhBuffer[udhBufferPointer++] = tmp >> 8; // Destination Port high byte
udhBuffer[udhBufferPointer++] = tmp & 0xff; // Destination Port low byte
}
break;
case 8: /* Concatenated short messages, 16-bit reference number */
/* length = 4 */
if(items == 3+1)
{
udhBuffer[udhBufferPointer++] = 6; // UDH Length
udhBuffer[udhBufferPointer++] = 8; // IEI
udhBuffer[udhBufferPointer++] = 4; // IEI Length
tmp = va_arg(tag, int);
udhBuffer[udhBufferPointer++] = tmp >> 8; // CSMS high byte
udhBuffer[udhBufferPointer++] = tmp & 0xff; // CSMS low byte
udhBuffer[udhBufferPointer++] = va_arg(tag, int); // num parts
udhBuffer[udhBufferPointer++] = va_arg(tag, int); // part number
}
break;
default:
;
}
va_end(tag);
return udhBufferPointer;
}
int decodeUDH(char *udhBuffer)
{
//TODO write methode
}
/**
* Parameter indicating whether or not the VP field is present
* Parameter identifying the time from where the message is no longer valid
* @param hour
* @param buffer
* @param VPF: validity period @ref PDU_VPF_t
* @param VP: validity period value
* VP value Validity period value
* 0 to 143 (VP + 1) x 5 minutes (i.e. 5 minutes intervals up to 12 hours)
* 144 to 167 12 hours + ((VP -143) x 30 minutes)
* 168 to 196 (VP - 166) x 1 day
* 197 to 255 (VP - 192) x 1 week
* @return Length of VPF
*/
int calculateVP(int hour, const char *buffer)
{
//TODO write method
}
/**
* @brief Creates an buffer in SMS SUBMIT format
* https://bluesecblog.wordpress.com/2016/11/16/sms-submit-tpdu-structure/
* @param SCA SMS Center Address
* @param ReplayPath
* @param headerPresent
* @param statusReport
* @param VPF_Format
* @param messageReference
* @param recipient
* @param DCS
* @param VPF_Value
* @param header
* @param message
* @return sms length without SCA
* @return < 0 If invalid in anyway
*/
int PDU_encode(const char *SCA, bool ReplayPath, bool headerPresent, bool statusReport, PDU_VPF_t VPF_Format, int messageReference, const char *recipient, int VPF_Value, const char *message, int items, ...)
{
char udhBuffer[10];
char tempBuffer[PDU_BINARY_MAX_LENGTH];
const char *savem = message;
int length = -1;
bufferHead = 0;
message_length = 0;
PDU_DataCodingScheme_t DCS = DCS_7BIT;
/* proper way to check if entire message default GSM 7 bit
scan UTF8 string, check each UCS2, bail out on 1st non-GSM7 value
*/
bool gsm7bit = true;
while (*message && gsm7bit)
{
unsigned short ucs2[2], target; // allow for surrogate pair
int length = utf8Length(message);
utf8_to_ucs2_single(message, ucs2); // translate to a single ucs2
// UCS2 is bigendian, swap to little endian
target = (ucs2[0] << 8) | ((ucs2[0] & 0xff00) >> 8);
gsm7bit = isGSM7(&target);
message += length; // bump to next
}
if (!gsm7bit)
DCS = DCS_16BIT;
/* Set SMSC Address */
if(!setAddress(SCA, OCTET, tempBuffer))
return ADDRESS_FORMAT; // bail out now
else
message_length = bufferHead;
/* Set PDU-Type */
int pdu_type = MTI_SMS_SUBMIT;
if(headerPresent)
pdu_type |= (1<<UDHI_BIT);
if(ReplayPath)
pdu_type |= (1<<RP_BIT);
if(statusReport)
pdu_type |= (1<<SRI_BIT);
if(VPF_Format != VPF_NOT_PRESENT)
pdu_type |= (VPF_Format << VPF_BIT);
tempBuffer[bufferHead++] = pdu_type;
/* Set Message Reference */
tempBuffer[bufferHead++] = 0;
/* Set Destination Address */
if(!setAddress(recipient, NIBBLES, tempBuffer))
return ADDRESS_FORMAT;
/* Set PID */
tempBuffer[bufferHead++] = PID_SHORT_MESSAGE;
/* Set Data Coding Schema */
int UDL_PlaceHolder = 0;
switch (DCS)
{
case DCS_7BIT:
tempBuffer[bufferHead++] = DCS_7BIT; break;
case DCS_8BIT:
tempBuffer[bufferHead++] = DCS_8BIT; break;
case DCS_16BIT:
tempBuffer[bufferHead++] = DCS_16BIT; break;
}
/* Set Validity Period */
if(VPF_Format != VPF_NOT_PRESENT)
{
tempBuffer[bufferHead++] = VPF_Value;
/*int vpfSize = calculateVP(VPF_Value, tempBuffer);
bufferHead += vpfSize;*///TODO
}
/* Save UDL index in tempBuffer */
UDL_PlaceHolder = bufferHead;
/* Skip UDL index in tempBuffer */
tempBuffer[bufferHead++] = 1;
/* Build UDH */
int udhSize;
if(!headerPresent)
udhSize = 0;
else
{
va_list args;
va_start(args,items);
udhSize = buildUDH(udhBuffer, items, args);
va_end(args);
}
/* Set UDL & Set UDH & Set message */
int delta = -1;
int septetCount = 0;
switch (DCS)
{
case DCS_7BIT:
if(udhSize == 6)
{ // 1 byte ref number, need to pad UDH to 7 octets
udhBuffer[6] = 0x40;
udhSize++;
}
/* Insert UDH if any */
if(udhSize != 0)
{
memcpy(&tempBuffer[bufferHead], udhBuffer, udhSize);
bufferHead += udhSize;
}
delta = utf8_to_packed7bit(savem, &tempBuffer[bufferHead], &septetCount, udhSize == 0 ? 0 : 8, MAX_NUMBER_OCTETS);
/*if (delta < 0)
overFlow = delta == WORK_BUFFER_TOO_SMALL;*/
break;
case DCS_8BIT:
break;
case DCS_16BIT:
/* Insert UDH if any */
if(udhSize != 0)
{
memcpy(&tempBuffer[bufferHead], udhBuffer, udhSize);
bufferHead += udhSize;
}
delta = utf8_to_ucs2(savem, (char *)&tempBuffer[bufferHead]);
if(delta > 0)
{
tempBuffer[UDL_PlaceHolder] = delta + udhSize; // correct message length
length = bufferHead + delta; // allow for length byte
}
break;
}
if(delta < 0) // sanity check
return delta;
/* Convert binary to printable */
if(pduBufferLength < (length * 2))
return WORK_BUFFER_TOO_SMALL;
int newOffset = 0;
for(int i = 0; i < length; i++)
{
putHex(tempBuffer[i], &pduBuffer[newOffset]);
newOffset += 2;
}
#ifdef CTRL_Z
pduBuffer[length * 2] = 0x1a; // add ctrl+z
pduBuffer[(length * 2) + 1] = 0; // add end marker
#endif
message_length = length - message_length;
return message_length;
}
/* The total bytes of PDU message is excluded the SCA address field expressed in decimal value. */
int PDU_getPDUCodeSize()
{
return message_length;
}
/*
Decode a complete message
returns true for success else false
*/
// Extract details from PDU
bool PDU_decode(const char *pdu)
{
int i, index = 0;
// +++ SCA
i = decodeAddress(pdu, deliverHeader.SCA ,OCTET);
if(i == 0)
return false;
/* Skip over SCA length and atn */
index = i + 4;
// --- SCA
// +++ PDU-Type
deliverHeader.PDU_TYPE.allfields = getHex(&pdu[index]);
/* Skip over PDU-Type */
index += 2;
// --- PDU-Type
// +++ OA
deliverHeader.OA_Len = decodeAddress(&pdu[index], deliverHeader.OA, NIBBLES);
if(deliverHeader.OA_Len == 0)
return false;
/* Skip over the phone number length and atn */
index += (deliverHeader.OA_Len +4);
// --- OA
// +++ PID
/* Skip over the PID */
index += 2;
// --- PID
// +++ DCS
deliverHeader.DCS = getHex(&pdu[index]);
/* Skip over the DCS */
index += 2;
// --- DCS
// +++ SCTS
int outIndex = 0;
unsigned char X;
for(i = 0; i < 7; i++)
{
X = getHex(&pdu[index]);
index += 2;
deliverHeader.SCTS[outIndex++] = (X & 0xf) + 0x30;
deliverHeader.SCTS[outIndex++] = (X >> 4) + 0x30;
}
deliverHeader.SCTS[outIndex] = 0;
// --- SCTS
// +++ UDL
int dataLength = getHex(&pdu[index]);
/* Skip over the Data length */
index += 2;
// --- UDL
// +++ UDH
int utfLength = 0, utfOffset, IEI;
unsigned short ucs2;
char udhFollower = 0;
*pduBuffer = 0;
if(deliverHeader.PDU_TYPE.deliver.UDHI)
{
int udhlength = getHex(&pdu[index]);
index += 2;
switch (udhlength)
{
default:
index += (udhlength + 1);
dataLength -= udhlength;
break; // skip over it
case 5:
case 6:
IEI = getHex(&pdu[index]);
index += 2;
int IEILength = getHex(&pdu[index]);
index += 2;
if ((udhlength == 5 && IEI == 0 && IEILength == 3) || (udhlength == 6 && IEI == 8 && IEILength == 4))//TODO support other header type
{
concatInfo[0] = getHex(&pdu[index]);
index += 2; // skip 8 bits of CSMS ref
if (udhlength == 6)
{ // get lo byte of ref number, have already goy hi byte
unsigned char lo = getHex(&pdu[index]);
concatInfo[0] <<= 8;
concatInfo[0] += lo;
index += 2;
}
concatInfo[2] = getHex(&pdu[index]); // get total number of parts
index += 2;
concatInfo[1] = getHex(&pdu[index]); // get part number
index += 2;
if ((deliverHeader.DCS) == DCS_7BIT_ALPHABET_MASK)
{
dataLength -= 7; // dataLength is in septets, last septet is first char of message
if (udhlength == 5) {
// retrieve first char from byte following UDH
// bug fix Issues 28 & 30
udhFollower = getHex(&pdu[index]) >> 1;
index += 2; // skip to next 7 octet (14 nibble) boundary
}
}
else
dataLength -= (udhlength + 1); // dataLength is in octets
}
else
return false;
break;
}
}
// --- UDH
else
// +++ UD
{
memset(concatInfo, 0, sizeof(concatInfo));
}
// --- UD
bool rc = true;
switch (deliverHeader.DCS)
{
case DCS_7BIT:
outIndex = 0;
i = pduGsm7_to_unicode(&pdu[index], dataLength, (char *)pduBuffer,udhFollower);
pduBuffer[i] = 0;
// utf8length = i;
rc = true;
break;
case DCS_8BIT:
rc = false;
break;
case DCS_16BIT:
// loop on all ucs2 words until done
utfOffset = 0;
while (dataLength)
{
pdu_to_ucs2(&pdu[index], 2, &ucs2); // treat 2 octets
index += 4;
dataLength -= 2;
utfLength = ucs2_to_utf8(ucs2, pduBuffer + utfOffset);
// check for overflow
if ((utfOffset + utfLength) >= pduBufferLength) {
//overFlow = true;
break;
}
utfOffset += utfLength;
}
// utf8length = utfOffset;
pduBuffer[utfOffset] = 0; // end marker
rc = true;
break;
default:
rc = false;
}
return rc;
}
/* Swap every second character */
static char* swapchars(char *string)
{
int Length;
int position;
char c;
Length = strlen(string);
for (position = 0; position < Length - 1; position += 2)
{
c = string[position];
string[position] = string[position + 1];
string[position + 1] = c;
}
return string;
}
bool isGSM7(unsigned short *pucs)
{
for (unsigned int i = 0; i < sizeof(gsm7_legal) / sizeof(sRange); i++)
{
#ifdef PM
if (*pucs >= pgm_read_word(&gsm7_legal[i].min) && *pucs <= pgm_read_word(&gsm7_legal[i].max))
#else
if (*pucs >= gsm7_legal[i].min && *pucs <= gsm7_legal[i].max)
#endif
return true;
}
return false;
}
int buildUtf(unsigned long cp, char *target)
{
unsigned char buf[5];
int length;
if (cp <= 0x7f) // ASCII
{
length = 1;
buf[0] = cp;
buf[length] = 0;
}
else if (cp <= 0x7ff)
{ // Extended latin, greek, hebrew, arabic, cyrillic
length = 2;
buf[0] = BITS76ON;
buf[1] = BIT7ON6OFF;
buf[length] = 0;
for (int k = 0; k < 11; ++k) // 11 bits in pack
{
if (k < 6)
buf[1] |= (cp % 64) & (1 << k);
else
buf[0] |= (cp >> 6) & (1 << (k - 6));
}
}
else if (cp <= 0xffff)
{ // many Asian languages
length = 3;
buf[0] = BITS765ON;
buf[1] = BIT7ON6OFF;
buf[2] = BIT7ON6OFF;
buf[length] = 0;
for (int k = 0; k < 16; ++k) // 16 bits in pack
{
if (k < 6)
buf[2] |= (cp % 64) & (1 << k);
else if (k < 12)
buf[1] |= (cp >> 6) & (1 << (k - 6));
else
buf[0] |= (cp >> 12) & (1 << (k - 12));
}
}
else if (cp > 0x10000)
{ // emojis, drawings, chinese
length = 4;
buf[0] = BITS7654ON;
buf[1] = BIT7ON6OFF;
buf[2] = BIT7ON6OFF;
buf[3] = BIT7ON6OFF;
buf[length] = 0;
for (int k = 0; k < 22; ++k) // 22 bits in pack
{
if (k < 6) // bits 0-6
buf[3] |= (cp % 64) & (1 << k);
else if (k < 12) // bits 6-11
buf[2] |= (cp >> 6) & (1 << (k - 6));
else if (k < 18) // bits 7-18
buf[1] |= (cp >> 12) & (1 << (k - 12));
else // bits 19-22
buf[0] |= (cp >> 18) & (1 << (k - 18));
}
}
strcpy(target, (char *)buf);
return strlen(target);
}
/**
* @brief convert 1 byte to 2 printable characters in hex
*
* @param b
* @param target
*/
void putHex(unsigned char b, char *target)
{
// upper nibble
if ((b >> 4) <= 9)
*target++ = (b >> 4) + '0';
else
*target++ = (b >> 4) + 'A' - 10;
// lower nibble
if ((b & 0xf) <= 9)
*target++ = (b & 0xf) + '0';
else
*target++ = (b & 0xf) + 'A' - 10;
}
/**
* @brief convert 2 printable characters to 1 byte
* @param pc
* @return
*/
unsigned char getHex(const char *pc)
{
int i;
char PC = toupper(*pc);
if (isdigit(PC))
i = ((unsigned char)(PC) - '0') * 16;
else
i = ((unsigned char)(PC) - 'A' + 10) * 16;
PC = toupper(*++pc);
if (isdigit(PC))
i += (unsigned char)(PC) - '0';
else
i += (unsigned char)(PC) - 'A' + 10;
return i;
}
/**
Input is ISO-8859 8 bit ASCII, 0 to 255 -- WRONG
Input is UTF8
Need to know UDH size in septets (0 or 8) to calculate when
message is too long
Return GSM7_TOO_LONG is message is longer than MAX_SMS_LENGTH_7BIT
*/
int convert_utf8_to_gsm7bit(const char *message, char *gsm7bit, int udhSize, int bufferSize)
{
int w = 0;
while (*message)
{
// sanity check against overflow
int length = utf8Length(message);
unsigned short ucs2[2], target; // allow for surrogate pair
utf8_to_ucs2_single(message, ucs2);
target = (ucs2[0] << 8) | ((ucs2[0] & 0xff00) >> 8); // swap endian
/*
Handle special cases, code a bit convoluted caused by the need to
keep translate tables as small as possible
*/
if (target == 0x20AC)
{ // euro ucs2
*gsm7bit++ = 27;
*gsm7bit++ = 0x65;
w += 2;
}
else if (target >= GREEK_UCS_MINIMUM)
{
#ifdef PM
*gsm7bit++ = pgm_read_word(lookup_UnicodeToGreek7 + target - GREEK_UCS_MINIMUM);
#else
*gsm7bit++ = lookup_UnicodeToGreek7[target - GREEK_UCS_MINIMUM];
#endif
w++;
}
else
{
#ifdef PM
short x = pgm_read_word(lookup_ascii8to7 + target);
#else
short x = lookup_ascii8to7[target];
#endif
if (x > 256)
{ // this is an escaped character
*gsm7bit++ = 27;
*gsm7bit++ = x - 256;
w += 2;
}
else
{
*gsm7bit++ = x;
w++;
}
}
message += length; // bump to next character
// check not exceeding max 160 characters for GSM7
if (w > MAX_SMS_LENGTH_7BIT)
break;
}
return w > MAX_SMS_LENGTH_7BIT ? GSM7_TOO_LONG : w;
}
/**
* @brief UTF8 string may contain characters that need to be changed from 8 bit ISO-8859
* to GSM 7 bit e.g. Pound Sterling from 0xA3 to 0x01 or escaped characters e.g.
* Left Square 0x5B to ESC/0x3C, Euro 0x20AC to ESC/0x65
* Special treatment for Greek
*
* @param utf8
* @param pdu
* @param septets
* @param udhSize
* @param bufferSize
* @return GSM7_TOO_LONG is the message is longer than MAX_SMS_LENGTH_7BIT
*/
int utf8_to_packed7bit(const char *utf8, char *pdu, int *septets, int udhSize, int bufferSize)
{
int r;
int w;
int len7bit;
char gsm7bit[MAX_SMS_LENGTH_7BIT + 2]; // allow for overflow
/* Start by converting the UTF8-string to a GSM7 string */
len7bit = convert_utf8_to_gsm7bit(utf8, gsm7bit, udhSize, bufferSize);
// check if workBuffer exceeded
if (len7bit < 0)
return len7bit;
/* Now, we can create a PDU string by packing the 7bit-string */
r = 0;
w = 0;
while (r < len7bit)
{
pdu[w] = ((gsm7bit[r] >> (w % 7)) & 0x7F) | ((gsm7bit[r + 1] << (7 - (w % 7))) & 0xFF);
if ((w % 7) == 6)
r++;
r++;
w++;
}
*septets = len7bit;
return w;
}
/**
*
* @param pdu
* @param numSeptets
* @param unicode
* @param firstChar
* @return
*/
int pduGsm7_to_unicode(const char *pdu, int numSeptets, char *unicode, char firstChar)
{
int r;
int w;
int length;
unsigned char gsm7bit[(numSeptets * 8) / 7];
// first decompress the 7-bit characters into octets
w = 0;
int index = 0; // index into the string
int ovflow = 0;
// if first character not zero it was retrieved from udh field, stick it into the final buffer
if (firstChar != 0)
{
gsm7bit[w++] = firstChar;
}
for (r = 0; w < numSeptets; r++)
{
index = r * 2;
if (r % 7 == 0)
{
gsm7bit[w++] = (getHex(&pdu[index]) << 0) & 0x7F;
}
else if (r % 7 == 6)
{
gsm7bit[w++] = ((getHex(&pdu[index]) << 6) | (getHex(&pdu[index - 2]) >> 2)) & 0x7F;
if (w < numSeptets) // Issue 33
gsm7bit[w++] = (getHex(&pdu[index]) >> 1) & 0x7F;
if (w >= numSeptets)
break;
ovflow++;
}
else
{
gsm7bit[w++] = ((getHex(&pdu[index]) << (r % 7)) | (getHex(&pdu[index - 2]) >> (7 + 1 - (r % 7)))) & 0x7F;
}
}
length = convert_7bit_to_unicode(gsm7bit, w, unicode);
return length;
}
bool SPstart = false;
unsigned short spair[2]; // save surrogate pair