1/*	$NetBSD: base64.c,v 1.2.6.1 2012/06/05 21:15:26 bouyer Exp $	*/
2
3/*
4 * Portions Copyright (C) 2004, 2005, 2007  Internet Systems Consortium, Inc. ("ISC")
5 * Portions Copyright (C) 2001  Internet Software Consortium.
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
10 *
11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
12 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
13 * OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY
14 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18 *
19 * Portions Copyright (C) 2001  Nominum, Inc.
20 *
21 * Permission to use, copy, modify, and/or distribute this software for any
22 * purpose with or without fee is hereby granted, provided that the above
23 * copyright notice and this permission notice appear in all copies.
24 *
25 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
26 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
27 * OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY
28 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
29 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
30 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
31 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
32 */
33
34/* Id: base64.c,v 1.8 2007/08/28 07:20:43 tbox Exp  */
35
36/*! \file */
37
38#include <config.h>
39
40#include <isc/base64.h>
41#include <isc/buffer.h>
42#include <isc/region.h>
43#include <isc/result.h>
44
45#include <isccc/base64.h>
46#include <isccc/result.h>
47#include <isccc/util.h>
48
49isc_result_t
50isccc_base64_encode(isccc_region_t *source, int wordlength,
51		  const char *wordbreak, isccc_region_t *target)
52{
53	isc_region_t sr;
54	isc_buffer_t tb;
55	isc_result_t result;
56
57	sr.base = source->rstart;
58	sr.length = source->rend - source->rstart;
59	isc_buffer_init(&tb, target->rstart, target->rend - target->rstart);
60
61	result = isc_base64_totext(&sr, wordlength, wordbreak, &tb);
62	if (result != ISC_R_SUCCESS)
63		return (result);
64	source->rstart = source->rend;
65	target->rstart = isc_buffer_used(&tb);
66	return (ISC_R_SUCCESS);
67}
68
69isc_result_t
70isccc_base64_decode(const char *cstr, isccc_region_t *target) {
71	isc_buffer_t b;
72	isc_result_t result;
73
74	isc_buffer_init(&b, target->rstart, target->rend - target->rstart);
75	result = isc_base64_decodestring(cstr, &b);
76	if (result != ISC_R_SUCCESS)
77		return (result);
78	target->rstart = isc_buffer_used(&b);
79	return (ISC_R_SUCCESS);
80}
81