1999-05-18 17:46:59 +00:00
|
|
|
/*
|
2000-06-21 22:05:07 +00:00
|
|
|
* Copyright (C) 1998-2000 Internet Software Consortium.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
1999-05-18 17:46:59 +00:00
|
|
|
* Permission to use, copy, modify, and distribute this software for any
|
|
|
|
* purpose with or without fee is hereby granted, provided that the above
|
|
|
|
* copyright notice and this permission notice appear in all copies.
|
2000-08-01 01:33:37 +00:00
|
|
|
*
|
2000-07-27 09:55:03 +00:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS" AND INTERNET SOFTWARE CONSORTIUM
|
|
|
|
* DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL
|
|
|
|
* INTERNET SOFTWARE CONSORTIUM BE LIABLE FOR ANY SPECIAL, DIRECT,
|
|
|
|
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING
|
|
|
|
* FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
1999-05-18 17:46:59 +00:00
|
|
|
*/
|
|
|
|
|
2000-09-08 00:34:21 +00:00
|
|
|
/* $Id: base64.c,v 1.19 2000/09/08 00:34:21 gson Exp $ */
|
1999-05-18 17:46:59 +00:00
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <isc/base64.h>
|
|
|
|
#include <isc/buffer.h>
|
|
|
|
#include <isc/lex.h>
|
2000-05-08 14:38:29 +00:00
|
|
|
#include <isc/string.h>
|
2000-04-28 01:12:23 +00:00
|
|
|
#include <isc/util.h>
|
1999-05-18 17:46:59 +00:00
|
|
|
|
|
|
|
#define RETERR(x) do { \
|
2000-05-16 05:19:47 +00:00
|
|
|
isc_result_t _r = (x); \
|
|
|
|
if (_r != ISC_R_SUCCESS) \
|
|
|
|
return (_r); \
|
1999-05-18 17:46:59 +00:00
|
|
|
} while (0)
|
|
|
|
|
|
|
|
|
2000-05-08 14:38:29 +00:00
|
|
|
/*
|
|
|
|
* These static functions are also present in lib/dns/rdata.c. I'm not
|
1999-05-18 17:46:59 +00:00
|
|
|
* sure where they should go. -- bwelling
|
|
|
|
*/
|
2000-06-01 17:20:56 +00:00
|
|
|
static isc_result_t
|
|
|
|
str_totext(const char *source, isc_buffer_t *target);
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
gettoken(isc_lex_t *lexer, isc_token_t *token, isc_tokentype_t expect,
|
|
|
|
isc_boolean_t eol);
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length);
|
1999-05-18 17:46:59 +00:00
|
|
|
|
|
|
|
static const char base64[] =
|
|
|
|
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
|
|
|
|
|
|
|
|
isc_result_t
|
1999-06-08 10:35:23 +00:00
|
|
|
isc_base64_totext(isc_region_t *source, int wordlength,
|
2000-06-01 17:20:56 +00:00
|
|
|
const char *wordbreak, isc_buffer_t *target)
|
1999-06-08 10:35:23 +00:00
|
|
|
{
|
1999-05-18 17:46:59 +00:00
|
|
|
char buf[5];
|
1999-06-08 10:35:23 +00:00
|
|
|
unsigned int loops = 0;
|
|
|
|
|
|
|
|
if (wordlength < 4)
|
|
|
|
wordlength = 4;
|
1999-05-18 17:46:59 +00:00
|
|
|
|
|
|
|
memset(buf, 0, sizeof buf);
|
|
|
|
while (source->length > 2) {
|
|
|
|
buf[0] = base64[(source->base[0]>>2)&0x3f];
|
|
|
|
buf[1] = base64[((source->base[0]<<4)&0x30)|
|
|
|
|
((source->base[1]>>4)&0x0f)];
|
|
|
|
buf[2] = base64[((source->base[1]<<2)&0x3c)|
|
|
|
|
((source->base[2]>>6)&0x03)];
|
|
|
|
buf[3] = base64[source->base[2]&0x3f];
|
|
|
|
RETERR(str_totext(buf, target));
|
|
|
|
isc_region_consume(source, 3);
|
1999-06-08 10:35:23 +00:00
|
|
|
|
|
|
|
loops++;
|
|
|
|
if (source->length != 0 &&
|
|
|
|
(int)((loops + 1) * 4) >= wordlength)
|
|
|
|
{
|
1999-05-18 17:46:59 +00:00
|
|
|
loops = 0;
|
1999-06-08 10:35:23 +00:00
|
|
|
RETERR(str_totext(wordbreak, target));
|
1999-05-18 17:46:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (source->length == 2) {
|
|
|
|
buf[0] = base64[(source->base[0]>>2)&0x3f];
|
|
|
|
buf[1] = base64[((source->base[0]<<4)&0x30)|
|
|
|
|
((source->base[1]>>4)&0x0f)];
|
|
|
|
buf[2] = base64[((source->base[1]<<2)&0x3c)];
|
|
|
|
buf[3] = '=';
|
|
|
|
RETERR(str_totext(buf, target));
|
|
|
|
} else if (source->length == 1) {
|
|
|
|
buf[0] = base64[(source->base[0]>>2)&0x3f];
|
|
|
|
buf[1] = base64[((source->base[0]<<4)&0x30)];
|
|
|
|
buf[2] = buf[3] = '=';
|
|
|
|
RETERR(str_totext(buf, target));
|
|
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2000-09-08 00:34:21 +00:00
|
|
|
/*
|
|
|
|
* State of a base64 decoding process in progress.
|
|
|
|
*/
|
|
|
|
typedef struct {
|
|
|
|
int length; /* Desired length of binary data or -1 */
|
|
|
|
isc_buffer_t *target; /* Buffer for resulting binary data */
|
|
|
|
int digits; /* Number of buffered base64 digits */
|
|
|
|
isc_boolean_t seen_end; /* True if "=" end marker seen */
|
|
|
|
int val[4];
|
|
|
|
} base64_decode_ctx_t;
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
base64_decode_init(base64_decode_ctx_t *ctx, int length, isc_buffer_t *target)
|
|
|
|
{
|
|
|
|
ctx->digits = 0;
|
|
|
|
ctx->seen_end = ISC_FALSE;
|
|
|
|
ctx->length = length;
|
|
|
|
ctx->target = target;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline isc_result_t
|
|
|
|
base64_decode_char(base64_decode_ctx_t *ctx, int c) {
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
if (ctx->seen_end)
|
|
|
|
return (ISC_R_BADBASE64);
|
|
|
|
if ((s = strchr(base64, c)) == NULL)
|
|
|
|
return (ISC_R_BADBASE64);
|
|
|
|
ctx->val[ctx->digits++] = s - base64;
|
|
|
|
if (ctx->digits == 4) {
|
|
|
|
int n;
|
|
|
|
unsigned char buf[3];
|
|
|
|
if (ctx->val[0] == 64 || ctx->val[1] == 64)
|
|
|
|
return (ISC_R_BADBASE64);
|
|
|
|
if (ctx->val[2] == 64 && ctx->val[3] != 64)
|
|
|
|
return (ISC_R_BADBASE64);
|
|
|
|
n = (ctx->val[2] == 64) ? 1 :
|
|
|
|
(ctx->val[3] == 64) ? 2 : 3;
|
|
|
|
if (n != 3) {
|
|
|
|
ctx->seen_end = ISC_TRUE;
|
|
|
|
if (ctx->val[2] == 64)
|
|
|
|
ctx->val[2] = 0;
|
|
|
|
if (ctx->val[3] == 64)
|
|
|
|
ctx->val[3] = 0;
|
|
|
|
}
|
|
|
|
buf[0] = (ctx->val[0]<<2)|(ctx->val[1]>>4);
|
|
|
|
buf[1] = (ctx->val[1]<<4)|(ctx->val[2]>>2);
|
|
|
|
buf[2] = (ctx->val[2]<<6)|(ctx->val[3]);
|
|
|
|
RETERR(mem_tobuffer(ctx->target, buf, n));
|
|
|
|
if (ctx->length >= 0) {
|
|
|
|
if (n > ctx->length)
|
|
|
|
return (ISC_R_BADBASE64);
|
|
|
|
else
|
|
|
|
ctx->length -= n;
|
|
|
|
}
|
|
|
|
ctx->digits = 0;
|
|
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline isc_result_t
|
|
|
|
base64_decode_finish(base64_decode_ctx_t *ctx) {
|
|
|
|
if (ctx->length > 0)
|
|
|
|
return (ISC_R_UNEXPECTEDEND);
|
|
|
|
if (ctx->digits != 0)
|
|
|
|
return (ISC_R_BADBASE64);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
1999-05-18 17:46:59 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_base64_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length) {
|
2000-09-08 00:34:21 +00:00
|
|
|
base64_decode_ctx_t ctx;
|
1999-05-18 17:46:59 +00:00
|
|
|
isc_textregion_t *tr;
|
|
|
|
isc_token_t token;
|
|
|
|
|
2000-09-08 00:34:21 +00:00
|
|
|
base64_decode_init(&ctx, length, target);
|
|
|
|
|
|
|
|
while (!ctx.seen_end && (ctx.length != 0)) {
|
|
|
|
unsigned int i;
|
2000-08-01 01:33:37 +00:00
|
|
|
|
1999-05-18 17:46:59 +00:00
|
|
|
if (length > 0)
|
|
|
|
RETERR(gettoken(lexer, &token, isc_tokentype_string,
|
|
|
|
ISC_FALSE));
|
|
|
|
else
|
|
|
|
RETERR(gettoken(lexer, &token, isc_tokentype_string,
|
|
|
|
ISC_TRUE));
|
|
|
|
if (token.type != isc_tokentype_string)
|
|
|
|
break;
|
|
|
|
tr = &token.value.as_textregion;
|
2000-09-08 00:34:21 +00:00
|
|
|
for (i = 0 ;i < tr->length; i++)
|
|
|
|
RETERR(base64_decode_char(&ctx, tr->base[i]));
|
1999-05-18 17:46:59 +00:00
|
|
|
}
|
2000-09-08 00:34:21 +00:00
|
|
|
if (ctx.length < 0 && !ctx.seen_end)
|
1999-05-18 17:46:59 +00:00
|
|
|
isc_lex_ungettoken(lexer, &token);
|
2000-09-08 00:34:21 +00:00
|
|
|
RETERR(base64_decode_finish(&ctx));
|
1999-05-18 17:46:59 +00:00
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
2000-07-11 21:51:13 +00:00
|
|
|
isc_result_t
|
|
|
|
isc_base64_decodestring(isc_mem_t *mctx, char *cstr, isc_buffer_t *target) {
|
2000-09-08 00:34:21 +00:00
|
|
|
base64_decode_ctx_t ctx;
|
2000-07-11 21:51:13 +00:00
|
|
|
|
2000-09-08 00:34:21 +00:00
|
|
|
UNUSED(mctx);
|
2000-07-11 21:51:13 +00:00
|
|
|
|
2000-09-08 00:34:21 +00:00
|
|
|
base64_decode_init(&ctx, -1, target);
|
|
|
|
for (;;) {
|
|
|
|
int c = *cstr++;
|
|
|
|
if (c == '\0')
|
|
|
|
break;
|
|
|
|
if (c == ' ' || c == '\t' || c == '\n' || c== '\r')
|
|
|
|
continue;
|
|
|
|
RETERR(base64_decode_char(&ctx, c));
|
2000-08-01 01:33:37 +00:00
|
|
|
}
|
2000-09-08 00:34:21 +00:00
|
|
|
RETERR(base64_decode_finish(&ctx));
|
|
|
|
return (ISC_R_SUCCESS);
|
2000-07-11 21:51:13 +00:00
|
|
|
}
|
|
|
|
|
1999-05-18 17:46:59 +00:00
|
|
|
static isc_result_t
|
2000-06-01 17:20:56 +00:00
|
|
|
str_totext(const char *source, isc_buffer_t *target) {
|
1999-05-18 17:46:59 +00:00
|
|
|
unsigned int l;
|
|
|
|
isc_region_t region;
|
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availableregion(target, ®ion);
|
1999-05-18 17:46:59 +00:00
|
|
|
l = strlen(source);
|
|
|
|
|
|
|
|
if (l > region.length)
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
|
|
|
|
memcpy(region.base, source, l);
|
|
|
|
isc_buffer_add(target, l);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
mem_tobuffer(isc_buffer_t *target, void *base, unsigned int length) {
|
|
|
|
isc_region_t tr;
|
|
|
|
|
103. [func] libisc buffer API changes for <isc/buffer.h>:
Added:
isc_buffer_base(b) (pointer)
isc_buffer_current(b) (pointer)
isc_buffer_active(b) (pointer)
isc_buffer_used(b) (pointer)
isc_buffer_length(b) (int)
isc_buffer_usedlength(b) (int)
isc_buffer_consumedlength(b) (int)
isc_buffer_remaininglength(b) (int)
isc_buffer_activelength(b) (int)
isc_buffer_availablelength(b) (int)
Removed:
ISC_BUFFER_USEDCOUNT(b)
ISC_BUFFER_AVAILABLECOUNT(b)
isc_buffer_type(b)
Changed names:
isc_buffer_used(b, r) ->
isc_buffer_usedregion(b, r)
isc_buffer_available(b, r) ->
isc_buffer_available_region(b, r)
isc_buffer_consumed(b, r) ->
isc_buffer_consumedregion(b, r)
isc_buffer_active(b, r) ->
isc_buffer_activeregion(b, r)
isc_buffer_remaining(b, r) ->
isc_buffer_remainingregion(b, r)
Buffer types were removed, so the ISC_BUFFERTYPE_*
macros are no more, and the type argument to
isc_buffer_init and isc_buffer_allocate were removed.
isc_buffer_putstr is now void (instead of isc_result_t)
and requires that the caller ensure that there
is enough available buffer space for the string.
2000-04-27 00:03:12 +00:00
|
|
|
isc_buffer_availableregion(target, &tr);
|
1999-05-18 17:46:59 +00:00
|
|
|
if (length > tr.length)
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
memcpy(tr.base, base, length);
|
|
|
|
isc_buffer_add(target, length);
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
static isc_result_t
|
|
|
|
gettoken(isc_lex_t *lexer, isc_token_t *token, isc_tokentype_t expect,
|
|
|
|
isc_boolean_t eol)
|
|
|
|
{
|
|
|
|
unsigned int options = ISC_LEXOPT_EOL | ISC_LEXOPT_EOF |
|
1999-06-09 11:56:45 +00:00
|
|
|
ISC_LEXOPT_DNSMULTILINE | ISC_LEXOPT_ESCAPE;
|
1999-05-18 17:46:59 +00:00
|
|
|
isc_result_t result;
|
|
|
|
|
|
|
|
if (expect == isc_tokentype_qstring)
|
|
|
|
options |= ISC_LEXOPT_QSTRING;
|
|
|
|
else if (expect == isc_tokentype_number)
|
|
|
|
options |= ISC_LEXOPT_NUMBER;
|
|
|
|
result = isc_lex_gettoken(lexer, options, token);
|
|
|
|
switch (result) {
|
|
|
|
case ISC_R_SUCCESS:
|
|
|
|
break;
|
|
|
|
case ISC_R_NOMEMORY:
|
|
|
|
return (ISC_R_NOMEMORY);
|
|
|
|
case ISC_R_NOSPACE:
|
|
|
|
return (ISC_R_NOSPACE);
|
|
|
|
default:
|
|
|
|
UNEXPECTED_ERROR(__FILE__, __LINE__,
|
2000-02-17 19:58:59 +00:00
|
|
|
"isc_lex_gettoken() failed: %s",
|
1999-05-18 17:46:59 +00:00
|
|
|
isc_result_totext(result));
|
|
|
|
return (ISC_R_UNEXPECTED);
|
|
|
|
}
|
|
|
|
if (eol && ((token->type == isc_tokentype_eol) ||
|
|
|
|
(token->type == isc_tokentype_eof)))
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
if (token->type == isc_tokentype_string &&
|
|
|
|
expect == isc_tokentype_qstring)
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
if (token->type != expect) {
|
|
|
|
isc_lex_ungettoken(lexer, token);
|
|
|
|
if (token->type == isc_tokentype_eol ||
|
|
|
|
token->type == isc_tokentype_eof)
|
|
|
|
return (ISC_R_UNEXPECTEDEND);
|
|
|
|
return (ISC_R_UNEXPECTEDTOKEN);
|
|
|
|
}
|
|
|
|
return (ISC_R_SUCCESS);
|
|
|
|
}
|