尝试生成base64编码值时出现Seg错误-C

尝试生成base64编码值时出现Seg错误-C,c,debugging,segmentation-fault,C,Debugging,Segmentation Fault,我试图通过使用上面提到的实现,为字符串值“simple”获取base64编码值 这里的错误是什么?我已经很长时间没有用C编写代码了,所以如果我在这里遗漏了一些明显的东西,我很抱歉 size_t *inLen = (size_t) strlen(ibuf); 这看起来不对 inLen是指向size\u t的指针,但您正在为其分配一个size\u t值 此外,本电话: base64_encode(ibuf, inLen, outLen); base64\u encode需要一个size\u t

我试图通过使用上面提到的实现,为字符串值“simple”获取base64编码值

这里的错误是什么?我已经很长时间没有用C编写代码了,所以如果我在这里遗漏了一些明显的东西,我很抱歉

 size_t *inLen = (size_t) strlen(ibuf);
这看起来不对

inLen
是指向
size\u t
的指针,但您正在为其分配一个
size\u t

此外,本电话:

base64_encode(ibuf, inLen, outLen);
base64\u encode
需要一个
size\u t
作为其第二个参数,但您正在传递一个
size\u t*

最后,
outLen
未初始化,但您正在将其值传递给
base64\u encode

size_t *outLen;
这看起来不对

inLen
是指向
size\u t
的指针,但您正在为其分配一个
size\u t

此外,本电话:

base64_encode(ibuf, inLen, outLen);
base64\u encode
需要一个
size\u t
作为其第二个参数,但您正在传递一个
size\u t*

最后,
outLen
未初始化,但您正在将其值传递给
base64\u encode

size_t *outLen;
您将未初始化的指针传递到
encodeVal
,并将其写入指向的位置:

*output_length = (size_t) (4.0 * ceil((double) input_length / 3.0));
您应该传递
size\t
变量的地址,或者至少传递指向已分配内存的初始化指针

而且

很可能是错误的,应该是
size\t inLen=strlen(ibuf)

可能,而不是

char *encoded_data = malloc(*output_length);
您应该再malloc一次,并以0结尾编码的字符串(但这取决于使用情况)

您将未初始化的指针传递到
encodeVal
,并将其写入指向的位置:

*output_length = (size_t) (4.0 * ceil((double) input_length / 3.0));
您应该传递
size\t
变量的地址,或者至少传递指向已分配内存的初始化指针

而且

很可能是错误的,应该是
size\t inLen=strlen(ibuf)

可能,而不是

char *encoded_data = malloc(*output_length);

您应该再malloc一次,然后0-终止编码字符串(但这取决于使用情况)。

此指针未初始化:

size\u t*outLen

在第25行,你想取消对它的引用。将代码更改为:

size_t outLen;
base64_encode(ibuf, inLen, &outLen);

此指针未初始化:

size\u t*outLen

在第25行,你想取消对它的引用。将代码更改为:

size_t outLen;
base64_encode(ibuf, inLen, &outLen);

我刚刚用过这个代码

这就是工作:

#include <stdint.h>
#include <stdlib.h>

static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                                'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                                'w', 'x', 'y', 'z', '0', '1', '2', '3',
                                '4', '5', '6', '7', '8', '9', '+', '/'};
static char *decoding_table = NULL;
static int mod_table[] = {0, 2, 1};

void build_decoding_table();

char *base64_encode(const unsigned char *data,
                    size_t input_length,
                    size_t *output_length) {
    int i, j;
    *output_length = 4 *((input_length + 2)/ 3);

    char *encoded_data = calloc(*output_length, 1);
    if (!encoded_data) return NULL;

    for (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 << 0x10) + (octet_b << 0x08) + octet_c;

        encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
    }

    for (i = 0; i < mod_table[input_length % 3]; i++)
        encoded_data[*output_length - 1 - i] = '=';

    return encoded_data;
}


unsigned char *base64_decode(const char *data,
                    size_t input_length,
                    size_t *output_length) {
    int i, j;
    if (decoding_table == NULL) build_decoding_table();

    if (input_length % 4 != 0) return NULL;

    *output_length = input_length / 4 * 3;
    if (data[input_length - 1] == '=') (*output_length)--;
    if (data[input_length - 2] == '=') (*output_length)--;

    unsigned char *decoded_data = calloc(*output_length, 1);
    if (!decoded_data) return NULL;

    for (i = 0, j = 0; i < input_length;) {

        uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];

        uint32_t triple = (sextet_a << 3 * 6)
                        + (sextet_b << 2 * 6)
                        + (sextet_c << 1 * 6)
                        + (sextet_d << 0 * 6);

        if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
    }

    return decoded_data;
}


void build_decoding_table() {
    int i;
    decoding_table = malloc(256);

    for (i = 0; i < 0x40; i++)
        decoding_table[encoding_table[i]] = i;
}


void base64_cleanup() {
    free(decoding_table);
}




#include <string.h>
#include <stdio.h>

