~ubuntu-branches/ubuntu/raring/curl/raring-updates

« back to all changes in this revision

Viewing changes to lib/base64.c

  • Committer: Package Import Robot
  • Author(s): Alessandro Ghedini
  • Date: 2011-11-13 21:07:32 UTC
  • mto: (3.6.1 experimental) (1.3.1)
  • mto: This revision was merged to the branch mainline in revision 55.
  • Revision ID: package-import@ubuntu.com-20111113210732-bk5n25x2tu7aplur
Tags: upstream-7.22.0
ImportĀ upstreamĀ versionĀ 7.22.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
 
25
25
#include "setup.h"
26
26
 
27
 
#include <stdlib.h>
28
 
#include <string.h>
29
 
 
30
27
#define _MPRINTF_REPLACE /* use our functions only */
31
28
#include <curl/mprintf.h>
32
29
 
71
68
/*
72
69
 * Curl_base64_decode()
73
70
 *
74
 
 * Given a base64 string at src, decode it and return an allocated memory in
75
 
 * the *outptr. Returns the length of the decoded data.
 
71
 * Given a base64 NUL-terminated string at src, decode it and return a
 
72
 * pointer in *outptr to a newly allocated memory area holding decoded
 
73
 * data. Size of decoded data is returned in variable pointed by outlen.
 
74
 *
 
75
 * Returns CURLE_OK on success, otherwise specific error code. Function
 
76
 * output shall not be considered valid unless CURLE_OK is returned.
 
77
 *
 
78
 * When decoded data length is 0, returns NULL in *outptr.
76
79
 *
77
80
 * @unittest: 1302
78
81
 */
79
 
size_t Curl_base64_decode(const char *src, unsigned char **outptr)
 
82
CURLcode Curl_base64_decode(const char *src,
 
83
                            unsigned char **outptr, size_t *outlen)
80
84
{
81
85
  size_t length = 0;
82
86
  size_t equalsTerm = 0;
87
91
  unsigned char *newstr;
88
92
 
89
93
  *outptr = NULL;
 
94
  *outlen = 0;
90
95
 
91
96
  while((src[length] != '=') && src[length])
92
97
    length++;
100
105
 
101
106
  /* Don't allocate a buffer if the decoded length is 0 */
102
107
  if(numQuantums == 0)
103
 
    return 0;
 
108
    return CURLE_OK;
104
109
 
105
110
  rawlen = (numQuantums * 3) - equalsTerm;
106
111
 
108
113
  (which may be partially thrown out) and the zero terminator. */
109
114
  newstr = malloc(rawlen+4);
110
115
  if(!newstr)
111
 
    return 0;
 
116
    return CURLE_OUT_OF_MEMORY;
112
117
 
113
118
  *outptr = newstr;
114
119
 
127
132
    newstr[i] = lastQuantum[i];
128
133
 
129
134
  newstr[i] = '\0'; /* zero terminate */
130
 
  return rawlen;
 
135
 
 
136
  *outlen = rawlen; /* return size of decoded data */
 
137
 
 
138
  return CURLE_OK;
131
139
}
132
140
 
133
141
/*
134
142
 * Curl_base64_encode()
135
143
 *
136
 
 * Returns the length of the newly created base64 string. The third argument
137
 
 * is a pointer to an allocated area holding the base64 data. If something
138
 
 * went wrong, 0 is returned.
 
144
 * Given a pointer to an input buffer and an input size, encode it and
 
145
 * return a pointer in *outptr to a newly allocated memory area holding
 
146
 * encoded data. Size of encoded data is returned in variable pointed by
 
147
 * outlen.
 
148
 *
 
149
 * Input length of 0 indicates input buffer holds a NUL-terminated string.
 
150
 *
 
151
 * Returns CURLE_OK on success, otherwise specific error code. Function
 
152
 * output shall not be considered valid unless CURLE_OK is returned.
 
153
 *
 
154
 * When encoded data length is 0, returns NULL in *outptr.
139
155
 *
140
156
 * @unittest: 1302
141
157
 */
142
 
size_t Curl_base64_encode(struct SessionHandle *data,
143
 
                          const char *inputbuff, size_t insize,
144
 
                          char **outptr)
 
158
CURLcode Curl_base64_encode(struct SessionHandle *data,
 
159
                            const char *inputbuff, size_t insize,
 
160
                            char **outptr, size_t *outlen)
145
161
{
146
 
  CURLcode res;
 
162
  CURLcode error;
147
163
  unsigned char ibuf[3];
148
164
  unsigned char obuf[4];
149
165
  int i;
154
170
 
155
171
  const char *indata = inputbuff;
156
172
 
157
 
  *outptr = NULL; /* set to NULL in case of failure before we reach the end */
 
173
  *outptr = NULL;
 
174
  *outlen = 0;
158
175
 
159
176
  if(0 == insize)
160
177
    insize = strlen(indata);
161
178
 
162
179
  base64data = output = malloc(insize*4/3+4);
163
180
  if(NULL == output)
164
 
    return 0;
 
181
    return CURLE_OUT_OF_MEMORY;
165
182
 
166
183
  /*
167
184
   * The base64 data needs to be created using the network encoding
168
185
   * not the host encoding.  And we can't change the actual input
169
186
   * so we copy it to a buffer, translate it, and use that instead.
170
187
   */
171
 
  res = Curl_convert_clone(data, indata, insize, &convbuf);
172
 
  if(res) {
 
188
  error = Curl_convert_clone(data, indata, insize, &convbuf);
 
189
  if(error) {
173
190
    free(output);
174
 
    return 0;
 
191
    return error;
175
192
  }
176
193
 
177
194
  if(convbuf)
218
235
    }
219
236
    output += 4;
220
237
  }
221
 
  *output=0;
222
 
  *outptr = base64data; /* make it return the actual data memory */
 
238
  *output = '\0';
 
239
  *outptr = base64data; /* return pointer to new data, allocated memory */
223
240
 
224
241
  if(convbuf)
225
242
    free(convbuf);
226
243
 
227
 
  return strlen(base64data); /* return the length of the new data */
 
244
  *outlen = strlen(base64data); /* return the length of the new data */
 
245
 
 
246
  return CURLE_OK;
228
247
}
229
248
/* ---- End of Base64 Encoding ---- */