source: box/trunk/lib/httpserver/cdecode.cpp @ 2442

Revision 2442, 2.5 KB checked in by chris, 3 years ago (diff)

Add Amazon S3 signature checking to simulator.

  • Property svn:eol-style set to native
  • Property svn:mime-type set to text/x-c++src
Line 
1/*
2cdecoder.c - c source to a base64 decoding 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
8extern "C"
9{
10
11#include "cdecode.h"
12
13int base64_decode_value(char value_in)
14{
15        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};
16        static const char decoding_size = sizeof(decoding);
17        value_in -= 43;
18        if (value_in < 0 || value_in > decoding_size) return -1;
19        return decoding[(int)value_in];
20}
21
22void base64_init_decodestate(base64_decodestate* state_in)
23{
24        state_in->step = step_a;
25        state_in->plainchar = 0;
26}
27
28int base64_decode_block(const char* code_in, const int length_in, char* plaintext_out, base64_decodestate* state_in)
29{
30        const char* codechar = code_in;
31        char* plainchar = plaintext_out;
32        char fragment;
33       
34        *plainchar = state_in->plainchar;
35       
36        switch (state_in->step)
37        {
38                while (1)
39                {
40        case step_a:
41                        do {
42                                if (codechar == code_in+length_in)
43                                {
44                                        state_in->step = step_a;
45                                        state_in->plainchar = *plainchar;
46                                        return plainchar - plaintext_out;
47                                }
48                                fragment = (char)base64_decode_value(*codechar++);
49                        } while (fragment < 0);
50                        *plainchar    = (fragment & 0x03f) << 2;
51        case step_b:
52                        do {
53                                if (codechar == code_in+length_in)
54                                {
55                                        state_in->step = step_b;
56                                        state_in->plainchar = *plainchar;
57                                        return plainchar - plaintext_out;
58                                }
59                                fragment = (char)base64_decode_value(*codechar++);
60                        } while (fragment < 0);
61                        *plainchar++ |= (fragment & 0x030) >> 4;
62                        *plainchar    = (fragment & 0x00f) << 4;
63        case step_c:
64                        do {
65                                if (codechar == code_in+length_in)
66                                {
67                                        state_in->step = step_c;
68                                        state_in->plainchar = *plainchar;
69                                        return plainchar - plaintext_out;
70                                }
71                                fragment = (char)base64_decode_value(*codechar++);
72                        } while (fragment < 0);
73                        *plainchar++ |= (fragment & 0x03c) >> 2;
74                        *plainchar    = (fragment & 0x003) << 6;
75        case step_d:
76                        do {
77                                if (codechar == code_in+length_in)
78                                {
79                                        state_in->step = step_d;
80                                        state_in->plainchar = *plainchar;
81                                        return plainchar - plaintext_out;
82                                }
83                                fragment = (char)base64_decode_value(*codechar++);
84                        } while (fragment < 0);
85                        *plainchar++   |= (fragment & 0x03f);
86                }
87        }
88        /* control should not reach here */
89        return plainchar - plaintext_out;
90}
91
92}
Note: See TracBrowser for help on using the repository browser.