1258945Sroberto/*
2258945Sroberto * Copyright (C) 2008  Internet Systems Consortium, Inc. ("ISC")
3258945Sroberto *
4258945Sroberto * Permission to use, copy, modify, and/or distribute this software for any
5258945Sroberto * purpose with or without fee is hereby granted, provided that the above
6258945Sroberto * copyright notice and this permission notice appear in all copies.
7258945Sroberto *
8258945Sroberto * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
9258945Sroberto * REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
10258945Sroberto * AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
11258945Sroberto * INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
12258945Sroberto * LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
13258945Sroberto * OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
14258945Sroberto * PERFORMANCE OF THIS SOFTWARE.
15258945Sroberto */
16258945Sroberto
17258945Sroberto/* $Id: base32.h,v 1.3 2008/09/25 04:02:39 tbox Exp $ */
18258945Sroberto
19258945Sroberto#ifndef ISC_BASE32_H
20258945Sroberto#define ISC_BASE32_H 1
21258945Sroberto
22258945Sroberto/*! \file */
23258945Sroberto
24258945Sroberto/*
25258945Sroberto * Routines for manipulating base 32 and base 32 hex encoded data.
26258945Sroberto * Based on RFC 4648.
27258945Sroberto *
28258945Sroberto * Base 32 hex preserves the sort order of data when it is encoded /
29258945Sroberto * decoded.
30258945Sroberto */
31258945Sroberto
32258945Sroberto#include <isc/lang.h>
33258945Sroberto#include <isc/types.h>
34258945Sroberto
35258945SrobertoISC_LANG_BEGINDECLS
36258945Sroberto
37258945Sroberto/***
38258945Sroberto *** Functions
39258945Sroberto ***/
40258945Sroberto
41258945Srobertoisc_result_t
42258945Srobertoisc_base32_totext(isc_region_t *source, int wordlength,
43258945Sroberto		  const char *wordbreak, isc_buffer_t *target);
44258945Srobertoisc_result_t
45258945Srobertoisc_base32hex_totext(isc_region_t *source, int wordlength,
46258945Sroberto		     const char *wordbreak, isc_buffer_t *target);
47258945Sroberto/*!<
48258945Sroberto * \brief Convert data into base32 encoded text.
49258945Sroberto *
50258945Sroberto * Notes:
51258945Sroberto *\li	The base32 encoded text in 'target' will be divided into
52258945Sroberto *	words of at most 'wordlength' characters, separated by
53258945Sroberto * 	the 'wordbreak' string.  No parentheses will surround
54258945Sroberto *	the text.
55258945Sroberto *
56258945Sroberto * Requires:
57258945Sroberto *\li	'source' is a region containing binary data
58258945Sroberto *\li	'target' is a text buffer containing available space
59258945Sroberto *\li	'wordbreak' points to a null-terminated string of
60258945Sroberto *		zero or more whitespace characters
61258945Sroberto *
62258945Sroberto * Ensures:
63258945Sroberto *\li	target will contain the base32 encoded version of the data
64258945Sroberto *	in source.  The 'used' pointer in target will be advanced as
65258945Sroberto *	necessary.
66258945Sroberto */
67258945Sroberto
68258945Srobertoisc_result_t
69258945Srobertoisc_base32_decodestring(const char *cstr, isc_buffer_t *target);
70258945Srobertoisc_result_t
71258945Srobertoisc_base32hex_decodestring(const char *cstr, isc_buffer_t *target);
72258945Sroberto/*!<
73258945Sroberto * \brief Decode a null-terminated base32 string.
74258945Sroberto *
75258945Sroberto * Requires:
76258945Sroberto *\li	'cstr' is non-null.
77258945Sroberto *\li	'target' is a valid buffer.
78258945Sroberto *
79258945Sroberto * Returns:
80258945Sroberto *\li	#ISC_R_SUCCESS	-- the entire decoded representation of 'cstring'
81258945Sroberto *			   fit in 'target'.
82258945Sroberto *\li	#ISC_R_BADBASE32 -- 'cstr' is not a valid base32 encoding.
83258945Sroberto *
84258945Sroberto * 	Other error returns are any possible error code from:
85258945Sroberto *\li		isc_lex_create(),
86258945Sroberto *\li		isc_lex_openbuffer(),
87258945Sroberto *\li		isc_base32_tobuffer().
88258945Sroberto */
89258945Sroberto
90258945Srobertoisc_result_t
91258945Srobertoisc_base32_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length);
92258945Srobertoisc_result_t
93258945Srobertoisc_base32hex_tobuffer(isc_lex_t *lexer, isc_buffer_t *target, int length);
94258945Sroberto/*!<
95258945Sroberto * \brief Convert base32 encoded text from a lexer context into data.
96258945Sroberto *
97258945Sroberto * Requires:
98258945Sroberto *\li	'lex' is a valid lexer context
99258945Sroberto *\li	'target' is a buffer containing binary data
100258945Sroberto *\li	'length' is an integer
101258945Sroberto *
102258945Sroberto * Ensures:
103258945Sroberto *\li	target will contain the data represented by the base32 encoded
104258945Sroberto *	string parsed by the lexer.  No more than length bytes will be read,
105258945Sroberto *	if length is positive.  The 'used' pointer in target will be
106258945Sroberto *	advanced as necessary.
107258945Sroberto */
108258945Sroberto
109258945Srobertoisc_result_t
110258945Srobertoisc_base32_decoderegion(isc_region_t *source, isc_buffer_t *target);
111258945Srobertoisc_result_t
112258945Srobertoisc_base32hex_decoderegion(isc_region_t *source, isc_buffer_t *target);
113258945Sroberto/*!<
114258945Sroberto * \brief Decode a packed (no white space permitted) base32 region.
115258945Sroberto *
116258945Sroberto * Requires:
117258945Sroberto *\li   'source' is a valid region.
118258945Sroberto *\li   'target' is a valid buffer.
119258945Sroberto *
120258945Sroberto * Returns:
121258945Sroberto *\li   #ISC_R_SUCCESS  -- the entire decoded representation of 'cstring'
122258945Sroberto *                         fit in 'target'.
123258945Sroberto *\li   #ISC_R_BADBASE32 -- 'source' is not a valid base32 encoding.
124258945Sroberto */
125258945Sroberto
126258945SrobertoISC_LANG_ENDDECLS
127258945Sroberto
128258945Sroberto#endif /* ISC_BASE32_H */
129