~ubuntu-branches/ubuntu/jaunty/cmake/jaunty-security

« back to all changes in this revision

Viewing changes to Source/CTest/Curl/base64.c

  • Committer: Bazaar Package Importer
  • Author(s): A. Maitland Bottoms
  • Date: 2006-06-18 16:34:11 UTC
  • mfrom: (1.4.1 upstream)
  • Revision ID: james.westby@ubuntu.com-20060618163411-pi234s3v6jwlcmof
Tags: 2.4.2-1
* New upstream release (Closes: #338324)
* Put cmake .vim files into /usr/share/vim/addons/plugin/
  where they can be used. (Closes: #366663)
* Install cmake-mode.el so it can be used. (Closes: #366664)
* Ensure cmake FindKDE locates KDE libraries on Debian
  based distributions.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *                                  _   _ ____  _
 
3
 *  Project                     ___| | | |  _ \| |
 
4
 *                             / __| | | | |_) | |
 
5
 *                            | (__| |_| |  _ <| |___
 
6
 *                             \___|\___/|_| \_\_____|
 
7
 *
 
8
 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
 
9
 *
 
10
 * This software is licensed as described in the file COPYING, which
 
11
 * you should have received as part of this distribution. The terms
 
12
 * are also available at http://curl.haxx.se/docs/copyright.html.
 
13
 *
 
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
 
15
 * copies of the Software, and permit persons to whom the Software is
 
16
 * furnished to do so, under the terms of the COPYING file.
 
17
 *
 
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 
19
 * KIND, either express or implied.
 
20
 *
 
21
 * $Id: base64.c,v 1.8 2004/10/13 14:01:04 andy Exp $
 
22
 ***************************************************************************/
 
23
 
 
24
/* Base64 encoding/decoding
 
25
 *
 
26
 * Test harnesses down the bottom - compile with -DTEST_ENCODE for
 
27
 * a program that will read in raw data from stdin and write out
 
28
 * a base64-encoded version to stdout, and the length returned by the
 
29
 * encoding function to stderr. Compile with -DTEST_DECODE for a program that
 
30
 * will go the other way.
 
31
 *
 
32
 * This code will break if int is smaller than 32 bits
 
33
 */
 
34
 
 
35
#include "setup.h"
 
36
 
 
37
#include <stdlib.h>
 
38
#include <string.h>
 
39
 
 
40
#define _MPRINTF_REPLACE /* use our functions only */
 
41
#include <curl/mprintf.h>
 
42
 
 
43
#include "base64.h"
 
44
#include "curl_memory.h"
 
45
 
 
46
/* include memdebug.h last */
 
47
#include "memdebug.h"
 
48
 
 
49
 
 
50
static void decodeQuantum(unsigned char *dest, const char *src)
 
51
{
 
52
  unsigned int x = 0;
 
53
  int i;
 
54
  for(i = 0; i < 4; i++) {
 
55
    if(src[i] >= 'A' && src[i] <= 'Z')
 
56
      x = (x << 6) + (unsigned int)(src[i] - 'A' + 0);
 
57
    else if(src[i] >= 'a' && src[i] <= 'z')
 
58
      x = (x << 6) + (unsigned int)(src[i] - 'a' + 26);
 
59
    else if(src[i] >= '0' && src[i] <= '9')
 
60
      x = (x << 6) + (unsigned int)(src[i] - '0' + 52);
 
61
    else if(src[i] == '+')
 
62
      x = (x << 6) + 62;
 
63
    else if(src[i] == '/')
 
64
      x = (x << 6) + 63;
 
65
    else if(src[i] == '=')
 
66
      x = (x << 6);
 
67
  }
 
68
 
 
69
  dest[2] = (unsigned char)(x & 255);
 
70
  x >>= 8;
 
71
  dest[1] = (unsigned char)(x & 255);
 
72
  x >>= 8;
 
73
  dest[0] = (unsigned char)(x & 255);
 
74
}
 
75
 
 
76
/*
 
77
 * Curl_base64_decode()
 
78
 *
 
79
 * Given a base64 string at src, decode it into the memory pointed to by
 
80
 * dest. Returns the length of the decoded data.
 
81
 */
 
82
size_t Curl_base64_decode(const char *src, char *dest)
 
83
{
 
84
  int length = 0;
 
85
  int equalsTerm = 0;
 
86
  int i;
 
87
  int numQuantums;
 
88
  unsigned char lastQuantum[3];
 
89
  size_t rawlen;
 
90
 
 
91
  while((src[length] != '=') && src[length])
 
92
    length++;
 
93
  while(src[length+equalsTerm] == '=')
 
94
    equalsTerm++;
 
95
 
 
96
  numQuantums = (length + equalsTerm) / 4;
 
97
 
 
98
  rawlen = (numQuantums * 3) - equalsTerm;
 
99
 
 
100
  for(i = 0; i < numQuantums - 1; i++) {
 
101
    decodeQuantum((unsigned char *)dest, src);
 
102
    dest += 3; src += 4;
 
103
  }
 
104
 
 
105
  decodeQuantum(lastQuantum, src);
 
106
  for(i = 0; i < 3 - equalsTerm; i++)
 
107
    dest[i] = lastQuantum[i];
 
108
 
 
109
  return rawlen;
 
110
}
 
111
 
 
112
/* ---- Base64 Encoding --- */
 
113
static char table64[]=
 
114
  "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
 
115
 
 
116
/*
 
117
 * Curl_base64_encode()
 
118
 *
 
119
 * Returns the length of the newly created base64 string. The third argument
 
120
 * is a pointer to an allocated area holding the base64 data. If something
 
121
 * went wrong, -1 is returned.
 
122
 *
 
123
 */
 
124
size_t Curl_base64_encode(const char *inp, size_t insize, char **outptr)
 
125
{
 
126
  unsigned char ibuf[3];
 
127
  unsigned char obuf[4];
 
128
  int i;
 
129
  int inputparts;
 
130
  char *output;
 
131
  char *base64data;
 
132
 
 
133
  char *indata = (char *)inp;
 
134
 
 
135
  *outptr = NULL; /* set to NULL in case of failure before we reach the end */
 
136
 
 
137
  if(0 == insize)
 
138
    insize = strlen(indata);
 
139
 
 
140
  base64data = output = (char*)malloc(insize*4/3+4);
 
141
  if(NULL == output)
 
142
    return 0;
 
143
 
 
144
  while(insize > 0) {
 
145
    for (i = inputparts = 0; i < 3; i++) {
 
146
      if(insize > 0) {
 
147
        inputparts++;
 
148
        ibuf[i] = *indata;
 
149
        indata++;
 
150
        insize--;
 
151
      }
 
152
      else
 
153
        ibuf[i] = 0;
 
154
    }
 
155
 
 
156
    obuf [0] = (ibuf [0] & 0xFC) >> 2;
 
157
    obuf [1] = ((ibuf [0] & 0x03) << 4) | ((ibuf [1] & 0xF0) >> 4);
 
158
    obuf [2] = ((ibuf [1] & 0x0F) << 2) | ((ibuf [2] & 0xC0) >> 6);
 
159
    obuf [3] = ibuf [2] & 0x3F;
 
160
 
 
161
    switch(inputparts) {
 
162
    case 1: /* only one byte read */
 
163
      snprintf(output, 5, "%c%c==",
 
164
               table64[obuf[0]],
 
165
               table64[obuf[1]]);
 
166
      break;
 
167
    case 2: /* two bytes read */
 
168
      snprintf(output, 5, "%c%c%c=",
 
169
               table64[obuf[0]],
 
170
               table64[obuf[1]],
 
171
               table64[obuf[2]]);
 
172
      break;
 
173
    default:
 
174
      snprintf(output, 5, "%c%c%c%c",
 
175
               table64[obuf[0]],
 
176
               table64[obuf[1]],
 
177
               table64[obuf[2]],
 
178
               table64[obuf[3]] );
 
179
      break;
 
180
    }
 
181
    output += 4;
 
182
  }
 
183
  *output=0;
 
184
  *outptr = base64data; /* make it return the actual data memory */
 
185
 
 
186
  return strlen(base64data); /* return the length of the new data */
 
187
}
 
188
/* ---- End of Base64 Encoding ---- */
 
189
 
 
190
/************* TEST HARNESS STUFF ****************/
 
191
 
 
192
 
 
193
#ifdef TEST_ENCODE
 
194
/* encoding test harness. Read in standard input and write out the length
 
195
 * returned by Curl_base64_encode, followed by the base64'd data itself
 
196
 */
 
197
#include <stdio.h>
 
198
 
 
199
#define TEST_NEED_SUCK
 
200
void *suck(int *);
 
201
 
 
202
int main(int argc, char **argv, char **envp)
 
203
{
 
204
  char *base64;
 
205
  size_t base64Len;
 
206
  unsigned char *data;
 
207
  int dataLen;
 
208
 
 
209
  data = (unsigned char *)suck(&dataLen);
 
210
  base64Len = Curl_base64_encode(data, dataLen, &base64);
 
211
 
 
212
  fprintf(stderr, "%d\n", base64Len);
 
213
  fprintf(stdout, "%s",   base64);
 
214
 
 
215
  free(base64); free(data);
 
216
  return 0;
 
217
}
 
218
#endif
 
219
 
 
220
#ifdef TEST_DECODE
 
221
/* decoding test harness. Read in a base64 string from stdin and write out the
 
222
 * length returned by Curl_base64_decode, followed by the decoded data itself
 
223
 *
 
224
 * gcc -DTEST_DECODE base64.c -o base64 mprintf.o memdebug.o
 
225
 */
 
226
#include <stdio.h>
 
227
 
 
228
#define TEST_NEED_SUCK
 
229
void *suck(int *);
 
230
 
 
231
int main(int argc, char **argv, char **envp)
 
232
{
 
233
  char *base64;
 
234
  int base64Len;
 
235
  unsigned char *data;
 
236
  int dataLen;
 
237
  int i, j;
 
238
 
 
239
  base64 = (char *)suck(&base64Len);
 
240
  data = (unsigned char *)malloc(base64Len * 3/4 + 8);
 
241
  dataLen = Curl_base64_decode(base64, data);
 
242
 
 
243
  fprintf(stderr, "%d\n", dataLen);
 
244
 
 
245
  for(i=0; i < dataLen; i+=0x10) {
 
246
    printf("0x%02x: ", i);
 
247
    for(j=0; j < 0x10; j++)
 
248
      if((j+i) < dataLen)
 
249
        printf("%02x ", data[i+j]);
 
250
      else
 
251
        printf("   ");
 
252
 
 
253
    printf(" | ");
 
254
 
 
255
    for(j=0; j < 0x10; j++)
 
256
      if((j+i) < dataLen)
 
257
        printf("%c", isgraph(data[i+j])?data[i+j]:'.');
 
258
      else
 
259
        break;
 
260
    puts("");
 
261
  }
 
262
 
 
263
  free(base64); free(data);
 
264
  return 0;
 
265
}
 
266
#endif
 
267
 
 
268
#ifdef TEST_NEED_SUCK
 
269
/* this function 'sucks' in as much as possible from stdin */
 
270
void *suck(int *lenptr)
 
271
{
 
272
  int cursize = 8192;
 
273
  unsigned char *buf = NULL;
 
274
  int lastread;
 
275
  int len = 0;
 
276
 
 
277
  do {
 
278
    cursize *= 2;
 
279
    buf = (unsigned char *)realloc(buf, cursize);
 
280
    memset(buf + len, 0, cursize - len);
 
281
    lastread = fread(buf + len, 1, cursize - len, stdin);
 
282
    len += lastread;
 
283
  } while(!feof(stdin));
 
284
 
 
285
  lenptr[0] = len;
 
286
  return (void *)buf;
 
287
}
 
288
#endif