1/*	$NetBSD: base64.c,v 1.6 2024/02/21 22:52:42 christos Exp $	*/
2
3/*
4 * Copyright (C) Internet Systems Consortium, Inc. ("ISC")
5 *
6 * SPDX-License-Identifier: MPL-2.0 AND ISC
7 *
8 * This Source Code Form is subject to the terms of the Mozilla Public
9 * License, v. 2.0. If a copy of the MPL was not distributed with this
10 * file, you can obtain one at https://mozilla.org/MPL/2.0/.
11 *
12 * See the COPYRIGHT file distributed with this work for additional
13 * information regarding copyright ownership.
14 */
15
16/*
17 * Copyright (C) 2001 Nominum, Inc.
18 *
19 * Permission to use, copy, modify, and/or distribute this software for any
20 * purpose with or without fee is hereby granted, provided that the above
21 * copyright notice and this permission notice appear in all copies.
22 *
23 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC AND NOMINUM DISCLAIMS ALL
24 * WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES
25 * OF MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY
26 * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
27 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
28 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
29 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
30 */
31
32/*! \file */
33
34#include <isc/base64.h>
35#include <isc/buffer.h>
36#include <isc/region.h>
37#include <isc/result.h>
38
39#include <isccc/base64.h>
40#include <isccc/util.h>
41
42isc_result_t
43isccc_base64_encode(isccc_region_t *source, int wordlength,
44		    const char *wordbreak, isccc_region_t *target) {
45	isc_region_t sr;
46	isc_buffer_t tb;
47	isc_result_t result;
48
49	sr.base = source->rstart;
50	sr.length = (unsigned int)(source->rend - source->rstart);
51	isc_buffer_init(&tb, target->rstart,
52			(unsigned int)(target->rend - target->rstart));
53
54	result = isc_base64_totext(&sr, wordlength, wordbreak, &tb);
55	if (result != ISC_R_SUCCESS) {
56		return (result);
57	}
58	source->rstart = source->rend;
59	target->rstart = isc_buffer_used(&tb);
60	return (ISC_R_SUCCESS);
61}
62
63isc_result_t
64isccc_base64_decode(const char *cstr, isccc_region_t *target) {
65	isc_buffer_t b;
66	isc_result_t result;
67
68	isc_buffer_init(&b, target->rstart,
69			(unsigned int)(target->rend - target->rstart));
70	result = isc_base64_decodestring(cstr, &b);
71	if (result != ISC_R_SUCCESS) {
72		return (result);
73	}
74	target->rstart = isc_buffer_used(&b);
75	return (ISC_R_SUCCESS);
76}
77