calypso_util.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. #include <stdlib.h>
  2. #include <string.h>
  3. #include "calypso_util.h"
  4. CalypsoElement make_calypso_final_element(
  5. const char* key,
  6. int size,
  7. const char* label,
  8. CalypsoFinalType final_type) {
  9. CalypsoElement final_element = {};
  10. final_element.type = CALYPSO_ELEMENT_TYPE_FINAL;
  11. final_element.final = malloc(sizeof(CalypsoFinalElement));
  12. final_element.final->size = size;
  13. final_element.final->final_type = final_type;
  14. strncpy(final_element.final->key, key, 64);
  15. strncpy(final_element.final->label, label, 64);
  16. return final_element;
  17. }
  18. CalypsoElement make_calypso_bitmap_element(const char* key, int size, CalypsoElement* elements) {
  19. CalypsoElement bitmap_element = {};
  20. bitmap_element.type = CALYPSO_ELEMENT_TYPE_BITMAP;
  21. bitmap_element.bitmap = malloc(sizeof(CalypsoBitmapElement));
  22. bitmap_element.bitmap->size = size;
  23. bitmap_element.bitmap->elements = malloc(size * sizeof(CalypsoElement));
  24. for(int i = 0; i < size; i++) {
  25. bitmap_element.bitmap->elements[i] = elements[i];
  26. }
  27. strncpy(bitmap_element.bitmap->key, key, 64);
  28. return bitmap_element;
  29. }
  30. void free_calypso_element(CalypsoElement* element) {
  31. if(element->type == CALYPSO_ELEMENT_TYPE_FINAL) {
  32. free(element->final);
  33. } else {
  34. for(int i = 0; i < element->bitmap->size; i++) {
  35. free_calypso_element(&element->bitmap->elements[i]);
  36. }
  37. free(element->bitmap->elements);
  38. free(element->bitmap);
  39. }
  40. }
  41. void free_calypso_structure(CalypsoApp* structure) {
  42. for(int i = 0; i < structure->elements_size; i++) {
  43. free_calypso_element(&structure->elements[i]);
  44. }
  45. free(structure->elements);
  46. free(structure);
  47. }
  48. int* get_bit_positions(const char* binary_string, int* count) {
  49. int length = strlen(binary_string);
  50. int* positions = malloc(length * sizeof(int));
  51. int pos_index = 0;
  52. for(int i = 0; i < length; i++) {
  53. if(binary_string[length - 1 - i] == '1') {
  54. positions[pos_index++] = i;
  55. }
  56. }
  57. *count = pos_index;
  58. return positions;
  59. }
  60. int is_bit_present(int* positions, int count, int bit) {
  61. for(int i = 0; i < count; i++) {
  62. if(positions[i] == bit) {
  63. return 1;
  64. }
  65. }
  66. return 0;
  67. }
  68. bool is_calypso_subnode_present(
  69. const char* binary_string,
  70. const char* key,
  71. CalypsoBitmapElement* bitmap) {
  72. char bit_slice[bitmap->size + 1];
  73. strncpy(bit_slice, binary_string, bitmap->size);
  74. bit_slice[bitmap->size] = '\0';
  75. int count = 0;
  76. int* positions = get_bit_positions(bit_slice, &count);
  77. int offset = bitmap->size;
  78. for(int i = 0; i < count; i++) {
  79. CalypsoElement* element = &bitmap->elements[positions[i]];
  80. if(element->type == CALYPSO_ELEMENT_TYPE_FINAL) {
  81. if(strcmp(element->final->key, key) == 0) {
  82. return true;
  83. }
  84. offset += element->final->size;
  85. } else {
  86. if(strcmp(element->bitmap->key, key) == 0) {
  87. return true;
  88. }
  89. int sub_binary_string_size = element->bitmap->size;
  90. char bit_slice[sub_binary_string_size + 1];
  91. strncpy(bit_slice, binary_string, sub_binary_string_size);
  92. bit_slice[sub_binary_string_size] = '\0';
  93. if(is_calypso_subnode_present(binary_string + offset, key, element->bitmap)) {
  94. return true;
  95. }
  96. offset += element->bitmap->size;
  97. }
  98. }
  99. return false;
  100. }
  101. bool is_calypso_node_present(const char* binary_string, const char* key, CalypsoApp* structure) {
  102. int offset = 0;
  103. for(int i = 0; i < structure->elements_size; i++) {
  104. if(structure->elements[i].type == CALYPSO_ELEMENT_TYPE_FINAL) {
  105. if(strcmp(structure->elements[i].final->key, key) == 0) {
  106. return true;
  107. }
  108. offset += structure->elements[i].final->size;
  109. } else {
  110. if(strcmp(structure->elements[i].bitmap->key, key) == 0) {
  111. return true;
  112. }
  113. int sub_binary_string_size = structure->elements[i].bitmap->size;
  114. char bit_slice[sub_binary_string_size + 1];
  115. strncpy(bit_slice, binary_string, sub_binary_string_size);
  116. bit_slice[sub_binary_string_size] = '\0';
  117. if(is_calypso_subnode_present(
  118. binary_string + offset, key, structure->elements[i].bitmap)) {
  119. return true;
  120. }
  121. offset += structure->elements[i].bitmap->size;
  122. }
  123. }
  124. return false;
  125. }
  126. int get_calypso_subnode_offset(
  127. const char* binary_string,
  128. const char* key,
  129. CalypsoBitmapElement* bitmap,
  130. bool* found) {
  131. char bit_slice[bitmap->size + 1];
  132. strncpy(bit_slice, binary_string, bitmap->size);
  133. bit_slice[bitmap->size] = '\0';
  134. int count = 0;
  135. int* positions = get_bit_positions(bit_slice, &count);
  136. int count_offset = bitmap->size;
  137. for(int i = 0; i < count; i++) {
  138. CalypsoElement element = bitmap->elements[positions[i]];
  139. if(element.type == CALYPSO_ELEMENT_TYPE_FINAL) {
  140. if(strcmp(element.final->key, key) == 0) {
  141. *found = true;
  142. free(positions);
  143. return count_offset;
  144. }
  145. count_offset += element.final->size;
  146. } else {
  147. if(strcmp(element.bitmap->key, key) == 0) {
  148. *found = true;
  149. free(positions);
  150. return count_offset;
  151. }
  152. count_offset += get_calypso_subnode_offset(
  153. binary_string + count_offset, key, element.bitmap, found);
  154. if(*found) {
  155. free(positions);
  156. return count_offset;
  157. }
  158. }
  159. }
  160. free(positions);
  161. return count_offset;
  162. }
  163. int get_calypso_node_offset(const char* binary_string, const char* key, CalypsoApp* structure) {
  164. int count = 0;
  165. bool found = false;
  166. for(int i = 0; i < structure->elements_size; i++) {
  167. if(structure->elements[i].type == CALYPSO_ELEMENT_TYPE_FINAL) {
  168. if(strcmp(structure->elements[i].final->key, key) == 0) {
  169. return count;
  170. }
  171. count += structure->elements[i].final->size;
  172. } else {
  173. if(strcmp(structure->elements[i].bitmap->key, key) == 0) {
  174. return count;
  175. }
  176. int sub_binary_string_size = structure->elements[i].bitmap->size;
  177. char bit_slice[sub_binary_string_size + 1];
  178. strncpy(bit_slice, binary_string + count, sub_binary_string_size);
  179. bit_slice[sub_binary_string_size] = '\0';
  180. count += get_calypso_subnode_offset(
  181. binary_string + count, key, structure->elements[i].bitmap, &found);
  182. if(found) {
  183. return count;
  184. }
  185. }
  186. }
  187. return 0;
  188. }
  189. int get_calypso_subnode_size(const char* key, CalypsoElement* element) {
  190. if(element->type == CALYPSO_ELEMENT_TYPE_FINAL) {
  191. if(strcmp(element->final->key, key) == 0) {
  192. return element->final->size;
  193. }
  194. } else {
  195. if(strcmp(element->bitmap->key, key) == 0) {
  196. return element->bitmap->size;
  197. }
  198. for(int i = 0; i < element->bitmap->size; i++) {
  199. int size = get_calypso_subnode_size(key, &element->bitmap->elements[i]);
  200. if(size != 0) {
  201. return size;
  202. }
  203. }
  204. }
  205. return 0;
  206. }
  207. int get_calypso_node_size(const char* key, CalypsoApp* structure) {
  208. for(int i = 0; i < structure->elements_size; i++) {
  209. if(structure->elements[i].type == CALYPSO_ELEMENT_TYPE_FINAL) {
  210. if(strcmp(structure->elements[i].final->key, key) == 0) {
  211. return structure->elements[i].final->size;
  212. }
  213. } else {
  214. if(strcmp(structure->elements[i].bitmap->key, key) == 0) {
  215. return structure->elements[i].bitmap->size;
  216. }
  217. for(int j = 0; j < structure->elements[i].bitmap->size; j++) {
  218. int size =
  219. get_calypso_subnode_size(key, &structure->elements[i].bitmap->elements[j]);
  220. if(size != 0) {
  221. return size;
  222. }
  223. }
  224. }
  225. }
  226. return 0;
  227. }