lnk commit front code
This commit is contained in:
189
cfg_parse/base64.cpp
Normal file
189
cfg_parse/base64.cpp
Normal file
@@ -0,0 +1,189 @@
|
||||
/**
|
||||
* @file: $RCSfile: base64.cpp,v $
|
||||
* @brief: base64 include
|
||||
*
|
||||
* @version: $Revision: 1.00 $
|
||||
* @date: $Date: 2024/09/24 18:34:00 $
|
||||
* @author: $Author: caizhouyu $
|
||||
* @state: $State: Exp $
|
||||
*
|
||||
* @latest: $Id: base64.cpp,v 1.00 2023/10/24 18:34:00 caizhouyu Exp $
|
||||
*
|
||||
*/
|
||||
|
||||
using namespace std;
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include <stdlib.h>
|
||||
#include <string>
|
||||
|
||||
#include "../mms/db_interface.h"
|
||||
#include "../json/cjson.h"//WW 2023-08-27<32><37><EFBFBD><EFBFBD>json<6F><6E><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
#include "../include/curl/curl.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif /* __cplusplus */
|
||||
|
||||
|
||||
// Base64 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
const char base64_chars[] =
|
||||
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||
"abcdefghijklmnopqrstuvwxyz"
|
||||
"0123456789+/";
|
||||
|
||||
//base64 <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
static const unsigned char base64_decode_table[] = {
|
||||
//ÿ<><C3BF>16<31><36>
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //1 - 16
|
||||
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //17 - 32
|
||||
0,0,0,0,0,0,0,0,0,0,0,62,0,0,0,63, //33 - 48
|
||||
52,53,54,55,56,57,58,59,60,61,0,0,0,0,0,0, //49 - 64
|
||||
0,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14, //65 - 80
|
||||
15,16,17,18,19,20,21,22,23,24,25,0,0,0,0,0, //81 - 96
|
||||
0,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40, //97 - 112
|
||||
41,42,43,44,45,46,47,48,49,50,51,0,0,0,0,0 //113 - 128
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief base64_decode base64<36><34><EFBFBD><EFBFBD>
|
||||
* @param indata <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param inlen <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>С
|
||||
* @param outdata <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
* @param outlen <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݴ<EFBFBD>С
|
||||
* @return int 0<><30><EFBFBD>ɹ<EFBFBD> -1<><31><EFBFBD><EFBFBD>Ч<EFBFBD><D0A7><EFBFBD><EFBFBD>
|
||||
* ע<>⣺<EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĴ<DDB5>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD><34><EFBFBD><EFBFBD><EFBFBD><EFBFBD>4<EFBFBD>ı<EFBFBD><C4B1><EFBFBD>
|
||||
*/
|
||||
int base64_decode(const char* indata, int inlen, char* outdata, long* outlen)
|
||||
{
|
||||
if (indata == NULL || inlen <= 0 || (outdata == NULL && outlen == NULL)) {
|
||||
return -1;
|
||||
}
|
||||
if (inlen < 4 || inlen % 4 != 0) { //<2F><>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3>Ȳ<EFBFBD><C8B2><EFBFBD>4<EFBFBD>ı<EFBFBD><C4B1><EFBFBD> //inlen < 4 ||
|
||||
return -1;
|
||||
}
|
||||
|
||||
int i, j;
|
||||
|
||||
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
int len = inlen / 4 * 3;
|
||||
if (indata[inlen - 1] == '=') {
|
||||
len--;
|
||||
}
|
||||
if (indata[inlen - 2] == '=') {
|
||||
len--;
|
||||
}
|
||||
|
||||
if (outdata != NULL) {
|
||||
for (i = 0, j = 0; i < inlen; i += 4, j += 3) {
|
||||
outdata[j] = (base64_decode_table[(unsigned char)indata[i]] << 2) | (base64_decode_table[(unsigned char)indata[i + 1]] >> 4);
|
||||
outdata[j + 1] = (base64_decode_table[(unsigned char)indata[i + 1]] << 4) | (base64_decode_table[(unsigned char)indata[i + 2]] >> 2);
|
||||
outdata[j + 2] = (base64_decode_table[(unsigned char)indata[i + 2]] << 6) | (base64_decode_table[(unsigned char)indata[i + 3]]);
|
||||
}
|
||||
}
|
||||
if (outlen != NULL) {
|
||||
*outlen = len;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Base64 <20><><EFBFBD>뺯<EFBFBD><EBBAAF>
|
||||
char* base64_encode_char(const unsigned char* data, size_t input_length, size_t* output_length) {
|
||||
*output_length = 4 * ((input_length + 2) / 3); // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȼ<EFBFBD><C8BC><EFBFBD>
|
||||
char* encoded_data = (char*)malloc(*output_length + 1); // <20><><EFBFBD><EFBFBD><EFBFBD>ڴ棬+1 Ϊ '\0'
|
||||
if (encoded_data == NULL) return NULL;
|
||||
|
||||
for (int i = 0, j = 0; i < input_length;) {
|
||||
uint32_t octet_a = i < input_length ? data[i++] : 0;
|
||||
uint32_t octet_b = i < input_length ? data[i++] : 0;
|
||||
uint32_t octet_c = i < input_length ? data[i++] : 0;
|
||||
|
||||
uint32_t triple = (octet_a << 16) | (octet_b << 8) | octet_c;
|
||||
|
||||
encoded_data[j++] = base64_chars[(triple >> 18) & 0x3F];
|
||||
encoded_data[j++] = base64_chars[(triple >> 12) & 0x3F];
|
||||
encoded_data[j++] = (i * 2 / 3) < *output_length ? base64_chars[(triple >> 6) & 0x3F] : '=';
|
||||
encoded_data[j++] = (i * 2 + 1 / 3) < *output_length ? base64_chars[triple & 0x3F] : '=';
|
||||
}
|
||||
|
||||
encoded_data[*output_length] = '\0'; // <20><><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
return encoded_data;
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// <20>ж<EFBFBD><D0B6>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD>Ƿ<EFBFBD>Ϊpower{}<7D><>ʽ
|
||||
/// </summary>
|
||||
/// <param name="str"><3E><><EFBFBD><EFBFBD>ȡ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD></param>
|
||||
/// <param name="output"><3E><><EFBFBD><EFBFBD><EFBFBD>ֶ<EFBFBD></param>
|
||||
/// <param name="output_size"><3E>ֶγ<D6B6><CEB3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD></param>
|
||||
/// <param name="extracted_length"><3E><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD></param>
|
||||
/// <returns></returns>
|
||||
bool extract_if_power(const char* str, char* output, size_t output_size, size_t* extracted_length) {
|
||||
const char* prefix = "power{";
|
||||
size_t prefix_length = strlen(prefix);
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>ǰ
|
||||
if (strncmp(str, prefix, prefix_length) != 0) {
|
||||
return false; // ǰ<C7B0><D7BA>ƥ<EFBFBD><C6A5>
|
||||
}
|
||||
|
||||
// <20><><EFBFBD>ұպϵĻ<CFB5><C4BB><EFBFBD><EFBFBD><EFBFBD>
|
||||
const char* close_brace = strchr(str + prefix_length, '}');
|
||||
if (close_brace == NULL) {
|
||||
return false; // û<><C3BB><EFBFBD>ҵ<EFBFBD><D2B5>պϵĻ<CFB5><C4BB><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||||
size_t content_length = close_brace - (str + prefix_length);
|
||||
if (content_length >= output_size) {
|
||||
return false; // <20><><EFBFBD><EFBFBD>̫<EFBFBD><CCAB><EFBFBD><EFBFBD><EFBFBD><EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵ<EFBFBD><DDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
strncpy(output, str + prefix_length, content_length);
|
||||
output[content_length] = '\0'; // <20><><EFBFBD>ӿ<EFBFBD><D3BF><EFBFBD>ֹ<EFBFBD><D6B9>
|
||||
printf("text: %s,length:%d\n", output, content_length); // ע<>⣺<EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫȷ<D2AA><C8B7><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>null<6C><6C>ֹ<EFBFBD><D6B9><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݳ<EFBFBD><DDB3><EFBFBD>
|
||||
*extracted_length = content_length;
|
||||
|
||||
return true; // <20><>ȡ<EFBFBD>ɹ<EFBFBD>
|
||||
}
|
||||
|
||||
//int testbase64(char ** decoded_text) {
|
||||
// unsigned char text[] = "power{SGVsbG8sIFdvcmxkIWV3ZA==}";
|
||||
// size_t encoded_length, decoded_length;
|
||||
|
||||
// char encoded_text[100];
|
||||
|
||||
// if (extract_if_power((char*)text, encoded_text, sizeof(encoded_text), &encoded_length)) {
|
||||
// // <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
// long decodedLen = strlen(encoded_text) * 3 / 4;
|
||||
// * decoded_text = (char*)malloc(decodedLen + 1);
|
||||
// // <20><><EFBFBD><EFBFBD>Base64<36><34><EFBFBD><EFBFBD>
|
||||
// int success = base64_decode(encoded_text, strlen(encoded_text), *decoded_text, &decodedLen);
|
||||
// if (decoded_text) {
|
||||
// printf("Decoded: %s\n", (char*)decoded_text); // ע<>⣺<EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫȷ<D2AA><C8B7><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>null<6C><6C>ֹ<EFBFBD><D6B9><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
// free(decoded_text); // ע<>⣺<EFBFBD><E2A3BA><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD>ϲ<EFBFBD>Ӧ<EFBFBD><D3A6>ʹ<EFBFBD><CAB9>decoded_text<78><74>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD>Ϊ<EFBFBD><CEAA><EFBFBD><EFBFBD>unsigned char*
|
||||
// // <20><>ʵ<EFBFBD><CAB5>Ӧ<EFBFBD><D3A6><EFBFBD>У<EFBFBD><D0A3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD>ת<EFBFBD><D7AA>Ϊchar*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>null<6C><6C>ֹ<EFBFBD><D6B9>
|
||||
// }
|
||||
// }
|
||||
// else {
|
||||
// printf(" no Decoded: %s\n", text); // ע<>⣺<EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ҫȷ<D2AA><C8B7><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD>null<6C><6C>ֹ<EFBFBD><D6B9><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
// }
|
||||
//
|
||||
|
||||
|
||||
// // ע<>⣺<EFBFBD><E2A3BA><EFBFBD><EFBFBD><EFBFBD><EFBFBD>decoded_text<78><74>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD>Dz<EFBFBD>ȷ<D7BC>ģ<EFBFBD><C4A3><EFBFBD>Ϊdecoded_text<78><74>unsigned char*<2A>ҿ<EFBFBD><D2BF>ܲ<EFBFBD><DCB2><EFBFBD>null<6C><6C>ֹ<EFBFBD><D6B9><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
|
||||
// // <20><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ǽ<EFBFBD>decoded_textת<74><D7AA>Ϊ<EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ı<EFBFBD><C4B1><EFBFBD><EFBFBD>ݣ<EFBFBD><DDA3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD>
|
||||
|
||||
// return 0;
|
||||
//}
|
||||
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
Reference in New Issue
Block a user