如何在C中散列CPU ID
我正在尝试缩短我的微控制器(STM32F1)的cpu id cpu id由3个字(3 x 4字节)组成。这是由3个字组成的id字符串:如何在C中散列CPU ID,c,hash,embedded,compression,hashids,C,Hash,Embedded,Compression,Hashids,我正在尝试缩短我的微控制器(STM32F1)的cpu id cpu id由3个字(3 x 4字节)组成。这是由3个字组成的id字符串:9804166578761680031125348904 我发现了一个非常有用的库,可以做到这一点 这个库很简单,有一个C代码 我尝试用“代码块IDE”在PC上构建一个测试代码,代码运行正常 但是当我将代码移动到嵌入式端(Keil v5 IDE)时,我在strdup()函数上得到一个错误:“strdup隐式函数声明” 这个问题与strdup函数不是标准的库函数以及
9804166578761680031125348904
我发现了一个非常有用的库,可以做到这一点
这个库很简单,有一个C代码
我尝试用“代码块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;
}
/*公共初始化*/
结构hashids\u t*
hashids_init3(常量字符*盐,大小\u最小\u哈希长度,常量字符*字母)
{
结构hashids_t*结果;
无符号整数i,j;
尺寸透镜;
char-ch,*p;
hashids\u errno=hashids\u ERROR\u OK;
/*分配结构*/
结果=_hashids_alloc(sizeof(struct hashids_t));
if(HASHIDS_不太可能(!result)){
hashids\u errno=hashids\u ERROR\u ALLOC;
返回NULL;
}
/*为字母表及其副本分配足够的空间*/
len=strlen(字母表)+1;
结果->字母表=\u hashids\u alloc(len);
结果->字母表\u副本\u 1=\u hashids\u alloc(len);
结果->字母表\u副本\u 2=\u hashids\u alloc(len);
如果(HASHIDS_不太可能(!result->alphabet|||!result->alphabet_copy_1
||!结果->字母表(复制2)){
hashids_free(结果);
hashids\u errno=hashids\u ERROR\u ALLOC;
返回NULL;
}
/*仅提取唯一字符*/
结果->字母表[0]='\0';
对于(i=0,j=0;i字母表,ch)){
结果->字母表[j++]=ch;
}
}
结果->字母表[j]='\0';
/*存储字母表长度*/
结果->字母长度=j;
/*检查长度和空格*/
if(结果->字母表长度字母表“”){
hashids_free(结果);
hashids\u errno=hashids\u ERROR\u ALPHABET\u SPACE;
返回NULL;
}
/*复制盐*/
结果->salt=strdup(salt?salt:HASHIDS\u DEFAULT\u salt);
结果->salt\u长度=(无符号整数)strlen(结果->salt);
/*为分离器分配足够的空间*/
结果->分隔符=\u hashids\u alloc((大小\u t)
(ceil((浮点)结果->字母表长度/HASHIDS_分隔符_除数)+1);
if(HASHIDS_不太可能(!result->separator)){
hashids_free(结果);
hashids\u errno=hashids\u ERROR\u ALLOC;
返回NULL;
}
/*非字母字符不能是分隔符*/
对于(i=0,j=0;i字母表,ch))){
结果->分隔符[j++]=ch;
/*还要从字母表中删除分隔符*/
memmove(p,p+1,
strlen(结果->字母表)-(p-结果->字母表);
}
}
/*存储分离器长度*/
结果->分隔符\u计数=j;
/*从字母表长度中减去分隔符计数*/
结果->字母长度-=结果->分隔符计数;
/*洗牌*/
hashids\u shuffle(结果->分隔符,结果->分隔符\u计数,
结果->盐,结果->盐长度);
/*检查我们是否有/有足够的分离器*/
如果(!结果->分隔符\u计数
||(((浮点)结果->字母表长度/(浮点)结果->分隔符计数)
>HASHIDS(分隔符(除数)){
无符号整数分隔符\u count=(无符号整数)ceil(
(浮点)结果->字母表\长度/哈希ID \分隔符\除数);
if(分隔符\u计数==1){
分离器数量=2;
}
如果(分隔符计数>结果->分隔符计数){
/*我们需要更多的分隔符-从字母表中获取一些分隔符*/
int diff=分隔符计数-结果->分隔符计数;
strncat(结果->分隔符,结果->字母表,差异);
memmove(结果->字母表,结果->字母表+差异,
结果->字母长度-差异+1);
结果->分隔符\u计数+=差异;
结果->字母长度-=差异;
}否则{
/*我们有足够多的-截断*/
结果->分隔符[分隔符\u计数]='\0';
结果->分隔符计数=分隔符计数;
}
}
/*洗牌字母表*/
hashids_shuffle(结果->字母表,结果->字母表长度,
结果->盐,结果->盐长度);
/*分配警卫*/
结果->保护计数=(无符号整数)单元((浮点)结果->字母表长度
/HASHIDS(保护除数);
结果->保护=\u hashids\u alloc(结果->保护计数+1);
如果(HASHIDS_不太可能(!result->guards)){
hashids_free(结果);
hashids\u errno=hashids\u ERROR\u ALLOC;
返回NULL;
}
如果(HASHIDS_不太可能(结果->字母长度<3)){
/*从分离器中取出一些*/
strncpy(结果->防护,结果->分隔符,结果->防护计数);
memmove(结果->分隔符,结果->分隔符+结果->守卫计数,
结果->分隔符计数-结果->保护计数+1);
结果->分隔符\u计数-=结果->保护\u计数;
}否则{
/*从字母表中取出来*/
strncpy(结果->防护,结果->字母表,结果->防护计数);
memmove(结果->al
char* strdup (const char* str)
{
size_t size = strlen(str);
char* result = malloc(size);
if(result != NULL)
{
memcpy(result, str, size+1);
}
return result;
}
unsigned int b = *(0x1FFFF7E8);
sprintf(id, "%08X%08X%08X", *(0x1FFFF7E8), *(0x1FFFF7E8 + 4), *(0x1FFFF7E8 + 8);
uint32_t *cpuid = ...;
uint32_t cpuid32 = cpuid[0] ^ cpuid[1] ^ cpuid[2];