~ubuntu-branches/ubuntu/edgy/curl/edgy

« back to all changes in this revision

Viewing changes to lib/base64.c

  • Committer: Bazaar Package Importer
  • Author(s): Matthias Klose
  • Date: 2005-07-26 19:03:01 UTC
  • mfrom: (1.1.3 upstream)
  • Revision ID: james.westby@ubuntu.com-20050726190301-x2m2vmjgc8fwnic5
Tags: 7.14.0-2ubuntu1
Synchronize with Debian.

Show diffs side-by-side

added added

removed removed

Lines of Context:
5
5
 *                            | (__| |_| |  _ <| |___
6
6
 *                             \___|\___/|_| \_\_____|
7
7
 *
8
 
 * Copyright (C) 1998 - 2004, Daniel Stenberg, <daniel@haxx.se>, et al.
 
8
 * Copyright (C) 1998 - 2005, 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.32 2004/12/15 01:38:25 danf Exp $
 
21
 * $Id: base64.c,v 1.35 2005/03/31 07:02:03 bagder Exp $
22
22
 ***************************************************************************/
23
23
 
24
24
/* Base64 encoding/decoding
76
76
/*
77
77
 * Curl_base64_decode()
78
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.
 
79
 * Given a base64 string at src, decode it and return an allocated memory in
 
80
 * the *outptr. Returns the length of the decoded data.
81
81
 */
82
 
size_t Curl_base64_decode(const char *src, char *dest)
 
82
size_t Curl_base64_decode(const char *src, unsigned char **outptr)
83
83
{
84
84
  int length = 0;
85
85
  int equalsTerm = 0;
87
87
  int numQuantums;
88
88
  unsigned char lastQuantum[3];
89
89
  size_t rawlen=0;
 
90
  unsigned char *newstr;
 
91
 
 
92
  *outptr = NULL;
90
93
 
91
94
  while((src[length] != '=') && src[length])
92
95
    length++;
93
 
  while(src[length+equalsTerm] == '=')
 
96
  /* A maximum of two = padding characters is allowed */
 
97
  if(src[length] == '=') {
94
98
    equalsTerm++;
95
 
 
 
99
    if(src[length+equalsTerm] == '=')
 
100
      equalsTerm++;
 
101
  }
96
102
  numQuantums = (length + equalsTerm) / 4;
97
103
 
 
104
  /* Don't allocate a buffer if the decoded length is 0 */
 
105
  if (numQuantums <= 0)
 
106
    return 0;
 
107
 
98
108
  rawlen = (numQuantums * 3) - equalsTerm;
99
109
 
 
110
  /* The buffer must be large enough to make room for the last quantum
 
111
  (which may be partially thrown out) and the zero terminator. */
 
112
  newstr = malloc(rawlen+4);
 
113
  if(!newstr)
 
114
    return 0;
 
115
 
 
116
  *outptr = newstr;
 
117
 
 
118
  /* Decode all but the last quantum (which may not decode to a
 
119
  multiple of 3 bytes) */
100
120
  for(i = 0; i < numQuantums - 1; i++) {
101
 
    decodeQuantum((unsigned char *)dest, src);
102
 
    dest += 3; src += 4;
 
121
    decodeQuantum((unsigned char *)newstr, src);
 
122
    newstr += 3; src += 4;
103
123
  }
104
124
 
 
125
  /* This final decode may actually read slightly past the end of the buffer
 
126
  if the input string is missing pad bytes.  This will almost always be
 
127
  harmless. */
105
128
  decodeQuantum(lastQuantum, src);
106
129
  for(i = 0; i < 3 - equalsTerm; i++)
107
 
    dest[i] = lastQuantum[i];
 
130
    newstr[i] = lastQuantum[i];
108
131
 
 
132
  newstr[i] = 0; /* zero terminate */
109
133
  return rawlen;
110
134
}
111
135