ResStringPool.cpp 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * Copyright (C) 2018 Hein-Pieter van Braam
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <stdlib.h>
  17. #include <string.h>
  18. #include <stdio.h>
  19. #include "ResStringPool.h"
  20. char* to_char_string(const char* data, uint32_t length) {
  21. char* string = (char*) calloc(length + 1, 1);
  22. for (int i = 0; i < length; ++i) {
  23. string[i] = *(data + (i * 2));
  24. }
  25. return string;
  26. }
  27. void to_wide_string(const char* string, uint16_t* dest) {
  28. uint32_t len = strlen(string);
  29. for (int i = 0; i < len; ++i) {
  30. dest[i] = string[i];
  31. }
  32. dest[len] = 0;
  33. }
  34. ResStringPool::ResStringPool() :
  35. _size(0), _strings(nullptr) {
  36. }
  37. ResStringPool::ResStringPool(const char* data) {
  38. ResStringPool_header header;
  39. uint32_t* entries;
  40. memcpy(&header, data, sizeof(ResStringPool_header));
  41. if (header.header.type != RES_STRING_POOL_TYPE) {
  42. printf("Not a ResStringPool!\n");
  43. return;
  44. }
  45. printf("ResStringPool stringCount: %i\n", header.stringCount);
  46. printf("ResStringPool styleCount: %i\n", header.styleCount);
  47. printf("ResStringPool stringsStart: %i\n", header.stringsStart);
  48. printf("ResStringPool flags: 0x%08X\n", header.flags);
  49. _size = header.stringCount;
  50. entries = (uint32_t*) (data + header.header.headerSize);
  51. _strings = (const char**)malloc(_size * sizeof(const char*));
  52. const char *stringdata = data + header.stringsStart;
  53. for (int i = 0; i < _size; ++i) {
  54. uint32_t offset = entries[i];
  55. size_t size = *((const uint16_t*)(stringdata + offset));
  56. if ((size & 0x8000) != 0) {
  57. offset += 2;
  58. size = ((size & 0x7FFF) << 16) | *((const uint16_t*)(stringdata + offset));
  59. }
  60. offset += 2;
  61. _strings[i] = to_char_string(stringdata + offset, size);
  62. }
  63. }
  64. const char* ResStringPool::get(int32_t index) const {
  65. if (index < 0 || index >= _size) {
  66. return nullptr;
  67. }
  68. return _strings[index];
  69. }
  70. ResStringPool_ref ResStringPool::put(const char* string) {
  71. ResStringPool_ref ref;
  72. if (! string) {
  73. printf("Empty string request\n");
  74. ref.index = -1;
  75. return ref;
  76. }
  77. for (int i = 0; i < _size; ++i) {
  78. if (strcmp(string, _strings[i]) == 0) {
  79. printf("Found string '%s' already returning %i\n", string, i);
  80. ref.index = i;
  81. return ref;
  82. }
  83. }
  84. ++_size;
  85. _strings = (const char **)realloc(_strings, _size * sizeof(const char**));
  86. _strings[_size - 1] = strdup(string);
  87. ref.index = _size - 1;
  88. printf("Adding new string : '%s' as %i\n", string, ref.index);
  89. return ref;
  90. }
  91. // TODO why is this necessary?
  92. void ResStringPool::put_empty() {
  93. printf("Putting empty string at %i\n", _size);
  94. ++_size;
  95. _strings = (const char **)realloc(_strings, _size * sizeof(const char**));
  96. _strings[_size - 1] = strdup("" );
  97. }
  98. uint32_t ResStringPool::serialize(char** data) {
  99. uint32_t datasize = sizeof(ResStringPool_header) + _size * sizeof(uint32_t);
  100. *data = (char*)malloc(datasize);
  101. uint32_t stringloc = 0;
  102. uint32_t stringsStart = datasize;
  103. for (int i = 0; i < _size; ++i) {
  104. uint32_t stringlen = strlen(_strings[i]);
  105. datasize += (stringlen * 2) + 4;
  106. *data = (char*)realloc(*data, datasize);
  107. uint16_t* stringdata = (uint16_t*)((char*)*data + stringsStart + stringloc);
  108. // TODO: Deal with longer strings, see the parser in the data constructor
  109. *stringdata = stringlen;
  110. to_wide_string(_strings[i], stringdata + 1);
  111. uint32_t* entries = (uint32_t*)((char*)*data + sizeof(ResStringPool_header));
  112. entries[i] = stringloc;
  113. stringloc += (stringlen * 2) + 4;
  114. }
  115. datasize = datasize + (datasize % 4);
  116. *data = (char*)realloc(*data, datasize);
  117. ResStringPool_header* header = (ResStringPool_header*)*data;
  118. header->stringCount = _size;
  119. header->stringsStart = stringsStart;
  120. header->flags = 0;
  121. header->styleCount = 0;
  122. header->stylesStart = 0;
  123. header->header.headerSize = sizeof(ResStringPool_header);
  124. header->header.type = RES_STRING_POOL_TYPE;
  125. header->header.size = datasize;
  126. return datasize;
  127. }
  128. void ResStringPool::dump() {
  129. for (int i = 0; i < _size; ++i) {
  130. printf("String %i: '%s'\n", i, _strings[i]);
  131. }
  132. }
  133. ResStringPool::~ResStringPool() {
  134. for (int i = 0; i < _size; ++i) {
  135. free((void*)_strings[i]);
  136. }
  137. free(_strings);
  138. }