xref: /minix/external/bsd/bind/dist/lib/isccc/base64.c (revision bb9622b5)
1 /*	$NetBSD: base64.c,v 1.5 2014/12/10 04:38:01 christos Exp $	*/
2 
3 /*
4  * Portions Copyright (C) 2004, 2005, 2007, 2013  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 
49 isc_result_t
50 isccc_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 = (unsigned int)(source->rend - source->rstart);
59 	isc_buffer_init(&tb, target->rstart,
60 			(unsigned int)(target->rend - target->rstart));
61 
62 	result = isc_base64_totext(&sr, wordlength, wordbreak, &tb);
63 	if (result != ISC_R_SUCCESS)
64 		return (result);
65 	source->rstart = source->rend;
66 	target->rstart = isc_buffer_used(&tb);
67 	return (ISC_R_SUCCESS);
68 }
69 
70 isc_result_t
71 isccc_base64_decode(const char *cstr, isccc_region_t *target) {
72 	isc_buffer_t b;
73 	isc_result_t result;
74 
75 	isc_buffer_init(&b, target->rstart,
76 			(unsigned int)(target->rend - target->rstart));
77 	result = isc_base64_decodestring(cstr, &b);
78 	if (result != ISC_R_SUCCESS)
79 		return (result);
80 	target->rstart = isc_buffer_used(&b);
81 	return (ISC_R_SUCCESS);
82 }
83