Skip to content

Commit

Permalink
[awtk]: update
Browse files Browse the repository at this point in the history
  • Loading branch information
jiale-gdyd committed Feb 20, 2023
1 parent d2e5eb8 commit 177167f
Show file tree
Hide file tree
Showing 5 changed files with 109 additions and 13 deletions.
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
BSD 3-Clause License

Copyright (c) 2021-2022, lujiale
Copyright (c) 2021-2023, lujiale
All rights reserved.

Redistribution and use in source and binary forms, with or without
Expand All @@ -26,4 +26,4 @@ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

41 changes: 36 additions & 5 deletions project/gui/awtk/src/tkc/crc.c
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
/*
Copyright 2016 (C) Alexey Dynda
This file is part of Tiny Protocol Library.
Protocol Library is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Protocol Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with Protocol Library. If not, see <http://www.gnu.org/licenses/>.
*/
Expand Down Expand Up @@ -183,4 +179,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];
}
#endif

#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
10 changes: 10 additions & 0 deletions project/gui/awtk/src/tkc/crc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 50 additions & 6 deletions project/gui/awtk/src/tkc/sha256.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,9 +203,19 @@ ret_t tk_sha256_done(tk_sha256_t* sha256, uint8_t hash[TK_SHA256_HASH_LEN + 1])
return RET_OK;
}

ret_t tk_sha256(const void* data, uint32_t len, str_t* hash) {
static ret_t tk_sha256_hash_to_str(str_t* hash, uint8_t result[TK_SHA256_HASH_LEN + 1]) {
char text[32];
uint32_t i = 0;

for (i = 0; i < TK_SHA256_HASH_LEN; i++) {
tk_snprintf(text, sizeof(text), "%02x", (int)(result[i]));
str_append(hash, text);
}

return RET_OK;
}

ret_t tk_sha256(const void* data, uint32_t len, str_t* hash) {
tk_sha256_t sha256;
uint8_t result[TK_SHA256_HASH_LEN + 1];
return_value_if_fail(data != NULL && hash != NULL, RET_BAD_PARAMS);
Expand All @@ -214,10 +224,44 @@ ret_t tk_sha256(const void* data, uint32_t len, str_t* hash) {
tk_sha256_hash(&sha256, (const uint8_t*)data, len);
tk_sha256_done(&sha256, result);

for (i = 0; i < TK_SHA256_HASH_LEN; i++) {
tk_snprintf(text, sizeof(text), "%02x", (int)(result[i]));
str_append(hash, text);
return tk_sha256_hash_to_str(hash, result);
}

#include "tkc/fs.h"
#include "tkc/mem.h"

ret_t tk_sha256_file(const char* filename, uint32_t block_size, str_t* hash) {
int32_t size = 0;
ret_t ret = RET_FAIL;
fs_file_t* fp = NULL;
uint8_t* buff = NULL;

tk_sha256_t sha256;
uint8_t result[TK_SHA256_HASH_LEN + 1];
return_value_if_fail(filename != NULL && hash != NULL, RET_BAD_PARAMS);

fp = fs_open_file(os_fs(), filename, "rb");
return_value_if_fail(fp != NULL, RET_BAD_PARAMS);
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);

tk_sha256_init(&sha256);
while (!fs_file_eof(fp)) {
size = fs_file_read(fp, buff, block_size);
if (size <= 0) {
break;
}
tk_sha256_hash(&sha256, buff, size);
}
tk_sha256_done(&sha256, result);
ret = tk_sha256_hash_to_str(hash, result);

return RET_OK;
}
error:
fs_file_close(fp);
TKMEM_FREE(buff);

return ret;
}
11 changes: 11 additions & 0 deletions project/gui/awtk/src/tkc/sha256.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,17 @@ ret_t tk_sha256_done(tk_sha256_t* sha256, uint8_t hash[TK_SHA256_HASH_LEN + 1]);
*/
ret_t tk_sha256(const void* data, uint32_t len, str_t* hash);

/**
* @method tk_sha256_file
* 计算文件的sha256哈希值。
* @param {const char*} filename 文件名。
* @param {uint32_t} block_size 数据块长度。
* @param {str_t*} hash 用于返回计算结果。
*
* @return {ret_t} 返回RET_OK表示成功,否则表示失败。
*/
ret_t tk_sha256_file(const char* filename, uint32_t block_size, str_t* hash);

END_C_DECLS

#endif /*TK_SHA256_H*/

0 comments on commit 177167f

Please sign in to comment.