Pārlūkot izejas kodu

feat: 添加字节码支持模块

工具模块添加字节码支持
添加了大小端字节码的支持
SongZihuan 3 gadi atpakaļ
vecāks
revīzija
44285c14ca
10 mainītis faili ar 282 papildinājumiem un 8 dzēšanām
  1. 1 0
      include/aFun.h
  2. 1 1
      include/cjson.h
  3. 5 4
      include/macro.h
  4. 30 1
      include/tool.h
  5. 1 1
      src/CMakeLists.txt
  6. 11 0
      src/core/init.c
  7. 2 0
      src/main.c
  8. 157 0
      src/tool/byte.c
  9. 2 1
      test/CMakeLists.txt
  10. 72 0
      test/test_byte.c

+ 1 - 0
include/aFun.h

@@ -6,4 +6,5 @@
 
 typedef struct af_ByteCode af_ByteCode;
 
+void aFunInit(void);
 #endif //AFUN__H

+ 1 - 1
include/cjson.h

@@ -1,6 +1,6 @@
 #ifndef CJSON__H
 #define CJSON__H
-
+#include "macro.h"
 #include "cJSON/cJSON.h"
 void cJsonInit();
 cJSON *parseJsonFile(FILE *file);

+ 5 - 4
include/macro.h

@@ -1,12 +1,13 @@
 /*
  * 文件名: macro.h
- * 目标: 定义公共宏
+ * 目标: 定义公共宏 和 公共头文件
  */
 
 #ifndef MACRO__H
 #define MACRO__H
 #include <stdbool.h>
 #include <inttypes.h>
+#include "mem.h"
 
 #ifndef __bool_true_false_are_defined
 #define bool int
@@ -17,9 +18,9 @@
 #define NUL ((char)0)
 #define W_NUL ((wchar_t)0)
 
-typedef int32_t FileLine;  // 文件航海
-typedef int8_t *FilePath;  // 文件路径
+typedef int32_t FileLine;  // 文件行号
+typedef char *FilePath;  // 文件路径
 
-typedef uint32_t ByteCodeUint;  // ByteCode int
+typedef unsigned int ByteCodeUint;  // ByteCode int
 
 #endif //MACRO__H

+ 30 - 1
include/tool.h

@@ -10,7 +10,6 @@
 #include <string.h>
 #include <signal.h>
 #include <dlfcn.h>
-
 #include "macro.h"
 
 // md5 工具
@@ -126,4 +125,34 @@ void freeSymbol_(struct DlcSymbol_ *symbol);
 bool freeLibary(struct DlcHandle *dlc);
 void dlcExit(void);
 
+// byte工具
+#define byteWriteInt_8(file, s) (byteWriteUint_8(file, ((uint8_t)(s))))
+#define byteWriteInt_16(file, s) (byteWriteUint_16(file, ((uint16_t)(s))))
+#define byteWriteInt_32(file, s) (byteWriteUint_32(file, ((uint32_t)(s))))
+#define byteWriteInt_64(file, s) (byteWriteUint_64(file, ((uint64_t)(s))))
+
+#define byteReadInt_8(file, s) (byteReadUint_8(file, ((uint8_t *)(s))))
+#define byteReadInt_16(file, s) (byteReadUint_16(file, ((uint16_t *)(s))))
+#define byteReadInt_32(file, s) (byteReadUint_32(file, ((uint32_t *)(s))))
+#define byteReadInt_64(file, s) (byteReadUint_64(file, ((uint64_t *)(s))))
+
+enum af_EndianType{
+    little_endian = 0,
+    big_endian
+};
+
+extern enum af_EndianType endian;
+
+void getEndian();
+bool byteWriteUint_8(FILE *file, uint8_t ch);
+bool byteWriteUint_16(FILE *file, uint16_t num);
+bool byteWriteUint_32(FILE *file, uint32_t num);
+bool byteWriteUint_64(FILE *file, uint64_t num);
+bool byteWriteStr(FILE *file, char *str);
+
+bool byteReadUint_8(FILE *file, uint8_t *ch);
+bool byteReadUint_16(FILE *file, uint16_t *num);
+bool byteReadUint_32(FILE *file, uint32_t *num);
+bool byteReadUint_64(FILE *file, uint64_t *num);
+bool byteReadStr(FILE *file, char **str);
 #endif //TOOL__H

+ 1 - 1
src/CMakeLists.txt

@@ -11,4 +11,4 @@ SET(libary af_tool af_json)
 ADD_SUBDIRECTORY(core)
 
 ADD_EXECUTABLE(aFun main.c)
-TARGET_LINK_LIBRARIES(aFun ${libary})
+TARGET_LINK_LIBRARIES(aFun ${libary} af_core)

+ 11 - 0
src/core/init.c

