this repo has no description
at develop 2.8 kB view raw
1/* 2cencoder.c - c source to a base64 encoding algorithm implementation 3 4This is part of the libb64 project, and has been placed in the public domain. 5For details, see http://sourceforge.net/projects/libb64 6*/ 7 8#include <minizinc/thirdparty/b64/cencode.h> 9 10const int CHARS_PER_LINE = 72; 11 12void base64_init_encodestate(base64_encodestate* state_in) { 13 state_in->step = step_A; 14 state_in->result = 0; 15 state_in->stepcount = 0; 16} 17 18char base64_encode_value(char value_in) { 19 static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; 20 if (value_in > 63) return '='; 21 return encoding[(int)value_in]; 22} 23 24int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, 25 base64_encodestate* state_in) { 26 const char* plainchar = plaintext_in; 27 const char* const plaintextend = plaintext_in + length_in; 28 char* codechar = code_out; 29 char result; 30 char fragment; 31 32 result = state_in->result; 33 34 switch (state_in->step) { 35 while (1) { 36 case step_A: 37 if (plainchar == plaintextend) { 38 state_in->result = result; 39 state_in->step = step_A; 40 return codechar - code_out; 41 } 42 fragment = *plainchar++; 43 result = (fragment & 0x0fc) >> 2; 44 *codechar++ = base64_encode_value(result); 45 result = (fragment & 0x003) << 4; 46 case step_B: 47 if (plainchar == plaintextend) { 48 state_in->result = result; 49 state_in->step = step_B; 50 return codechar - code_out; 51 } 52 fragment = *plainchar++; 53 result |= (fragment & 0x0f0) >> 4; 54 *codechar++ = base64_encode_value(result); 55 result = (fragment & 0x00f) << 2; 56 case step_C: 57 if (plainchar == plaintextend) { 58 state_in->result = result; 59 state_in->step = step_C; 60 return codechar - code_out; 61 } 62 fragment = *plainchar++; 63 result |= (fragment & 0x0c0) >> 6; 64 *codechar++ = base64_encode_value(result); 65 result = (fragment & 0x03f) >> 0; 66 *codechar++ = base64_encode_value(result); 67 68 ++(state_in->stepcount); 69 if (state_in->stepcount == CHARS_PER_LINE / 4) { 70 *codechar++ = '\n'; 71 state_in->stepcount = 0; 72 } 73 } 74 } 75 /* control should not reach here */ 76 return codechar - code_out; 77} 78 79int base64_encode_blockend(char* code_out, base64_encodestate* state_in) { 80 char* codechar = code_out; 81 82 switch (state_in->step) { 83 case step_B: 84 *codechar++ = base64_encode_value(state_in->result); 85 *codechar++ = '='; 86 *codechar++ = '='; 87 break; 88 case step_C: 89 *codechar++ = base64_encode_value(state_in->result); 90 *codechar++ = '='; 91 break; 92 case step_A: 93 break; 94 } 95 *codechar++ = '\n'; 96 97 return codechar - code_out; 98}