/src/assimp/code/AssetLib/Assjson/cencode.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | cencoder.c - c source to a base64 encoding algorithm implementation |
3 | | |
4 | | This is part of the libb64 project, and has been placed in the public domain. |
5 | | For details, see http://sourceforge.net/projects/libb64 |
6 | | */ |
7 | | |
8 | | #include "cencode.h" // changed from <B64/cencode.h> |
9 | | |
10 | | static const int CHARS_PER_LINE = 72; |
11 | | |
12 | | #ifdef _MSC_VER |
13 | | #pragma warning(push) |
14 | | #pragma warning(disable : 4244) |
15 | | #endif // _MSC_VER |
16 | | |
17 | | void base64_init_encodestate(base64_encodestate* state_in) |
18 | 0 | { |
19 | 0 | state_in->step = step_A; |
20 | 0 | state_in->result = 0; |
21 | 0 | state_in->stepcount = 0; |
22 | 0 | } |
23 | | |
24 | | char base64_encode_value(char value_in) |
25 | 0 | { |
26 | 0 | static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
27 | 0 | if (value_in > 63) return '='; |
28 | 0 | return encoding[(int)value_in]; |
29 | 0 | } |
30 | | |
31 | | int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in) |
32 | 0 | { |
33 | 0 | const char* plainchar = plaintext_in; |
34 | 0 | const char* const plaintextend = plaintext_in + length_in; |
35 | 0 | char* codechar = code_out; |
36 | 0 | char result; |
37 | 0 | char fragment; |
38 | |
|
39 | 0 | result = state_in->result; |
40 | |
|
41 | 0 | switch (state_in->step) |
42 | 0 | { |
43 | 0 | while (1) |
44 | 0 | { |
45 | 0 | case step_A: |
46 | 0 | if (plainchar == plaintextend) |
47 | 0 | { |
48 | 0 | state_in->result = result; |
49 | 0 | state_in->step = step_A; |
50 | 0 | return (int)(codechar - code_out); |
51 | 0 | } |
52 | 0 | fragment = *plainchar++; |
53 | 0 | result = (fragment & 0x0fc) >> 2; |
54 | 0 | *codechar++ = base64_encode_value(result); |
55 | 0 | result = (fragment & 0x003) << 4; |
56 | 0 | case step_B: |
57 | 0 | if (plainchar == plaintextend) |
58 | 0 | { |
59 | 0 | state_in->result = result; |
60 | 0 | state_in->step = step_B; |
61 | 0 | return (int)(codechar - code_out); |
62 | 0 | } |
63 | 0 | fragment = *plainchar++; |
64 | 0 | result |= (fragment & 0x0f0) >> 4; |
65 | 0 | *codechar++ = base64_encode_value(result); |
66 | 0 | result = (fragment & 0x00f) << 2; |
67 | 0 | case step_C: |
68 | 0 | if (plainchar == plaintextend) |
69 | 0 | { |
70 | 0 | state_in->result = result; |
71 | 0 | state_in->step = step_C; |
72 | 0 | return (int)(codechar - code_out); |
73 | 0 | } |
74 | 0 | fragment = *plainchar++; |
75 | 0 | result |= (fragment & 0x0c0) >> 6; |
76 | 0 | *codechar++ = base64_encode_value(result); |
77 | 0 | result = (fragment & 0x03f) >> 0; |
78 | 0 | *codechar++ = base64_encode_value(result); |
79 | |
|
80 | 0 | ++(state_in->stepcount); |
81 | 0 | if (state_in->stepcount == CHARS_PER_LINE/4) |
82 | 0 | { |
83 | 0 | *codechar++ = '\n'; |
84 | 0 | state_in->stepcount = 0; |
85 | 0 | } |
86 | 0 | } |
87 | 0 | } |
88 | | /* control should not reach here */ |
89 | 0 | return (int)(codechar - code_out); |
90 | 0 | } |
91 | | |
92 | | int base64_encode_blockend(char* code_out, base64_encodestate* state_in) |
93 | 0 | { |
94 | 0 | char* codechar = code_out; |
95 | |
|
96 | 0 | switch (state_in->step) |
97 | 0 | { |
98 | 0 | case step_B: |
99 | 0 | *codechar++ = base64_encode_value(state_in->result); |
100 | 0 | *codechar++ = '='; |
101 | 0 | *codechar++ = '='; |
102 | 0 | break; |
103 | 0 | case step_C: |
104 | 0 | *codechar++ = base64_encode_value(state_in->result); |
105 | 0 | *codechar++ = '='; |
106 | 0 | break; |
107 | 0 | case step_A: |
108 | 0 | break; |
109 | 0 | } |
110 | 0 | *codechar++ = '\n'; |
111 | |
|
112 | 0 | return (int)(codechar - code_out); |
113 | 0 | } |
114 | | |
115 | | #ifdef _MSC_VER |
116 | | #pragma warning(pop) |
117 | | #endif // _MSC_VER |