@@ -0,0 +1,11 @@
+/*
+ * 文件名: init.c
+ * 目标: 初始化函数
+ */
+
+#include "macro.h"
+#include "tool.h"
+
+void aFunInit(void) {
+    getEndian();
+}

+ 2 - 0
src/main.c

@@ -1,6 +1,8 @@
 #include <stdio.h>
+#include "aFun.h"
 
 int main() {
+    aFunInit();
     printf("Hello World\n");
     return 0;
 }

+ 157 - 0
src/tool/byte.c

@@ -0,0 +1,157 @@
+#include <inttypes.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include "tool.h"
+
+enum af_EndianType endian = little_endian;
+enum af_EndianType save_as = big_endian;  // 以小端序存储
+
+void getEndian() {
+    union {
+        int16_t a;//元素a,占2个字节
+        int8_t b;//元素b,占1个字节,b在内存中的地址为a最低字节的地址
+    } test = {.a = 0x1234};
+
+    if (test.b == 0x34)
+        endian = little_endian;
+    else if (test.b == 0x12)
+        endian = big_endian;
+    else
+        abort();
+}
+
+bool byteWriteUint_8(FILE *file, uint8_t ch) {
+    return fwrite(&ch, sizeof(uint8_t), 1, file) == 1;
+}
+
+bool byteWriteUint_16(FILE *file, uint16_t num) {
+    if (endian != save_as) {
+        union {
+            uint16_t a;//元素a,占2个字节
+            uint8_t b[2];//元素b,占1个字节,b在内存中的地址为a最低字节的地址
+        } in = {.a = num}, out;
+
+        out.b[1] = in.b[0];
+        out.b[0] = in.b[1];
+        num = out.a;
+    }
+
+    return fwrite(&num, sizeof(uint16_t), 1, file) == 1;
+}
+
+bool byteWriteUint_32(FILE *file, uint32_t num) {
+    if (endian != save_as) {
+        union {
+            uint32_t a;//元素a,占2个字节
+            uint8_t b[4];//元素b,占1个字节,b在内存中的地址为a最低字节的地址
+        } in = {.a = num}, out;
+
+        out.b[3] = in.b[0];
+        out.b[2] = in.b[1];
+        out.b[1] = in.b[2];
+        out.b[0] = in.b[3];
+        num = out.a;
+    }
+
+    return fwrite(&num, sizeof(uint32_t), 1, file) == 1;
+}
+
+bool byteWriteUint_64(FILE *file, uint64_t num) {
+    if (endian != save_as) {
+        union {
+            uint64_t a;//元素a,占2个字节
+            uint8_t b[8];//元素b,占1个字节,b在内存中的地址为a最低字节的地址
+        } in = {.a = num}, out;
+
+        out.b[7] = in.b[0];
+        out.b[6] = in.b[1];
+        out.b[5] = in.b[2];
+        out.b[4] = in.b[3];
+        out.b[3] = in.b[4];
+        out.b[2] = in.b[5];
+        out.b[1] = in.b[6];
+        out.b[0] = in.b[7];
+        num = out.a;
+    }
+
+    return fwrite(&num, sizeof(uint64_t), 1, file) == 1;
+}
+
+bool byteReadUint_8(FILE *file, uint8_t *ch) {
+    return fread(ch, sizeof(uint8_t), 1, file) == 1;
+}
+
+bool byteReadUint_16(FILE *file, uint16_t *num) {
+    size_t re = fread(num, sizeof(uint16_t), 1, file);
+
+    if (endian != save_as) {
+        union {
+            uint16_t a;//元素a,占2个字节
+            uint8_t b[2];//元素b,占1个字节,b在内存中的地址为a最低字节的地址
+        } in = {.a = *num}, out;
+
+        out.b[1] = in.b[0];
+        out.b[0] = in.b[1];
+        *num = out.a;
+    }
+
+    return re == 1;
+}
+
+bool byteReadUint_32(FILE *file, uint32_t *num) {
+    size_t re = fread(num, sizeof(uint32_t), 1, file);
+
+    if (endian != save_as) {
+        union {
+            uint32_t a;//元素a,占2个字节
+            uint8_t b[4];//元素b,占1个字节,b在内存中的地址为a最低字节的地址
+        } in = {.a = *num}, out;
+
+        out.b[3] = in.b[0];
+        out.b[2] = in.b[1];
+        out.b[1] = in.b[2];
+        out.b[0] = in.b[3];
+        *num = out.a;
+    }
+
+    return re == 1;
+}
+
+bool byteReadUint_64(FILE *file, uint64_t *num) {
+    size_t re = fread(num, sizeof(uint64_t), 1, file);
+
+    if (endian != save_as) {
+        union {
+            uint64_t a;//元素a,占2个字节
+            uint8_t b[8];//元素b,占1个字节,b在内存中的地址为a最低字节的地址
+        } in = {.a = *num}, out;
+
+        out.b[7] = in.b[0];
+        out.b[6] = in.b[1];
+        out.b[5] = in.b[2];
+        out.b[4] = in.b[3];
+        out.b[3] = in.b[4];
+        out.b[2] = in.b[5];
+        out.b[1] = in.b[6];
+        out.b[0] = in.b[7];
+        *num = out.a;
+    }
+
+    return re == 1;
+}
+
+bool byteWriteStr(FILE *file, char *str) {
+    if (!byteWriteUint_16(file, strlen(str)))
+        return false;
+    return fwrite(str, sizeof(char), strlen(str), file) == strlen(str);
+}
+
+bool byteReadStr(FILE *file, char **str) {
+    uint16_t len;
+    if (!byteReadUint_16(file, &len))
+        return false;
+
+    *str = calloc(len + 1, sizeof(char));
+    return fread(*str, sizeof(char), len, file) == len;
+}
+

