gpt4 book ai didi

c - 程序打印出与它应该打印的完全不同的字符串

转载 作者:太空宇宙 更新时间:2023-11-04 08:23:31 27 4
gpt4 key购买 nike

在下面的代码中,从注释中应该是不言自明的,而不是打印字符串 tmp,它实际上打印了 argv[2] 的值。我已经通过 LLDB 运行它并且一切正常,所有中间值都是正确的并且 tmp 在调用 long2hex 之后保存了正确的字符串但它没有被打印到屏幕。我可以在 LLDB 中勉强过关,检查变量等等,但不知道如何调试这样的东西,即使经过一些广泛的谷歌搜索。有没有人知道为什么要打印 argv[2],或者可以指导我使用 LLDB 来调试行为?

程序使用两个命令行参数 argv[1]="1c0111001f010100061a024b53535009181c"argv[2]="686974207468652062756c6c277320657965"

如果有人感兴趣,这是 matasano 加密挑战之一!第 1 组,问题 2。可能有更简单的方法,但这就是我想出的方法!

代码如下:

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

/* Converts a character to the corresponding integer value */
uint8_t char2hex (const char a_char) {
uint8_t rv = 0;

if (a_char >= 48 && a_char <= 57) /* '0'-'9' */
rv = (uint8_t)a_char - 48;
else if (a_char >= 65 && a_char <=70) /* 'A'-'F' */
rv = (uint8_t)a_char - 55;
else if (a_char >= 97 && a_char <= 102) /* 'a'-'f' */
rv = (uint8_t)a_char - 87;

return rv;
}

/* Converts an integer value to its corresponding hex character */
char hex2char (uint8_t a_hex) {
char rv;

if (a_hex >= 0 && a_hex <= 9)
rv = a_hex + 48;
else if (a_hex >= 10 && a_hex <= 15)
rv = a_hex + 87;
else
rv = 'z';

return rv;

}

/* Coverts an unsigned 32-bit integer value into its hexadecimal representation */
void long2hex (uint32_t val, char result[9]) {
char hex[9];

int j = 0;
for (int i = 0; i < 4; i++,j+=2){
uint8_t tmpVal;
tmpVal = (val>>((3-i)*8));
hex[j] = hex2char((tmpVal >> 4) & 0x0F);
hex[j+1] = hex2char(tmpVal & 0x0F);
}
hex[8] = '\0';

result = hex;
}


/* Converts an 8 character hex string into its unsigned integer form */
uint32_t hex2int32(uint8_t h[8]) {
uint32_t b = 0;

for (int i = 0; i < 8; i++)
b += (h[i]) << (4*(7-i));

return b;

}

/* Splits a string into 8 character chunks */
char ** split_hex_8 (const char * hex_string, int * num_strings) {
int string_length = strlen(hex_string);
int num_blocks = (int)ceil((double)string_length / 8);

char ** split_hex = (char **) malloc(num_blocks * sizeof(char*));

int length_counter = string_length;

for (int i = 0; i < num_blocks; i++) {
split_hex[i] = (char *) malloc(9);
if (length_counter < 8) {
memcpy(split_hex[i], hex_string+(i*8), length_counter);
for (int j=length_counter; j < 8; j++)
(split_hex[i])[j] = '0';
} else
memcpy(split_hex[i], hex_string+(i*8), 8);
(split_hex[i])[8] = '\0';
}

*num_strings = num_blocks;

return split_hex;
}



int main (int argc, char * argv[]) {

char * hex_str_1 = argv[1];
char * hex_str_2 = argv[2];

int len1 = strlen(argv[1]);
int len2 = strlen(argv[2]);

char ** hex_split_1, ** hex_split_2;

int num_blocks_1, num_blocks_2;

/* Split the input strings */
hex_split_1 = split_hex_8(hex_str_1,&num_blocks_1);
hex_split_2 = split_hex_8(hex_str_2,&num_blocks_2);


/* For each of the inputs, xor them together by xoring the individual 32-bit unsigned integers together */
for (int i = 0; i < num_blocks_1; i++) {
uint32_t val1, val2, xor;
uint8_t hex1[8] = {0};
uint8_t hex2[8] = {0};
for (int j = 0; j < 8; j++){ /* convert each hex character in each string to its integer equivalent */
hex1[j] = char2hex((hex_split_1[i])[j]);
hex2[j] = char2hex((hex_split_2[i])[j]);
}
free(hex_split_1[i]); /* We are now done with this memory, so free it. */
free(hex_split_2[i]);

/* Shift in all of the 8-bit decoded hex into a 32-bit integer - gets the unsigned 32 bit representation of each 8 byte block of input */
val1 = hex2int32(hex1);
val2 = hex2int32(hex2);

/* Create some temporary storage for the new hex string */
char * tmp;
tmp = malloc(9);

xor = val1^val2;
/* convert this xor-ed value to it's corresponding hex representation, store it in tmp */
long2hex(xor,tmp);
/* print tmp to the screen */
/* THIS IS THE ERROR: instead of printing tmp to the screen, it ends up printing out argv[2]. */
printf("%s",tmp);
/* done with tmp! */
free(tmp);
}


/* don't forget to free the containers for the split hex */
free(hex_split_1);
free(hex_split_2);


return 0;

}

最佳答案

result = hex;

那是你的问题。 result 是一个局部变量。分配给它不会传播回调用者。你想要:

strncpy(result, hex, sizeof(result));

关于c - 程序打印出与它应该打印的完全不同的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32262291/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com