提交 100d4e2e 编写于 作者: W WangFengTu 提交者: lifeng68

refactor base64 interface

It's more simple to allocate memory in function but not allocate
by user.
Signed-off-by: NWangFengTu <wangfengtu@huawei.com>
上级 4e4652e0
......@@ -43,17 +43,7 @@ static int decode_auth(const char *auth, char **username, char **password)
return -1;
}
decoded_len = util_base64_decode_len(auth, strlen(auth));
if (decoded_len < 0) {
return -1;
}
decoded = util_common_calloc_s(decoded_len + 1);
if (decoded == NULL) {
ERROR("out of memory");
return -1;
}
nret = util_base64_decode(auth, strlen(auth), decoded, decoded_len);
nret = util_base64_decode(auth, strlen(auth), &decoded, &decoded_len);
if (nret < 0) {
ERROR("decode auth from base64 failed");
ret = -1;
......
......@@ -78,17 +78,7 @@ static int decode_auth_aes(char *encoded, char **username, char **password)
return -1;
}
decoded_len = util_base64_decode_len(encoded, strlen(encoded));
if (decoded_len < 0) {
return -1;
}
decoded = util_common_calloc_s(decoded_len);
if (decoded == NULL) {
ERROR("out of memory");
return -1;
}
nret = util_base64_decode(encoded, strlen(encoded), decoded, decoded_len);
nret = util_base64_decode(encoded, strlen(encoded), &decoded, &decoded_len);
if (nret < 0) {
ERROR("decode auth from base64 failed");
ret = -1;
......@@ -108,21 +98,14 @@ static int decode_auth_aes(char *encoded, char **username, char **password)
goto out;
}
// auth is encoded by base64.
decoded_len = util_base64_decode_len(auth, strlen(auth));
if (decoded_len < 0) {
ERROR("calc base64 decode length for auth failed");
ret = -1;
goto out;
}
nret = util_base64_decode(auth, strlen(auth), decoded, decoded_len);
free(decoded);
decoded = NULL;
nret = util_base64_decode(auth, strlen(auth), &decoded, &decoded_len);
if (nret < 0) {
ERROR("decode auth from base64 failed");
ret = -1;
goto out;
}
decoded[decoded_len] = 0;
auth_parts = util_string_split((char *)decoded, ':');
if (auth_parts == NULL || util_array_len((const char **)auth_parts) != 2) {
......@@ -152,7 +135,6 @@ static char *encode_auth_aes(char *username, char *password)
int ret = 0;
int nret = 0;
int sret = 0;
size_t plain_text_base64_len = 0;
size_t plain_text_base64_encode_len = 0;
char *plain_text_base64 = NULL;
char plain_text[PATH_MAX] = { 0 };
......@@ -160,7 +142,6 @@ static char *encode_auth_aes(char *username, char *password)
size_t aes_buf_len = 0;
size_t aes_len = 0;
char *aes_base64 = NULL;
size_t aes_base64_len = 0;
sret = snprintf(plain_text, sizeof(plain_text), "%s:%s", username, password);
if (sret < 0 || (size_t)sret >= sizeof(plain_text)) {
......@@ -169,16 +150,7 @@ static char *encode_auth_aes(char *username, char *password)
goto out;
}
plain_text_base64_len = util_base64_encode_len(strlen(plain_text));
plain_text_base64 = util_common_calloc_s(plain_text_base64_len);
if (plain_text_base64 == NULL) {
ERROR("out of memory");
ret = -1;
goto out;
}
nret = util_base64_encode((unsigned char *)plain_text, strlen(plain_text), plain_text_base64,
plain_text_base64_len);
nret = util_base64_encode((unsigned char *)plain_text, strlen(plain_text), &plain_text_base64);
if (nret < 0) {
ERROR("encode plain text to auth failed");
ret = -1;
......@@ -186,7 +158,7 @@ static char *encode_auth_aes(char *username, char *password)
}
// Do not encode char '\0'
plain_text_base64_encode_len = plain_text_base64_len - 1;
plain_text_base64_encode_len = strlen(plain_text_base64);
aes_buf_len = util_aes_encode_buf_len(plain_text_base64_encode_len);
aes_len = AES_256_CFB_IV_LEN + plain_text_base64_encode_len;
aes = util_common_calloc_s(aes_buf_len);
......@@ -202,15 +174,7 @@ static char *encode_auth_aes(char *username, char *password)
goto out;
}
aes_base64_len = util_base64_encode_len(aes_len);
aes_base64 = util_common_calloc_s(aes_base64_len + 1);
if (aes_base64 == NULL) {
ERROR("out of memory");
ret = -1;
goto out;
}
nret = util_base64_encode(aes, aes_len, aes_base64, aes_base64_len);
nret = util_base64_encode(aes, aes_len, &aes_base64);
if (nret < 0) {
ERROR("encode plain text to auth failed");
ret = -1;
......
......@@ -134,7 +134,6 @@ static char *encode_auth(const char *username, const char *password)
char *auth = NULL;
size_t auth_len = 0;
char *auth_base64 = NULL;
size_t auth_base64_len = 0;
int ret = 0;
int nret = 0;
......@@ -157,15 +156,7 @@ static char *encode_auth(const char *username, const char *password)
goto out;
}
auth_base64_len = util_base64_encode_len(strlen(auth));
auth_base64 = util_common_calloc_s(auth_base64_len);
if (auth_base64 == NULL) {
ret = -1;
ERROR("Failed to sprintf username and password");
goto out;
}
nret = util_base64_encode((unsigned char *)auth, strlen(auth), auth_base64, auth_base64_len);
nret = util_base64_encode((unsigned char *)auth, strlen(auth), &auth_base64);
if (nret < 0) {
ret = -1;
ERROR("Encode auth to base64 failed");
......
......@@ -46,7 +46,6 @@
#include "constants.h"
#define PAYLOAD_CRC_LEN 12
#define FILE_CRC_LEN 8
struct io_read_wrapper;
......@@ -1676,20 +1675,21 @@ static uint64_t payload_to_crc(char *payload)
int ret = 0;
int i = 0;
uint64_t crc = 0;
uint8_t crc_sums[FILE_CRC_LEN] = {0};
uint8_t *crc_sums = NULL;
size_t crc_sums_len = 0;
ret = util_base64_decode(payload, PAYLOAD_CRC_LEN, crc_sums, FILE_CRC_LEN);
ret = util_base64_decode(payload, strlen(payload), &crc_sums, &crc_sums_len);
if (ret < 0) {
ERROR("decode tar split payload from base64 failed, payload %s", payload);
return -1;
}
for (i = 0; i < FILE_CRC_LEN; i++) {
for (i = 0; i < crc_sums_len; i++) {
crc |= crc_sums[i];
if (i == FILE_CRC_LEN - 1) {
if (i == crc_sums_len - 1) {
break;
}
crc <<= FILE_CRC_LEN;
crc <<= crc_sums_len;
}
return crc;
......
......@@ -286,7 +286,6 @@ char *make_big_data_base_name(const char *key)
int ret = 0;
int nret = 0;
char *b64_encode_name = NULL;
size_t b64_encode_name_len = 0;
char *base_name = NULL;
size_t name_size;
......@@ -294,15 +293,7 @@ char *make_big_data_base_name(const char *key)
return util_strdup_s(key);
}
b64_encode_name_len = util_base64_encode_len(strlen(key));
b64_encode_name = util_common_calloc_s(b64_encode_name_len + 1);
if (b64_encode_name == NULL) {
ERROR("Out of memory");
ret = -1;
goto out;
}
nret = util_base64_encode((unsigned char *)key, strlen(key), b64_encode_name, b64_encode_name_len);
nret = util_base64_encode((unsigned char *)key, strlen(key), &b64_encode_name);
if (nret < 0) {
ret = -1;
ERROR("Encode auth to base64 failed");
......
......@@ -23,20 +23,21 @@
#include "isula_libutils/log.h"
#include "utils_base64.h"
#include "openssl/bio.h"
#include "utils.h"
size_t util_base64_encode(unsigned char *bytes, size_t len, char *out, size_t out_len)
int util_base64_encode(unsigned char *bytes, size_t len, char **out)
{
BIO *base64 = NULL;
BIO *io = NULL;
size_t result_len = 0;
int ret = 0;
int bio_ret = 0;
BUF_MEM *pmem = NULL;
size_t i = 0;
char *out_put = NULL;
size_t count = 0;
if (bytes == NULL || len == 0 || out == NULL || out_len < util_base64_encode_len(len)) {
ERROR("Invalid param for encoding base64, input length %zu, out length %zu", len, out_len);
if (bytes == NULL || len == 0 || out == NULL) {
ERROR("Invalid param for encoding base64");
return -1;
}
......@@ -69,18 +70,19 @@ size_t util_base64_encode(unsigned char *bytes, size_t len, char *out, size_t ou
}
(void)BIO_get_mem_ptr(io, &pmem);
out_put = util_common_calloc_s(pmem->length + 1);
if (out_put == NULL) {
ERROR("out of memory");
ret = -1;
goto out;
}
// BIO_write append '\n' if every 76 chars have be output, so we need to strip them.
for (i = 0; i < pmem->length; i++) {
if (pmem->data[i] == '\n') {
continue;
}
if (count + 1 == out_len) {
ERROR("result length larger than output length, result length %zu, input length %zu, output length %zu",
pmem->length, len, out_len);
ret = -1;
goto out;
}
out[count] = pmem->data[i];
out_put[count] = pmem->data[i];
count++;
}
......@@ -90,8 +92,8 @@ size_t util_base64_encode(unsigned char *bytes, size_t len, char *out, size_t ou
goto out;
}
out[count] = 0;
result_len = count + 1;
out_put[count] = 0;
*out = out_put;
out:
......@@ -101,19 +103,11 @@ out:
}
if (ret != 0) {
return -1;
} else {
return result_len;
free(out_put);
out_put = NULL;
}
}
size_t util_base64_encode_len(size_t len)
{
if (len % 3 == 0) {
return len / 3 * 4 + 1;
} else {
return (len / 3 + 1) * 4 + 1;
}
return ret;
}
size_t util_base64_decode_len(const char *input, size_t len)
......@@ -135,17 +129,17 @@ size_t util_base64_decode_len(const char *input, size_t len)
return (strlen(input) / 4 * 3) - padding_count;
}
size_t util_base64_decode(const char *input, size_t len, unsigned char *out, size_t out_len)
int util_base64_decode(const char *input, size_t len, unsigned char **out, size_t *out_len)
{
BIO *base64 = NULL;
BIO *io = NULL;
int ret = 0;
size_t result_len = util_base64_decode_len(input, len);
size_t size = 0;
int bio_ret = 0;
unsigned char *out_put = NULL;
size_t out_put_len = 0;
if (input == NULL || result_len < 0 || out == 0 || result_len > out_len) {
ERROR("Invalid param for base64 decode, input length %zu, result length %zu, output length %zu", len,
result_len, out_len);
if (input == NULL || len % 4 != 0 || out == NULL || out_len == NULL) {
ERROR("Invalid param for base64 decode");
return -1;
}
......@@ -161,12 +155,21 @@ size_t util_base64_decode(const char *input, size_t len, unsigned char *out, siz
io = BIO_new_mem_buf(input, len);
io = BIO_push(base64, io);
size = BIO_read(io, out, out_len);
if (size != result_len) {
ERROR("base64 decode failed, actual length not match calculated length, expected %zu, got %zu", result_len,
size);
out_put_len = util_base64_decode_len(input, len);
out_put = util_common_calloc_s(out_put_len + 1); // '+1' for '\0'
if (out_put == NULL) {
ERROR("out of memory");
ret = -1;
goto out;
}
bio_ret = BIO_read(io, out_put, out_put_len);
if (bio_ret <= 0) {
ERROR("base64 decode failed, result is %d", bio_ret);
}
*out = out_put;
*out_len = out_put_len;
out:
if (io != NULL) {
BIO_free_all(io);
......@@ -174,8 +177,9 @@ out:
}
if (ret != 0) {
return -1;
} else {
return result_len;
free(out_put);
out_put = NULL;
}
return ret;
}
......@@ -25,11 +25,11 @@
extern "C" {
#endif
// note: the output length must include the '\0' and the return size is include the '\0'.
size_t util_base64_encode(unsigned char *bytes, size_t len, char *out, size_t out_len);
size_t util_base64_encode_len(size_t len);
size_t util_base64_decode(const char *input, size_t len, unsigned char *out, size_t out_len);
size_t util_base64_decode_len(const char *input, size_t len);
int util_base64_encode(unsigned char *bytes, size_t len, char **out);
// note: The result out put will be *out_len + 1, and it's filled with '\0', so if the decoded
// data is a string, it's safe to use it as a string.
int util_base64_decode(const char *input, size_t len, unsigned char **out, size_t *out_len);
#ifdef __cplusplus
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册