luat_luadb2.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. /*
  2. 本文件用于管理luadb的数据
  3. */
  4. #include "luat_base.h"
  5. #include "luat_fs.h"
  6. #include "luat_malloc.h"
  7. #include "luat_msgbus.h"
  8. #include "luat_luadb2.h"
  9. #include <stdlib.h>
  10. #include <string.h>//add for memset
  11. #define LUAT_LOG_TAG "luadb2"
  12. #include "luat_log.h"
  13. // 初始化上下文
  14. int luat_luadb2_init(luat_luadb2_ctx_t* ctx) {
  15. if (ctx->dataptr) {
  16. return 0;
  17. }
  18. ctx->dataptr = malloc(1024);
  19. ctx->size = 1024;
  20. ctx->offset = 0;
  21. char *tmp = ctx->dataptr;
  22. size_t offset = 0;
  23. // magic of luadb
  24. tmp[offset + 0] = 0x01;
  25. tmp[offset + 1] = 0x04;
  26. tmp[offset + 2] = 0x5A;
  27. tmp[offset + 3] = 0xA5;
  28. tmp[offset + 4] = 0x5A;
  29. tmp[offset + 5] = 0xA5;
  30. offset += 6;
  31. // version 0x00 0x02
  32. tmp[offset + 0] = 0x02;
  33. tmp[offset + 1] = 0x02;
  34. tmp[offset + 2] = 0x02;
  35. tmp[offset + 3] = 0x00;
  36. offset += 4;
  37. // headers total size
  38. tmp[offset + 0] = 0x03;
  39. tmp[offset + 1] = 0x04;
  40. tmp[offset + 2] = 0x00;
  41. tmp[offset + 3] = 0x00;
  42. tmp[offset + 4] = 0x00;
  43. tmp[offset + 5] = 0x18;
  44. offset += 6;
  45. // file count
  46. tmp[offset + 0] = 0x04;
  47. tmp[offset + 1] = 0x02;
  48. tmp[offset + 2] = 0x00;
  49. tmp[offset + 3] = 0x00;
  50. offset += 4;
  51. // crc
  52. tmp[offset + 0] = 0xFE;
  53. tmp[offset + 1] = 0x02;
  54. tmp[offset + 2] = 0x00;
  55. tmp[offset + 3] = 0x00;
  56. offset += 4;
  57. ctx->offset = offset;
  58. // LLOGD("初始化luadb2成功, 当前偏移量 %d", ctx->offset);
  59. return 0;
  60. }
  61. // 写入数据, 就是添加一个文件
  62. int luat_luadb2_write(luat_luadb2_ctx_t* ctx, const char* name, const char* data, size_t len) {
  63. if (!ctx->dataptr) {
  64. LLOGE("需要先初始化才能写入数据");
  65. return -1;
  66. }
  67. size_t offset = ctx->offset;
  68. char *tmp = realloc(ctx->dataptr, offset + len + 512);
  69. if (tmp == NULL)
  70. {
  71. LLOGE("内存不足, 无法新增文件");
  72. return -2;
  73. }
  74. // magic of file
  75. tmp[offset + 0] = 0x01;
  76. tmp[offset + 1] = 0x04;
  77. tmp[offset + 2] = 0x5A;
  78. tmp[offset + 3] = 0xA5;
  79. tmp[offset + 4] = 0x5A;
  80. tmp[offset + 5] = 0xA5;
  81. offset += 6;
  82. // name of file
  83. tmp[offset + 0] = 0x02;
  84. tmp[offset + 1] = (uint8_t)(strlen(name) & 0xFF);
  85. memcpy(tmp + offset + 2, name, strlen(name));
  86. offset += 2 + strlen(name);
  87. // len of file data
  88. tmp[offset + 0] = 0x03;
  89. tmp[offset + 1] = 0x04;
  90. tmp[offset + 2] = (len >> 0) & 0xFF;
  91. tmp[offset + 3] = (len >> 8) & 0xFF;
  92. tmp[offset + 4] = (len >> 16) & 0xFF;
  93. tmp[offset + 5] = (len >> 24) & 0xFF;
  94. offset += 6;
  95. // crc
  96. tmp[offset + 0] = 0xFE;
  97. tmp[offset + 1] = 0x02;
  98. tmp[offset + 2] = 0x00;
  99. tmp[offset + 3] = 0x00;
  100. offset += 4;
  101. memcpy(tmp + offset, data, len);
  102. offset += len;
  103. ctx->offset = offset;
  104. ctx->dataptr = tmp;
  105. // 调整文件数量, TODO 兼容256个以上的文件
  106. ctx->dataptr[0x12]++;
  107. // LLOGD("新增文件完成 文件名 %s, len=%d", name, len);
  108. // LLOGD("文件总数 %d", ctx->dataptr[0x12]);
  109. // LLOGD("指针偏移量 %d", ctx->offset);
  110. return 0;
  111. }
  112. // 读取数据, 就是删除一个文件
  113. int luat_luadb2_read(luat_luadb2_ctx_t* ctx, const char* key, char* data, size_t* len);