简体   繁体   English

如何仅使用 C 预处理器计算字符串文字的 hash?

[英]How to calculate the hash of a string literal using only the C preprocessor?

I need to get a string checksum or hash (or something equivalent) using just the C preprocessor, if possible.如果可能的话,我需要仅使用 C 预处理器来获取字符串校验和或 hash(或类似的东西)。

The use case is as follows: I'm doing error logging on an embedded device with very limited memory and cpu.用例如下:我在 memory 和 cpu 非常有限的嵌入式设备上进行错误记录。 I would like to define a LogError() macro which inserts hash(__FILE__) and __LINE__ in a circular buffer (as 16bit numbers).我想定义一个LogError()宏,它将hash(__FILE__)__LINE__插入循环缓冲区(作为 16 位数字)。 But hash(__FILE__) needs to be compiled to a constant;但是hash(__FILE__)需要编译成常量; if the actual filenames are stored as strings in the program that would use too much memory. The hash can be calculated using any method.如果实际的文件名在程序中存储为字符串,将使用太多 memory。可以使用任何方法计算 hash。

It is possible to #define FILE_ID with some unique number at the top of every file, and use that when logging, but that is not the preferred solution, it has a bit of a maintenance cost.可以在每个文件的顶部用一些唯一的编号来#define FILE_ID ,并在记录日志时使用它,但这不是首选的解决方案,它有一点维护成本。 Is there a better method?有更好的方法吗?

The question "How to calculate the hash of a string literal using only the C preprocessor?"问题“如何仅使用 C 预处理器计算字符串文字的哈希?” is valid, however I think you're adding a red-herring by including details about __FILE__ and logging ID's.是有效的,但是我认为您通过包含有关__FILE__和日志记录 ID 的详细信息来添加红鲱鱼。

This means anyone answering needs to solve the problem you describe, or answer the question on hashing a string with the pre-processor (which may not be a good solution in your particular case!) .这意味着任何回答的人都需要解决您描述的问题,或者回答有关使用预处理器散列字符串的问题(在您的特定情况下这可能不是一个好的解决方案!)

As it happens, __FILE__ expands to variable, not a literal string (GCC at least), so you will need to define the filename as a constant.碰巧的是, __FILE__扩展为变量,而不是文字字符串(至少是 GCC),因此您需要将文件名定义为常量。 You could use the build system to pass along a define for each for example.例如,您可以使用构建系统为每个传递定义。

As others have pointed out you can calculate the hash and pass this in via the build-system, although this avoids the question about hashing a sting literal.正如其他人指出的那样,您可以计算散列并通过构建系统将其传入,尽管这避免了有关散列字符串文字的问题。


Whatever the case, this question comes up when I searched for using the pre-processor for hashing, and none of the answers cover this, so heres is an answer that covers the string hashing part.无论如何,当我搜索使用预处理器进行散列时会出现这个问题,并且没有任何答案涵盖这一点,因此这是一个涵盖字符串散列部分的答案。

This is possible, albeit quite verbose这是可能的,虽然很冗长

/**
 * Implement compile-time string hashing on string literals.
 *
 * This macro implements the widely used "djb" hash apparently posted
 * by Daniel Bernstein to comp.lang.c some time ago.  The 32 bit
 * unsigned hash value starts at 5381 and for each byte 'c' in the
 * string, is updated: ``hash = hash * 33 + c``.  This
 * function uses the signed value of each byte.
 *
 * note: this is the same hash method that glib 2.34.0 uses.
 */

#define SEED 5381

#if 0
// correct but causes insane expansion
#  define _SH(e, c) (((e) << 5) + (e) + (unsigned char)(c))
#elif defined(__GNUC__)
// Use statement-expression extension
#  define _SH(e, c) ({ unsigned int _e = (unsigned int)(e); (_e << 5) + _e + (unsigned char)c; })
#else
// use an inline function, the compiler will be able to optimize this out.
static inline unsigned int _SH(unsigned int e, unsigned char c)
{
    unsigned int _e = (unsigned int)e;
    return (_e << 5) + _e + (unsigned char)c;
}
#endif

