mirror of
https://github.com/zlgopen/awtk.git
synced 2025-05-08 19:44:45 +08:00
add tk_crc32_file
This commit is contained in:
parent
26a0d26496
commit
010c47f110
@ -183,4 +183,39 @@ uint32_t tk_crc32(uint32_t init, const uint8_t* data, int size) {
|
||||
uint32_t tk_crc32_byte(uint32_t crc, uint8_t data) {
|
||||
return (crc >> 8) ^ crc_tab32[(crc ^ (uint32_t)data) & 0x000000FFul];
|
||||
}
|
||||
|
||||
#include "tkc/fs.h"
|
||||
#include "tkc/mem.h"
|
||||
#include "tkc/utils.h"
|
||||
|
||||
uint32_t tk_crc32_file(const char* filename, uint32_t block_size) {
|
||||
int32_t size = 0;
|
||||
fs_file_t* fp = NULL;
|
||||
uint8_t* buff = NULL;
|
||||
uint32_t crc32 = PPPINITFCS32;
|
||||
|
||||
return_value_if_fail(filename != NULL, crc32);
|
||||
|
||||
fp = fs_open_file(os_fs(), filename, "rb");
|
||||
return_value_if_fail(fp != NULL, crc32);
|
||||
block_size = tk_max_int(block_size, 256);
|
||||
block_size = tk_min_int(block_size, 1024 * 1024);
|
||||
|
||||
buff = TKMEM_ALLOC(block_size + 1);
|
||||
goto_error_if_fail(buff != NULL);
|
||||
|
||||
while (!fs_file_eof(fp)) {
|
||||
size = fs_file_read(fp, buff, block_size);
|
||||
if (size <= 0) {
|
||||
break;
|
||||
}
|
||||
crc32 = tk_crc32(crc32, buff, size);
|
||||
}
|
||||
|
||||
error:
|
||||
fs_file_close(fp);
|
||||
TKMEM_FREE(buff);
|
||||
|
||||
return crc32;
|
||||
}
|
||||
#endif
|
||||
|
@ -113,6 +113,16 @@ uint32_t tk_crc32(uint32_t crc, const uint8_t* data, int size);
|
||||
* @return {uint32_t} 返回计算结果。
|
||||
*/
|
||||
uint32_t tk_crc32_byte(uint32_t crc, uint8_t data);
|
||||
|
||||
/**
|
||||
* @method tk_crc32_file
|
||||
* 计算文件的crc32哈希值。
|
||||
* @param {const char*} filename 文件名。
|
||||
* @param {uint32_t} block_size 数据块长度。
|
||||
*
|
||||
* @return {uint32_t} 返回计算结果。
|
||||
*/
|
||||
uint32_t tk_crc32_file(const char* filename, uint32_t block_size);
|
||||
#endif
|
||||
|
||||
END_C_DECLS
|
||||
|
6
tests/crc_test.cc
Normal file
6
tests/crc_test.cc
Normal file
@ -0,0 +1,6 @@
|
||||
#include "tkc/crc.h"
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
TEST(crc, crc32_file) {
|
||||
ASSERT_EQ(tk_crc32_file("tests/testdata/test.png", 1024), 1567897941);
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user