libfastcommon/src/base64.h

142 lines
4.3 KiB
C

/*
* Copyright (c) 2020 YuQing <384681@qq.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the Lesser GNU General Public License, version 3
* or later ("LGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the Lesser GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
//base64.h
#ifndef _BASE64_H
#define _BASE64_H
#include "common_define.h"
struct base64_context
{
char line_separator[16];
int line_sep_len;
/**
* max chars per line, excluding line_separator. A multiple of 4.
*/
int line_length;
/**
* letter of the alphabet used to encode binary values 0..63
*/
unsigned char valueToChar[64];
/**
* binary value encoded by a given letter of the alphabet 0..63
*/
int charToValue[256];
int pad_ch;
};
#ifdef __cplusplus
extern "C" {
#endif
/** use stardand base64 charset
*/
#define base64_init(context, nLineLength) \
base64_init_ex(context, nLineLength, '+', '/', '=')
/** stardand base64 encode
*/
#define base64_encode(context, src, nSrcLen, dest, dest_len) \
base64_encode_ex(context, src, nSrcLen, dest, dest_len, true)
/** base64 init function, before use base64 function, you should call
* this function
* parameters:
* context: the base64 context
* nLineLength: length of a line, 0 for never add line seperator
* chPlus: specify a char for base64 char plus (+)
* chSplash: specify a char for base64 char splash (/)
* chPad: specify a char for base64 padding char =
* return: none
*/
void base64_init_ex(struct base64_context *context, const int nLineLength, \
const unsigned char chPlus, const unsigned char chSplash, \
const unsigned char chPad);
/** calculate the encoded length of the source buffer
* parameters:
* context: the base64 context
* nSrcLen: the source buffer length
* return: the encoded length of the source buffer
*/
int base64_get_encode_length(struct base64_context *context, const int nSrcLen);
/** base64 encode buffer
* parameters:
* context: the base64 context
* src: the source buffer
* nSrcLen: the source buffer length
* dest: the dest buffer
* dest_len: return dest buffer length
* bPad: if padding
* return: the encoded buffer
*/
char *base64_encode_ex(struct base64_context *context, const char *src, \
const int nSrcLen, char *dest, int *dest_len, const bool bPad);
/** base64 decode buffer, work only with padding source string
* parameters:
* context: the base64 context
* src: the source buffer with padding
* nSrcLen: the source buffer length
* dest: the dest buffer
* dest_len: return dest buffer length
* return: the decoded buffer
*/
char *base64_decode(struct base64_context *context, const char *src, \
const int nSrcLen, char *dest, int *dest_len);
/** base64 decode buffer, can work with no padding source string
* parameters:
* context: the base64 context
* src: the source buffer with padding or no padding
* nSrcLen: the source buffer length
* dest: the dest buffer
* dest_len: return dest buffer length
* return: the decoded buffer
*/
char *base64_decode_auto(struct base64_context *context, const char *src, \
const int nSrcLen, char *dest, int *dest_len);
/** set line separator string, such as \n or \r\n
* parameters:
* context: the base64 context
* pLineSeparator: the line separator string
* return: none
*/
void base64_set_line_separator(struct base64_context *context, \
const char *pLineSeparator);
/** set line length, 0 for never add line separators
* parameters:
* context: the base64 context
* length: the line length
* return: none
*/
void base64_set_line_length(struct base64_context *context, const int length);
#ifdef __cplusplus
}
#endif
#endif