~ubuntu-branches/ubuntu/vivid/curl/vivid

« back to all changes in this revision

Viewing changes to lib/base64.c

  • Committer: Bazaar Package Importer
  • Author(s): Andreas Schuldei
  • Date: 2009-04-02 23:35:45 UTC
  • mto: (1.2.1 upstream) (3.2.3 sid)
  • mto: This revision was merged to the branch mainline in revision 38.
  • Revision ID: james.westby@ubuntu.com-20090402233545-geixkwhe3izccjt7
Tags: upstream-7.19.4
ImportĀ upstreamĀ versionĀ 7.19.4

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2007, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2008, Daniel Stenberg, <daniel@haxx.se>, et al.
9
9
 *
10
10
 * This software is licensed as described in the file COPYING, which
11
11
 * you should have received as part of this distribution. The terms
18
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
19
 * KIND, either express or implied.
20
20
 *
21
 
 * $Id: base64.c,v 1.45 2007-11-05 09:45:09 bagder Exp $
 
21
 * $Id: base64.c,v 1.51 2008-11-14 16:22:18 bagder Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
/* Base64 encoding/decoding
42
42
 
43
43
#include "urldata.h" /* for the SessionHandle definition */
44
44
#include "easyif.h"  /* for Curl_convert_... prototypes */
45
 
#include "base64.h"
 
45
#include "curl_base64.h"
46
46
#include "memory.h"
47
47
 
48
48
/* include memdebug.h last */
117
117
  /* Decode all but the last quantum (which may not decode to a
118
118
  multiple of 3 bytes) */
119
119
  for(i = 0; i < numQuantums - 1; i++) {
120
 
    decodeQuantum((unsigned char *)newstr, src);
 
120
    decodeQuantum(newstr, src);
121
121
    newstr += 3; src += 4;
122
122
  }
123
123
 
153
153
  char *convbuf = NULL;
154
154
#endif
155
155
 
156
 
  char *indata = (char *)inp;
 
156
  const char *indata = inp;
157
157
 
158
158
  *outptr = NULL; /* set to NULL in case of failure before we reach the end */
159
159
 
160
160
  if(0 == insize)
161
161
    insize = strlen(indata);
162
162
 
163
 
  base64data = output = (char*)malloc(insize*4/3+4);
 
163
  base64data = output = malloc(insize*4/3+4);
164
164
  if(NULL == output)
165
165
    return 0;
166
166
 
171
171
   * so we copy it to a buffer, translate it, and use that instead.
172
172
   */
