|
|
@@ -474,13 +474,13 @@ const struct luat_vfs_filesystem vfs_fs_luadb = {
|
|
|
#endif
|
|
|
|
|
|
int luat_luadb_checkfile(const char* path) {
|
|
|
- #define LUADB_HEAD_MAX_SIZE (128)
|
|
|
- uint8_t binbuff[LUADB_HEAD_MAX_SIZE];
|
|
|
- memset(binbuff, 0, LUADB_HEAD_MAX_SIZE);
|
|
|
+ size_t binsize = luat_fs_fsize(path);
|
|
|
+ uint8_t* binbuff = (uint8_t*)luat_heap_malloc(binsize * sizeof(uint8_t));
|
|
|
+ memset(binbuff, 0, binsize);
|
|
|
FILE * fd = luat_fs_fopen(path, "rb");
|
|
|
int res = -1;
|
|
|
if (fd) {
|
|
|
- luat_fs_fread(binbuff, sizeof(uint8_t), LUADB_HEAD_MAX_SIZE, fd);
|
|
|
+ luat_fs_fread(binbuff, sizeof(uint8_t), binsize, fd);
|
|
|
//做一下校验
|
|
|
if (binbuff[0] != 0x01 || binbuff[1] != 0x04 || binbuff[2]+(binbuff[3]<<8) != 0xA55A || binbuff[4]+(binbuff[5]<<8) != 0xA55A){
|
|
|
LLOGI("Magic error");
|
|
|
@@ -491,26 +491,102 @@ int luat_luadb_checkfile(const char* path) {
|
|
|
LLOGI("Version error");
|
|
|
goto _close;
|
|
|
}
|
|
|
- LLOGI("Version OK");
|
|
|
+ uint16_t version = binbuff[8]+(binbuff[9]<<8);
|
|
|
+ LLOGI("Version:%d",version);
|
|
|
if (binbuff[10] != 0x03 || binbuff[11] != 0x04){
|
|
|
LLOGI("Header error");
|
|
|
goto _close;
|
|
|
}
|
|
|
uint32_t headsize = binbuff[12]+(binbuff[13]<<8)+(binbuff[14]<<16)+(binbuff[15]<<24);
|
|
|
- LLOGI("Header OK headers:%08x",headsize);
|
|
|
+ LLOGI("headers:%08x",headsize);
|
|
|
if (binbuff[16] != 0x04 || binbuff[17] != 0x02){
|
|
|
LLOGI("file count error");
|
|
|
goto _close;
|
|
|
}
|
|
|
uint16_t filecount = binbuff[18]+(binbuff[19]<<8);
|
|
|
- LLOGI("file count:%04x",filecount);
|
|
|
+ LLOGI("file count:%d",filecount);
|
|
|
if (binbuff[20] != 0xFE || binbuff[21] != 0x02){
|
|
|
LLOGI("CRC16 error");
|
|
|
goto _close;
|
|
|
}
|
|
|
- // 计算md5, 然后校验MD5
|
|
|
- res = 0;
|
|
|
+ uint16_t crc16 = binbuff[22]+(binbuff[23]<<8);
|
|
|
+ int index = 24;
|
|
|
+ uint8_t type = 0;
|
|
|
+ uint32_t len = 0;
|
|
|
+ for (size_t i = 0; i < (int)filecount; i++){
|
|
|
+ LLOGD("LuaDB check files .... %d", i+1);
|
|
|
+ type = binbuff[index++];
|
|
|
+ len = binbuff[index++];
|
|
|
+ if (type != 1 || len != 4) {
|
|
|
+ LLOGD("bad file data 1 : %d %d %d", type, len, index);
|
|
|
+ goto _close;
|
|
|
+ }
|
|
|
+ index += 4;
|
|
|
+ // 2. 然后是名字
|
|
|
+ type = binbuff[index++];
|
|
|
+ len = binbuff[index++];
|
|
|
+ if (type != 2) {
|
|
|
+ LLOGD("bad file data 2 : %d %d %d", type, len, index);
|
|
|
+ goto _close;
|
|
|
+ }
|
|
|
+ // 拷贝文件名
|
|
|
+ LLOGD("LuaDB file name len = %d", len);
|
|
|
+ char test[10];
|
|
|
+ memcpy(test, &(binbuff[index]), len);
|
|
|
+ test[len] = 0x00;
|
|
|
+ index += len;
|
|
|
+ LLOGD("LuaDB file name %s", test);
|
|
|
+ if (strcmp(".airm2m_all_crc#.bin", test)==0){
|
|
|
+ LLOGD(".airm2m_all_crc#.bin");
|
|
|
+ // 3. 文件大小
|
|
|
+ type = binbuff[index++];
|
|
|
+ len = binbuff[index++];
|
|
|
+ if (type != 3 || len != 4) {
|
|
|
+ LLOGD("bad file data 3 : %d %d %d", type, len, index);
|
|
|
+ goto _close;
|
|
|
+ }
|
|
|
+ uint32_t fssize = binbuff[index]+(binbuff[index+1]<<8)+(binbuff[index+2]<<16)+(binbuff[index+3]<<24);
|
|
|
+ index+=4;
|
|
|
+ // 0xFE校验码
|
|
|
+ type = binbuff[index++];
|
|
|
+ len = binbuff[index++];
|
|
|
+ if (type != 0xFE || len != 2) {
|
|
|
+ LLOGD("bad file data 4 : %d %d %d", type, len, index);
|
|
|
+ goto _close;
|
|
|
+ }
|
|
|
+ index += len;
|
|
|
+ uint8_t md5data[16];
|
|
|
+ luat_crypto_md5_simple(binbuff, index, md5data);
|
|
|
+ for (size_t i = 0; i < 16; i++){
|
|
|
+ if (md5data[i]!=binbuff[index++]){
|
|
|
+ LLOGD("md5data error");
|
|
|
+ goto _close;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ res = 0;
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ // 3. 文件大小
|
|
|
+ type = binbuff[index++];
|
|
|
+ len = binbuff[index++];
|
|
|
+ if (type != 3 || len != 4) {
|
|
|
+ LLOGD("bad file data 3 : %d %d %d", type, len, index);
|
|
|
+ goto _close;
|
|
|
+ }
|
|
|
+ uint32_t fssize = binbuff[index]+(binbuff[index+1]<<8)+(binbuff[index+2]<<16)+(binbuff[index+3]<<24);
|
|
|
+ index+=4;
|
|
|
+ // 0xFE校验码
|
|
|
+ type = binbuff[index++];
|
|
|
+ len = binbuff[index++];
|
|
|
+ if (type != 0xFE || len != 2) {
|
|
|
+ LLOGD("bad file data 4 : %d %d %d", type, len, index);
|
|
|
+ goto _close;
|
|
|
+ }
|
|
|
+ index += len;
|
|
|
+ index += fssize;
|
|
|
+ }
|
|
|
_close:
|
|
|
+ luat_heap_free(binbuff);
|
|
|
luat_fs_fclose(fd);
|
|
|
}
|
|
|
return res;
|