1 /* 2 * These are conversion functions for MIME Base64 (as opposed to MIME in 3 * base6.[ch] and crypt(3) encoding found in common.[ch]). This code will 4 * convert between many base64 types, raw memory, hex, etc. 5 * functions added to convert between the 3 types (JimF) 6 * 7 * Coded Fall 2014 by Jim Fougeron. Code placed in public domain. 8 * 9 * Redistribution and use in source and binary forms, with or without 10 * modification, are permitted, as long an unmodified copy of this 11 * license/disclaimer accompanies the source. 12 * 13 * There's ABSOLUTELY NO WARRANTY, express or implied. 14 */ 15 16 #ifndef _BASE64_CONVERT_H 17 #define _BASE64_CONVERT_H 18 19 /********************************************************************* 20 * Length macros which convert from one system to the other. 21 * RAW_TO_B64_LEN(x) returns 'exact' base64 string length needed. NOTE, some 22 * base64 will padd to an even 4 characters. The length from this macro 23 * DOES NOT include padding values. 24 * B64_TO_RAW_LEN(x) returns raw string length needed for the base-64 string 25 * that is NOT padded in any way (i.e. needs to be same value as returned 26 * from RAW_TO_B64_LEN(x) ) 27 *********************************************************************/ 28 #define RAW_TO_B64_LEN(a) (((a)*4+2)/3) 29 #define B64_TO_RAW_LEN(a) (((a)*3+1)/4) 30 31 typedef enum { 32 e_b64_unk=-1, /* invalid type seen from command line usage */ 33 e_b64_raw, /* raw memory */ 34 e_b64_hex, /* hex */ 35 e_b64_mime, /* mime */ 36 e_b64_crypt, /* crypt encoding */ 37 e_b64_cryptBS, /* crypt encoding, network order (WPA, cisco9, etc) */ 38 } b64_convert_type; 39 40 /* 41 * Base-64 modification flags 42 */ 43 #define flg_Base64_NO_FLAGS 0x00 44 #define flg_Base64_HEX_UPCASE 0x01 45 #define flg_Base64_HEX_LOCASE 0x02 46 #define flg_Base64_MIME_TRAIL_EQ 0x04 47 #define flg_Base64_CRYPT_TRAIL_DOTS 0x08 48 #define flg_Base64_MIME_PLUS_TO_DOT 0x10 49 // mime alphabet, BUT last 2 chars are -_ (instead of +/ ) 50 #define flg_Base64_MIME_DASH_UNDER 0x20 51 #define flg_Base64_MIME_TRAIL_EQ_CNT 0x40 52 // #define flg_Base64_RET_NEG_IF_NOT_PURE 0x80 // Depricated, invalid flag! 53 #define flg_Base64_DONOT_NULL_TERMINATE 0x100 54 55 /* 56 * return will be number of bytes converted and placed into *to (can be less 57 * than to_len). 58 * 59 * the length of the to, MUST be at least max len + 1 (for the null byte), for 60 * types, e_b64_hex, e_b64_mime, e_b64_crypt and e_b64_cryptBS. Each of those 61 * 'to' types will get a null byte added. The exception is if flag 62 * flg_Base64_DONOT_NULL_TERMINATE is used. In that case, the code will NOT 63 * add a null terminator. The output type e_b64_raw NEVER null terminates. 64 * 65 * NOTE, the buffer should be max len + 4 for any base-64 conversion. 66 */ 67 size_t base64_convert(const void *from, // input data. 68 b64_convert_type from_t, // b64_convert_type of input data 69 size_t from_len, // length of input to use 70 void *to, // output buf (large enough + x) 71 b64_convert_type to_t, // b64_convert_type to convert to 72 size_t to_len, 73 unsigned flags, 74 int *err); // optional pointer to int, set on error. NULL is fine, but no error return given. 75 /* same function as base64_convert(), but returns the *to as a char* */ 76 char *base64_convert_cp(const void *from, 77 b64_convert_type from_t, 78 size_t from_len, 79 void *to, 80 b64_convert_type to_t, 81 size_t to_len, 82 unsigned flags, 83 int *err); 84 size_t base64_valid_length(const char *from, b64_convert_type from_t, unsigned flags, int *err); 85 void base64_convert_error_exit(int err); 86 char *base64_convert_error(int err); /* allocates buffer, caller must free */ 87 88 #endif // _BASE64_CONVERT_H 89