1 /* 2 * Copyrights 3 * 4 * Portions created or assigned to Cisco Systems, Inc. are 5 * Copyright (c) 2014-2016 Cisco Systems, Inc. All Rights Reserved. 6 */ 7 /** 8 * \file 9 * \brief 10 * Functions for encoding to and decoding from base64 and base64url. 11 * 12 * \b NOTE: When successful, the output of each function MUST be 13 * released by calling free(), even if the output is of 0 length. 14 */ 15 16 #ifndef CJOSE_BASE64_H 17 #define CJOSE_BASE64_H 18 19 #include <stdbool.h> 20 #include <stddef.h> 21 #include <stdint.h> 22 #include "cjose/error.h" 23 24 #ifdef __cplusplus 25 extern "C" { 26 #endif 27 28 /** 29 * Encodes the given octet string to Base64. 30 * 31 * \param input The octet string to encode. 32 * \param inlen The length of <tt>input</tt>. 33 * \param output The encoded text string. 34 * \param outlen The length of <tt>output</tt> 35 * (not including the terminating NULL). 36 * \param err [out] An optional error object which can be used to get additional 37 * information in the event of an error. 38 */ 39 bool cjose_base64_encode(const uint8_t *input, const size_t inlen, char **output, size_t *outlen, cjose_err *err); 40 41 /** 42 * Encodes the given octet string to URL-safe Base64. 43 * 44 * \param input The octet string to encode. 45 * \param inlen The length of <tt>input</tt>. 46 * \param output The encoded output string. 47 * \param outlen The length of <tt>output</tt> 48 * (not including the terminating NULL). 49 * \param err [out] An optional error object which can be used to get additional 50 * information in the event of an error. 51 */ 52 bool cjose_base64url_encode(const uint8_t *input, const size_t inlen, char **output, size_t *outlen, cjose_err *err); 53 54 /** 55 * Decodes the given string from Base64. 56 * 57 * \b NOTE: <tt>output</tt> is \b NOT NULL-terminated. 58 * 59 * \param input The text string to decode. 60 * \param inlen The length of <tt>input</tt>. 61 * \param output The decoded octet string. 62 * \param outlen The length of <tt>output</tt>. 63 * \param err [out] An optional error object which can be used to get additional 64 * information in the event of an error. 65 */ 66 bool cjose_base64_decode(const char *input, const size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err); 67 68 /** 69 * Decodes the given string from URL-Safe Base64. 70 * 71 * \b NOTE: <tt>output</tt> is \b NOT NULL-terminated. 72 * 73 * \param input The text string to decode. 74 * \param inlen The length of <tt>input</tt>. 75 * \param output The decoded octet string. 76 * \param outlen The length of <tt>output</tt>. 77 * \param err [out] An optional error object which can be used to get additional 78 * information in the event of an error. 79 */ 80 bool cjose_base64url_decode(const char *input, const size_t inlen, uint8_t **output, size_t *outlen, cjose_err *err); 81 82 #ifdef __cplusplus 83 } 84 #endif 85 86 #endif // CJOSE_BASE64_H 87