aboutsummaryrefslogtreecommitdiff
path: root/libxsde/xsde/cxx/parser/non-validating/base64-binary.cxx
blob: 34c1e487db73359b3a77f40b31cc74c0f7116566 (plain)
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
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
// file      : xsde/cxx/parser/non-validating/base64-binary.cxx
// author    : Boris Kolpackov <boris@codesynthesis.com>
// copyright : Copyright (c) 2005-2011 Code Synthesis Tools CC
// license   : GNU GPL v2 + exceptions; see accompanying LICENSE file

#include <xsde/cxx/config.hxx>

#ifdef XSDE_CUSTOM_ALLOCATOR
#  include <xsde/cxx/allocator.hxx>
#endif

#include <xsde/cxx/parser/non-validating/base64-binary.hxx>

static unsigned char
base64_decode (char c)
{
  unsigned char r = 0xFF;

  if (c >= 'A' && c <= 'Z')
    r = static_cast<unsigned char> (c - 'A');
  else if (c >= 'a' && c <= 'z')
    r = static_cast<unsigned char> (c - 'a' + 26);
  else if (c >= '0' && c <= '9')
    r = static_cast<unsigned char> (c - '0' + 52);
  else if (c == '+')
    r = 62;
  else if (c == '/')
    r = 63;

  return r;
}

namespace xsde
{
  namespace cxx
  {
    namespace parser
    {
      namespace non_validating
      {
        base64_binary_pimpl::
        ~base64_binary_pimpl ()
        {
          if (!base_ && buf_)
          {
#ifndef XSDE_CUSTOM_ALLOCATOR
            delete buf_;
#else
            buf_->~buffer ();
            cxx::free (buf_);
#endif
          }
        }

        void base64_binary_pimpl::
        _reset ()
        {
          base64_binary_pskel::_reset ();

          if (!base_ && buf_)
          {
#ifndef XSDE_CUSTOM_ALLOCATOR
            delete buf_;
#else
            buf_->~buffer ();
            cxx::free (buf_);
#endif
            buf_ = 0;
          }
        }

        base64_binary_pimpl::
        base64_binary_pimpl (bool base)
            : base_ (base), buf_ (0)
        {
        }

        void base64_binary_pimpl::
        pre_impl (buffer* b)
        {
          buf_ = b;
        }

        void base64_binary_pimpl::
        _pre ()
        {
          if (buf_ == 0)
          {
#ifndef XSDE_CUSTOM_ALLOCATOR
            buf_ = new buffer ();
#else
            buf_ = static_cast<buffer*> (alloc (sizeof (buffer)));

#ifdef XSDE_EXCEPTIONS
            alloc_guard ag (buf_);
            new (buf_) buffer ();
            ag.release ();
#else
            if (buf_)
              new (buf_) buffer ();
#endif
#endif

#ifndef XSDE_EXCEPTIONS
            if (buf_ == 0)
            {
              _sys_error (sys_error::no_memory);
              return;
            }
#endif
          }

#ifdef XSDE_EXCEPTIONS
          str_.assign ("", 0);
#else
          if (str_.assign ("", 0))
            _sys_error (sys_error::no_memory);
#endif
        }

        void base64_binary_pimpl::
        _characters (const ro_string& s)
        {
          if (str_.size () == 0)
          {
            ro_string tmp (s.data (), s.size ());

            if (trim_left (tmp) != 0)
            {
#ifdef XSDE_EXCEPTIONS
              str_.append (tmp.data (), tmp.size ());
#else
              if (str_.append (tmp.data (), tmp.size ()))
                _sys_error (sys_error::no_memory);
#endif
            }
          }
          else
          {
#ifdef XSDE_EXCEPTIONS
            str_.append (s.data (), s.size ());
#else
            if (str_.append (s.data (), s.size ()))
              _sys_error (sys_error::no_memory);
#endif
          }
        }

        buffer* base64_binary_pimpl::
        post_base64_binary ()
        {
          typedef string::size_type size_type;

          size_type size = str_.size ();
          const char* src = str_.data ();

          // Remove all whitespaces.
          //
          {
            size_type j = 0;
            bool subs = false;

            for (size_type i = 0; i < size; ++i)
            {
              char c = str_[i];

              if (c == 0x20 || c == 0x0A || c == 0x0D || c == 0x09)
              {
                subs = true;
              }
              else
              {
                if (subs)
                  subs = false;

                str_[j++] = c;
              }
            }

            size = j;
            str_.truncate (size);
          }

          // Our length should be a multiple of four.
          //
          size_type quad_count = size / 4;
          size_type capacity = quad_count * 3 + 1;

#ifdef XSDE_EXCEPTIONS
          buf_->size (capacity);
#else
          if (buf_->size (capacity))
          {
            _sys_error (sys_error::no_memory);
            return 0;
          }
#endif
          char* dst = buf_->data ();

          // Source and destination indexes.
          //
          size_type si = 0;
          size_type di = 0;

          // Process all quads except the last one.
          //
          unsigned char b1, b2, b3, b4;

          for (size_type q = 0; q < quad_count - 1; ++q)
          {
            b1 = base64_decode (src[si++]);
            b2 = base64_decode (src[si++]);
            b3 = base64_decode (src[si++]);
            b4 = base64_decode (src[si++]);

            dst[di++] = (b1 << 2) | (b2 >> 4);
            dst[di++] = (b2 << 4) | (b3 >> 2);
            dst[di++] = (b3 << 6) | b4;
          }

          // Process the last quad. The first two octets are always there.
          //
          b1 = base64_decode (src[si++]);
          b2 = base64_decode (src[si++]);

          char e3 = src[si++];
          char e4 = src[si++];

          if (e4 == '=')
          {
            if (e3 == '=')
            {
              // Two pads. Last 4 bits in b2 should be zero.
              //
              dst[di++] = (b1 << 2) | (b2 >> 4);
            }
            else
            {
              // One pad. Last 2 bits in b3 should be zero.
              //
              b3 = base64_decode (e3);

              dst[di++] = (b1 << 2) | (b2 >> 4);
              dst[di++] = (b2 << 4) | (b3 >> 2);
            }
          }
          else
          {
            // No pads.
            //
            b3 = base64_decode (e3);
            b4 = base64_decode (e4);

            dst[di++] = (b1 << 2) | (b2 >> 4);
            dst[di++] = (b2 << 4) | (b3 >> 2);
            dst[di++] = (b3 << 6) | b4;
          }

          // Set the real size.
          //
          buf_->size (di);

          buffer* r = buf_;
          buf_ = 0;
          return r;
        }
      }
    }
  }
}