#define _SH_1(a) _SH(SEED, (a)[0])
#define _SH_2(a) _SH(_SH_1(a), (a)[1])
#define _SH_3(a) _SH(_SH_2(a), (a)[2])
#define _SH_4(a) _SH(_SH_3(a), (a)[3])
#define _SH_5(a) _SH(_SH_4(a), (a)[4])
#define _SH_6(a) _SH(_SH_5(a), (a)[5])
#define _SH_7(a) _SH(_SH_6(a), (a)[6])
#define _SH_8(a) _SH(_SH_7(a), (a)[7])
#define _SH_9(a) _SH(_SH_8(a), (a)[8])
#define _SH_10(a) _SH(_SH_9(a), (a)[9])
#define _SH_11(a) _SH(_SH_10(a), (a)[10])
#define _SH_12(a) _SH(_SH_11(a), (a)[11])
#define _SH_13(a) _SH(_SH_12(a), (a)[12])
#define _SH_14(a) _SH(_SH_13(a), (a)[13])
#define _SH_15(a) _SH(_SH_14(a), (a)[14])
#define _SH_16(a) _SH(_SH_15(a), (a)[15])
#define _SH_17(a) _SH(_SH_16(a), (a)[16])
#define _SH_18(a) _SH(_SH_17(a), (a)[17])
#define _SH_19(a) _SH(_SH_18(a), (a)[18])
#define _SH_20(a) _SH(_SH_19(a), (a)[19])
#define _SH_21(a) _SH(_SH_20(a), (a)[20])
#define _SH_22(a) _SH(_SH_21(a), (a)[21])
#define _SH_23(a) _SH(_SH_22(a), (a)[22])
#define _SH_24(a) _SH(_SH_23(a), (a)[23])
#define _SH_25(a) _SH(_SH_24(a), (a)[24])
#define _SH_26(a) _SH(_SH_25(a), (a)[25])
#define _SH_27(a) _SH(_SH_26(a), (a)[26])
#define _SH_28(a) _SH(_SH_27(a), (a)[27])
#define _SH_29(a) _SH(_SH_28(a), (a)[28])
#define _SH_30(a) _SH(_SH_29(a), (a)[29])
#define _SH_31(a) _SH(_SH_30(a), (a)[30])
#define _SH_32(a) _SH(_SH_31(a), (a)[31])

// initial check prevents too-large strings from compiling
#define STRHASH(a) ( \
    (void)(sizeof(int[(sizeof(a) > 33 ? -1 : 1)])), \
    (sizeof(a) == 1) ? SEED : \
    (sizeof(a) == 2) ? _SH_1(a) : \
    (sizeof(a) == 3) ? _SH_2(a) : \
    (sizeof(a) == 4) ? _SH_3(a) : \
    (sizeof(a) == 4) ? _SH_3(a) : \
    (sizeof(a) == 5) ? _SH_4(a) : \
    (sizeof(a) == 6) ? _SH_5(a) : \
    (sizeof(a) == 7) ? _SH_6(a) : \
    (sizeof(a) == 8) ? _SH_7(a) : \
    (sizeof(a) == 9) ? _SH_8(a) : \
    (sizeof(a) == 10) ? _SH_9(a) : \
    (sizeof(a) == 11) ? _SH_10(a) : \
    (sizeof(a) == 12) ? _SH_11(a) : \
    (sizeof(a) == 13) ? _SH_12(a) : \
    (sizeof(a) == 14) ? _SH_13(a) : \
    (sizeof(a) == 15) ? _SH_14(a) : \
    (sizeof(a) == 16) ? _SH_15(a) : \
    (sizeof(a) == 17) ? _SH_16(a) : \
    (sizeof(a) == 18) ? _SH_17(a) : \
    (sizeof(a) == 19) ? _SH_18(a) : \
    (sizeof(a) == 20) ? _SH_19(a) : \
    (sizeof(a) == 21) ? _SH_20(a) : \
    (sizeof(a) == 22) ? _SH_21(a) : \
    (sizeof(a) == 23) ? _SH_22(a) : \
    (sizeof(a) == 24) ? _SH_23(a) : \
    (sizeof(a) == 25) ? _SH_24(a) : \
    (sizeof(a) == 26) ? _SH_25(a) : \
    (sizeof(a) == 27) ? _SH_26(a) : \
    (sizeof(a) == 28) ? _SH_27(a) : \
    (sizeof(a) == 29) ? _SH_28(a) : \
    (sizeof(a) == 30) ? _SH_29(a) : \
    (sizeof(a) == 31) ? _SH_30(a) : \
    (sizeof(a) == 32) ? _SH_31(a) : \
    (sizeof(a) == 33) ? _SH_32(a) : \
    0)
// last zero is unreachable

// only for comparison
unsigned int strhash_func(const void *str)
{
    const signed char *p;
    unsigned int h = 5381;

    for (p = str; *p != '\0'; p++) {
        h = (h << 5) + h + (unsigned int)*p;
    }

    return h;
}

/* -------------------------------------------------------------------- */
#include <stdio.h>

