1 /* 2 * Copyright 2014-2022 The GmSSL Project. All Rights Reserved. 3 * 4 * Licensed under the Apache License, Version 2.0 (the License); you may 5 * not use this file except in compliance with the License. 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 */ 9 10 11 12 #ifndef GMSSL_BASE64_H 13 #define GMSSL_BASE64_H 14 15 #include <stdint.h> 16 #include <stdlib.h> 17 18 #ifdef __cplusplus 19 extern "C" { 20 #endif 21 22 /* 23 BASE64 Public API 24 25 BASE64_CTX 26 base64_encode_init 27 base64_encode_update 28 base64_encode_finish 29 base64_decode_init 30 base64_decode_update 31 base64_decode_finish 32 33 */ 34 35 36 typedef struct { 37 /* number saved in a partial encode/decode */ 38 int num; 39 /* 40 * The length is either the output line length (in input bytes) or the 41 * shortest input line length that is ok. Once decoding begins, the 42 * length is adjusted up each time a longer line is decoded 43 */ 44 int length; 45 /* data to encode */ 46 unsigned char enc_data[80]; 47 /* number read on current line */ 48 int line_num; 49 int expect_nl; 50 } BASE64_CTX; 51 52 # define BASE64_ENCODE_LENGTH(l) (((l+2)/3*4)+(l/48+1)*2+80) 53 # define BASE64_DECODE_LENGTH(l) ((l+3)/4*3+80) 54 55 56 void base64_encode_init(BASE64_CTX *ctx); 57 int base64_encode_update(BASE64_CTX *ctx, const uint8_t *in, int inlen, uint8_t *out, int *outlen); 58 void base64_encode_finish(BASE64_CTX *ctx, uint8_t *out, int *outlen); 59 60 void base64_decode_init(BASE64_CTX *ctx); 61 int base64_decode_update(BASE64_CTX *ctx, const uint8_t *in, int inlen, uint8_t *out, int *outlen); 62 int base64_decode_finish(BASE64_CTX *ctx, uint8_t *out, int *outlen); 63 64 65 int base64_encode_block(unsigned char *t, const unsigned char *f, int dlen); 66 int base64_decode_block(unsigned char *t, const unsigned char *f, int n); 67 68 69 #ifdef __cplusplus 70 } 71 #endif 72 #endif 73