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