#define TEST_STR1 "Hello World"
#define TEST_STR2 "Testing 123"
int main(void)
{
    unsigned int A = STRHASH(TEST_STR1);
    unsigned int B = STRHASH(TEST_STR2);

    printf("String hash: const %u <- '%s'\n", STRHASH(TEST_STR1), TEST_STR1);
    printf("String hash: const %u <- '%s'\n", STRHASH(TEST_STR2), TEST_STR2);
    printf("String hash: dyn %u <- '%s'\n", strhash_func(TEST_STR1), TEST_STR1);
    printf("String hash: dyn %u <- '%s'\n", strhash_func(TEST_STR2), TEST_STR2);

#if defined(__GNUC__)
    printf("Is this known at compile time?, answer is: %d\n", __builtin_constant_p(A));
#endif
}

Note, for some reason Clang 5.0 prints answer is: 0 , however on closer inspection is does in fact know the value at compile time, __builtin_constant_p just doesn't seem to work as GCC does.请注意,出于某种原因,Clang 5.0 打印的answer is: 0 ,但是仔细检查确实知道编译时的值, __builtin_constant_p似乎不像 GCC 那样工作。

You're asking too much of the preprocessor.你对预处理器的要求太多了。

Better just build each file with a 16bit checksum of its name defined as preprocessor macro, as @nm suggests.正如@nm 建议的那样,最好只使用定义为预处理器宏的名称的 16 位校验和来构建每个文件。

One trifling difficulty for this solution is laying your hands on a 16bit checksum utility.此解决方案的一个小困难是将您的手放在 16 位校验和实用程序上。 The GNU cksum tool is just 32-bit. GNU cksum工具只有 32 位。

But FreeBSD has a better one that lets you choose 16 or 32 bits.但是 FreeBSD 有一个更好的让你选择 16 位或 32 位的。 If your development system is a Debian derivative then you can get it by:如果您的开发系统是 Debian 衍生版,那么您可以通过以下方式获得:

sudo apt-get install freebsd-buildutils

Then run:然后运行:

dpkg-query -L freebsd-buildutils

to see where the FreeBSD cksum has been installed.查看 FreeBSD cksum安装位置。 In my case, it is:就我而言,它是:

/usr/bin/freebsd-cksum

but you might find differently.但你可能会发现不同。

You direct FreeBSD cksum to produce a 16bit checksum by passing the option -o 1 .您可以通过传递选项-o 1来指示 FreeBSD cksum生成 16 位校验和。 You can check its man page.您可以查看其手册页。

Take care when generating the preprocessor macro that defines a filename checksum that you define the checksum as a 16 bit unsigned int , as that's what you want.生成定义文件名校验和的预处理器宏时要小心,您将校验和定义为 16 位 unsigned int ,因为这就是您想要的。 Should it come out as a plain decimal numeral, for instance, it would default to signed int, which could cause you surprises.例如,如果它是一个普通的十进制数字,它将默认为有符号整数,这可能会让您感到惊讶。

Here's a sketch of the solution with GNU make :这是 GNU make解决方案的草图:

main.c主文件

#include <stdio.h>
#include <stdint.h>

int main(int argc, char **argv)
{
    printf("%hu\n",FILE_CHKSUM);
    return 0;
}

Makefile生成文件

.phony: all clean

SRCS = main.c
OBJS = $(SRCS:.c=.o)
# Take the 1st word of the output of `echo <filename> | freebsd-cksum -o 1`
FILE_CHKSUM = $(word 1,$(shell echo $(1) | freebsd-cksum -o 1))

all: prog

%.o:%.c
    $(CC) $(CPPLAGS) $(CFLAGS) -DFILE_CHKSUM='((uint16_t)$(call FILE_CHKSUM,$<))' -c -o $@ $< 

prog: $(OBJS)
    $(CC) $(CFLAGS) $(LDFLAGS) -o $@ $(OBJS) $(LDLIBS)

clean:
    rm -f $(OBJS) prog

Running make :运行make

cc   -DFILE_CHKSUM='((uint16_t)3168)' -c -o main.o main.c 
cc   -o prog main.o

Run prog :运行prog

./prog
3168

How to do this in CMake?如何在 CMake 中执行此操作? I can not figure out how to run script for every source file, and the result place as a value for -DFILE_CHEKSUM我不知道如何为每个源文件运行脚本,并将结果作为 -DFILE_CHEKSUM 的值

Method with STRHASH, has a big limitation - it takes maximum of 32 characters as input, and when I have longer names, it just does not compile.使用 STRHASH 的方法有一个很大的限制 - 它最多需要 32 个字符作为输入,当我有更长的名称时,它就不会编译。 I think that computing it before build and passing it as -D is beter approach, but I just can not manage to do it in cmake:(我认为在构建之前计算它并将其作为 -D 传递是更好的方法,但我无法在 cmake 中做到这一点:(

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM