cencode.c
Go to the documentation of this file.
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 
10 const int CHARS_PER_LINE = 72;
11 
13 {
14  state_in->step = step_A;
15  state_in->result = 0;
16  state_in->stepcount = 0;
17 }
18 
19 char base64_encode_value(char value_in)
20 {
21  static const char* encoding = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
22  if (value_in > 63) return '=';
23  return encoding[(int)value_in];
24 }
25 
26 int base64_encode_block(const char* plaintext_in, int length_in, char* code_out, base64_encodestate* state_in)
27 {
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  {
38  while (1)
39  {
40  case step_A:
41  if (plainchar == plaintextend)
42  {
43  state_in->result = result;
44  state_in->step = step_A;
45  return codechar - code_out;
46  }
47  fragment = *plainchar++;
48  result = (fragment & 0x0fc) >> 2;
49  *codechar++ = base64_encode_value(result);
50  result = (fragment & 0x003) << 4;
51  //lint -fallthrough
52  case step_B:
53  if (plainchar == plaintextend)
54  {
55  state_in->result = result;
56  state_in->step = step_B;
57  return codechar - code_out;
58  }
59  fragment = *plainchar++;
60  result |= (fragment & 0x0f0) >> 4;
61  *codechar++ = base64_encode_value(result);
62  result = (fragment & 0x00f) << 2;
63  //lint -fallthrough
64  case step_C:
65  if (plainchar == plaintextend)
66  {
67  state_in->result = result;
68  state_in->step = step_C;
69  return codechar - code_out;
70  }
71  fragment = *plainchar++;
72  result |= (fragment & 0x0c0) >> 6;
73  *codechar++ = base64_encode_value(result);
74  result = (fragment & 0x03f) >> 0;
75  *codechar++ = base64_encode_value(result);
76 
77  ++(state_in->stepcount);
78  if (state_in->stepcount == CHARS_PER_LINE/4)
79  {
80  *codechar++ = '\n';
81  state_in->stepcount = 0;
82  }
83  }
84  }
85  /* control should not reach here */
86  return codechar - code_out;
87 }
88 
89 int base64_encode_blockend(char* code_out, base64_encodestate* state_in)
90 {
91  char* codechar = code_out;
92 
93  switch (state_in->step)
94  {
95  case step_B:
96  *codechar++ = base64_encode_value(state_in->result);
97  *codechar++ = '=';
98  *codechar++ = '=';
99  break;
100  case step_C:
101  *codechar++ = base64_encode_value(state_in->result);
102  *codechar++ = '=';
103  break;
104  case step_A:
105  break;
106  }
107  *codechar++ = '\n';
108 
109  return codechar - code_out;
110 }
111 
int base64_encode_blockend(char *code_out, base64_encodestate *state_in)
Definition: cencode.c:89
Definition: cencode.h:13
base64_encodestep step
Definition: cencode.h:18
Definition: cencode.h:13
char base64_encode_value(char value_in)
Definition: cencode.c:19
void base64_init_encodestate(base64_encodestate *state_in)
Definition: cencode.c:12
Definition: cencode.h:13
int base64_encode_block(const char *plaintext_in, int length_in, char *code_out, base64_encodestate *state_in)
Definition: cencode.c:26
const int CHARS_PER_LINE
Definition: cencode.c:10


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix
autogenerated on Fri Jan 10 2020 03:19:09