gpt4 book ai didi

c - 如何在 C 中哈希 CPU ID

转载 作者:行者123 更新时间:2023-11-30 20:04:35 28 4
gpt4 key购买 nike

我正在尝试缩短我的微 Controller (STM32F1) 的 cpu id。

CPU id 由 3 个字(3 x 4 字节)组成。这是由 3 个单词构建的 id 字符串:980416578761680031125348904

我发现了一个非常有用的库可以做到这一点。

图书馆是 Hashids并且有一个C代码。

我尝试使用“Code Blocks IDE”在 PC 上构建测试代码,代码可以正常工作。

但是当我将代码移动到嵌入式端(Keil v5 IDE)时,我在 strdup() 函数上收到错误:“strdup 隐式声明函数”。

该问题与 strdup 函数不是标准库函数且 ins 未包含在 string.h 中有关。

我将避免用自定义函数(模仿 strdup 的行为)替换 strdup 函数,以避免内存泄漏,因为 strdup 使用 malloc 复制字符串。

是否有不同的方法来压缩长数字?

感谢您的帮助!

<---附录--->

这是使用strdup的函数。

    /* common init */
struct hashids_t *
hashids_init3(const char *salt, size_t min_hash_length, const char *alphabet)
{
struct hashids_t *result;
unsigned int i, j;
size_t len;
char ch, *p;

hashids_errno = HASHIDS_ERROR_OK;

/* allocate the structure */
result = _hashids_alloc(sizeof(struct hashids_t));
if (HASHIDS_UNLIKELY(!result)) {
hashids_errno = HASHIDS_ERROR_ALLOC;
return NULL;
}

/* allocate enough space for the alphabet and its copies */
len = strlen(alphabet) + 1;
result->alphabet = _hashids_alloc(len);
result->alphabet_copy_1 = _hashids_alloc(len);
result->alphabet_copy_2 = _hashids_alloc(len);
if (HASHIDS_UNLIKELY(!result->alphabet || !result->alphabet_copy_1
|| !result->alphabet_copy_2)) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALLOC;
return NULL;
}

/* extract only the unique characters */
result->alphabet[0] = '\0';
for (i = 0, j = 0; i < len; ++i) {
ch = alphabet[i];
if (!strchr(result->alphabet, ch)) {
result->alphabet[j++] = ch;
}
}
result->alphabet[j] = '\0';

/* store alphabet length */
result->alphabet_length = j;

/* check length and whitespace */
if (result->alphabet_length < HASHIDS_MIN_ALPHABET_LENGTH) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALPHABET_LENGTH;
return NULL;
}
if (strchr(result->alphabet, ' ')) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALPHABET_SPACE;
return NULL;
}

/* copy salt */
result->salt = strdup(salt ? salt : HASHIDS_DEFAULT_SALT);
result->salt_length = (unsigned int) strlen(result->salt);

/* allocate enough space for separators */
result->separators = _hashids_alloc((size_t)
(ceil((float)result->alphabet_length / HASHIDS_SEPARATOR_DIVISOR) + 1));
if (HASHIDS_UNLIKELY(!result->separators)) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALLOC;
return NULL;
}

/* non-alphabet characters cannot be separators */
for (i = 0, j = 0; i < strlen(HASHIDS_DEFAULT_SEPARATORS); ++i) {
ch = HASHIDS_DEFAULT_SEPARATORS[i];
if ((p = strchr(result->alphabet, ch))) {
result->separators[j++] = ch;

/* also remove separators from alphabet */
memmove(p, p + 1,
strlen(result->alphabet) - (p - result->alphabet));
}
}

/* store separators length */
result->separators_count = j;

/* subtract separators count from alphabet length */
result->alphabet_length -= result->separators_count;

/* shuffle the separators */
hashids_shuffle(result->separators, result->separators_count,
result->salt, result->salt_length);

/* check if we have any/enough separators */
if (!result->separators_count
|| (((float)result->alphabet_length / (float)result->separators_count)
> HASHIDS_SEPARATOR_DIVISOR)) {
unsigned int separators_count = (unsigned int)ceil(
(float)result->alphabet_length / HASHIDS_SEPARATOR_DIVISOR);

if (separators_count == 1) {
separators_count = 2;
}

if (separators_count > result->separators_count) {
/* we need more separators - get some from alphabet */
int diff = separators_count - result->separators_count;
strncat(result->separators, result->alphabet, diff);
memmove(result->alphabet, result->alphabet + diff,
result->alphabet_length - diff + 1);

result->separators_count += diff;
result->alphabet_length -= diff;
} else {
/* we have more than enough - truncate */
result->separators[separators_count] = '\0';
result->separators_count = separators_count;
}
}

/* shuffle alphabet */
hashids_shuffle(result->alphabet, result->alphabet_length,
result->salt, result->salt_length);

/* allocate guards */
result->guards_count = (unsigned int) ceil((float)result->alphabet_length
/ HASHIDS_GUARD_DIVISOR);
result->guards = _hashids_alloc(result->guards_count + 1);
if (HASHIDS_UNLIKELY(!result->guards)) {
hashids_free(result);
hashids_errno = HASHIDS_ERROR_ALLOC;
return NULL;
}

if (HASHIDS_UNLIKELY(result->alphabet_length < 3)) {
/* take some from separators */
strncpy(result->guards, result->separators, result->guards_count);
memmove(result->separators, result->separators + result->guards_count,
result->separators_count - result->guards_count + 1);

result->separators_count -= result->guards_count;
} else {
/* take them from alphabet */
strncpy(result->guards, result->alphabet, result->guards_count);
memmove(result->alphabet, result->alphabet + result->guards_count,
result->alphabet_length - result->guards_count + 1);

result->alphabet_length -= result->guards_count;
}

/* set min hash length */
result->min_hash_length = min_hash_length;

/* return result happily */
return result;
}

最佳答案

真正的问题似乎是

Is there a different approach to compress long numbers?

有很多。它们在几个方面有所不同,包括输入的哪些位对输出有贡献、有多少输入映射到同一输出,以及输入的转换方式使输出保持不变。

作为一个简单的示例,您可以通过以下任何方法将输入压缩为一位:

  • 选择输入的最低位
  • 选择输入的最高位
  • 输出始终为 1
  • 等等

或者您可以通过使用输入中 1 位数作为输出来压缩为 7 位。

当然,您可能不会对这些特定选项感兴趣。

也许您对为 96 位输入生成 32 位输出更感兴趣。 请注意,在这种情况下,平均至少有 264 个可能的输入映射到每个可能的输出。这仅取决于输入和输出的大小,而不是转换的任何细节。

例如,假设您有

uint32_t *cpuid = ...;

指向硬件CPU ID。您只需执行以下操作即可从中生成一个 32 位值,该值取决于输入的所有位:

uint32_t cpuid32 = cpuid[0] ^ cpuid[1] ^ cpuid[2];

这是否适合您的目的取决于您打算如何使用它。

关于c - 如何在 C 中哈希 CPU ID,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39229073/

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