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