base64.c revision ab023a65562e62b85a824509d829b6fad87e00b1
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews/*
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Portions Copyright (C) 2004 Internet Systems Consortium, Inc. ("ISC")
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Portions Copyright (C) 2001 Internet Software Consortium.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews * Portions Copyright (C) 2001 Nominum, Inc.
0c27b3fe77ac1d5094ba3521e8142d9e7973133fMark Andrews *
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson * Permission to use, copy, modify, and distribute this software for any
dafcb997e390efa4423883dafd100c975c4095d6Mark Andrews * purpose with or without fee is hereby granted, provided that the above
d4ef65050feac78554addf6e16a06c6e2e0bd331Brian Wellington * copyright notice and this permission notice appear in all copies.
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson *
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson * OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson */
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson/* $Id: base64.c,v 1.4 2005/04/27 04:57:25 sra Exp $ */
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson/*! \file */
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson#include <config.h>
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson#include <isc/base64.h>
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson#include <isc/buffer.h>
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson#include <isc/region.h>
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson#include <isc/result.h>
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson#include <isccc/base64.h>
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson#include <isccc/result.h>
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson#include <isccc/util.h>
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafssonisc_result_t
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafssonisccc_base64_encode(isccc_region_t *source, int wordlength,
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson const char *wordbreak, isccc_region_t *target)
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson{
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson isc_region_t sr;
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson isc_buffer_t tb;
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson isc_result_t result;
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson sr.base = source->rstart;
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson sr.length = source->rend - source->rstart;
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson isc_buffer_init(&tb, target->rstart, target->rend - target->rstart);
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson result = isc_base64_totext(&sr, wordlength, wordbreak, &tb);
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson if (result != ISC_R_SUCCESS)
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson return (result);
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson source->rstart = source->rend;
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson target->rstart = isc_buffer_used(&tb);
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson return (ISC_R_SUCCESS);
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson}
39f1df3fbd71b6d57c31f6a1b3d7e5194ef63fccAndreas Gustafsson
c3cc5b5bcfd045692d2a36cbf2da62bc356ca69fAndreas Gustafssonisc_result_t
c3cc5b5bcfd045692d2a36cbf2da62bc356ca69fAndreas Gustafssonisccc_base64_decode(const char *cstr, isccc_region_t *target) {
c3cc5b5bcfd045692d2a36cbf2da62bc356ca69fAndreas Gustafsson isc_buffer_t b;
c3cc5b5bcfd045692d2a36cbf2da62bc356ca69fAndreas Gustafsson isc_result_t result;
c3cc5b5bcfd045692d2a36cbf2da62bc356ca69fAndreas Gustafsson
c3cc5b5bcfd045692d2a36cbf2da62bc356ca69fAndreas Gustafsson isc_buffer_init(&b, target->rstart, target->rend - target->rstart);
result = isc_base64_decodestring(cstr, &b);
if (result != ISC_R_SUCCESS)
return (result);
target->rstart = isc_buffer_used(&b);
return (ISC_R_SUCCESS);
}