cdecode.c
Go to the documentation of this file.
1 /*
2 cdecoder.c - c source to a base64 decoding 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/cdecode.h>
9 
10 int base64_decode_value(char value_in)
11 {
12  static const char decoding[] = {62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-2,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51};
13  static const char decoding_size = sizeof(decoding);
14  value_in -= 43;
15  if (value_in < 0 || value_in >= decoding_size) return -1;
16  return decoding[(int)value_in];
17 }
18 
20 {
21  state_in->step = step_a;
22  state_in->plainchar = 0;
23 }
24 
25 int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in)
26 {
27  const char* codechar = code_in;
28  char* plainchar = plaintext_out;
29  char fragment;
30 
31  if(length_in == 0) {
32  return 0;
33  }
34 
35  *plainchar = state_in->plainchar;
36 
37  switch (state_in->step)
38  {
39  while (1)
40  {
41  case step_a:
42  do {
43  if (codechar == code_in+length_in)
44  {
45  state_in->step = step_a;
46  state_in->plainchar = 0; // no state to save; use default value
47  return plainchar - plaintext_out;
48  }
49  fragment = (char)base64_decode_value(*codechar++);
50  } while (fragment < 0);
51  *plainchar = (fragment & 0x03f) << 2;
52  //lint -fallthrough
53  case step_b:
54  do {
55  if (codechar == code_in+length_in)
56  {
57  state_in->step = step_b;
58  state_in->plainchar = *plainchar;
59  return plainchar - plaintext_out;
60  }
61  fragment = (char)base64_decode_value(*codechar++);
62  } while (fragment < 0);
63  *plainchar++ |= (fragment & 0x030) >> 4;
64  *plainchar = (fragment & 0x00f) << 4;
65  //lint -fallthrough
66  case step_c:
67  do {
68  if (codechar == code_in+length_in)
69  {
70  state_in->step = step_c;
71  state_in->plainchar = *plainchar;
72  return plainchar - plaintext_out;
73  }
74  fragment = (char)base64_decode_value(*codechar++);
75  } while (fragment < 0);
76  *plainchar++ |= (fragment & 0x03c) >> 2;
77  *plainchar = (fragment & 0x003) << 6;
78  //lint -fallthrough
79  case step_d:
80  do {
81  if (codechar == code_in+length_in)
82  {
83  state_in->step = step_d;
84  state_in->plainchar = *plainchar;
85  return plainchar - plaintext_out;
86  }
87  fragment = (char)base64_decode_value(*codechar++);
88  } while (fragment < 0);
89  *plainchar++ |= (fragment & 0x03f);
90  }
91  }
92  /* control should not reach here */
93  return plainchar - plaintext_out;
94 }
95 
Definition: cdecode.h:13
Definition: cdecode.h:13
int base64_decode_value(char value_in)
Definition: cdecode.c:10
Definition: cdecode.h:13
void base64_init_decodestate(base64_decodestate *state_in)
Definition: cdecode.c:19
Definition: cdecode.h:13
base64_decodestep step
Definition: cdecode.h:18
int base64_decode_block(const char *code_in, const int length_in, char *plaintext_out, base64_decodestate *state_in)
Definition: cdecode.c:25


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix
autogenerated on Sun Feb 3 2019 03:29:51