| 24 | } |
| 25 | |
| 26 | int base64_encode_block(const char *plaintext_in, int length_in, char *code_out, |
| 27 | base64_encodestate *state_in) { |
| 28 | const char *plainchar = plaintext_in; |
| 29 | const char *const plaintextend = plaintext_in + length_in; |
| 30 | char *codechar = code_out; |
| 31 | char result; |
| 32 | char fragment; |
| 33 | |
| 34 | result = state_in->result; |
| 35 | |
| 36 | switch (state_in->step) { |
| 37 | while (1) { |
| 38 | case step_A: |
| 39 | if (plainchar == plaintextend) { |
| 40 | state_in->result = result; |
| 41 | state_in->step = step_A; |
| 42 | return (int)(codechar - code_out); |
| 43 | } |
| 44 | fragment = *plainchar++; |
| 45 | result = (fragment & 0x0fc) >> 2; |
| 46 | *codechar++ = base64_encode_value(result); |
| 47 | result = (fragment & 0x003) << 4; |
| 48 | case step_B: |
| 49 | if (plainchar == plaintextend) { |
| 50 | state_in->result = result; |
| 51 | state_in->step = step_B; |
| 52 | return (int)(codechar - code_out); |
| 53 | } |
| 54 | fragment = *plainchar++; |
| 55 | result |= (fragment & 0x0f0) >> 4; |
| 56 | *codechar++ = base64_encode_value(result); |
| 57 | result = (fragment & 0x00f) << 2; |
| 58 | case step_C: |
| 59 | if (plainchar == plaintextend) { |
| 60 | state_in->result = result; |
| 61 | state_in->step = step_C; |
| 62 | return (int)(codechar - code_out); |
| 63 | } |
| 64 | fragment = *plainchar++; |
| 65 | result |= (fragment & 0x0c0) >> 6; |
| 66 | *codechar++ = base64_encode_value(result); |
| 67 | result = (fragment & 0x03f) >> 0; |
| 68 | *codechar++ = base64_encode_value(result); |
| 69 | |
| 70 | ++(state_in->stepcount); |
| 71 | if (state_in->chars_per_line > 0 && |
| 72 | state_in->stepcount == state_in->chars_per_line / 4) { |
| 73 | *codechar++ = '\n'; |
| 74 | state_in->stepcount = 0; |
| 75 | } |
| 76 | } |
| 77 | } |
| 78 | /* control should not reach here */ |
| 79 | return (int)(codechar - code_out); |
| 80 | } |
| 81 | |
| 82 | int base64_encode_blockend(char *code_out, base64_encodestate *state_in) { |
| 83 | char *codechar = code_out; |
no test coverage detected