类型转换问题 unsigned char, string, hex

Type conversion issues unsigned char, string, hex

以下代码有几个问题 - 我恳请您提供各种类型的输入/更正/反馈:首先,我无法让它做我想做的事情,即 传递给它一个“词”并让它输出它的elf_hashelf_gnu_hash ( EDIT 3: This should have been elf_hash, hence my surprise for the wrong hash below ) 函数的代码显示在 ELF_DT_HASH 中。我想做的是将这个功能合并到一个小的独立程序中,但我似乎无法做到正确,即打印的输出绝不是预期的输出(与提到的文章相比)。

其次,我确信代码(很明显)暴露给任何进行 C 编程的人 - 我被排除在这种 'list' 对数据类型、转换等的误解之外,我希望得到一些澄清/关于一些常见新手(包括我)的误解的提示。 第三,也是最有趣的是,每次我编译和 运行 这个程序,并在 scanf 函数中输入 相同的字符串 ,它 打印不同的结果 !

编译时有很多警告,但老实说我不确定如何修复它们。 你们能帮我解决这个问题 + 解决对 C 的一些误解/误用吗?

我也很感激一些关于输入清理的意见(即避免缓冲区溢出等)。

正在编译它 - 如果它很重要,不确定 - 就像这样:

gcc -Wall elf_hash-calculaTor.c && ./a.out

谢谢

作为奖励:这是 Linux OS amd64 elf 二进制文件中使用的算法,例如 f.e.

$ file hexedit
hexedit: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, BuildID[sha1]=d3f6cd413abaa25d5b7a2427f05598f3152e2efa, for GNU/Linux 3.2.0, stripped

,还是这个 DT_GNU_HASH 而不是?

#include <stdint.h>                                                                                                                                                         
  
/** The ELF symbol GNU hashing function */
static unsigned long elf_gnu_hash(const unsigned char*name) {
  unsigned long h = 5381;
  unsigned char c;
  while ((c = *name++) != '[=11=]') {
    h = (h << 5) + h + c;
  }
  return h & 0xffffffff;
}
 
int main(int ac,char **av){
  unsigned char in[10] = "[=11=]";
  printf("Type word for which to calculate the elf-hash:");
  scanf("%hhu", &in);
  printf("Typed in:");
  printf("%hhu\n", in);
  printf("processing elf-hash:\n");
  //elf_gnu_hash(in);
  // following nok: printf("%#lx\n", elf_gnu_hash("freelocal"));
  printf("%#lx\n", elf_gnu_hash(in)); 
  printf("%s", "Thanks guys!");
}

编辑

: 回答您的问题:

elf_hash("freelocal") = 0x0bc334fc

,在我的情况下没有。

是否因为我输入了一个字符串(使用 scanf "%s",但该函数需要一个 char(指针?)这可以是 - 如果我的理解是正确的 - char 数组 的第一个数组(或者这个从字符串到 char 数组的转换是在 C 中自动完成的吗?)。函数然后继续遍历这个数组中的所有字符:

...   
while ((c = *name++) != '[=13=]') {

并进行一些按位移位。

中介 printf 的作用只是回应输入的内容并证明我的类型转换正确(我也没有)。

tox@tox-bookp:/var/tmp$ ./a.out 
Type word for which to calculate the elf-hash:cucu
Typed in:126
processing elf-hash:
0x1505

 
tox@tox-bookp:/var/tmp$ ./a.out 
Type word for which to calculate the elf-hash:cucu
Typed in:62
...
 
tox@tox-bookp:/var/tmp$ ./a.out 
Type word for which to calculate the elf-hash:cucu
Typed in:174
...

编辑 2

代码现在看起来像这样(基于@chux 的评论)

int main(int ac,char **av){
unsigned char in[10] = "";
printf("Type word for which to calculate the elf-hash:");
//scanf("%hhu", &in);
//printf("%hhu\n", in);
if (scanf("%9s", in) == 1) {
    printf("Typed in:");
    printf("<%s>\n", in);
    printf("processing elf-hash:\n");
//elf_gnu_hash(in);
// following nok:
    printf("%#lx\n", elf_gnu_hash("freelocal"));
    printf("%#lx\n", elf_gnu_hash(in));
    }                                                      
printf("%s\n", "Thanks guys!");
}

但还是输出错误 elf_hash :

Type-in item for which to calculate the elf-hash:freelocal
Typed in:<freelocal>
processing elf-hash:
0xe3364372   **<- is this ok, and if so, why ?**
0xe3364372   **<- incorrect** 
Thanks guys!

pass it a string ("word") and have it output its elf_hash.

使用 "%s"width 限制来读取用户输入并保存为 string,而不是 "%hhu"(这对于读取数字文本并保存为字节很有用)。

unsigned char in[10] = "[=10=]";  // Can simplify to ""

// scanf("%hhu", &in);
// printf("%hhu\n", in);

if (scanf("%9s", in) == 1) {
  printf("<%s>\n", in);
  ...