app_base64.c
33.3 KB
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
/*****************************************************************************
* Copyright Statement:
* --------------------
* This software is protected by Copyright and the information contained
* herein is confidential. The software may not be copied and the information
* contained herein may not be used or disclosed except with the written
* permission of MediaTek Inc. (C) 2005
*
* BY OPENING THIS FILE, BUYER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
* THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
* RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO BUYER ON
* AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
* NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
* SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
* SUPPLIED WITH THE MEDIATEK SOFTWARE, AND BUYER AGREES TO LOOK ONLY TO SUCH
* THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. MEDIATEK SHALL ALSO
* NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO BUYER'S
* SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN FORUM.
*
* BUYER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND CUMULATIVE
* LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
* AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
* OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY BUYER TO
* MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
*
* THE TRANSACTION CONTEMPLATED HEREUNDER SHALL BE CONSTRUED IN ACCORDANCE
* WITH THE LAWS OF THE STATE OF CALIFORNIA, USA, EXCLUDING ITS CONFLICT OF
* LAWS PRINCIPLES. ANY DISPUTES, CONTROVERSIES OR CLAIMS ARISING THEREOF AND
* RELATED THERETO SHALL BE SETTLED BY ARBITRATION IN SAN FRANCISCO, CA, UNDER
* THE RULES OF THE INTERNATIONAL CHAMBER OF COMMERCE (ICC).
*
*****************************************************************************/
/*****************************************************************************
*
* Filename:
* ---------
* app_base64.c
*
* Project:
* --------
* MAUI
*
* Description:
* ------------
* This file contains Base64 decode / encode functions.
*
* Author:
* -------
* -------
*
*============================================================================
* HISTORY
* Below this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*------------------------------------------------------------------------------
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
* removed!
* removed!
* removed!
*
*------------------------------------------------------------------------------
* Upper this line, this part is controlled by PVCS VM. DO NOT MODIFY!!
*============================================================================
****************************************************************************/
#include "kal_release.h"
#include "app_base64.h"
#include "kal_general_types.h"
#include "string.h"
#define BASE64_WARING()
#define BASE64_ASSERT(b)
#define BASE64_LINE_WRAP_COUNT 76
#define DPAD 0x7D
#define DWSP 0x7E
#define DNIL 0x7F
#define BASE64_ENCODE_1(dst,src) do{ \
dst[0] = BASE64_ENCODE_TABLE[src[0] >> 2]; \
dst[1] = BASE64_ENCODE_TABLE[(src[0] << 4) & 0x3f]; \
dst[2] = '='; \
dst[3] = '='; \
} while(0)
#define BASE64_ENCODE_2(dst,src) do{ \
dst[0] = BASE64_ENCODE_TABLE[src[0] >> 2]; \
dst[1] = BASE64_ENCODE_TABLE[((src[0] << 4) + (src[1] >> 4)) & 0x3f]; \
dst[2] = BASE64_ENCODE_TABLE[(src[1] << 2) & 0x3f]; \
dst[3] = '='; \
} while(0)
#define BASE64_ENCODE_3(dst,src) do{ \
dst[0] = BASE64_ENCODE_TABLE[src[0] >> 2]; \
dst[1] = BASE64_ENCODE_TABLE[((src[0] << 4) + (src[1] >> 4)) & 0x3f]; \
dst[2] = BASE64_ENCODE_TABLE[((src[1] << 2) + (src[2] >> 6)) & 0x3f]; \
dst[3] = BASE64_ENCODE_TABLE[src[2] & 0x3f]; \
} while(0)
#define BASE64_DECODE_2(dst,src) do{ \
dst[0] = (src[0] << 2) | (src[1] >> 4); \
} while(0)
#define BASE64_DECODE_3(dst,src) do{ \
dst[0] = (src[0] << 2) | (src[1] >> 4); \
dst[1] = (src[1] << 4) | (src[2] >> 2); \
} while(0)
#define BASE64_DECODE_4(dst,src) do{ \
dst[0] = (src[0] << 2) | (src[1] >> 4); \
dst[1] = (src[1] << 4) | (src[2] >> 2); \
dst[2] = (src[2] << 6) | (src[3]); \
} while(0)
static kal_char BASE64_ENCODE_TABLE[64] =
{
'A','B','C','D','E','F','G','H', 'I','J','K','L','M','N','O','P',
'Q','R','S','T','U','V','W','X', 'Y','Z','a','b','c','d','e','f',
'g','h','i','j','k','l','m','n', 'o','p','q','r','s','t','u','v',
'w','x','y','z','0','1','2','3', '4','5','6','7','8','9','+','/'
};
static kal_char BASE64_DECODE_TABLE[256] =
{
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DWSP, DWSP, DWSP, DNIL, DWSP, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DWSP, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, 0x3E, DNIL, DNIL, DNIL, 0x3F,
0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, 0x3B,
0x3C, 0x3D, DNIL, DNIL, DNIL, DPAD, DNIL, DNIL,
DNIL, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,
0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16,
0x17, 0x18, 0x19, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20,
0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28,
0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, 0x2F, 0x30,
0x31, 0x32, 0x33, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL,
DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL, DNIL
};
static void check_illegal_pad(applib_base64_part_context* cntx,
const kal_char *src,kal_int32 srcl);
/*****************************************************************************
* FUNCTION
* applib_base64_part_encode_init
* DESCRIPTION
* This function initializes the STREAM-STYLE part by part BASE64
* encode process and calculate the needed output buffer size per-part.
* PARAMETERS
* cntx [IN] point to the applib_base64_part_context
* max_part_size [IN] the number of MAX input buffer size per-part.
* line_wrap_count [IN] line wrap count
* RETURNS
* negative: invalid parameter.
* ow: the MIN output buffer size needed for the per-parts process.
*****************************************************************************/
kal_int32 applib_base64_part_encode_init(
applib_base64_part_context *cntx,
kal_int32 max_part_buf_size,
kal_bool auto_line_wrap)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_int32 size;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (cntx == NULL)
{
BASE64_ASSERT(0);
return -1;
}
size = 0;
if (max_part_buf_size > 0)
{
size = max_part_buf_size + 2; /* the last part remainder */
size = (size + 2 ) / 3 * 4; /* 3bytes to 4 bytes */
}
cntx->wrap_max = 0;
if (auto_line_wrap)
{
cntx->wrap_max = BASE64_LINE_WRAP_COUNT; /* 76 */
if (size > 0)
{
size += (size / BASE64_LINE_WRAP_COUNT + 1) * 2;
}
}
cntx->buf_cnt = 0;
cntx->wrap_count = 0;
return size;
}
/*****************************************************************************
* FUNCTION
* applib_base64_part_encode_append
* DESCRIPTION
* This function encode next part data to BASE64.
* PARAMETERS
* cntx [IN] Part process context
* src [IN] Source data
* srcl [IN/OUT] Source data length
* dest [IN/OUT] Encoded data
* destl [IN] Destination buffer length
* RETURNS
* negative : parameter error
* otherwise : length of encoded bytes
*****************************************************************************/
kal_int32 applib_base64_part_encode_append(
applib_base64_part_context *cntx,
const kal_char *src,
kal_int32 *srcl,
kal_char *dst,
kal_int32 dstl)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_int32 i_input,i_output;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (cntx == NULL || cntx->buf_cnt > 2 ||
srcl == NULL || (*srcl > 0 && src == NULL))
{
BASE64_ASSERT(0);
return -1;
}
/* encode it */
i_input = i_output = 0;
while(i_input < *srcl)
{
BASE64_ASSERT(cntx->buf_cnt <= 2);
cntx->buf[cntx->buf_cnt++] = src[i_input++];
if (cntx->buf_cnt < 3)
{
continue;
}
/* cntx->buf_cnt == 3 */
if (i_output + 4 > dstl)
{
/* roll back */
i_input--;
cntx->buf_cnt--;
break;
}
if (dst == NULL)
{
BASE64_ASSERT(0);
return -1;
}
/* Encode the three-bytes to four bytes */
BASE64_ENCODE_3((dst+i_output),cntx->buf);
i_output += 4;
cntx->buf_cnt = 0;
/* line break */
if (cntx->wrap_max == 0)
{
/* No need to break line */
continue;
}
cntx->wrap_count += 4;
if (cntx->wrap_count < cntx->wrap_max)
{
/* Not reach the wrap count */
continue;
}
/* Check if output buffer is enough */
if (i_output + 2 > dstl)
{
/* roll back one bytes */
i_input--;
cntx->buf_cnt = 2;
i_output -= 4;
cntx->wrap_count -= 4;
break;
}
dst[i_output++] = '\r';
dst[i_output++] = '\n';
cntx->wrap_count = 0;
}
if (*srcl != i_input)
{
/* Waring */
BASE64_WARING();
}
BASE64_ASSERT(cntx->buf_cnt <= 2);
*srcl = i_input;
return i_output;
}
/*****************************************************************************
* FUNCTION
* applib_base64_part_encode_finish
* DESCRIPTION
* This function finish the part-by-part BASE64 encode.
* PARAMETERS
* cntx [IN] Part process context
* dest [IN/OUT] Encoded data
* destl [IN] Destination buffer length
* RETURNS
* negative : parameter error or buffer NOT enough
* otherwise : length of encoded bytes
*****************************************************************************/
kal_int32 applib_base64_part_encode_finish(
applib_base64_part_context* cntx,
kal_char *dst,
kal_int32 dstl)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (cntx == NULL || cntx->buf_cnt > 2)
{
BASE64_ASSERT(0);
return -1;
}
/* no unprocessed data */
if (cntx->buf_cnt == 0)
{
if (cntx->wrap_max > 0 && cntx->wrap_count > 0)
{
if (dstl < 2)
{
BASE64_ASSERT(0);
return -1;
}
dst[0] = '\r';
dst[1] = '\n';
return 2;
}
else
{
return 0;
}
}
if (dst == NULL || dstl < 4)
{
BASE64_ASSERT(0);
return -1;
}
if (cntx->buf_cnt == 1)
{
BASE64_ENCODE_1(dst,cntx->buf);
}
else if (cntx->buf_cnt == 2)
{
BASE64_ENCODE_2(dst,cntx->buf);
}
else
{
BASE64_ASSERT(0);
}
if (cntx->wrap_max > 0 && cntx->wrap_count > 0)
{
if (dstl < 6)
{
BASE64_ASSERT(0);
return -1;
}
dst[4] = '\r';
dst[5] = '\n';
return 6;
}
else
{
return 4;
}
}
/*****************************************************************************
* FUNCTION
* applib_base64_part_decode_init
* DESCRIPTION
* This function initializes the STREAM-STYLE part by part BASE64
* decode process and calculate the needed output buffer size per-part.
* PARAMETERS
* cntx [IN] point to the applib_base64_part_context
* max_part_size [IN] the number of MAX input buffer size per-part.
* RETURNS
* negative: invalid parameter.
* ow: the MIN output buffer size needed for the per-parts process.
*****************************************************************************/
kal_int32 applib_base64_part_decode_init(
applib_base64_part_context *cntx,
kal_int32 max_part_buf_size)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_int32 size;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (cntx == NULL)
{
BASE64_ASSERT(0);
return -1;
}
cntx->buf_cnt = 0;
cntx->finish_flag = 0;
cntx->illchr_flag = 0;
cntx->illpad_flag = 0;
size = 0;
if (max_part_buf_size > 0)
{
size = max_part_buf_size + 3; /* the last part remainder */
size = (size + 3 ) / 4 * 3; /* 4Bytes to 3Bytes */
}
return size;
}
/*****************************************************************************
* FUNCTION
* applib_base64_part_decode_append
* DESCRIPTION
* This function accepts next part of source data;
* decode them from BASE64 format
* PARAMETERS
* cntx [IN] point to the applib_base64_part_context
* src [IN] pointer to the input buffer
* srcl [IN] length of the input buffer
* dest [IN/OUT] pointer to the output buffer
* destl [IN] length of the output buffer
* RETURNS
* negative: Invalid paramter.
* ow: the number of bytes written to output buffer.
*****************************************************************************/
kal_int32 applib_base64_part_decode_append(
applib_base64_part_context* cntx,
const kal_char *src,
kal_int32 *srcl,
kal_char *dst,
kal_int32 dstl)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_char c;
kal_int32 i_input,i_output;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (cntx == NULL ||
(cntx->finish_flag && cntx->buf_cnt > 4) ||
(!cntx->finish_flag && cntx->buf_cnt > 3) ||
srcl == NULL ||
(*srcl > 0 && src == NULL))
{
BASE64_ASSERT(0);
return -1;
}
if (cntx->finish_flag)
{
/* the "=" had been reached before */
if (!cntx->illpad_flag)
{
check_illegal_pad(cntx,src,*srcl);
}
return 0;
}
/*
* begin decode this part content
* Skip all NOT BASE64 CHARACTER
* End the decode if '=' was founded.
*/
i_input = i_output = 0;
while(i_input < *srcl)
{
/* assert check */
BASE64_ASSERT(cntx->buf_cnt <= 3);
c = BASE64_DECODE_TABLE[(kal_uint8)(src[i_input++])];
if (c == DWSP)
{
continue;
}
else if (c == DNIL)
{
cntx->illchr_flag = 1;
continue;
}
else if (c == DPAD)
{
cntx->finish_flag = 1;
if (cntx->buf_cnt < 2)
{
BASE64_WARING();
cntx->illpad_flag = 1;
break;
}
if (i_output + cntx->buf_cnt - 1 > dstl)
{
/* output buffer is NOT enough */
/* roll back */
cntx->finish_flag = 0;
i_input--;
break;
}
if (cntx->buf_cnt == 2)
{
BASE64_DECODE_2((dst+i_output),cntx->buf);
}
else
{
BASE64_DECODE_3((dst+i_output),cntx->buf);
}
i_output += cntx->buf_cnt - 1;
cntx->buf[cntx->buf_cnt++] = c;
check_illegal_pad(cntx,src+i_input,*srcl-i_input);
i_input = *srcl; /* all data have been processed */
break;
}
else
{
/* Decode the bytes upto 4-group */
cntx->buf[cntx->buf_cnt++] = c;
/* four bytes readed,DECODE IT */
if (cntx->buf_cnt < 4)
{
continue;
}
if (i_output + 3 > dstl)
{
/* roll back */
cntx->buf_cnt--;
i_input--;
break;
}
BASE64_DECODE_4((dst+i_output),cntx->buf);
i_output += 3;
cntx->buf_cnt = 0;
}
/* assert check */
BASE64_ASSERT(cntx->buf_cnt <= 3);
}
if (*srcl != i_input)
{
/* warning */
BASE64_WARING();
}
*srcl = i_input;
return i_output;
}
/*****************************************************************************
* FUNCTION
* applib_base64_part_decode_finish
* DESCRIPTION
* This function finishes the part-by-part decoding process.
* PARAMETERS
* cntx [IN] point to the applib_base64_part_context
* illegal_format [IN/OUT] pointer to the illegal format flag
* RETURNS
* 0: success
* ow: the context was NULL
*****************************************************************************/
kal_int32 applib_base64_part_decode_finish(
applib_base64_part_context* cntx,
kal_bool* illegal_format)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (cntx == NULL)
{
BASE64_ASSERT(0);
return -1;
}
/* No need to report the illegal flag */
if (cntx->finish_flag)
{
if (cntx->buf_cnt != 4)
{
BASE64_WARING();
cntx->illpad_flag = 1;
}
}
else
{
if (cntx->buf_cnt != 0)
{
BASE64_WARING();
cntx->illpad_flag = 1;
}
}
if (illegal_format != NULL)
{
if (cntx->illchr_flag || cntx->illpad_flag)
{
*illegal_format = KAL_TRUE;
}
else
{
*illegal_format = KAL_FALSE;
}
}
return 0;
}
/*****************************************************************************
* FUNCTION
* check_illegal_pad
* DESCRIPTION
* This function check if the input data is illegal formatted.
* PARAMETERS
* cntx [IN] point to the applib_base64_part_context
* src [IN] pointer to the input buffer
* srcl [IN] length of the input buffer
* RETURNS
* void
*****************************************************************************/
static void check_illegal_pad(applib_base64_part_context* cntx,
const kal_char *src,
kal_int32 srcl)
{
kal_int32 pos;
kal_char c;
if (cntx->illpad_flag)
{
return;
}
if (srcl <= 0)
{
return;
}
BASE64_ASSERT(src != NULL);
pos = 0;
if (cntx->buf_cnt == 3)
{
while(pos < srcl)
{
c = BASE64_DECODE_TABLE[(kal_uint8)src[pos++]];
if (c == DPAD)
{
cntx->buf[cntx->buf_cnt++] = DPAD;
break;
}
else if (c == DWSP)
{
continue;
}
else
{
BASE64_WARING();
cntx->illpad_flag = 1;
/* no need to check the following character */
return;
}
}
}
if (cntx->buf_cnt == 4)
{
while(pos < srcl)
{
c = BASE64_DECODE_TABLE[(kal_uint8)src[pos++]];
if (c != DWSP)
{
BASE64_WARING();
cntx->illpad_flag = 1;
/* no need to check the following character */
return;
}
}
}
}
/*****************************************************************************
* FUNCTION
* applib_base64_encode_basic
* DESCRIPTION
* This function is used to encode given string into base64 encoded string.
* PARAMETERS
* src [IN] Source data
* srcl [IN] Source data length
* dest [IN/OUT] Encoded data
* destl [IN] Destination buffer length
* RETURNS
* negative: error
* ow:length of encoded string
*****************************************************************************/
kal_int32 applib_base64_encode_basic(const kal_char *src, kal_int32 srcl,
kal_char *dst,kal_int32 dstl,kal_bool auto_line_wrap)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_int32 len, size, temp, moved;
applib_base64_part_context cntx;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (srcl <= 0)
{
return 0;
}
/* cal the needed output buffer size */
size = (srcl + 2) / 3 * 4;
if (auto_line_wrap)
{
size += (size / BASE64_LINE_WRAP_COUNT + 1) * 2;
}
if (dst == NULL)
{
/* just report the needed output buffer size */
return size;
}
if (src == NULL || dstl < size)
{
/* the output buffer is not enough */
BASE64_ASSERT(0);
return -1;
}
moved = 0;
if (src + srcl > dst && src < dst + (size - srcl))
{
moved = dstl - srcl;
memmove((kal_char*)(src + moved), src, srcl);
}
/* encode it */
applib_base64_part_encode_init(&cntx, 0, auto_line_wrap);
temp = srcl;
len = applib_base64_part_encode_append(&cntx, src + moved, &temp, dst, dstl);
BASE64_ASSERT(temp == srcl);
BASE64_ASSERT(len <= dstl);
temp = applib_base64_part_encode_finish(&cntx, dst + len, dstl - len);
BASE64_ASSERT(temp >= 0);
len += temp;
BASE64_ASSERT(len <= dstl);
return len;
}
/*****************************************************************************
* FUNCTION
* applib_base64_decode_basic
* DESCRIPTION
* This function is used to decode Base64 string.
* PARAMETERS
* src [IN] Pointer to the buffer containing encoded data
* srcl [IN] Length of the encoded data
* dest [IN/OUT] Pointer to the buffer containing decoded data
* destl [IN] Length of destination buffer.
* RETURNS
* negative: error
* ow: Length of the decoded data
*****************************************************************************/
kal_int32 applib_base64_decode_basic(const kal_char *src, kal_int32 srcl,
kal_char *dst,kal_int32 dstl)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_int32 len,temp;
applib_base64_part_context cntx;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (srcl <= 0)
{
/* No data need to be processed */
return 0;
}
if (dst == NULL)
{
/* just report the needed output buffer size */
len = srcl / 4 * 3;
if (srcl > 1 && src[srcl-1] == '=')
{
len--;
}
if (srcl > 2 && src[srcl-1] == '=' && src[srcl - 2] == '=')
{
len --;
}
return len;
}
/* decode it */
temp = srcl;
applib_base64_part_decode_init(&cntx,0);
len = applib_base64_part_decode_append(&cntx,src,&temp,dst,dstl);
applib_base64_part_decode_finish(&cntx,NULL);
if (len < 0 || temp != srcl)
{
/* the output buffer is not enough */
BASE64_ASSERT(0);
return -1;
}
return len;
}
/*
* NOTE:
* the Functions below will be obsoleted!
* them are here just for compatible with old code.
*/
/*****************************************************************************
* FUNCTION
* applib_base64_decode
* DESCRIPTION
* This function is used to decode Base64 string.
* PARAMETERS
* src [IN] Pointer to the buffer containing encoded data
* srcl [IN] Length of the encoded data
* dest [IN/OUT] Pointer to the buffer containing decoded data
* destl [IN] Length of destination buffer Will append null character.
* RETURNS
* negative: error
* ow: Length of the decoded data (not include null character)
*****************************************************************************/
kal_int32 applib_base64_decode(kal_char *src, kal_uint32 srcl,
kal_char *dest, kal_uint32 destl)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_int32 ret;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (dest == NULL)
{
return -1;
}
ret = applib_base64_decode_basic(src,srcl,dest,destl);
if (ret <= 0 || ret >= (kal_int32)destl)
{
return -1;
}
/*
* add the null-end,
* but the return length not include the null-terminator
*/
dest[ret] = 0;
return ret;
}
/*****************************************************************************
* FUNCTION
* applib_base64_decode_len
* DESCRIPTION
* This function is used to calculate decoded base64 string length
* PARAMETERS
* len [IN] Source data byte length
* RETURNS
* negative: error
* ow:length of encoded string
*****************************************************************************/
kal_uint32 applib_base64_decode_len(const kal_char *str, kal_uint32 len)
{
/* kal_uint32 new_len = (len / 4) * 3;
if ((len > 0) && (str[len - 1] == '='))
new_len--;
if ((len > 1) && (str[len - 2] == '='))
new_len--;
return new_len;*/
return len;
}
/*****************************************************************************
* FUNCTION
* applib_find_base64_boundary
* DESCRIPTION
* This function finds the boundary in the base64 encoded text.
* PARAMETERS
* ptr [IN] Pointer to the encoded buffer
* len [IN] Length of encoded data
* RETURNS
* number of bytes shall be skipped.
*****************************************************************************/
kal_uint16 applib_find_base64_boundary(kal_char *ptr, kal_int32 len)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_int32 i;
/* with initial values */
kal_uint16 left_over_bytes = len;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (len <= 0) /* no data */
{
return 0;
}
for (i = len - 1; i > 0; i--)
{
if (*(ptr + i - 1) == '\r' && *(ptr + i) == '\n')
{
left_over_bytes = len - i + 1 - 2 /* crlf */ ;
break;
}
/* Handle the case that the line is ended with only LF not CRLF */
if (*(ptr + i) == '\n')
{
left_over_bytes = len - i + 1 - 1 /* lf */ ;
break;
}
}
return left_over_bytes;
} /* end of applib_find_base64_boundary */
/*****************************************************************************
* FUNCTION
* applib_base64_encode
* DESCRIPTION
* This function is used to encode given string into base64 encoded string.
* PARAMETERS
* src [IN] Source data
* srcl [IN] Source data length
* dest [IN/OUT] Encoded data
* destl [IN] Destination buffer length
* RETURNS
* negative: error
* ow:length of encoded string
*****************************************************************************/
kal_int32 applib_base64_encode(kal_char *src, kal_uint32 srcl,
kal_char *dest, kal_uint32 destl)
{
/*----------------------------------------------------------------*/
/* Local Variables */
/*----------------------------------------------------------------*/
kal_int32 ret;
/*----------------------------------------------------------------*/
/* Code Body */
/*----------------------------------------------------------------*/
if (dest == NULL)
{
return -1;
}
ret = applib_base64_encode_basic(src,srcl,dest,destl,KAL_TRUE);
if (ret <= 0 || (kal_uint32)ret >= destl)
{
return -1;
}
/*
* add the null-end,
* but the return length not include the null-terminator
*/
dest[ret] = 0;
return ret;
}
/*****************************************************************************
* FUNCTION
* applib_base64_encode_len
* DESCRIPTION
* This function is used to calculate converted base64 string length
* PARAMETERS
* len [IN] Source data byte length
* RETURNS
* negative: error
* ow:length of encoded string
*****************************************************************************/
kal_uint32 applib_base64_encode_len(kal_uint32 len)
{
/*return ((len + 2) / 3) * 4;*/
return (2 * len);
}