Parcourir la source

fix align issue on unpack file

pikastech il y a 3 ans
Parent
commit
aeef23fc3b

+ 2 - 1
port/linux/.vscode/launch.json

@@ -12,7 +12,8 @@
             // "program": "${workspaceFolder}/build/boot/demo06-pikamain/pikascript_demo06-pikamain",
             "args": [
                 // "--gtest_filter=pikaui.*"
-                "--gtest_filter=doc.*"
+                // "--gtest_filter=doc.*"
+                "--gtest_filter=packtool.*"
             ],
             "stopAtEntry": false,
             "cwd": "${workspaceFolder}",

+ 42 - 9
src/PikaCompiler.c

@@ -319,7 +319,7 @@ static int32_t __foreach_handler_libWriteBytecode(Arg* argEach, void* context) {
         char* bytecode = obj_getPtr(module_obj, "bytecode");
         size_t bytecode_size = obj_getBytesSize(module_obj, "buff");
         size_t aline_size =
-            aline_by(bytecode_size, sizeof(uint32_t)) - bytecode_size;
+            align_by(bytecode_size, sizeof(uint32_t)) - bytecode_size;
         char aline_buff[sizeof(uint32_t)] = {0};
         pika_platform_fwrite(bytecode, 1, bytecode_size, out_file);
         pika_platform_fwrite(aline_buff, 1, aline_size, out_file);
@@ -327,6 +327,7 @@ static int32_t __foreach_handler_libWriteBytecode(Arg* argEach, void* context) {
     return 0;
 }
 
+#define NAME_BUFF_SIZE LIB_INFO_BLOCK_SIZE - sizeof(uint32_t)
 static int32_t __foreach_handler_libWriteIndex(Arg* argEach, void* context) {
     Args* args = context;
     FILE* out_file = args_getPtr(args, "out_file");
@@ -334,14 +335,13 @@ static int32_t __foreach_handler_libWriteIndex(Arg* argEach, void* context) {
     if (arg_isObject(argEach)) {
         PikaObj* module_obj = arg_getPtr(argEach);
         uint32_t bytecode_size = obj_getBytesSize(module_obj, "buff");
-        char buff[LIB_INFO_BLOCK_SIZE - sizeof(uint32_t)] = {0};
-        bytecode_size = aline_by(bytecode_size, sizeof(uint32_t));
+        char name_buff[NAME_BUFF_SIZE] = {0};
+        // bytecode_size = align_by(bytecode_size, sizeof(uint32_t));
         char* module_name = obj_getStr(module_obj, "name");
         module_name = strsReplace(&buffs, module_name, "|", ".");
         // pika_platform_printf("   %s:%d\r\n", module_name, bytecode_size);
-        pika_platform_memcpy(buff, module_name, strGetSize(module_name));
-        pika_platform_fwrite(
-            buff, 1, LIB_INFO_BLOCK_SIZE - sizeof(bytecode_size), out_file);
+        pika_platform_memcpy(name_buff, module_name, strGetSize(module_name));
+        pika_platform_fwrite(name_buff, 1, NAME_BUFF_SIZE, out_file);
         pika_platform_fwrite(&bytecode_size, 1, sizeof(bytecode_size),
                              out_file);
     }
@@ -354,7 +354,7 @@ static int32_t __foreach_handler_libSumSize(Arg* argEach, void* context) {
     if (arg_isObject(argEach)) {
         PikaObj* module_obj = arg_getPtr(argEach);
         uint32_t bytecode_size = obj_getBytesSize(module_obj, "buff");
-        bytecode_size = aline_by(bytecode_size, sizeof(uint32_t));
+        bytecode_size = align_by(bytecode_size, sizeof(uint32_t));
         args_setInt(args, "sum_size",
                     args_getInt(args, "sum_size") + bytecode_size);
     }
@@ -467,7 +467,7 @@ static PIKA_RES _loadModuleDataWithIndex(uint8_t* library_bytes,
         size_t module_size =
             *(uint32_t*)(module_name + LIB_INFO_BLOCK_SIZE - sizeof(uint32_t));
         *size = module_size;
-        bytecode_addr += module_size;
+        bytecode_addr += align_by(module_size, sizeof(uint32_t));
     }
     return PIKA_RES_OK;
 }
@@ -586,7 +586,7 @@ int LibObj_loadLibraryFile(LibObj* self, char* lib_file_name) {
  * @param out_path   output path
  * @return
  */
-PIKA_RES LibObj_unpackFileToPath(char* pack_name, char* out_path) {
+PIKA_RES pikafs_unpack_files(char* pack_name, char* out_path) {
     PIKA_RES stat = PIKA_RES_OK;
     Arg* file_arg = NULL;
     uint8_t* library_bytes = NULL;
@@ -616,6 +616,10 @@ PIKA_RES LibObj_unpackFileToPath(char* pack_name, char* out_path) {
                                  &size);
         output_file_path = strsPathJoin(&buffs, out_path, name);
         new_fp = pika_platform_fopen(output_file_path, "wb+");
+        /* remove the last '\0' for stirng */
+        if (addr[size - 1] == 0) {
+            size -= 1;
+        }
         if (NULL != new_fp) {
             pika_platform_fwrite(addr, size, 1, new_fp);
             pika_platform_fclose(new_fp);
@@ -633,6 +637,35 @@ PIKA_RES LibObj_unpackFileToPath(char* pack_name, char* out_path) {
     return PIKA_RES_OK;
 }
 
+/** @brief pack files to *.pack file
+ *
+ * @param pack_name the name of *.pack file
+ * @param ...       the name of files to pack
+ * @return         PIKA_RES_OK if success
+ */
+PIKA_RES pikafs_pack_files(char* pack_name, ...) {
+    PikaMaker* maker = New_PikaMaker();
+    PIKA_RES ret = PIKA_RES_OK;
+    va_list args;
+    va_start(args, pack_name);
+    char* file_name = va_arg(args, char*);
+    while (NULL != file_name) {
+        ret = pikaMaker_linkRaw(maker, file_name);
+        if (PIKA_RES_OK != ret) {
+            goto __exit;
+        }
+        file_name = va_arg(args, char*);
+    }
+    ret = pikaMaker_linkCompiledModulesFullPath(maker, pack_name);
+    if (PIKA_RES_OK != ret) {
+        goto __exit;
+    }
+__exit:
+    va_end(args);
+    pikaMaker_deinit(maker);
+    return ret;
+}
+
 size_t pika_fputs(char* str, FILE* fp) {
     size_t size = strGetSize(str);
     return pika_platform_fwrite(str, 1, size, fp);

+ 3 - 2
src/PikaCompiler.h

@@ -19,7 +19,6 @@ int LibObj_staticLinkFile(LibObj* self, char* input_file_name);
 void LibObj_listModules(LibObj* self);
 int LibObj_saveLibraryFile(LibObj* self, char* output_file_name);
 int LibObj_loadLibraryFile(LibObj* self, char* input_file_name);
-PIKA_RES LibObj_unpackFileToPath(char* pack_name, char* out_path);
 int Lib_loadLibraryFileToArray(char* origin_file_name, char* pikascript_root);
 PikaMaker* New_PikaMaker(void);
 void pikaMaker_setPWD(PikaMaker* self, char* pwd);
@@ -42,7 +41,7 @@ PIKA_RES _loadModuleDataWithName(uint8_t* library_bytes,
                                  uint8_t** addr_p,
                                  size_t* size_p);
 
-#define LIB_VERSION_NUMBER 3
+#define LIB_VERSION_NUMBER 4
 #define LIB_INFO_BLOCK_SIZE 32
 #define PIKA_APP_MAGIC_CODE_OFFSET 0
 #define PIKA_APP_MODULE_SIZE_OFFSET 1
@@ -60,5 +59,7 @@ pikafs_FILE* pikafs_fopen_pack(char* pack_name, char* file_name);
 int pikafs_fread(void* buf, size_t size, size_t count, pikafs_FILE* file);
 int pikafs_fwrite(void* buf, size_t size, size_t count, pikafs_FILE* file);
 int pikafs_fclose(pikafs_FILE* file);
+PIKA_RES pikafs_unpack_files(char* pack_name, char* out_path);
+PIKA_RES pikafs_pack_files(char* pack_name, ...);
 
 #endif

+ 1 - 1
src/dataArg.c

@@ -142,7 +142,7 @@ static Arg* _arg_set_hash(Arg* self,
         pika_platform_memcpy(arg_getContent(self), content, size);
     } else {
         pika_platform_memset(arg_getContent(self), 0,
-                             aline_by(size, sizeof(uint32_t)));
+                             align_by(size, sizeof(uint32_t)));
     }
     pika_assert(self->flag < ARG_FLAG_MAX);
     return self;

+ 1 - 1
src/dataMemory.h

@@ -66,7 +66,7 @@ struct Pool{
 };
 /* clang-format on */
 
-#define aline_by(size, aline) \
+#define align_by(size, aline) \
     (((size) == 0) ? 0 : (((size)-1) / (aline) + 1) * (aline))
 
 void pikaFree(void* mem, uint32_t size);

+ 0 - 0
test/out/unpack/path


+ 5 - 5
test/packtool-test.cpp

@@ -3,15 +3,15 @@ TEST_START
 
 #include "PikaCompiler.h"
 
-#if 0  // TODO add a.pack
-
-TEST(packtool, unpack) {
-
-    PIKA_RES res = LibObj_unpackFileToPath("test/assets/a.pack", "test/out/unpackout");
+TEST(packtool, pack_unpack) {
+    PIKA_RES res =
+        pikafs_pack_files("test/out/a.pack", "test/assets/test.txt", "test/assets/widget_config.ini");
+        pikafs_unpack_files("test/out/a.pack", "test/out/unpack");
 
     EXPECT_EQ(res, PIKA_RES_OK);
 }
 
+#if 0  // TODO add a.pack
 
 TEST(packtool, packread) {
     size_t n = 0;

BIN
tools/pikaCompiler/rust-msc-latest-win10.exe