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