/
wsputil.c
1149 lines (915 loc) · 24.2 KB
/
wsputil.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.
*
* 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 <string.h>
#include <glib.h>
#include "wsputil.h"
struct wsp_hex_str_entry {
unsigned int type;
const char *type_str;
};
/*
* http://www.wapforum.org/wina/wsp-content-type.htm
*/
static const char *content_types[] = {
"*/*",
"text/*",
"text/html",
"text/plain",
"text/x-hdml",
"text/x-ttml",
"text/x-vCalendar",
"text/x-vCard",
"text/vnd.wap.wml",
"text/vnd.wap.wmlscript",
"text/vnd.wap.wta-event",
"multipart/*",
"multipart/mixed",
"multipart/form-data",
"multipart/byterantes",
"multipart/alternative",
"application/*",
"application/java-vm",
"application/x-www-form-urlencoded",
"application/x-hdmlc",
"application/vnd.wap.wmlc",
"application/vnd.wap.wmlscriptc",
"application/vnd.wap.wta-eventc",
"application/vnd.wap.uaprof",
"application/vnd.wap.wtls-ca-certificate",
"application/vnd.wap.wtls-user-certificate",
"application/x-x509-ca-cert",
"application/x-x509-user-cert",
"image/*",
"image/gif",
"image/jpeg",
"image/tiff",
"image/png",
"image/vnd.wap.wbmp",
"application/vnd.wap.multipart.*",
"application/vnd.wap.multipart.mixed",
"application/vnd.wap.multipart.form-data",
"application/vnd.wap.multipart.byteranges",
"application/vnd.wap.multipart.alternative",
"application/xml",
"text/xml",
"application/vnd.wap.wbxml",
"application/x-x968-cross-cert",
"application/x-x968-ca-cert",
"application/x-x968-user-cert",
"text/vnd.wap.si",
"application/vnd.wap.sic",
"text/vnd.wap.sl",
"application/vnd.wap.slc",
"text/vnd.wap.co",
"application/vnd.wap.coc",
"application/vnd.wap.multipart.related",
"application/vnd.wap.sia",
"text/vnd.wap.connectivity-xml",
"application/vnd.wap.connectivity-wbxml",
"application/pkcs7-mime",
"application/vnd.wap.hashed-certificate",
"application/vnd.wap.signed-certificate",
"application/vnd.wap.cert-response",
"application/xhtml+xml",
"application/wml+xml",
"text/css",
"application/vnd.wap.mms-message",
"application/vnd.wap.rollover-certificate",
"application/vnd.wap.locc+wbxml",
"application/vnd.wap.loc+xml",
"application/vnd.syncml.dm+wbxml",
"application/vnd.syncml.dm+xml",
"application/vnd.syncml.notification",
"application/vnd.wap.xhtml+xml",
"application/vnd.wv.csp.cir",
"application/vnd.oma.dd+xml",
"application/vnd.oma.drm.message",
"application/vnd.oma.drm.content",
"application/vnd.oma.drm.rights+xml",
"application/vnd.oma.drm.rights+wbxml",
};
#define LAST_CONTENT_TYPE (sizeof(content_types) / sizeof(const char *))
/*
* http://www.wapforum.org/wina/push-app-id.htm
*/
static const struct wsp_hex_str_entry app_id[] = {
{ 0x04, "x-wap-application:mms.ua" },
{ 0x07, "x-wap-application:syncml.dm" },
{ 0x08, "x-wap-application:drm.ua" },
{ 0xFF, NULL }
};
/* http://www.wapforum.org/wina/wsp-content-type.htm */
static const struct wsp_hex_str_entry extension_mimetypes[] = {
{ 0x0201, "application/vnd.uplanet.cacheop-wbxml" },
{ 0x0202, "application/vnd.uplanet.signal" },
{ 0x0203, "application/vnd.uplanet.alert-wbxml" },
{ 0x0204, "application/vnd.uplanet.list-wbxml" },
{ 0x0205, "application/vnd.uplanet.listcmd-wbxml" },
{ 0x0206, "application/vnd.uplanet.channel-wbxml" },
{ 0x0207, "application/vnd.uplanet.provisioning-status-uri" },
{ 0x0208, "x-wap.multipart/vnd.uplanet.header-set" },
{ 0x0209, "application/vnd.uplanet.bearer-choice-wbxml" },
{ 0x020A, "application/vnd.phonecom.mmc-wbxml" },
{ 0x020B, "application/vnd.nokia.syncset+wbxml" },
{ 0x020C, "image/x-up-wpng" },
{ 0xFFFF, NULL },
};
static const struct wsp_hex_str_entry charset_assignments[] = {
{ 0x0000, "*" },
{ 0x07EA, "big5" },
{ 0x03E8, "iso-10646-ucs-2" },
{ 0x0004, "iso-8859-1" },
{ 0x0005, "iso-8859-2" },
{ 0x0006, "iso-8859-3" },
{ 0x0007, "iso-8859-4" },
{ 0x0008, "iso-8859-5" },
{ 0x0009, "iso-8859-6" },
{ 0x000A, "iso-8859-7" },
{ 0x000B, "iso-8859-8" },
{ 0x000C, "iso-8859-9" },
{ 0x0011, "shift_JIS" },
{ 0x0003, "us-ascii" },
{ 0x006A, "utf-8" },
{ 0x03F7, "utf-16" },
{ 0xFFFF, NULL },
};
/*
* Control Characters 0-8, 10-31 and 127. The tab character is omitted
* since it is included in the sep chars array and the most generic TEXT
* type of RFC 2616 explicitly allows tabs
*/
static const char *ctl_chars = "\x01\x02\x03\x04\x05\x06\x07\x08\x0A"
"\x0B\x0C\x0D\x0E\x0F\x10\x11\x12\x13\x14"
"\x15\x16\x17\x18\x19\x1A\x1B\x1C\x1D\x1E"
"\x1F\x7F";
static const char *sep_chars = "()<>@,;:\\\"/[]?={} \t";
static const char *decode_text_common(const unsigned char *pdu,
unsigned int len,
gboolean filter_ctl,
gboolean filter_sep,
unsigned int *consumed)
{
unsigned char *c;
c = memchr(pdu, '\0', len);
if (c == NULL)
return NULL;
c += 1;
/* RFC 2616 Section 2.2 */
if (filter_ctl && strpbrk((const char *) pdu, ctl_chars) != NULL)
return NULL;
if (filter_sep && strpbrk((const char *) pdu, sep_chars) != NULL)
return NULL;
if (consumed)
*consumed = c - pdu;
return (const char *) pdu;
}
const char *wsp_decode_token_text(const unsigned char *pdu, unsigned int len,
unsigned int *consumed)
{
return decode_text_common(pdu, len, TRUE, TRUE, consumed);
}
const char *wsp_decode_text(const unsigned char *pdu, unsigned int len,
unsigned int *consumed)
{
const char *r;
unsigned int fudge = 0;
if (*pdu == 127) {
pdu++;
if (*pdu < 128)
return NULL;
len -= 1;
fudge += 1;
}
r = decode_text_common(pdu, len, TRUE, FALSE, consumed);
if (consumed)
*consumed += fudge;
return r;
}
const char *wsp_decode_quoted_string(const unsigned char *pdu, unsigned int len,
unsigned int *consumed)
{
const char *text;
text = wsp_decode_text(pdu, len, consumed);
if (text == NULL)
return NULL;
if (*text != '"')
return NULL;
/* Skip initial quote */
text++;
return text;
}
gboolean wsp_decode_uintvar(const unsigned char *pdu, unsigned int len,
unsigned int *out_len, unsigned int *consumed)
{
unsigned int var;
unsigned int i;
unsigned int cont;
for (i = 0, var = 0, cont = TRUE; i < 5 && i < len && cont; i++) {
cont = pdu[i] & 0x80;
var = (var << 7) | (pdu[i] & 0x7f);
}
if (cont)
return FALSE;
if (out_len)
*out_len = var;
if (consumed)
*consumed = i;
return TRUE;
}
gboolean wsp_decode_integer(const unsigned char *pdu, unsigned int len,
unsigned int *out_val, unsigned int *consumed)
{
unsigned int var;
unsigned int i;
unsigned int count;
if (pdu[0] & 0x80) {
var = pdu[0] & 0x7f;
count = 1;
} else if (pdu[0] <= 30) {
unsigned int value_len = *pdu;
if (value_len > (len - 1))
return FALSE;
if (value_len > sizeof(unsigned int))
return FALSE;
var = 0;
for (i = 0; i < value_len; i++)
var = (var << 8) | pdu[i + 1];
count = value_len + 1;
} else
return FALSE;
if (out_val)
*out_val = var;
if (consumed)
*consumed = count;
return TRUE;
}
gboolean wsp_decode_field(const unsigned char *pdu, unsigned int max,
enum wsp_value_type *out_type,
const void **out_value,
unsigned int *out_len,
unsigned int *out_read)
{
const unsigned char *end = pdu + max;
const unsigned char *begin = pdu;
unsigned int len;
enum wsp_value_type value;
unsigned int consumed;
if (*pdu <= 30) {
len = *pdu;
pdu++;
if (pdu + len > end)
return FALSE;
value = WSP_VALUE_TYPE_LONG;
} else if (*pdu >= 128) {
len = 1;
value = WSP_VALUE_TYPE_SHORT;
} else if (*pdu == 31) {
pdu++;
if (pdu == end)
return FALSE;
if (wsp_decode_uintvar(pdu, end - pdu,
&len, &consumed) == FALSE)
return FALSE;
pdu += consumed;
if (pdu + len > end)
return FALSE;
value = WSP_VALUE_TYPE_LONG;
} else {
if (decode_text_common(pdu, end - pdu,
TRUE, FALSE, &len) == NULL)
return FALSE;
value = WSP_VALUE_TYPE_TEXT;
}
if (out_type)
*out_type = value;
if (out_value)
*out_value = pdu;
if (out_len)
*out_len = len;
if (out_read)
*out_read = pdu - begin + len;
return TRUE;
}
gboolean wsp_get_well_known_content_type(const char *text,
unsigned int *out_val)
{
unsigned int i;
for (i = 0; i < LAST_CONTENT_TYPE; i++) {
if (g_str_equal(text, content_types[i]) == TRUE) {
*out_val = i;
return TRUE;
}
}
return FALSE;
}
gboolean wsp_get_well_known_charset(const char *text, unsigned int *out_val)
{
unsigned int i;
for (i = 0; charset_assignments[i].type_str != NULL; i++) {
if (g_str_equal(charset_assignments[i].type_str,
text) == TRUE) {
*out_val = charset_assignments[i].type;
return TRUE;
}
}
return FALSE;
}
static const char *get_text_entry(unsigned int value,
const struct wsp_hex_str_entry *table)
{
unsigned int i;
for (i = 0; table[i].type_str != NULL; i++) {
if (table[i].type == value)
return table[i].type_str;
}
return NULL;
}
gboolean wsp_decode_content_type(const unsigned char *pdu, unsigned int max,
const void **out_value,
unsigned int *out_read,
unsigned int *out_param_len)
{
unsigned int param_len = 0;
unsigned int len;
const void *data;
enum wsp_value_type value_type;
unsigned int consumed;
if (wsp_decode_field(pdu, max, &value_type, &data,
&len, &consumed) != TRUE)
return FALSE;
if (value_type == WSP_VALUE_TYPE_LONG) {
unsigned int media_len;
unsigned int value_len;
if (wsp_decode_field(data, max, &value_type, &data,
&value_len, &media_len) != TRUE)
return FALSE;
param_len = len - media_len;
consumed -= param_len;
/* Handle Well-Known-Media Long-Integer case */
if (value_type == WSP_VALUE_TYPE_LONG) {
const unsigned char *pdu_val = data;
unsigned int var = 0;
unsigned int i;
if (value_len > sizeof(unsigned int))
return FALSE;
for (i = 0; i < value_len; i++)
var = (var << 8) | pdu_val[i + 1];
data = get_text_entry(var, extension_mimetypes);
}
}
if (value_type == WSP_VALUE_TYPE_SHORT) {
const unsigned char *pdu_val = data;
unsigned int val;
val = *pdu_val & 0x7f;
if (val >= LAST_CONTENT_TYPE)
return FALSE;
data = content_types[val];
}
if (out_value)
*out_value = data;
if (out_read)
*out_read = consumed;
if (out_param_len)
*out_param_len = param_len;
return TRUE;
}
gboolean wsp_decode_application_id(struct wsp_header_iter *iter,
const void **out_value)
{
const unsigned char *pdu_val = wsp_header_iter_get_val(iter);
unsigned int val;
unsigned int val_len;
unsigned int i;
switch (wsp_header_iter_get_val_type(iter)) {
case WSP_VALUE_TYPE_TEXT:
if (out_value)
*out_value = pdu_val;
break;
/*
* Well-known field values MUST be encoded using the
* compact binary formats
*/
case WSP_VALUE_TYPE_SHORT:
val = *pdu_val & 0x7f;
if (out_value)
*out_value = get_text_entry(val, app_id);
break;
case WSP_VALUE_TYPE_LONG:
val_len = wsp_header_iter_get_val_len(iter);
if (val_len > 2)
return FALSE;
for (i = 0, val = 0; i < val_len && i < sizeof(val); i++)
val = (val << 8) | pdu_val[i];
if (out_value)
*out_value = get_text_entry(val, app_id);
break;
}
return TRUE;
}
static inline gboolean is_content_type_header(const unsigned char *pdu,
struct wsp_header_iter *iter)
{
/* Check for Content-Type header */
if (iter->flags & WSP_HEADER_ITER_FLAG_DETECT_MULTIPART)
if (iter->code_page == 1 && *pdu == iter->content_type_header)
return TRUE;
return FALSE;
}
gboolean wsp_encode_uintvar(unsigned int value, unsigned char *dest,
unsigned int dest_size, unsigned int *written)
{
unsigned char d[5];
unsigned int count = 0;
/* Separate into 7-bit chunks, LS first */
while (value || !count) {
d[count++] = value & 0x7F;
value = value >> 7;
}
if (count > dest_size)
return FALSE;
*written = count;
/*
* Output to stream, MS first!
* 0x80 flag = "continue". LS byte does not have this flag.
*/
while (--count)
*dest++ = d[count] | 0x80;
*dest = d[count];
return TRUE;
}
gboolean wsp_encode_value_length(unsigned int len, unsigned char *dest,
unsigned int dest_size, unsigned int *written)
{
if (dest_size < 1)
return FALSE;
if (len <= 30) {
*dest = len;
*written = 1;
return TRUE;
}
/* 31 is escape for variable length int */
*dest++ = 31;
dest_size--;
if (wsp_encode_uintvar(len, dest, dest_size, written) == FALSE)
return FALSE;
*written += 1;
return TRUE;
}
gboolean wsp_encode_integer(unsigned int value, unsigned char *dest,
unsigned int dest_size, unsigned int *written)
{
unsigned char moi[sizeof(unsigned int)];
unsigned int count;
if (dest_size < 1)
return FALSE;
if (value < 0x80) {
*dest = value | 0x80;
*written = 1;
return TRUE;
}
for (count = 0; count < sizeof(unsigned int) && value; count++) {
moi[count] = value & 0xFF;
value = value >> 8;
}
if (count + 1 > dest_size)
return FALSE;
*written = count + 1;
*dest++ = count;
for (; count > 0; count--)
*dest++ = moi[count - 1];
return TRUE;
}
void wsp_header_iter_init(struct wsp_header_iter *iter,
const unsigned char *pdu, unsigned int len,
unsigned int flags)
{
iter->pdu = pdu;
iter->pos = 0;
iter->max = len;
iter->code_page = 1;
iter->flags = flags;
if (flags & WSP_HEADER_ITER_FLAG_DETECT_MMS_MULTIPART) {
iter->flags |= WSP_HEADER_ITER_FLAG_DETECT_MULTIPART;
iter->content_type_header = 0x84;
} else {
iter->content_type_header = 0x91;
}
}
gboolean wsp_header_iter_next(struct wsp_header_iter *iter)
{
const unsigned char *pdu = iter->pdu + iter->pos;
const unsigned char *end = iter->pdu + iter->max;
enum wsp_header_type header;
const void *hdr;
unsigned int consumed;
if (pdu == end)
return FALSE;
/*
* 8.4.2.6 Header
* The following rules are used to encode headers.
* Header = Message-header | Shift-sequence
* Shift-sequence = (Shift-delimiter Page-identity) |
* Short-cut-shift-delimiter
* Shift-delimiter = <Octet 127>
* Page-identity = <Any octet 1-255>
* Short-cut-shift-delimiter = <Any octet 1-31>
* Message-header = Well-known-header | Application-header
* Well-known-header = Well-known-field-name Wap-value
* Application-header = Token-text Application-specific-value
* Well-known-field-name = Short-integer
* Application-specific-value = Text-string
*/
while (*pdu == 127 || (*pdu >= 1 && *pdu <= 31)) {
if (iter->flags & WSP_HEADER_ITER_FLAG_REJECT_CP)
return FALSE;
if (*pdu == 127) {
pdu++;
if (pdu == end)
return FALSE;
iter->code_page = *pdu;
pdu++;
} else
iter->code_page = *pdu++;
}
if (pdu == end)
return FALSE;
if (*pdu >= 0x80) {
if (is_content_type_header(pdu, iter))
return FALSE;
header = WSP_HEADER_TYPE_WELL_KNOWN;
hdr = pdu;
pdu++;
} else {
if (wsp_decode_token_text(pdu, end - pdu, &consumed) == NULL)
return FALSE;
header = WSP_HEADER_TYPE_APPLICATION;
hdr = pdu;
pdu += consumed;
}
if (pdu == end)
return FALSE;
/*
* Section 8.4.1.2 of WAP-230:
* If the field name is encoded in text format, textual values MUST
* be used.
*/
if ((*pdu < 32 || *pdu > 127) && header == WSP_HEADER_TYPE_APPLICATION)
return FALSE;
if (wsp_decode_field(pdu, end - pdu, &iter->value_type,
&iter->value, &iter->len, &consumed) == FALSE)
return FALSE;
iter->header_type = header;
iter->header = hdr;
iter->pos = pdu + consumed - iter->pdu;
return TRUE;
}
unsigned char wsp_header_iter_get_code_page(struct wsp_header_iter *iter)
{
return iter->code_page;
}
gboolean wsp_header_iter_at_end(struct wsp_header_iter *iter)
{
if (iter->pos == iter->max)
return TRUE;
return FALSE;
}
gboolean wsp_header_iter_is_multipart(struct wsp_header_iter *iter)
{
const unsigned char *pdu = iter->pdu + iter->pos;
return is_content_type_header(pdu, iter);
}
enum wsp_header_type wsp_header_iter_get_hdr_type(struct wsp_header_iter *iter)
{
return iter->header_type;
}
const unsigned char *wsp_header_iter_get_pdu(struct wsp_header_iter *iter)
{
return iter->pdu;
}
const void *wsp_header_iter_get_hdr(struct wsp_header_iter *iter)
{
return iter->header;
}
enum wsp_value_type wsp_header_iter_get_val_type(struct wsp_header_iter *iter)
{
return iter->value_type;
}
const void *wsp_header_iter_get_val(struct wsp_header_iter *iter)
{
return iter->value;
}
unsigned int wsp_header_iter_get_val_len(struct wsp_header_iter *iter)
{
return iter->len;
}
gboolean wsp_multipart_iter_init(struct wsp_multipart_iter *mi,
struct wsp_header_iter *hi,
const void **out_content_type,
unsigned int *out_content_type_len)
{
const unsigned char *pdu = hi->pdu + hi->pos;
const unsigned char *end = hi->pdu + hi->max;
unsigned int consumed;
unsigned int ct_len;
/* Assume content-type header is well known */
if (pdu + 1 > end)
return FALSE;
pdu++;
/* Consume the Content-Type value of Content-Type header */
if (wsp_decode_field(pdu, end - pdu,
NULL, NULL, NULL, &consumed) == FALSE)
return FALSE;
pdu += consumed;
ct_len = consumed;
/*
* Consume the uinvar specifying the number of parts. This is set to
* 0 in later specifications and can be safely ignored
*/
if (wsp_decode_uintvar(pdu, end - pdu, NULL, &consumed) == FALSE)
return FALSE;
memset(mi, 0, sizeof(*mi));
mi->pdu = hi->pdu + hi->pos;
mi->max = hi->max - hi->pos;
mi->pos = pdu + consumed - mi->pdu;
if (out_content_type)
*out_content_type = mi->pdu + 1;
if (out_content_type_len)
*out_content_type_len = ct_len;
return TRUE;
}
gboolean wsp_multipart_iter_next(struct wsp_multipart_iter *mi)
{
const unsigned char *pdu = mi->pdu + mi->pos;
const unsigned char *end = mi->pdu + mi->max;
unsigned int headers_len;
unsigned int body_len;
unsigned int consumed;
if (wsp_decode_uintvar(pdu, end - pdu,
&headers_len, &consumed) == FALSE)
return FALSE;
pdu += consumed;
if (wsp_decode_uintvar(pdu, end - pdu, &body_len, &consumed) == FALSE)
return FALSE;
pdu += consumed;
if (pdu + headers_len + body_len > end)
return FALSE;
/* Consume the Content-Type value */
if (wsp_decode_field(pdu, end - pdu,
NULL, NULL, NULL, &consumed) == FALSE)
return FALSE;
mi->content_type = pdu;
mi->content_type_len = consumed;
mi->headers = pdu + consumed;
mi->headers_len = headers_len - consumed;
mi->body = pdu + headers_len;
mi->body_len = body_len;
mi->pos = pdu - mi->pdu + headers_len + body_len;
return TRUE;
}
const void *wsp_multipart_iter_get_content_type(struct wsp_multipart_iter *mi)
{
return mi->content_type;
}
unsigned int wsp_multipart_iter_get_content_type_len(
struct wsp_multipart_iter *mi)
{
return mi->content_type_len;
}
const void *wsp_multipart_iter_get_hdr(struct wsp_multipart_iter *mi)
{
return mi->headers;
}
unsigned int wsp_multipart_iter_get_hdr_len(struct wsp_multipart_iter *mi)
{
return mi->headers_len;
}
const void *wsp_multipart_iter_get_body(struct wsp_multipart_iter *mi)
{
return mi->body;
}
unsigned int wsp_multipart_iter_get_body_len(struct wsp_multipart_iter *mi)
{
return mi->body_len;
}
gboolean wsp_multipart_iter_close(struct wsp_multipart_iter *mi,
struct wsp_header_iter *hi)
{
if (mi->pos != mi->max)
return FALSE;
hi->pos += mi->pos;
return TRUE;
}
void wsp_parameter_iter_init(struct wsp_parameter_iter *pi,
const unsigned char *pdu, unsigned int len)
{
pi->pdu = pdu;
pi->max = len;
pi->pos = 0;
}
gboolean wsp_parameter_iter_next(struct wsp_parameter_iter *pi,
struct wsp_parameter *out)
{
const unsigned char *pdu = pi->pdu + pi->pos;
const unsigned char *end = pi->pdu + pi->max;
unsigned int token;
unsigned int consumed;
const char *untyped;
const char *value;
/* Well known parameter token */
if (wsp_decode_integer(pdu, end - pdu, &token, &consumed) == TRUE) {
pdu += consumed;
switch (token) {
case WSP_PARAMETER_TYPE_LEVEL:
case WSP_PARAMETER_TYPE_DIFFERENCES:
if (*pdu & 0x80) {
unsigned int i = *pdu & 0x7f;
pdu += 1;
pi->pos = pdu - pi->pdu;
if (out) {
out->type = token;
out->value = WSP_PARAMETER_VALUE_INT;
out->integer = i;
}
return TRUE;
}
/* Fall through to the string case */
case WSP_PARAMETER_TYPE_NAME_DEFUNCT:
case WSP_PARAMETER_TYPE_FILENAME_DEFUNCT:
case WSP_PARAMETER_TYPE_START_DEFUNCT:
case WSP_PARAMETER_TYPE_START_INFO_DEFUNCT:
case WSP_PARAMETER_TYPE_COMMENT_DEFUNCT:
case WSP_PARAMETER_TYPE_DOMAIN_DEFUNCT:
case WSP_PARAMETER_TYPE_PATH_DEFUNCT:
case WSP_PARAMETER_TYPE_NAME:
case WSP_PARAMETER_TYPE_FILENAME:
case WSP_PARAMETER_TYPE_START:
case WSP_PARAMETER_TYPE_START_INFO:
case WSP_PARAMETER_TYPE_COMMENT:
case WSP_PARAMETER_TYPE_DOMAIN:
case WSP_PARAMETER_TYPE_PATH:
case WSP_PARAMETER_TYPE_MAC:
{
const char *text = wsp_decode_text(pdu, end - pdu,
&consumed);
if (text == NULL)
return FALSE;
pdu += consumed;
pi->pos = pdu - pi->pdu;
if (out) {
out->type = token;
out->value = WSP_PARAMETER_VALUE_TEXT;
out->text = text;
}
return TRUE;
}
case WSP_PARAMETER_TYPE_TYPE:
case WSP_PARAMETER_TYPE_SIZE:
case WSP_PARAMETER_TYPE_MAX_AGE:
{
unsigned int i;
if (wsp_decode_integer(pdu, end - pdu,
&i, &consumed) == FALSE)
return FALSE;
pdu += consumed;
pi->pos = pdu - pi->pdu;
if (out) {
out->type = token;
out->value = WSP_PARAMETER_VALUE_INT;
out->integer = i;
}
return TRUE;
}