+ 2 - 1
test/CMakeLists.txt

@@ -25,8 +25,9 @@ ADD_DEFINITIONS(-DLIB_TEST1="$<TARGET_FILE:lib_Test1>")  # 设置宏: lib的所
 ADD_aFunTest(mem test_mem.c)
 ADD_aFunTest(lib test_test_lib.c)
 ADD_aFunTest(dlc test_dlc.c)
+ADD_aFunTest(byte test_byte.c)
 
 SET_LINK(lib lib_Test1)  # 链接测试程序需要的动态库
 
-# SET_PASS(lib "num = 100 test = 110")
+SET_PASS(lib "num = 100 test = 110")
 SET_PASS(dlc "a = 100, test = 110")

+ 72 - 0
test/test_byte.c

@@ -0,0 +1,72 @@
+#include <stdio.h>
+#include "aFun.h"
+#include "tool.h"
+
+
+#define TEST_WRITE(test, type) do{\
+if (!(test)) { \
+    fprintf(stderr, "Test write wrong: " #type "\n"); \
+    return EXIT_FAILURE; \
+}}while(0)
+
+#define TEST_READ(test, type) do{\
+if (!(test)) { \
+fprintf(stderr, "Test read wrong: " #type "\n"); \
+return EXIT_FAILURE; \
+}}while(0)
+
+int main() {
+    int8_t test8 = 10;
+    int16_t test16 = 20;
+    int32_t test32 = 30;
+    int64_t test64 = 40;
+    char *testStr = "test";
+
+    getEndian();
+
+    FILE *file = fopen("test.byte", "wb");
+    if (file == NULL) {
+        fprintf(stderr, "Can't not creat file: test.byte\n");
+        return EXIT_FAILURE;
+    }
+
+    TEST_WRITE(byteWriteInt_8(file, test8), uint8_t);
+    TEST_WRITE(byteWriteInt_16(file, test16), uint16_t);
+    TEST_WRITE(byteWriteInt_32(file, test32), uint32_t);
+    TEST_WRITE(byteWriteInt_64(file, test64), uint64_t);
+    TEST_WRITE(byteWriteStr(file, testStr), str);
+
+    fclose(file);
+
+    int8_t rtest8;
+    int16_t rtest16;
+    int32_t rtest32;
+    int64_t rtest64;
+    char *rtestStr;
+
+    file = fopen("test.byte", "rb");
+    if (file == NULL) {
+        fprintf(stderr, "Can't not read file: test.byte\n");
+        return EXIT_FAILURE;
+    }
+
+    TEST_READ(byteReadInt_8(file, &rtest8), uint8_t);
+    TEST_READ(byteReadInt_16(file, &rtest16), uint16_t);
+    TEST_READ(byteReadInt_32(file, &rtest32), uint32_t);
+    TEST_READ(byteReadInt_64(file, &rtest64), uint64_t);
+    TEST_READ(byteReadStr(file, &rtestStr), str);
+
+    if (rtest8 != test8 || rtest16 != test16 || rtest32 != test32 || rtest64 != test64 || !EQ_STR(rtestStr, testStr)) {
+        printf("error.\n");
+        printf("rtest8 = %d , test = %d\n", rtest8, test8);
+        printf("rtest16 = %d, test = %d\n", rtest16, test16);
+        printf("rtest32 = %d, test = %d\n", rtest32, test32);
+        printf("rtest64 = %ld, test = %ld\n", rtest64, test64);
+        printf("rtestStr = %s\ntestStr = %s\n", rtestStr, testStr);
+        return EXIT_FAILURE;
+    }
+
+    free(rtestStr);
+    printf("success.\n");
+    return EXIT_SUCCESS;
+}