我想制作一个 Base64 编码器和解码器。我们得到了 3 个文件:
base64.c
:
#include "base64.h"
// ------- hidden header
#include <stdio.h>
#include <string.h>
#include <stdbool.h>
#include <limits.h>
#include <stdlib.h>
typedef bool bin;
// -------
// ------- hidden logic
unsigned short power(unsigned short base, unsigned short exponent)
{
if (exponent == 0)
return 1;
int out = base;
for (int i=0; i<exponent-1; i++)
{
out *= base;
}
return out;
}
// place bits pointer to first bit
void decToBin(unsigned short dec, bin *bits, short bits_count)
{
short i=bits_count;
while (dec != 0)
{
i--;
bits[i] = dec % 2;
dec /= 2;
}
}
// place bits pointer to first bit
unsigned short binToDec(bin *bits, short bits_count)
{
unsigned short dec=0;
for (short i=bits_count-1, j=0; i >= 0; i--, j++)
{
dec += bits[i] * power(2, j);
}
return dec;
}
unsigned short strfind(const char *str, char sub)
{
char *str_pos = strchr(str, sub);
return str_pos - str;
}
// -------
void b64_encode(const char *str, char *base64)
{
// ------- part 0: preparation
const size_t BYTES_COUNT = strlen(str),
BITS_COUNT = BYTES_COUNT * 8,
BASE_BYTES_COUNT = BITS_COUNT / 6;
const unsigned short BASE_BYTES_MOD_COUNT = 6 - BITS_COUNT % 6;
bin *bits = calloc(BITS_COUNT, sizeof(bin));
if (bits == NULL)
{
printf("Failed memory allocation\n");
return;
}
// ------- part 1: converting bytes to bits
for (size_t i=0, j=0; i<BYTES_COUNT; i++, j+=8)
{
decToBin(str[i], &bits[j], 8);
}
// ------- part 2: dividing bits to base-bytes & encoding to base64
size_t i=0, j=0;
for (; j<BASE_BYTES_COUNT; i+=6, j++)
{
const unsigned short dec = binToDec(&bits[i], 6);
base64[j] = BASE64_ALPHABET[dec];
}
if (BASE_BYTES_MOD_COUNT == 6)
{
free(bits);
return;
}
// ------- part X: encoding mod bits to base64
bin mod_base_byte[6] = {};
for (unsigned short k=0; k< BASE_BYTES_MOD_COUNT/2; k++, i++)
mod_base_byte[k] = bits[i];
unsigned short dec = binToDec(&mod_base_byte[0], 6);
base64[j] = BASE64_ALPHABET[dec];
j++;
for (unsigned short k=0; k< BASE_BYTES_MOD_COUNT/2; k++, j++)
{
base64[j] = '=';
}
free(bits);
}
void b64_decode(const char *base64, char *str)
{
// ------- part 0: preparation
const size_t BASE64_LEN = strlen(base64);
unsigned short BASE64_MOD_COUNT = 6;
if (base64[BASE64_LEN-1] == '=') BASE64_MOD_COUNT -= 2;
if (base64[BASE64_LEN-2] == '=') BASE64_MOD_COUNT -= 2;
if (BASE64_MOD_COUNT == 6) BASE64_MOD_COUNT = 0;
const size_t BASE64_LEN_NO_MOD = BASE64_LEN - (6-BASE64_MOD_COUNT)/2,
BITS_COUNT = BASE64_LEN_NO_MOD * 6 - (6-BASE64_MOD_COUNT),
BYTES_COUNT = BITS_COUNT / 8;
bin *bits = calloc(BITS_COUNT, sizeof(bin));
if (bits == NULL)
{
printf("Failed memory allocation\n");
return;
}
// ------- part 1: decoding base64 to bits
for (size_t i=0, j=0; i<BASE64_LEN_NO_MOD; i++, j+=6)
{
unsigned short dec = strfind(BASE64_ALPHABET, base64[i]);
if ((j > BITS_COUNT) && (BASE64_MOD_COUNT == 2))
{
decToBin(
dec & 0b110000,
&bits[j-4],
2
);
continue;
}
else if ((j > BITS_COUNT) && (BASE64_MOD_COUNT == 4))
{
decToBin(
dec & 0b111100,
&bits[j-2],
4
);
continue;
}
decToBin(dec, &bits[j], 6);
}
// ------- part 2: dividing bits to bytes
for (size_t i=0, j=0; i<BYTES_COUNT; i++, j+=8)
{
str[i] = (char) binToDec(&bits[j], 8);
}
free(bits);
}
base64.h
:
#ifndef BASE64_H_
#define BASE64_H_
#define BASE64_ALPHABET "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
void b64_encode(const char *str, char *base64);
void b64_decode(const char *base64, char *str);
#endif
test.c
:
#include <stdio.h>
#include "base64.h"
int main()
{
char str[14] = "Hello, World!";
char base64[100] = "";
char str_out[14] = "";
b64_encode(str, base64);
printf("%s\n", base64);
b64_decode(base64, str_out);
printf("%s\n", str_out);
char str2[14] = "Man";
char base64_2[100] = "";
char str2_out[14] = "";
b64_encode(str2, base64_2);
printf("%s\n", base64_2);
b64_decode(base64_2, str2_out);
printf("%s\n", str2_out);
return 0;
}
运行时显示两个printf
,并出现两行错误:
SGVsbG8sIFdvcmxkIQ==
Hello, World!
base64: malloc.c:2617: sysmalloc: Assertion `(old_top == initial_top (av) && old_size == 0) || ((unsigned long) (old_size) >= MINSIZE && prev_inuse (old_top) && ((unsigned long) old_end & (pagesize - 1)) == 0)' failed.
Aborted (core dumped)
通过其他打印,我发现问题出在该行中的b64_encode :
bin *bits = calloc(BITS_COUNT, sizeof(bin));
有趣的是,第一个函数调用工作正常,但第二个函数调用创建了一个错误而不返回 NULL(它只是创建了一个严重错误)。
我试图了解发生了什么,但无法通过调试来做到这一点。
谁知道问题出在哪里?
添加。信息
- 在Linux上编程
- 使用以下命令编译并启动:
# ↓ Получилось без предупреждений
gcc -Wall base64.c test.c -o base64
./base64