173
173
  if(data) {
174
 
    convbuf = (char*)malloc(insize);
 
174
    convbuf = malloc(insize);
175
175
    if(!convbuf) {
176
176
      free(output);
177
177
      return 0;
239
239
  return strlen(base64data); /* return the length of the new data */
240
240
}
241
241
/* ---- End of Base64 Encoding ---- */
242
 
 
243
 
/************* TEST HARNESS STUFF ****************/
244
 
 
245
 
 
246
 
#ifdef TEST_ENCODE
247
 
/* encoding test harness. Read in standard input and write out the length
248
 
 * returned by Curl_base64_encode, followed by the base64'd data itself
249
 
 */
250
 
#include <stdio.h>
251
 
 
252
 
#define TEST_NEED_SUCK
253
 
void *suck(int *);
254
 
 
255
 
int main(int argc, argv_item_t argv[], char **envp)
256
 
{
257
 
  char *base64;
258
 
  size_t base64Len;
259
 
  unsigned char *data;
260
 
  int dataLen;
261
 
  struct SessionHandle *handle = NULL;
262
 
 
263
 
#ifdef CURL_DOES_CONVERSIONS
264
 
  /* get a Curl handle so Curl_base64_encode can translate properly */
265
 
  handle = curl_easy_init();
266
 
  if(handle == NULL) {
267
 
    fprintf(stderr, "Error: curl_easy_init failed\n");
268
 
    return 1;
269
 
  }
270
 
#endif
271
 
  data = (unsigned char *)suck(&dataLen);
272
 
  base64Len = Curl_base64_encode(handle, data, dataLen, &base64);
273
 
 
274
 
  fprintf(stderr, "%d\n", base64Len);
275
 
  fprintf(stdout, "%s\n", base64);
276
 
 
277
 
  free(base64); free(data);
278
 
#ifdef CURL_DOES_CONVERSIONS
279
 
  curl_easy_cleanup(handle);
280
 
#endif
281
 
  return 0;
282
 
}
283
 
#endif
284
 
 
285
 
#ifdef TEST_DECODE
286
 
/* decoding test harness. Read in a base64 string from stdin and write out the
287
 
 * length returned by Curl_base64_decode, followed by the decoded data itself
288
 
 *
289
 
 * gcc -DTEST_DECODE base64.c -o base64 mprintf.o memdebug.o
290
 
 */
291
 
#include <stdio.h>
292
 
 
293
 
#define TEST_NEED_SUCK
294
 
void *suck(int *);
295
 
 
296
 
int main(int argc, argv_item_t argv[], char **envp)
297
 
{
298
 
  char *base64;
299
 
  int base64Len;
300
 
  unsigned char *data;
301
 
  int dataLen;
302
 
  int i, j;
303
 
#ifdef CURL_DOES_CONVERSIONS
304
 
  /* get a Curl handle so main can translate properly */
305
 
  struct SessionHandle *handle = curl_easy_init();
306
 
  if(handle == NULL) {
307
 
    fprintf(stderr, "Error: curl_easy_init failed\n");
308
 
    return 1;
309
 
  }
310
 
#endif
311
 
 
312
 
  base64 = (char *)suck(&base64Len);
313
 
  dataLen = Curl_base64_decode(base64, &data);
314
 
 
315
 
  fprintf(stderr, "%d\n", dataLen);
316
 
 
317
 
  for(i=0; i < dataLen; i+=0x10) {
318
 
    printf("0x%02x: ", i);
319
 
    for(j=0; j < 0x10; j++)
320
 
      if((j+i) < dataLen)
321
 
        printf("%02x ", data[i+j]);
322
 
      else
323
 
        printf("   ");
324
 
 
325
 
    printf(" | ");
326
 
 
327
 
    for(j=0; j < 0x10; j++)
328
 
      if((j+i) < dataLen) {
329
 
#ifdef CURL_DOES_CONVERSIONS
330
 
        if(CURLE_OK !=
331
 
             Curl_convert_from_network(handle, &data[i+j], (size_t)1))
332
 
          data[i+j] = '.';
333
 
#endif /* CURL_DOES_CONVERSIONS */
334
 
        printf("%c", ISGRAPH(data[i+j])?data[i+j]:'.');
335
 
      } else
336
 
        break;
337
 
    puts("");
338
 
  }
339
 
 
340
 
#ifdef CURL_DOES_CONVERSIONS
341
 
  curl_easy_cleanup(handle);
342
 
#endif
343
 
  free(base64); free(data);
344
 
  return 0;
345
 
}
346
 
#endif
347
 
 
348
 
#ifdef TEST_NEED_SUCK
349
 
/* this function 'sucks' in as much as possible from stdin */
350
 
void *suck(int *lenptr)
351
 
{
352
 
  int cursize = 8192;
353
 
  unsigned char *buf = NULL;
354
 
  int lastread;
355
 
  int len = 0;
356
 
 
357
 
  do {
358
 
    cursize *= 2;
359
 
    buf = (unsigned char *)realloc(buf, cursize);
360
 
    memset(buf + len, 0, cursize - len);
361
 
    lastread = fread(buf + len, 1, cursize - len, stdin);
362
 
    len += lastread;
363
 
  } while(!feof(stdin));
364
 
 
365
 
  lenptr[0] = len;
366
 
  return (void *)buf;
367
 
}
368
 
#endif