summaryrefslogtreecommitdiff
path: root/mysys/base64.c
diff options
context:
space:
mode:
authorunknown <mats@romeo.(none)>2006-10-06 15:31:05 +0200
committerunknown <mats@romeo.(none)>2006-10-06 15:31:05 +0200
commit5e81d1301e375fc903466df025d1f9f22a3b0994 (patch)
treebacce208c70954e5ab55bde5d8e31391840ead28 /mysys/base64.c
parentcc4435ad8fb8037f8e6353655d2342ec25fbd207 (diff)
parentd8be3113351929f12e4277f4306a1428a280d970 (diff)
downloadmariadb-git-5e81d1301e375fc903466df025d1f9f22a3b0994.tar.gz
Merge romeo.(none):/home/bkroot/mysql-5.1-new-rpl
into romeo.(none):/home/bk/b19459-mysql-5.1-new client/mysqlbinlog.cc: Auto merged include/my_sys.h: Auto merged mysys/base64.c: Auto merged sql/log_event.cc: Auto merged sql/log_event.h: Auto merged storage/ndb/src/mgmapi/mgmapi.cpp: Auto merged sql/share/errmsg.txt: SCCS merged
Diffstat (limited to 'mysys/base64.c')
-rw-r--r--mysys/base64.c65
1 files changed, 47 insertions, 18 deletions
diff --git a/mysys/base64.c b/mysys/base64.c
index fb51bdb3a60..363aa2cc739 100644
--- a/mysys/base64.c
+++ b/mysys/base64.c
@@ -125,44 +125,69 @@ pos(unsigned char c)
/*
Decode a base64 string
- Note: We require that dst is pre-allocated to correct size.
- See base64_needed_decoded_length().
-
- RETURN Number of bytes produced in dst or -1 in case of failure
+ SYNOPSIS
+ base64_decode()
+ src Pointer to base64-encoded string
+ len Length of string at 'src'
+ dst Pointer to location where decoded data will be stored
+ end_ptr Pointer to variable that will refer to the character
+ after the end of the encoded data that were decoded. Can
+ be NULL.
+
+ DESCRIPTION
+
+ The base64-encoded data in the range ['src','*end_ptr') will be
+ decoded and stored starting at 'dst'. The decoding will stop
+ after 'len' characters have been read from 'src', or when padding
+ occurs in the base64-encoded data. In either case: if 'end_ptr' is
+ non-null, '*end_ptr' will be set to point to the character after
+ the last read character, even in the presence of error.
+
+ NOTE
+ We require that 'dst' is pre-allocated to correct size.
+
+ SEE ALSO
+ base64_needed_decoded_length().
+
+ RETURN VALUE
+ Number of bytes written at 'dst' or -1 in case of failure
*/
int
-base64_decode(const char *src, size_t size, void *dst)
+base64_decode(const char *const src_base, size_t const len,
+ void *dst, const char **end_ptr)
{
char b[3];
size_t i= 0;
char *dst_base= (char *)dst;
+ char const *src= src_base;
char *d= dst_base;
size_t j;
- while (i < size)
+ while (i < len)
{
unsigned c= 0;
size_t mark= 0;
- SKIP_SPACE(src, i, size);
+ SKIP_SPACE(src, i, len);
c += pos(*src++);
c <<= 6;
i++;
- SKIP_SPACE(src, i, size);
+ SKIP_SPACE(src, i, len);
c += pos(*src++);
c <<= 6;
i++;
- SKIP_SPACE(src, i, size);
+ SKIP_SPACE(src, i, len);
- if (* src != '=')
+ if (*src != '=')
c += pos(*src++);
else
{
- i= size;
+ src += 2; /* There should be two bytes padding */
+ i= len;
mark= 2;
c <<= 6;
goto end;
@@ -170,13 +195,14 @@ base64_decode(const char *src, size_t size, void *dst)
c <<= 6;
i++;
- SKIP_SPACE(src, i, size);
+ SKIP_SPACE(src, i, len);
if (*src != '=')
c += pos(*src++);
else
{
- i= size;
+ src += 1; /* There should be one byte padding */
+ i= len;
mark= 1;
goto end;
}
@@ -191,11 +217,14 @@ base64_decode(const char *src, size_t size, void *dst)
*d++= b[j];
}
- if (i != size)
- {
- return -1;
- }
- return d - dst_base;
+ if (end_ptr != NULL)
+ *end_ptr= src;
+
+ /*
+ The variable 'i' is set to 'len' when padding has been read, so it
+ does not actually reflect the number of bytes read from 'src'.
+ */
+ return i != len ? -1 : d - dst_base;
}