int main(int argc, char **argv){
    if(argc != 2) return -1;
    size_t len = strlen(argv[1]), olen;
    printf("len: %zd\n", 4 *((len + 2)/ 3));
    printf("argument %s coded: %s\n", argv[1], base64_encode(argv[1], len, &olen));
    base64_cleanup();
    return 0;
}
#包括
#包括
静态字符编码_表[]={'A','B','C','D','E','F','G','H',
‘I’、‘J’、‘K’、‘L’、‘M’、‘N’、‘O’、‘P’,
‘Q’、‘R’、‘S’、‘T’、‘U’、‘V’、‘W’、‘X’,
‘Y’、‘Z’、‘a’、‘b’、‘c’、‘d’、‘e’、‘f’,
‘g’、‘h’、‘i’、‘j’、‘k’、‘l’、‘m’、‘n’,
‘o’、‘p’、‘q’、‘r’、‘s’、‘t’、‘u’、‘v’,
“w”、“x”、“y”、“z”、“0”、“1”、“2”、“3”,
'4', '5', '6', '7', '8', '9', '+', '/'};
静态字符*解码_表=NULL;
静态int mod_表[]={0,2,1};
void build_decoding_table();
字符*base64_编码(常量无符号字符*数据,
大小输入长度,
尺寸(t*输出长度){
int i,j;
*输出长度=4*((输入长度+2)/3);
char*encoded_data=calloc(*输出_长度,1);
如果(!encoded_data)返回NULL;
对于(i=0,j=0;i>2*6)&0x3F];
编码的_数据[j++]=编码的_表[(三元组>>1*6)&0x3F];
编码的_数据[j++]=编码的_表[(三重>>0*6)&0x3F];
}
对于(i=0;i>0*8)&0xFF;
}
返回解码后的数据;
}
无效生成\u解码\u表(){
int i;
解码表=malloc(256);
对于(i=0;i<0x40;i++)
解码_表[编码_表[i]]=i;
}
void base64_cleanup(){
免费(解码表);
}
#包括
#包括
int main(int argc,字符**argv){
如果(argc!=2)返回-1;
尺寸_t len=strlen(argv[1]),olen;
printf(“len:%zd\n”,4*((len+2)/3));
printf(“参数%s编码:%s\n”,argv[1],base64_编码(argv[1],len和olen));
base64_cleanup();
返回0;
}

我刚刚使用了这段代码

这就是工作:

#include <stdint.h>
#include <stdlib.h>

static char encoding_table[] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
                                'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
                                'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X',
                                'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
                                'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n',
                                'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                                'w', 'x', 'y', 'z', '0', '1', '2', '3',
                                '4', '5', '6', '7', '8', '9', '+', '/'};
static char *decoding_table = NULL;
static int mod_table[] = {0, 2, 1};

void build_decoding_table();

char *base64_encode(const unsigned char *data,
                    size_t input_length,
                    size_t *output_length) {
    int i, j;
    *output_length = 4 *((input_length + 2)/ 3);

    char *encoded_data = calloc(*output_length, 1);
    if (!encoded_data) return NULL;

    for (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 << 0x10) + (octet_b << 0x08) + octet_c;

        encoded_data[j++] = encoding_table[(triple >> 3 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 2 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 1 * 6) & 0x3F];
        encoded_data[j++] = encoding_table[(triple >> 0 * 6) & 0x3F];
    }

    for (i = 0; i < mod_table[input_length % 3]; i++)
        encoded_data[*output_length - 1 - i] = '=';

    return encoded_data;
}


unsigned char *base64_decode(const char *data,
                    size_t input_length,
                    size_t *output_length) {
    int i, j;
    if (decoding_table == NULL) build_decoding_table();

    if (input_length % 4 != 0) return NULL;

    *output_length = input_length / 4 * 3;
    if (data[input_length - 1] == '=') (*output_length)--;
    if (data[input_length - 2] == '=') (*output_length)--;

    unsigned char *decoded_data = calloc(*output_length, 1);
    if (!decoded_data) return NULL;

    for (i = 0, j = 0; i < input_length;) {

        uint32_t sextet_a = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_b = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_c = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];
        uint32_t sextet_d = data[i] == '=' ? 0 & i++ : decoding_table[data[i++]];

        uint32_t triple = (sextet_a << 3 * 6)
                        + (sextet_b << 2 * 6)
                        + (sextet_c << 1 * 6)
                        + (sextet_d << 0 * 6);

        if (j < *output_length) decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
        if (j < *output_length) decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
    }

    return decoded_data;
}


void build_decoding_table() {
    int i;
    decoding_table = malloc(256);

    for (i = 0; i < 0x40; i++)
        decoding_table[encoding_table[i]] = i;
}


void base64_cleanup() {
    free(decoding_table);
}




#include <string.h>
#include <stdio.h>

int main(int argc, char **argv){
    if(argc != 2) return -1;
    size_t len = strlen(argv[1]), olen;
    printf("len: %zd\n", 4 *((len + 2)/ 3));
    printf("argument %s coded: %s\n", argv[1], base64_encode(argv[1], len, &olen));
    base64_cleanup();
    return 0;
}
#包括
#包括
静态字符编码_表[]={'A','B','C','D','E','F','G','H',
‘I’、‘J’、‘K’、‘L’、‘M’、‘N’、‘O’、‘P’,
‘Q’、‘R’、‘S’、‘T’、‘U’、‘V’、‘W’、‘X’,
‘Y’、‘Z’、‘a’、‘b’、‘c’、‘d’、‘e’、‘f’,
“g”、“h”、“i”、“j”