1/*
2 * Copyright (C) 2008  Internet Systems Consortium, Inc. ("ISC")
3 *
4 * Permission to use, copy, modify, and/or distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
7 *
8 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9 * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10 * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11 * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12 * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13 * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14 * PERFORMANCE OF THIS SOFTWARE.
15 */
16
17/* $Id: base32.h,v 1.3 2008/09/25 04:02:39 tbox Exp $ */
18
19#ifndef ISC_BASE32_H
20#define ISC_BASE32_H 1
21
22/*! \file */
23
24/*
25 * Routines for manipulating base 32 and base 32 hex encoded data.
26 * Based on RFC 4648.
27 *
28 * Base 32 hex preserves the sort order of data when it is encoded /
29 * decoded.
30 */
31
32#include <isc/lang.h>
33#include <isc/types.h>
34
35ISC_LANG_BEGINDECLS
36
37/***
38 *** Functions
39 ***/
40
41isc_result_t
42isc_base32_totext(isc_region_t *source, int wordlength,
43		  const char *wordbreak, isc_buffer_t *target);
44isc_result_t
45isc_base32hex_totext(isc_region_t *source, int wordlength,
46		     const char *wordbreak, isc_buffer_t *target);
47/*!<
48 * \brief Convert data into base32 encoded text.
49 *
50 * Notes:
51 *\li	The base32 encoded text in 'target' will be divided into
52 *	words of at most 'wordlength' characters, separated by
53 * 	the 'wordbreak' string.  No parentheses will surround
54 *	the text.
55 *
56 * Requires:
57 *\li	'source' is a region containing binary data
58 *\li	'target' is a text buffer containing available space
59 *\li	'wordbreak' points to a null-terminated string of
60 *		zero or more whitespace characters
61 *
62 * Ensures:
63 *\li	target will contain the base32 encoded version of the data
64 *	in source.  The 'used' pointer in target will be advanced as
65 *	necessary.
66 */
67
68isc_result_t
69isc_base32_decodestring(const char *cstr, isc_buffer_t *target);
70isc_result_t
71isc_base32hex_decodestring(const char *cstr, isc_buffer_t *target);
72/*!<
73 * \brief Decode a null-terminated base32 string.
74 *
75 * Requires:
76 *\li	'cstr' is non-null.
77 *\li	'target' is a valid buffer.
78 *
79 * Returns:
80 *\li	#ISC_R_SUCCESS	-- the entire decoded representation of 'cstring'
81 *			   fit in 'target'.
82 *\li	#ISC_R_BADBASE32 -- 'cstr' is not a valid base32 encoding.
83 *
84 * 	Other error returns are any possible error code from:
85 *\li		isc_lex_create(),
86 *\li		isc_lex_openbuffer(),
87 *\li		isc_base32_tobuffer().
88 */
89
90isc_result_t
91isc_base32_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length);
92isc_result_t
93isc_base32hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length);
94/*!<
95 * \brief Convert base32 encoded text from a lexer context into data.
96 *
97 * Requires:
98 *\li	'lex' is a valid lexer context
99 *\li	'target' is a buffer containing binary data
100 *\li	'length' is an integer
101 *
102 * Ensures:
103 *\li	target will contain the data represented by the base32 encoded
104 *	string parsed by the lexer.  No more than length bytes will be read,
105 *	if length is positive.  The 'used' pointer in target will be
106 *	advanced as necessary.
107 */
108
109isc_result_t
110isc_base32_decoderegion(isc_region_t *source, isc_buffer_t *target);
111isc_result_t
112isc_base32hex_decoderegion(isc_region_t *source, isc_buffer_t *target);
113/*!<
114 * \brief Decode a packed (no white space permitted) base32 region.
115 *
116 * Requires:
117 *\li   'source' is a valid region.
118 *\li   'target' is a valid buffer.
119 *
120 * Returns:
121 *\li   #ISC_R_SUCCESS  -- the entire decoded representation of 'cstring'
122 *                         fit in 'target'.
123 *\li   #ISC_R_BADBASE32 -- 'source' is not a valid base32 encoding.
124 */
125
126ISC_LANG_ENDDECLS
127
128#endif /* ISC_BASE32_H */
129