@@ -6,89 +6,94 @@ For details, see http://sourceforge.net/projects/libb64
6
6
*/
7
7
8
8
#include "cdecode.h"
9
+ #include <stdint.h>
9
10
10
- int base64_decode_value (char value_in )
11
- {
12
- static const char decoding [] = {62 ,-1 ,-1 ,-1 ,63 ,52 ,53 ,54 ,55 ,56 ,57 ,58 ,59 ,60 ,61 ,-1 ,-1 ,-1 ,-2 ,-1 ,-1 ,-1 ,0 ,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 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,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 };
13
- static const char decoding_size = sizeof (decoding );
14
- value_in -= 43 ;
15
- if (value_in < 0 || value_in > decoding_size ) {
16
- return -1 ;
17
- }
18
- return decoding [(int )value_in ];
11
+ static int base64_decode_value_signed (int8_t value_in ){
12
+ static const int8_t decoding [] = {62 ,-1 ,-1 ,-1 ,63 ,52 ,53 ,54 ,55 ,56 ,57 ,58 ,59 ,60 ,61 ,-1 ,-1 ,-1 ,-2 ,-1 ,-1 ,-1 ,0 ,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 ,-1 ,-1 ,-1 ,-1 ,-1 ,-1 ,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 };
13
+ static const int8_t decoding_size = sizeof (decoding );
14
+ value_in -= 43 ;
15
+ if (value_in < 0 || value_in > decoding_size ) return -1 ;
16
+ return decoding [(int )value_in ];
17
+ }
18
+
19
+ void base64_init_decodestate (base64_decodestate * state_in ){
20
+ state_in -> step = step_a ;
21
+ state_in -> plainchar = 0 ;
19
22
}
20
23
21
- void base64_init_decodestate (base64_decodestate * state_in )
22
- {
23
- state_in -> step = step_a ;
24
- state_in -> plainchar = 0 ;
24
+ static int base64_decode_block_signed (const int8_t * code_in , const int length_in , int8_t * plaintext_out , base64_decodestate * state_in ){
25
+ const int8_t * codechar = code_in ;
26
+ int8_t * plainchar = plaintext_out ;
27
+ int8_t fragment ;
28
+
29
+ * plainchar = state_in -> plainchar ;
30
+
31
+ switch (state_in -> step ){
32
+ while (1 ){
33
+ case step_a :
34
+ do {
35
+ if (codechar == code_in + length_in ){
36
+ state_in -> step = step_a ;
37
+ state_in -> plainchar = * plainchar ;
38
+ return plainchar - plaintext_out ;
39
+ }
40
+ fragment = (int8_t )base64_decode_value_signed (* codechar ++ );
41
+ } while (fragment < 0 );
42
+ * plainchar = (fragment & 0x03f ) << 2 ;
43
+ case step_b :
44
+ do {
45
+ if (codechar == code_in + length_in ){
46
+ state_in -> step = step_b ;
47
+ state_in -> plainchar = * plainchar ;
48
+ return plainchar - plaintext_out ;
49
+ }
50
+ fragment = (int8_t )base64_decode_value_signed (* codechar ++ );
51
+ } while (fragment < 0 );
52
+ * plainchar ++ |= (fragment & 0x030 ) >> 4 ;
53
+ * plainchar = (fragment & 0x00f ) << 4 ;
54
+ case step_c :
55
+ do {
56
+ if (codechar == code_in + length_in ){
57
+ state_in -> step = step_c ;
58
+ state_in -> plainchar = * plainchar ;
59
+ return plainchar - plaintext_out ;
60
+ }
61
+ fragment = (int8_t )base64_decode_value_signed (* codechar ++ );
62
+ } while (fragment < 0 );
63
+ * plainchar ++ |= (fragment & 0x03c ) >> 2 ;
64
+ * plainchar = (fragment & 0x003 ) << 6 ;
65
+ case step_d :
66
+ do {
67
+ if (codechar == code_in + length_in ){
68
+ state_in -> step = step_d ;
69
+ state_in -> plainchar = * plainchar ;
70
+ return plainchar - plaintext_out ;
71
+ }
72
+ fragment = (int8_t )base64_decode_value_signed (* codechar ++ );
73
+ } while (fragment < 0 );
74
+ * plainchar ++ |= (fragment & 0x03f );
75
+ }
76
+ }
77
+ /* control should not reach here */
78
+ return plainchar - plaintext_out ;
25
79
}
26
80
27
- int base64_decode_block (const char * code_in , const int length_in , char * plaintext_out , base64_decodestate * state_in )
28
- {
29
- const char * codechar = code_in ;
30
- char * plainchar = plaintext_out ;
31
- char fragment ;
81
+ static int base64_decode_chars_signed (const int8_t * code_in , const int length_in , int8_t * plaintext_out ){
82
+ base64_decodestate _state ;
83
+ base64_init_decodestate (& _state );
84
+ int len = base64_decode_block_signed (code_in , length_in , plaintext_out , & _state );
85
+ if (len > 0 ) plaintext_out [len ] = 0 ;
86
+ return len ;
87
+ }
32
88
33
- * plainchar = state_in -> plainchar ;
89
+ int base64_decode_value (char value_in ){
90
+ return base64_decode_value_signed (* ((int8_t * ) & value_in ));
91
+ }
34
92
35
- switch (state_in -> step ) {
36
- while (1 ) {
37
- case step_a :
38
- do {
39
- if (codechar == code_in + length_in ) {
40
- state_in -> step = step_a ;
41
- state_in -> plainchar = * plainchar ;
42
- return plainchar - plaintext_out ;
43
- }
44
- fragment = (char )base64_decode_value (* codechar ++ );
45
- } while (fragment < 0 );
46
- * plainchar = (fragment & 0x03f ) << 2 ;
47
- case step_b :
48
- do {
49
- if (codechar == code_in + length_in ) {
50
- state_in -> step = step_b ;
51
- state_in -> plainchar = * plainchar ;
52
- return plainchar - plaintext_out ;
53
- }
54
- fragment = (char )base64_decode_value (* codechar ++ );
55
- } while (fragment < 0 );
56
- * plainchar ++ |= (fragment & 0x030 ) >> 4 ;
57
- * plainchar = (fragment & 0x00f ) << 4 ;
58
- case step_c :
59
- do {
60
- if (codechar == code_in + length_in ) {
61
- state_in -> step = step_c ;
62
- state_in -> plainchar = * plainchar ;
63
- return plainchar - plaintext_out ;
64
- }
65
- fragment = (char )base64_decode_value (* codechar ++ );
66
- } while (fragment < 0 );
67
- * plainchar ++ |= (fragment & 0x03c ) >> 2 ;
68
- * plainchar = (fragment & 0x003 ) << 6 ;
69
- case step_d :
70
- do {
71
- if (codechar == code_in + length_in ) {
72
- state_in -> step = step_d ;
73
- state_in -> plainchar = * plainchar ;
74
- return plainchar - plaintext_out ;
75
- }
76
- fragment = (char )base64_decode_value (* codechar ++ );
77
- } while (fragment < 0 );
78
- * plainchar ++ |= (fragment & 0x03f );
79
- }
80
- }
81
- /* control should not reach here */
82
- return plainchar - plaintext_out ;
93
+ int base64_decode_block (const char * code_in , const int length_in , char * plaintext_out , base64_decodestate * state_in ){
94
+ return base64_decode_block_signed ((int8_t * ) code_in , length_in , (int8_t * ) plaintext_out , state_in );
83
95
}
84
96
85
- int base64_decode_chars (const char * code_in , const int length_in , char * plaintext_out )
86
- {
87
- base64_decodestate _state ;
88
- base64_init_decodestate (& _state );
89
- int len = base64_decode_block (code_in , length_in , plaintext_out , & _state );
90
- if (len > 0 ) {
91
- plaintext_out [len ] = 0 ;
92
- }
93
- return len ;
97
+ int base64_decode_chars (const char * code_in , const int length_in , char * plaintext_out ){
98
+ return base64_decode_chars_signed ((int8_t * ) code_in , length_in , (int8_t * ) plaintext_out );
94
99
}
0 commit comments