ソースを参照

fix string size for packread

pikastech 2 年 前
コミット
938cf0efdc
2 ファイル変更27 行追加29 行削除
  1. 17 19
      src/PikaCompiler.c
  2. 10 10
      test/packtool-test.cpp

+ 17 - 19
src/PikaCompiler.c

@@ -467,6 +467,21 @@ 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;
+        /* fix size for string */
+        PIKA_BOOL bIsString = PIKA_TRUE;
+        for (size_t i = 0; i < *size - 1; ++i) {
+            if (bytecode_addr[i] == 0) {
+                bIsString = PIKA_FALSE;
+                break;
+            }
+        }
+        if (bIsString) {
+            /* remove the last '\0' for stirng */
+            if (bytecode_addr[*size - 1] == 0) {
+                *size -= 1;
+            }
+        }
+        /* next module */
         bytecode_addr += align_by(module_size, sizeof(uint32_t));
     }
     return PIKA_RES_OK;
@@ -616,28 +631,11 @@ PIKA_RES pikafs_unpack_files(char* pack_name, char* out_path) {
                                  &size);
         output_file_path = strsPathJoin(&buffs, out_path, name);
         new_fp = pika_platform_fopen(output_file_path, "wb+");
-        PIKA_BOOL bIsString = PIKA_TRUE;
-        for (size_t i = 0; i < size - 1; ++i) {
-            if (addr[i] == 0) {
-                bIsString = PIKA_FALSE;
-                break;
-            }
-        }
-        char* type = NULL;
-        if (bIsString) {
-            type = "string";
-            /* remove the last '\0' for stirng */
-            if (addr[size - 1] == 0) {
-                size -= 1;
-            }
-        } else {
-            type = "binary";
-        }
+
         if (NULL != new_fp) {
             pika_platform_fwrite(addr, size, 1, new_fp);
             pika_platform_fclose(new_fp);
-            pika_platform_printf("unpack %s(%s) to %s\r\n", name, type,
-                                 output_file_path);
+            pika_platform_printf("unpack %s to %s\r\n", name, output_file_path);
         } else {
             pika_platform_printf("can't open %s\r\n", output_file_path);
             break;

+ 10 - 10
test/packtool-test.cpp

@@ -12,27 +12,27 @@ TEST(packtool, pack_unpack) {
     EXPECT_EQ(res, PIKA_RES_OK);
 }
 
-#if 0  // TODO add a.pack
-
 TEST(packtool, packread) {
     size_t n = 0;
-    //Arg* fileArg = NULL;
-     pikafs_FILE* pack_file = pikafs_fopen_pack("test/assets/a.pack", "main.py");
-   // pikafs_FILE* pack_file = pikafs_fopen_pack_new(&fileArg, "test/assets/a.pack", "main.py");
-    FILE* file = pika_platform_fopen("test/out/unpackout/main2.py", "wb+");
+    // Arg* fileArg = NULL;
+    pikafs_pack_files("test/out/a2.pack", 3, "test/assets/test.txt",
+                      "test/assets/widget_config.ini", "test/assets/test.jpg");
+    pikafs_FILE* pack_file = pikafs_fopen_pack("test/out/a2.pack", "test.txt");
+    // pikafs_FILE* pack_file = pikafs_fopen_pack_new(&fileArg,
+    // "test/assets/a.pack", "main.py");
+    FILE* file = pika_platform_fopen("test/out/unpack/test2.txt", "wb+");
     if (NULL == file) {
-        pika_platform_printf("open file: %s error\r\n", "test/out/unpackout/main2.py");
+        pika_platform_printf("open file: %s error\r\n",
+                             "test/out/unpack/test2.txt");
     }
 
     n = pika_platform_fwrite(pack_file->addr, pack_file->size, 1, file);
     EXPECT_NE(n, 0);
 
-    //arg_deinit(fileArg);
+    // arg_deinit(fileArg);
     pikaFree(pack_file, sizeof(pikafs_FILE));
     pika_platform_fclose(file);
     pack_file = NULL;
-
 }
-#endif
 
 TEST_END