75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch#ifndef BASE32_H
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch#define BASE32_H
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch/* Translates binary data into base32 (RFC 4648, Section 6). The src must not
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch point to dest buffer. The pad argument determines whether output is padded
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch with '='.
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch */
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschvoid base32_encode(bool pad, const void *src, size_t src_size,
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch buffer_t *dest);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch/* Translates binary data into base32hex (RFC 4648, Section 7). The src must
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch not point to dest buffer. The pad argument determines whether output is
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch padded with '='.
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch */
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschvoid base32hex_encode(bool pad, const void *src, size_t src_size,
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch buffer_t *dest);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch/* Translates base32/base32hex data into binary and appends it to dest buffer.
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch dest may point to same buffer as src. Returns 1 if all ok, 0 if end of
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch base32 data found, -1 if data is invalid.
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch Any whitespace characters are ignored.
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch This function may be called multiple times for parsing the same stream.
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch If src_pos is non-NULL, it's updated to first non-translated character in
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch src. */
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschint base32_decode(const void *src, size_t src_size,
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch size_t *src_pos_r, buffer_t *dest) ATTR_NULL(4);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschint base32hex_decode(const void *src, size_t src_size,
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch size_t *src_pos_r, buffer_t *dest) ATTR_NULL(4);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch/* Decode given string to a buffer allocated from data stack. */
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschbuffer_t *t_base32_decode_str(const char *str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschbuffer_t *t_base32hex_decode_str(const char *str);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch/* Returns TRUE if c is a valid base32 encoding character (excluding '=') */
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschbool base32_is_valid_char(char c);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Boschbool base32hex_is_valid_char(char c);
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch/* max. buffer size required for base32_encode()/base32hex_encode() */
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch#define MAX_BASE32_ENCODED_SIZE(size) \
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch ((size) / 5 * 8 + 8)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch/* max. buffer size required for base32_decode()/base32hex_decode() */
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch#define MAX_BASE32_DECODED_SIZE(size) \
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch ((size) / 8 * 5 + 5)
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch
75f7e423ea22446ed529d33cb660dadf680e13e0Stephan Bosch#endif