~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to mysys/base64.cc

  • Committer: Monty Taylor
  • Date: 2009-09-30 07:01:32 UTC
  • mto: This revision was merged to the branch mainline in revision 1184.
  • Revision ID: mordred@inaugust.com-20090930070132-b1ol1xu1rpajdddy
Small namespace cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
14
14
   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA */
15
15
 
16
16
#include <drizzled/global.h>
17
 
#include CMATH_H
18
17
#include <mystrings/m_string.h>  /* strchr() */
19
18
#include <mystrings/m_ctype.h>  /* my_isspace() */
20
19
#include <mysys/base64.h>
21
 
 
22
 
#if defined(CMATH_NAMESPACE)
23
 
using namespace CMATH_NAMESPACE;
24
 
#endif
 
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
}
25
39
 
26
40
#ifndef MAIN
27
41
 
46
60
int
47
61
base64_needed_decoded_length(int length_of_encoded_data)
48
62
{
49
 
  return (int) ceil(length_of_encoded_data * 3 / 4);
 
63
  return (int) ceil((double)(length_of_encoded_data * 3 / 4));
50
64
}
51
65
 
52
66
 
108
122
static inline uint
109
123
pos(unsigned char c)
110
124
{
111
 
  return (uint) (strchr(base64_table, c) - base64_table);
112
 
}
113
 
 
114
 
 
115
 
#define SKIP_SPACE(src, i, size)                                \
116
 
{                                                               \
117
 
  while (i < size && my_isspace(&my_charset_utf8_general_ci, * src))     \
118
 
  {                                                             \
119
 
    i++;                                                        \
120
 
    src++;                                                      \
121
 
  }                                                             \
122
 
  if (i == size)                                                \
123
 
  {                                                             \
124
 
    break;                                                      \
125
 
  }                                                             \
 
125
  return (uint32_t) (strchr(base64_table, c) - base64_table);
126
126
}
127
127
 
128
128
 
172
172
    unsigned c= 0;
173
173
    size_t mark= 0;
174
174
 
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);
 
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);
188
188
 
189
189
    if (*src != '=')
190
190
      c += pos(*src++);
199
199
    c <<= 6;
200
200
    i++;
201
201
 
202
 
    SKIP_SPACE(src, i, len);
 
202
    skip_space(src, i, len);
203
203
 
204
204
    if (*src != '=')
205
205
      c += pos(*src++);
213
213
    i++;
214
214
 
215
215
  end:
216
 
    b[0]= (c >> 16) & 0xff;
217
 
    b[1]= (c >>  8) & 0xff;
218
 
    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);
219
219
 
220
220
    for (j=0; j<3-mark; j++)
221
221
      *d++= b[j];
228
228
    The variable 'i' is set to 'len' when padding has been read, so it
229
229
    does not actually reflect the number of bytes read from 'src'.
230
230
   */
231
 
  return i != len ? -1 : d - dst_base;
 
231
  return (i != len) ? -1 : int(d - dst_base);
232
232
}
233
233
 
234
234
 
288
288
      printf("       --------- src ---------   --------- dst ---------\n");
289
289
      for (k= 0; k<src_len; k+=8)
290
290
      {
291
 
        printf("%.4x   ", (uint) k);
 
291
        printf("%.4x   ", (uint32_t) k);
292
292
        for (l=0; l<8 && k+l<src_len; l++)
293
293
        {
294
294
          unsigned char c= src[k+l];
305
305
        printf("\n");
306
306
      }
307
307
      printf("src length: %.8x, dst length: %.8x\n",
308
 
             (uint) src_len, (uint) dst_len);
 
308
             (uint32_t) src_len, (uint32_t) dst_len);
309
309
      require(0);
310
310
    }
311
311
  }