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


xmlrpcpp
Author(s): Chris Morley, Konstantin Pilipchuk, Morgan Quigley, Austin Hendrix, Dirk Thomas , Jacob Perron
autogenerated on Sat Sep 14 2024 02:59:32