| 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 <b64/cencode.h> |
| 9 |
|
| #include <iostream> |
| 10 |
|
| |
| 11 |
0 |
| void base64_init_encodestate(base64_encodestate* state_in) |
| 12 |
|
| { |
| 13 |
0 |
| state_in->step = step_A; |
| 14 |
0 |
| state_in->result = 0; |
| 15 |
0 |
| state_in->stepcount = 0; |
| 16 |
0 |
| } |
| 17 |
|
| |
| 18 |
0 |
| char base64_encode_value(char value_in) |
| 19 |
|
| { |
| 20 |
|
| static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; |
| 21 |
0 |
▶ | if (value_in > 63) return '='; |
Branch: 1 branch
| Branch | True | False | Total |
| if (value_in > 63) return '='; |
0 |
0 |
0 |
|
| 22 |
0 |
| return encoding[(int)value_in]; |
| 23 |
|
| } |
| 24 |
|
| |
| 25 |
0 |
| int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in) |
| 26 |
|
| { |
| 27 |
0 |
| const char* plainchar = plaintext_in; |
| 28 |
0 |
| const char* const plaintextend = plaintext_in + length_in; |
| 29 |
0 |
| char* codechar = code_out; |
| 30 |
|
| char result; |
| 31 |
|
| char fragment; |
| 32 |
|
| |
| 33 |
0 |
| result = state_in->result; |
| 34 |
|
| |
| 35 |
0 |
| switch (state_in->step) |
| 36 |
|
| { |
| 37 |
|
| while (1) |
| 38 |
|
| { |
| 39 |
0 |
| case step_A: |
| 40 |
0 |
▶ | if (plainchar == plaintextend) |
Branch: 1 branch
| Branch | True | False | Total |
| if (plainchar == plaintextend) |
0 |
0 |
0 |
|
| 41 |
|
| { |
| 42 |
0 |
| state_in->result = result; |
| 43 |
0 |
| state_in->step = step_A; |
| 44 |
0 |
| return (int)(codechar - code_out); |
| 45 |
|
| } |
| 46 |
0 |
| fragment = *plainchar++; |
| 47 |
0 |
| result = (fragment & 0x0fc) >> 2; |
| 48 |
0 |
| *codechar++ = base64_encode_value(result); |
| 49 |
0 |
| result = (fragment & 0x003) << 4; |
| 50 |
|
| /* fall through */ |
| 51 |
|
| |
| 52 |
0 |
| case step_B: |
| 53 |
0 |
▶ | if (plainchar == plaintextend) |
Branch: 1 branch
| Branch | True | False | Total |
| if (plainchar == plaintextend) |
0 |
0 |
0 |
|
| 54 |
|
| { |
| 55 |
0 |
| state_in->result = result; |
| 56 |
0 |
| state_in->step = step_B; |
| 57 |
0 |
| return (int)(codechar - code_out); |
| 58 |
|
| } |
| 59 |
0 |
| fragment = *plainchar++; |
| 60 |
0 |
| result |= (fragment & 0x0f0) >> 4; |
| 61 |
0 |
| *codechar++ = base64_encode_value(result); |
| 62 |
0 |
| result = (fragment & 0x00f) << 2; |
| 63 |
|
| /* fall through */ |
| 64 |
|
| |
| 65 |
0 |
| case step_C: |
| 66 |
0 |
▶ | if (plainchar == plaintextend) |
Branch: 1 branch
| Branch | True | False | Total |
| if (plainchar == plaintextend) |
0 |
0 |
0 |
|
| 67 |
|
| { |
| 68 |
0 |
| state_in->result = result; |
| 69 |
0 |
| state_in->step = step_C; |
| 70 |
0 |
| return (int)(codechar - code_out); |
| 71 |
|
| } |
| 72 |
0 |
| fragment = *plainchar++; |
| 73 |
0 |
| result |= (fragment & 0x0c0) >> 6; |
| 74 |
0 |
| *codechar++ = base64_encode_value(result); |
| 75 |
0 |
| result = (fragment & 0x03f) >> 0; |
| 76 |
0 |
| *codechar++ = base64_encode_value(result); |
| 77 |
0 |
| ++(state_in->stepcount); |
| 78 |
|
| } |
| 79 |
|
| } |
| 80 |
|
| /* control should not reach here */ |
| 81 |
0 |
| return (int)(codechar - code_out); |
| 82 |
|
| } |
| 83 |
|
| |
| 84 |
0 |
| int base64_encode_blockend(char* code_out, base64_encodestate* state_in) |
| 85 |
|
| { |
| 86 |
0 |
| char* codechar = code_out; |
| 87 |
|
| |
| 88 |
0 |
| switch (state_in->step) |
| 89 |
|
| { |
| 90 |
0 |
| case step_B: |
| 91 |
0 |
| *codechar++ = base64_encode_value(state_in->result); |
| 92 |
0 |
| *codechar++ = '='; |
| 93 |
0 |
| *codechar++ = '='; |
| 94 |
0 |
| break; |
| 95 |
0 |
| case step_C: |
| 96 |
0 |
| *codechar++ = base64_encode_value(state_in->result); |
| 97 |
0 |
| *codechar++ = '='; |
| 98 |
0 |
| break; |
| 99 |
0 |
| case step_A: |
| 100 |
0 |
| break; |
| 101 |
|
| } |
| 102 |
0 |
| *codechar++ = '\0'; |
| 103 |
|
| |
| 104 |
0 |
| return (int)(codechar - code_out); |
| 105 |
|
| } |
| 106 |
|
| |