/
mms_codec.c
2354 lines (1961 loc) · 55.5 KB
/
mms_codec.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
/*
*
* Multimedia Messaging Service
*
* Copyright (C) 2010-2011 Intel Corporation. All rights reserved.
* Copyright (C) 2013-2014 Jolla Ltd.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <ctype.h>
#include <string.h>
#include <time.h>
#ifdef _WIN32
# define ssize_t int
# define write _write
# include <io.h>
# define uninitialized_var(x) x
#else
# include <unistd.h>
# define uninitialized_var(x) x = x
#endif
#include <glib.h>
#include "wsputil.h"
#include "mms_codec.h"
#define MAX_ENC_VALUE_BYTES 6
#ifdef TEMP_FAILURE_RETRY
#define TFR TEMP_FAILURE_RETRY
#else
#define TFR
#endif
#define QUOTE (127)
enum mms_message_value_bool {
MMS_MESSAGE_VALUE_BOOL_YES = 128,
MMS_MESSAGE_VALUE_BOOL_NO = 129,
};
enum header_flag {
HEADER_FLAG_MANDATORY = 1,
HEADER_FLAG_ALLOW_MULTI = 2,
HEADER_FLAG_PRESET_POS = 4,
HEADER_FLAG_MARKED = 8,
};
enum mms_header {
MMS_HEADER_BCC = 0x01,
MMS_HEADER_CC = 0x02,
MMS_HEADER_CONTENT_LOCATION = 0x03,
MMS_HEADER_CONTENT_TYPE = 0x04,
MMS_HEADER_DATE = 0x05,
MMS_HEADER_DELIVERY_REPORT = 0x06,
MMS_HEADER_DELIVERY_TIME = 0x07,
MMS_HEADER_EXPIRY = 0x08,
MMS_HEADER_FROM = 0x09,
MMS_HEADER_MESSAGE_CLASS = 0x0a,
MMS_HEADER_MESSAGE_ID = 0x0b,
MMS_HEADER_MESSAGE_TYPE = 0x0c,
MMS_HEADER_MMS_VERSION = 0x0d,
MMS_HEADER_MESSAGE_SIZE = 0x0e,
MMS_HEADER_PRIORITY = 0x0f,
MMS_HEADER_READ_REPORT = 0x10,
MMS_HEADER_REPORT_ALLOWED = 0x11,
MMS_HEADER_RESPONSE_STATUS = 0x12,
MMS_HEADER_RESPONSE_TEXT = 0x13,
MMS_HEADER_SENDER_VISIBILITY = 0x14,
MMS_HEADER_STATUS = 0x15,
MMS_HEADER_SUBJECT = 0x16,
MMS_HEADER_TO = 0x17,
MMS_HEADER_TRANSACTION_ID = 0x18,
MMS_HEADER_RETRIEVE_STATUS = 0x19,
MMS_HEADER_RETRIEVE_TEXT = 0x1a,
MMS_HEADER_READ_STATUS = 0x1b,
__MMS_HEADER_MAX = 0x1c,
MMS_HEADER_INVALID = 0x80,
};
enum mms_part_header {
MMS_PART_HEADER_CONTENT_LOCATION = 0x0e,
MMS_PART_HEADER_CONTENT_DISPOSITION = 0x2e,
MMS_PART_HEADER_CONTENT_ID = 0x40,
};
/*
* Reference: IANA http://www.iana.org/assignments/character-sets
*/
static const struct {
unsigned int mib_enum;
const char *charset;
} charset_assignments[] = {
{ 3, "US-ASCII" },
{ 4, "ISO_8859-1" },
{ 5, "ISO_8859-2" },
{ 6, "ISO_8859-3" },
{ 7, "ISO_8859-4" },
{ 8, "ISO_8859-5" },
{ 9, "ISO_8859-6" },
{ 10, "ISO_8859-7" },
{ 11, "ISO_8859-8" },
{ 12, "ISO_8859-9" },
{ 13, "ISO-8859-10" },
{ 17, "Shift_JIS" },
{ 18, "EUC-JP" },
{ 36, "KS_C_5601-1987" },
{ 37, "ISO-2022-KR" },
{ 38, "EUC-KR" },
{ 39, "ISO-2022-JP" },
{ 40, "ISO-2022-JP-2" },
{ 81, "ISO_8859-6-E" },
{ 82, "ISO_8859-6-I" },
{ 84, "ISO_8859-8-E" },
{ 85, "ISO_8859-8-I" },
{ 106, "UTF-8" },
{ 109, "ISO-8859-13" },
{ 110, "ISO-8859-14" },
{ 111, "ISO-8859-15" },
{ 112, "ISO-8859-16" },
{ 113, "GBK" },
{ 114, "GB18030" },
{ 1000, "ISO-10646-UCS-2" },
{ 1001, "ISO-10646-UCS-4" },
{ 1004, "ISO-10646-J-1" },
{ 1012, "UTF-7" },
{ 1013, "UTF-16BE" },
{ 1014, "UTF-16LE" },
{ 1015, "UTF-16" },
{ 1017, "UTF-32" },
{ 1018, "UTF-32BE" },
{ 1019, "UTF-32LE" },
{ 2025, "GB2312" },
{ 2026, "Big5" },
{ 2027, "macintosh" },
{ 2084, "KOI8-R" },
{ 2109, "windows-874" },
{ 2250, "windows-1250" },
{ 2251, "windows-1251" },
{ 2252, "windows-1252" },
{ 2253, "windows-1253" },
{ 2254, "windows-1254" },
{ 2255, "windows-1255" },
{ 2256, "windows-1256" },
{ 2257, "windows-1257" },
{ 2258, "windows-1258" }
};
#define FB_SIZE 256
struct file_buffer {
unsigned char buf[FB_SIZE];
unsigned int size;
unsigned int fsize;
int fd;
};
typedef gboolean (*header_handler)(struct wsp_header_iter *, void *);
typedef gboolean (*header_encoder)(struct file_buffer *, enum mms_header,
void *);
/*
* mms_parse_http_content_type() parses HTTP media type as defined
* in section 3.7 "Media Types" of the HTTP/1.1 specification. The
* grammar is defined as follows:
*
* media-type = type "/" subtype *( ";" parameter )
* type = token
* subtype = token
* parameter = attribute "=" value
* attribute = token
* value = token | quoted-string
* token = 1*<any CHAR except CTLs or separators>
* quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
* qdtext = <any TEXT except <">>
* quoted-pair = "\" CHAR
*
* Returns the NULL terminated array of strings which consists of the
* type/subtype part followed by attribute + value pairs. For example,
* this string:
*
* "text/html; charset=ISO-8859-4"
*
* would be parsed into the followring:
*
* "text/html"
* "charset"
* "ISO-8859-4"
* NULL
*
* The caller is responsible for deallocating the returned string
* with g_strfreev
*/
/*
* token = 1*<any CHAR except CTLs or separators>
* CHAR = <any US-ASCII character (octets 0 - 127)>
* CTL = <any US-ASCII control character
* (octets 0 - 31) and DEL (127)>
* separators = "(" | ")" | "<" | ">" | "@"
* | "," | ";" | ":" | "\" | <">
* | "/" | "[" | "]" | "?" | "="
* | "{" | "}" | SP | HT
*/
static const char http_token[] = {
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x00 - 0x0f */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 - 0x1f */
0,1,0,1,1,1,1,1,0,0,1,1,0,1,1,0, /* !"#$%&'()*+,-./ */
1,1,1,1,1,1,1,1,1,1,0,0,0,0,0,0, /* 0123456789:;<=>? */
0,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* @ABCDEFGHIJKLMNO */
1,1,1,1,1,1,1,1,1,1,1,0,0,0,1,1, /* PQRSTUVWXYZ[\]^_ */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* `abcdefghijklmno */
1,1,1,1,1,1,1,1,1,1,1,0,1,0,1,0, /* pqrstuvwxyz{|}~ */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* 0x80 - 0xFF */
};
/*
* quoted-string = ( <"> *(qdtext | quoted-pair ) <"> )
* quoted-pair = "\" CHAR
* qdtext = <any TEXT except <">>
* TEXT = <any OCTET except CTLs,
* but including LWS>
* CTL = <any US-ASCII control character
* (octets 0 - 31) and DEL (127)>
*/
static const char http_qdtext[] = {
0,0,0,0,0,0,0,0,0,0,1,0,0,1,0,0, /* 0x00 - 0x0f */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 - 0x1f */
1,1,0,1,1,1,1,1,1,1,1,1,1,1,1,1, /* !"#$%&'()*+,-./ */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* 0123456789:;<=>? */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* @ABCDEFGHIJKLMNO */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* PQRSTUVWXYZ[\]^_ */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, /* `abcdefghijklmno */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,0, /* pqrstuvwxyz{|}~ */
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1 /* 0x80 - 0xFF */
};
static const char http_space[] = {
0,0,0,0,0,0,0,0,0,1,1,0,0,1,0,0, /* 0x00 - 0x0f */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0x10 - 0x1f */
1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* !"#$%&'()*+,-./ */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* 0123456789:;<=>? */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* @ABCDEFGHIJKLMNO */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* PQRSTUVWXYZ[\]^_ */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* `abcdefghijklmno */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, /* pqrstuvwxyz{|}~ */
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 /* 0x80 - 0xFF */
};
static const unsigned char *mms_parse_skip_spaces(const unsigned char *ptr)
{
while (http_space[*ptr]) ptr++;
return ptr;
}
static gboolean mms_is_http_token(const unsigned char *ptr)
{
if (ptr && *ptr) {
while (*ptr) {
if (!http_token[*ptr++]) {
return FALSE;
}
}
return TRUE;
}
return FALSE;
}
static const unsigned char *mms_parse_http_token(const unsigned char *ptr,
GString *token)
{
int len = 0;
ptr = mms_parse_skip_spaces(ptr);
while (http_token[*ptr]) {
g_string_append_c(token, *ptr);
ptr++;
len++;
}
return len > 0 ? mms_parse_skip_spaces(ptr) : NULL;
}
static const unsigned char *mms_parse_quoted_string(const unsigned char *ptr,
GString *value)
{
ptr = mms_parse_skip_spaces(ptr);
if (*ptr == '"') {
ptr++;
while (*ptr == '\\' || http_qdtext[*ptr]) {
if (*ptr == '\\') {
ptr++;
if (*ptr) g_string_append_c(value, *ptr++);
} else {
g_string_append_c(value, *ptr++);
}
}
if (*ptr == '"') {
return mms_parse_skip_spaces(ptr+1);
}
}
return NULL;
}
static void mms_parse_collect_value(GPtrArray *output, GString *value)
{
g_ptr_array_add(output, g_strdup(value->str));
g_string_truncate(value, 0);
}
static gboolean mms_parse_parameters(const unsigned char *ptr,
GString *token, GPtrArray *output)
{
g_string_truncate(token, 0);
ptr = mms_parse_skip_spaces(ptr);
while (ptr && *ptr == ';') {
if ((ptr = mms_parse_http_token(ptr+1, token)) != NULL) {
if (*ptr == '=') {
mms_parse_collect_value(output, token);
ptr = mms_parse_skip_spaces(ptr+1);
ptr = (*ptr == '"') ?
mms_parse_quoted_string(ptr, token) :
mms_parse_http_token(ptr, token);
if (ptr) {
mms_parse_collect_value(output, token);
}
} else {
return FALSE; /* Missing = */
}
}
}
return ptr && !*ptr;
}
static const unsigned char *mms_parse_type(const unsigned char *ptr,
GString *token)
{
ptr = mms_parse_http_token(ptr, token);
if (ptr && *ptr == '/') {
g_string_append_c(token, *ptr++);
return mms_parse_http_token(ptr, token);
}
return NULL;
}
char **mms_parse_http_content_type(const char *str)
{
gboolean ok = FALSE;
GPtrArray *output = NULL;
GString *tmp = g_string_new(NULL);
const unsigned char *ptr = mms_parse_type((unsigned char*)str, tmp);
if (ptr) {
output = g_ptr_array_new();
mms_parse_collect_value(output, tmp);
ok = mms_parse_parameters(ptr, tmp, output);
}
g_string_free(tmp, TRUE);
if (ok) {
g_ptr_array_add(output, NULL);
return (char**)g_ptr_array_free(output, FALSE);
}
if (output) {
g_ptr_array_set_free_func(output, g_free);
g_ptr_array_free(output, TRUE);
}
return NULL;
}
char *mms_unparse_http_content_type(char **ct)
{
if (ct && ct[0]) {
int i;
GString *str = g_string_new(ct[0]);
for (i = 1; ct[i]; i += 2) {
const char *val = ct[i+1];
g_string_append(str, "; ");
g_string_append(str, ct[i]);
g_string_append_c(str, '=');
if (mms_is_http_token((unsigned char*)val)) {
g_string_append(str, val);
} else {
g_string_append_c(str, '"');
while (*val) {
const unsigned char uval = *val;
if (!http_qdtext[uval]) {
g_string_append_c(str, '\\');
}
g_string_append_c(str, *val++);
}
g_string_append_c(str, '"');
}
}
return g_string_free(str, FALSE);
}
return NULL;
}
static const char *charset_index2string(unsigned int index)
{
int low = 0;
int high = G_N_ELEMENTS(charset_assignments) - 1;
while (low <= high) {
const int mid = (low + high)/2;
const unsigned int val = charset_assignments[mid].mib_enum;
if (val < index) {
low = mid + 1;
} else if (val > index) {
high = mid - 1;
} else {
return charset_assignments[mid].charset;
}
}
return NULL;
}
static gboolean extract_short(struct wsp_header_iter *iter, void *user)
{
unsigned char *out = user;
const unsigned char *p;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_SHORT)
return FALSE;
p = wsp_header_iter_get_val(iter);
*out = p[0];
return TRUE;
}
static const char *decode_text(struct wsp_header_iter *iter)
{
const unsigned char *p;
unsigned int l;
if (wsp_header_iter_get_val_type(iter) == WSP_VALUE_TYPE_LONG) {
if (iter->len == 0) /* The only way to encode an empty string */
return (char*)(iter->pdu + iter->pos - 1);
return NULL;
}
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_TEXT)
return NULL;
p = wsp_header_iter_get_val(iter);
l = wsp_header_iter_get_val_len(iter);
return wsp_decode_text(p, l, NULL);
}
static gboolean extract_text(struct wsp_header_iter *iter, void *user)
{
char **out = user;
const char *text;
text = decode_text(iter);
if (text == NULL)
return FALSE;
*out = g_strdup(text);
return TRUE;
}
static char *decode_encoded_string_with_mib_enum(const unsigned char *p,
unsigned int l)
{
unsigned int mib_enum;
unsigned int consumed;
const char *text;
const char *from_codeset;
const char *to_codeset = "UTF-8";
gsize bytes_read;
gsize bytes_written;
if (wsp_decode_integer(p, l, &mib_enum, &consumed) == FALSE)
return NULL;
if (mib_enum == 106) {
/* header is UTF-8 already */
text = wsp_decode_text(p + consumed, l - consumed, NULL);
return g_strdup(text);
}
/* convert to UTF-8 */
from_codeset = charset_index2string(mib_enum);
if (from_codeset == NULL)
return NULL;
return g_convert((const char *) p + consumed, l - consumed,
to_codeset, from_codeset,
&bytes_read, &bytes_written, NULL);
}
static char* decode_encoded_text(enum wsp_value_type t,
const unsigned char *p, unsigned int l)
{
switch (t) {
case WSP_VALUE_TYPE_TEXT:
/* Text-string */
return g_strdup(wsp_decode_text(p, l, NULL));
case WSP_VALUE_TYPE_LONG:
/* (Value-len) Char-set Text-string */
return decode_encoded_string_with_mib_enum(p, l);
default:
return NULL;
}
}
static gboolean extract_encoded_text(struct wsp_header_iter *iter, void *user)
{
char **out = user;
char *dec_text = decode_encoded_text(
wsp_header_iter_get_val_type(iter),
wsp_header_iter_get_val(iter),
wsp_header_iter_get_val_len(iter));
if (dec_text == NULL)
return FALSE;
*out = dec_text;
return TRUE;
}
static gboolean extract_encoded_text_element(struct wsp_header_iter *iter,
void *user)
{
char **out = user;
char *element;
char *tmp;
if (!extract_encoded_text(iter, &element))
return FALSE;
if (*out == NULL) {
*out = element;
return TRUE;
}
tmp = g_strjoin(",", *out, element, NULL);
if (tmp == NULL) {
g_free(element);
return FALSE;
}
g_free(*out);
g_free(element);
*out = tmp;
return TRUE;
}
static gboolean extract_subject(struct wsp_header_iter *iter, void *user)
{
/* Ignore incorrectly encoded subjects */
extract_encoded_text(iter, user);
return TRUE;
}
static gboolean extract_date(struct wsp_header_iter *iter, void *user)
{
time_t *out = user;
const unsigned char *p;
unsigned int l;
unsigned int i;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_LONG)
return FALSE;
p = wsp_header_iter_get_val(iter);
l = wsp_header_iter_get_val_len(iter);
if (l > 4)
return FALSE;
for (i = 0, *out = 0; i < l; i++)
*out = *out << 8 | p[i];
/* It is possible to overflow time_t on 32 bit systems */
*out = *out & 0x7fffffff;
return TRUE;
}
static gboolean extract_absolute_relative_date(struct wsp_header_iter *iter,
void *user)
{
time_t *out = user;
const unsigned char *p;
unsigned int l;
unsigned int i;
unsigned int seconds;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_LONG)
return FALSE;
p = wsp_header_iter_get_val(iter);
l = wsp_header_iter_get_val_len(iter);
if (l < 2 || l > 5)
return FALSE;
if (p[0] != 128 && p[0] != 129)
return FALSE;
for (i = 2, seconds = 0; i < l; i++)
seconds = seconds << 8 | p[i];
if (p[0] == 129) {
*out = time(NULL);
*out += seconds;
} else
*out = seconds;
/* It is possible to overflow time_t on 32 bit systems */
*out = *out & 0x7fffffff;
return TRUE;
}
static gboolean extract_boolean(struct wsp_header_iter *iter, void *user)
{
gboolean *out = user;
const unsigned char *p;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_SHORT)
return FALSE;
p = wsp_header_iter_get_val(iter);
if (p[0] == MMS_MESSAGE_VALUE_BOOL_YES) {
*out = TRUE;
return TRUE;
} else if (p[0] == MMS_MESSAGE_VALUE_BOOL_NO) {
*out = FALSE;
return TRUE;
} else {
return TRUE;
}
}
static gboolean extract_from(struct wsp_header_iter *iter, void *user)
{
char **out = user;
const unsigned char *p;
unsigned int l;
enum wsp_value_type t;
char *text;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_LONG)
return FALSE;
p = wsp_header_iter_get_val(iter);
l = wsp_header_iter_get_val_len(iter);
if (p[0] != 128 && p[0] != 129)
return FALSE;
if (p[0] == 129) {
*out = NULL;
return TRUE;
}
if (!wsp_decode_field(p + 1, l - 1, &t, (const void **)&p, &l, NULL))
return FALSE;
text = decode_encoded_text(t, p, l);
if (text == NULL)
return FALSE;
*out = text;
return TRUE;
}
static gboolean extract_message_class(struct wsp_header_iter *iter, void *user)
{
char **out = user;
const unsigned char *p;
unsigned int l;
const char *text;
if (wsp_header_iter_get_val_type(iter) == WSP_VALUE_TYPE_LONG)
return FALSE;
p = wsp_header_iter_get_val(iter);
if (wsp_header_iter_get_val_type(iter) == WSP_VALUE_TYPE_SHORT) {
switch (p[0]) {
case 128:
*out = g_strdup(MMS_MESSAGE_CLASS_PERSONAL);
return TRUE;
case 129:
*out = g_strdup(MMS_MESSAGE_CLASS_ADVERTISEMENT);
return TRUE;
case 130:
*out = g_strdup(MMS_MESSAGE_CLASS_INFORMATIONAL);
return TRUE;
case 131:
*out = g_strdup(MMS_MESSAGE_CLASS_AUTO);
return TRUE;
default:
return FALSE;
}
}
l = wsp_header_iter_get_val_len(iter);
text = wsp_decode_token_text(p, l, NULL);
if (text == NULL)
return FALSE;
*out = g_strdup(text);
return TRUE;
}
static gboolean extract_sender_visibility(struct wsp_header_iter *iter,
void *user)
{
enum mms_message_sender_visibility *out = user;
const unsigned char *p;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_SHORT)
return FALSE;
p = wsp_header_iter_get_val(iter);
if (p[0] != 128 && p[0] != 129)
return FALSE;
*out = p[0];
return TRUE;
}
static gboolean extract_priority(struct wsp_header_iter *iter, void *user)
{
enum mms_message_priority *out = user;
const unsigned char *p;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_SHORT)
return FALSE;
p = wsp_header_iter_get_val(iter);
switch (p[0]) {
case MMS_MESSAGE_PRIORITY_LOW:
case MMS_MESSAGE_PRIORITY_NORMAL:
case MMS_MESSAGE_PRIORITY_HIGH:
*out = p[0];
return TRUE;
}
return FALSE;
}
static gboolean extract_rsp_status(struct wsp_header_iter *iter, void *user)
{
unsigned char *out = user;
const unsigned char *p;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_SHORT)
return FALSE;
p = wsp_header_iter_get_val(iter);
switch (p[0]) {
case MMS_MESSAGE_RSP_STATUS_OK:
case MMS_MESSAGE_RSP_STATUS_ERR_UNSPECIFIED:
case MMS_MESSAGE_RSP_STATUS_ERR_SERVICE_DENIED:
case MMS_MESSAGE_RSP_STATUS_ERR_MESSAGE_FORMAT_CORRUPT:
case MMS_MESSAGE_RSP_STATUS_ERR_SENDING_ADDRESS_UNRESOLVED:
case MMS_MESSAGE_RSP_STATUS_ERR_MESSAGE_NOT_FOUND:
case MMS_MESSAGE_RSP_STATUS_ERR_NETWORK_PROBLEM:
case MMS_MESSAGE_RSP_STATUS_ERR_CONTENT_NOT_ACCEPTED:
case MMS_MESSAGE_RSP_STATUS_ERR_UNSUPPORTED_MESSAGE:
case MMS_MESSAGE_RSP_STATUS_ERR_TRANS_FAILURE:
case MMS_MESSAGE_RSP_STATUS_ERR_TRANS_SENDING_ADDRESS_UNRESOLVED:
case MMS_MESSAGE_RSP_STATUS_ERR_TRANS_MESSAGE_NOT_FOUND:
case MMS_MESSAGE_RSP_STATUS_ERR_TRANS_NETWORK_PROBLEM:
case MMS_MESSAGE_RSP_STATUS_ERR_TRANS_PARTIAL_SUCCESS:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_FAILURE:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_SERVICE_DENIED:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_MESSAGE_FORMAT_CORRUPT:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_SENDING_ADDRESS_UNRESOLVED:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_MESSAGE_NOT_FOUND:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_CONTENT_NOT_ACCEPTED:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_REPLY_CHARGING_LIMIT_NOT_MET:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_REPLY_CHARGING_REQ_NOT_ACCEPT:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_REPLY_CHARGING_FORWARD_DENIED:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_REPLY_CHARGING_NOT_SUPPORTED:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_ADDRESS_HIDING_NOT_SUPPORTED:
case MMS_MESSAGE_RSP_STATUS_ERR_PERM_LACK_OF_PREPAID:
*out = p[0];
break;
default:
/*
* 7.2.27. X-Mms-Response-Status field
*
* The values 196 through 223 are reserved for future use to
* indicate other transient failures. An MMS Client MUST react
* the same to a value in range 196 to 223 as it does to the
* value 192 (Error-transient-failure).
*
* The values 234 through 255 are reserved for future use to
* indicate other permanent failures. An MMS Client MUST react
* the same to a value in range 234 to 255 as it does to the
* value 224 (Error-permanent-failure).
*/
if (p[0] >= MMS_MESSAGE_RSP_STATUS_ERR_PERM_FAILURE)
*out = MMS_MESSAGE_RSP_STATUS_ERR_PERM_FAILURE;
else if (p[0] >= MMS_MESSAGE_RSP_STATUS_ERR_TRANS_FAILURE)
*out = MMS_MESSAGE_RSP_STATUS_ERR_TRANS_FAILURE;
break;
}
return TRUE;
}
static gboolean extract_status(struct wsp_header_iter *iter, void *user)
{
enum mms_message_delivery_status *out = user;
const unsigned char *p;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_SHORT)
return FALSE;
p = wsp_header_iter_get_val(iter);
switch (p[0]) {
case MMS_MESSAGE_DELIVERY_STATUS_EXPIRED:
case MMS_MESSAGE_DELIVERY_STATUS_RETRIEVED:
case MMS_MESSAGE_DELIVERY_STATUS_REJECTED:
case MMS_MESSAGE_DELIVERY_STATUS_DEFERRED:
case MMS_MESSAGE_DELIVERY_STATUS_UNRECOGNISED:
case MMS_MESSAGE_DELIVERY_STATUS_INDETERMINATE:
case MMS_MESSAGE_DELIVERY_STATUS_FORWARDED:
case MMS_MESSAGE_DELIVERY_STATUS_UNREACHABLE:
*out = p[0];
return TRUE;
}
return FALSE;
}
static gboolean extract_retrieve_status(struct wsp_header_iter *iter,
void *user)
{
unsigned char *out = user;
const unsigned char *p;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_SHORT)
return FALSE;
p = wsp_header_iter_get_val(iter);
/* Accept known codes, ignore unknown ones */
switch (p[0]) {
case MMS_MESSAGE_RETRIEVE_STATUS_OK:
case MMS_MESSAGE_RETRIEVE_STATUS_ERR_TRANS_FAILURE:
case MMS_MESSAGE_RETRIEVE_STATUS_ERR_TRANS_MESSAGE_NOT_FOUND:
case MMS_MESSAGE_RETRIEVE_STATUS_ERR_TRANS_NETWORK_PROBLEM:
case MMS_MESSAGE_RETRIEVE_STATUS_ERR_PERM_FAILURE:
case MMS_MESSAGE_RETRIEVE_STATUS_ERR_PERM_SERVICE_DENIED:
case MMS_MESSAGE_RETRIEVE_STATUS_ERR_PERM_MESSAGE_NOT_FOUND:
case MMS_MESSAGE_RETRIEVE_STATUS_ERR_PERM_CONTENT_UNSUPPORTED:
*out = p[0];
break;
}
return TRUE;
}
static gboolean extract_read_status(struct wsp_header_iter *iter, void *user)
{
enum mms_message_read_status *out = user;
const unsigned char *p;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_SHORT)
return FALSE;
p = wsp_header_iter_get_val(iter);
switch (p[0]) {
case MMS_MESSAGE_READ_STATUS_READ:
case MMS_MESSAGE_READ_STATUS_DELETED:
*out = p[0];
return TRUE;
}
return FALSE;
}
static gboolean extract_unsigned(struct wsp_header_iter *iter, void *user)
{
unsigned long *out = user;
const unsigned char *p;
unsigned int l;
unsigned int i;
if (wsp_header_iter_get_val_type(iter) != WSP_VALUE_TYPE_LONG)
return FALSE;
p = wsp_header_iter_get_val(iter);
l = wsp_header_iter_get_val_len(iter);
if (l > sizeof(unsigned long))
return FALSE;
for (i = 0, *out = 0; i < l; i++)
*out = *out << 8 | p[i];
return TRUE;
}
static header_handler handler_for_type(enum mms_header header)
{
switch (header) {
case MMS_HEADER_BCC:
return &extract_encoded_text_element;
case MMS_HEADER_CC:
return &extract_encoded_text_element;
case MMS_HEADER_CONTENT_LOCATION:
return &extract_text;
case MMS_HEADER_CONTENT_TYPE:
return &extract_text;
case MMS_HEADER_DATE:
return &extract_date;
case MMS_HEADER_DELIVERY_REPORT:
return &extract_boolean;
case MMS_HEADER_DELIVERY_TIME:
return &extract_absolute_relative_date;
case MMS_HEADER_EXPIRY:
return &extract_absolute_relative_date;
case MMS_HEADER_FROM:
return &extract_from;
case MMS_HEADER_MESSAGE_CLASS:
return &extract_message_class;
case MMS_HEADER_MESSAGE_ID:
return &extract_text;
case MMS_HEADER_MESSAGE_TYPE:
return &extract_short;
case MMS_HEADER_MMS_VERSION:
return &extract_short;
case MMS_HEADER_MESSAGE_SIZE:
return &extract_unsigned;
case MMS_HEADER_PRIORITY:
return &extract_priority;
case MMS_HEADER_READ_REPORT:
return &extract_boolean;
case MMS_HEADER_REPORT_ALLOWED:
return &extract_boolean;
case MMS_HEADER_RESPONSE_STATUS:
return &extract_rsp_status;
case MMS_HEADER_RESPONSE_TEXT: