如何在用户空间程序中使用内核libcrc32c(或相同的函数)?

我想在我自己的用户空间程序中进行一些CRC检查。 我发现内核加密lib已经在系统中,并且支持SSE4.2。

我试着直接#include 并用-I/usr/src/linux/include/运行gcc。 但是,它不起作用。

有什么办法可以使用某种libcrc32c吗?

您可以在Linux上通过套接字系列AF_ALG从用户空间使用内核加密CRC32c (和其他哈希/密码函数):

 #include  #include  #include  #include  #include  #include  #include  #include  #include  int main (int argc, char **argv) { int sds[2] = { -1, -1 }; struct sockaddr_alg sa = { .salg_family = AF_ALG, .salg_type = "hash", .salg_name = "crc32c" }; if ((sds[0] = socket(AF_ALG, SOCK_SEQPACKET, 0)) == -1 ) return -1; if( bind(sds[0], (struct sockaddr *) &sa, sizeof(sa)) != 0 ) return -1; if( (sds[1] = accept(sds[0], NULL, 0)) == -1 ) return -1; char *s = "hello"; size_t n = strlen(s); if (send(sds[1], s, n, MSG_MORE) != n) return -1; int crc32c = 0x00000000; if(read(sds[1], &crc32c, 4) != 4) return -1; printf("%08X\n", crc32c); return 0; } 

如果您正在散列文件或套接字数据,则可以使用零拷贝方法加快速度,以避免使用sendfile和/或splice进行内核 – >用户空间缓冲区复制。

快乐的编码。