~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/base64.cc

MergedĀ inĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
   along with this program; if not, write to the Free Software
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
 
16
#include <drizzled/global.h>
16
17
#include <mystrings/m_string.h>  /* strchr() */
17
18
#include <mystrings/m_ctype.h>  /* my_isspace() */
18
19
#include <mysys/base64.h>
 
20
#include <cstdio>
 
21
#include <cmath>
 
22
 
 
23
using namespace std;
 
24
 
 
25
template <class T, class U>
 
26
inline void skip_space(T src, U i, const U size)
 
27
{
 
28
  while (i < size && my_isspace(&my_charset_utf8_general_ci, * src))
 
29
  {
 
30
    i++;
 
31
    src++;
 
32
 
 
33
    if (i == size)
 
34
    {
 
35
      break;
 
36
    }
 
37
  }
 
38
}
19
39
 
20
40
#ifndef MAIN
21
41
 
40
60
int
41
61
base64_needed_decoded_length(int length_of_encoded_data)
42
62
{
43
 
  return (int) ceil(length_of_encoded_data * 3 / 4);
 
63
  return (int) ceil((double)(length_of_encoded_data * 3 / 4));
44
64
}
45
65
 
46
66
 
102
122
static inline uint
103
123
pos(unsigned char c)
104
124
{
105
 
  return (uint) (strchr(base64_table, c) - base64_table);
106
 
}
107
 
 
108
 
 
109
 
#define SKIP_SPACE(src, i, size)                                \
110
 
{                                                               \
111
 
  while (i < size && my_isspace(&my_charset_utf8_general_ci, * src))     \
112
 
  {                                                             \
113
 
    i++;                                                        \
114
 
    src++;                                                      \
115
 
  }                                                             \
116
 
  if (i == size)                                                \
117
 
  {                                                             \
118
 
    break;                                                      \
119
 
  }                                                             \
 
125
  return (uint32_t) (strchr(base64_table, c) - base64_table);
120
126
}
121
127
 
122
128
 
166
172
    unsigned c= 0;
167
173
    size_t mark= 0;
168
174
 
169
 
    SKIP_SPACE(src, i, len);
170
 
 
171
 
    c += pos(*src++);
172
 
    c <<= 6;
173
 
    i++;
174
 
 
175
 
    SKIP_SPACE(src, i, len);
176
 
 
177
 
    c += pos(*src++);
178
 
    c <<= 6;
179
 
    i++;
180
 
 
181
 
    SKIP_SPACE(src, i, len);
 
175
    skip_space(src, i, len);
 
176
 
 
177
    c += pos(*src++);
 
178
    c <<= 6;
 
179
    i++;
 
180
 
 
181
    skip_space(src, i, len);
 
182
 
 
183
    c += pos(*src++);
 
184
    c <<= 6;
 
185
    i++;
 
186
 
 
187
    skip_space(src, i, len);
182
188
 
183
189
    if (*src != '=')
184
190
      c += pos(*src++);
193
199
    c <<= 6;
194
200
    i++;
195
201
 
196
 
    SKIP_SPACE(src, i, len);
 
202
    skip_space(src, i, len);
197
203
 
198
204
    if (*src != '=')
199
205
      c += pos(*src++);
207
213
    i++;
208
214
 
209
215
  end:
210
 
    b[0]= (c >> 16) & 0xff;
211
 
    b[1]= (c >>  8) & 0xff;
212
 
    b[2]= (c >>  0) & 0xff;
 
216
    b[0]= char((c >> 16) & 0xff);
 
217
    b[1]= char((c >>  8) & 0xff);
 
218
    b[2]= char((c >>  0) & 0xff);
213
219
 
214
220
    for (j=0; j<3-mark; j++)
215
221
      *d++= b[j];
222
228
    The variable 'i' is set to 'len' when padding has been read, so it
223
229
    does not actually reflect the number of bytes read from 'src'.
224
230
   */
225
 
  return i != len ? -1 : d - dst_base;
 
231
  return (i != len) ? -1 : int(d - dst_base);
226
232
}
227
233
 
228
234
 
282
288
      printf("       --------- src ---------   --------- dst ---------\n");
283
289
      for (k= 0; k<src_len; k+=8)
284
290
      {
285
 
        printf("%.4x   ", (uint) k);
 
291
        printf("%.4x   ", (uint32_t) k);
286
292
        for (l=0; l<8 && k+l<src_len; l++)
287
293
        {
288
294
          unsigned char c= src[k+l];
299
305
        printf("\n");
300
306
      }
301
307
      printf("src length: %.8x, dst length: %.8x\n",
302
 
             (uint) src_len, (uint) dst_len);
 
308
             (uint32_t) src_len, (uint32_t) dst_len);
303
309
      require(0);
304
310
    }
305
311
  }