/src/assimp/code/AssetLib/Assjson/cencode.c
Line | Count | Source |
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 | 619 | { |
19 | 619 | state_in->step = step_A; |
20 | 619 | state_in->result = 0; |
21 | 619 | state_in->stepcount = 0; |
22 | 619 | } |
23 | | |
24 | | char base64_encode_value(char value_in) |
25 | 3.57M | { |
26 | 3.57M | static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
27 | 3.57M | if (value_in > 63) return '='; |
28 | 3.57M | return encoding[(int)value_in]; |
29 | 3.57M | } |
30 | | |
31 | | int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in) |
32 | 619 | { |
33 | 619 | const char* plainchar = plaintext_in; |
34 | 619 | const char* const plaintextend = plaintext_in + length_in; |
35 | 619 | char* codechar = code_out; |
36 | 619 | char result; |
37 | 619 | char fragment; |
38 | | |
39 | 619 | result = state_in->result; |
40 | | |
41 | 619 | switch (state_in->step) |
42 | 619 | { |
43 | 892k | while (1) |
44 | 892k | { |
45 | 892k | case step_A: |
46 | 892k | if (plainchar == plaintextend) |
47 | 40 | { |
48 | 40 | state_in->result = result; |
49 | 40 | state_in->step = step_A; |
50 | 40 | return (int)(codechar - code_out); |
51 | 40 | } |
52 | 892k | fragment = *plainchar++; |
53 | 892k | result = (fragment & 0x0fc) >> 2; |
54 | 892k | *codechar++ = base64_encode_value(result); |
55 | 892k | result = (fragment & 0x003) << 4; |
56 | 892k | case step_B: |
57 | 892k | if (plainchar == plaintextend) |
58 | 570 | { |
59 | 570 | state_in->result = result; |
60 | 570 | state_in->step = step_B; |
61 | 570 | return (int)(codechar - code_out); |
62 | 570 | } |
63 | 892k | fragment = *plainchar++; |
64 | 892k | result |= (fragment & 0x0f0) >> 4; |
65 | 892k | *codechar++ = base64_encode_value(result); |
66 | 892k | result = (fragment & 0x00f) << 2; |
67 | 892k | case step_C: |
68 | 892k | if (plainchar == plaintextend) |
69 | 9 | { |
70 | 9 | state_in->result = result; |
71 | 9 | state_in->step = step_C; |
72 | 9 | return (int)(codechar - code_out); |
73 | 9 | } |
74 | 892k | fragment = *plainchar++; |
75 | 892k | result |= (fragment & 0x0c0) >> 6; |
76 | 892k | *codechar++ = base64_encode_value(result); |
77 | 892k | result = (fragment & 0x03f) >> 0; |
78 | 892k | *codechar++ = base64_encode_value(result); |
79 | | |
80 | 892k | ++(state_in->stepcount); |
81 | 892k | if (state_in->stepcount == CHARS_PER_LINE/4) |
82 | 49.5k | { |
83 | 49.5k | *codechar++ = '\n'; |
84 | 49.5k | state_in->stepcount = 0; |
85 | 49.5k | } |
86 | 892k | } |
87 | 619 | } |
88 | | /* control should not reach here */ |
89 | 0 | return (int)(codechar - code_out); |
90 | 619 | } |
91 | | |
92 | | int base64_encode_blockend(char* code_out, base64_encodestate* state_in) |
93 | 619 | { |
94 | 619 | char* codechar = code_out; |
95 | | |
96 | 619 | switch (state_in->step) |
97 | 619 | { |
98 | 570 | case step_B: |
99 | 570 | *codechar++ = base64_encode_value(state_in->result); |
100 | 570 | *codechar++ = '='; |
101 | 570 | *codechar++ = '='; |
102 | 570 | break; |
103 | 9 | case step_C: |
104 | 9 | *codechar++ = base64_encode_value(state_in->result); |
105 | 9 | *codechar++ = '='; |
106 | 9 | break; |
107 | 40 | case step_A: |
108 | 40 | break; |
109 | 619 | } |
110 | 619 | *codechar++ = '\n'; |
111 | | |
112 | 619 | return (int)(codechar - code_out); |
113 | 619 | } |
114 | | |
115 | | #ifdef _MSC_VER |
116 | | #pragma warning(pop) |
117 | | #endif // _MSC_VER |