calypso_util.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  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, 36);
  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, 36);
  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. free(positions);
  83. return true;
  84. }
  85. offset += element->final->size;
  86. } else {
  87. if(strcmp(element->bitmap->key, key) == 0) {
  88. free(positions);
  89. return true;
  90. }
  91. int sub_binary_string_size = element->bitmap->size;
  92. char bit_slice[sub_binary_string_size + 1];
  93. strncpy(bit_slice, binary_string, sub_binary_string_size);
  94. bit_slice[sub_binary_string_size] = '\0';
  95. if(is_calypso_subnode_present(binary_string + offset, key, element->bitmap)) {
  96. free(positions);
  97. return true;
  98. }
  99. offset += element->bitmap->size;
  100. }
  101. }
  102. free(positions);
  103. return false;
  104. }
  105. bool is_calypso_node_present(const char* binary_string, const char* key, CalypsoApp* structure) {
  106. int offset = 0;
  107. for(int i = 0; i < structure->elements_size; i++) {
  108. if(structure->elements[i].type == CALYPSO_ELEMENT_TYPE_FINAL) {
  109. if(strcmp(structure->elements[i].final->key, key) == 0) {
  110. return true;
  111. }
  112. offset += structure->elements[i].final->size;
  113. } else {
  114. if(strcmp(structure->elements[i].bitmap->key, key) == 0) {
  115. return true;
  116. }
  117. int sub_binary_string_size = structure->elements[i].bitmap->size;
  118. char bit_slice[sub_binary_string_size + 1];
  119. strncpy(bit_slice, binary_string, sub_binary_string_size);
  120. bit_slice[sub_binary_string_size] = '\0';
  121. if(is_calypso_subnode_present(
  122. binary_string + offset, key, structure->elements[i].bitmap)) {
  123. return true;
  124. }
  125. offset += structure->elements[i].bitmap->size;
  126. }
  127. }
  128. return false;
  129. }
  130. int get_calypso_subnode_offset(
  131. const char* binary_string,
  132. const char* key,
  133. CalypsoBitmapElement* bitmap,
  134. bool* found) {
  135. char bit_slice[bitmap->size + 1];
  136. strncpy(bit_slice, binary_string, bitmap->size);
  137. bit_slice[bitmap->size] = '\0';
  138. int count = 0;
  139. int* positions = get_bit_positions(bit_slice, &count);
  140. int count_offset = bitmap->size;
  141. for(int i = 0; i < count; i++) {
  142. CalypsoElement element = bitmap->elements[positions[i]];
  143. if(element.type == CALYPSO_ELEMENT_TYPE_FINAL) {
  144. if(strcmp(element.final->key, key) == 0) {
  145. *found = true;
  146. free(positions);
  147. return count_offset;
  148. }
  149. count_offset += element.final->size;
  150. } else {
  151. if(strcmp(element.bitmap->key, key) == 0) {
  152. *found = true;
  153. free(positions);
  154. return count_offset;
  155. }
  156. count_offset += get_calypso_subnode_offset(
  157. binary_string + count_offset, key, element.bitmap, found);
  158. if(*found) {
  159. free(positions);
  160. return count_offset;
  161. }
  162. }
  163. }
  164. free(positions);
  165. return count_offset;
  166. }
  167. int get_calypso_node_offset(const char* binary_string, const char* key, CalypsoApp* structure) {
  168. int count = 0;
  169. bool found = false;
  170. for(int i = 0; i < structure->elements_size; i++) {
  171. if(structure->elements[i].type == CALYPSO_ELEMENT_TYPE_FINAL) {
  172. if(strcmp(structure->elements[i].final->key, key) == 0) {
  173. return count;
  174. }
  175. count += structure->elements[i].final->size;
  176. } else {
  177. if(strcmp(structure->elements[i].bitmap->key, key) == 0) {
  178. return count;
  179. }
  180. int sub_binary_string_size = structure->elements[i].bitmap->size;
  181. char bit_slice[sub_binary_string_size + 1];
  182. strncpy(bit_slice, binary_string + count, sub_binary_string_size);
  183. bit_slice[sub_binary_string_size] = '\0';
  184. count += get_calypso_subnode_offset(
  185. binary_string + count, key, structure->elements[i].bitmap, &found);
  186. if(found) {
  187. return count;
  188. }
  189. }
  190. }
  191. return 0;
  192. }
  193. int get_calypso_subnode_size(const char* key, CalypsoElement* element) {
  194. if(element->type == CALYPSO_ELEMENT_TYPE_FINAL) {
  195. if(strcmp(element->final->key, key) == 0) {
  196. return element->final->size;
  197. }
  198. } else {
  199. if(strcmp(element->bitmap->key, key) == 0) {
  200. return element->bitmap->size;
  201. }
  202. for(int i = 0; i < element->bitmap->size; i++) {
  203. int size = get_calypso_subnode_size(key, &element->bitmap->elements[i]);
  204. if(size != 0) {
  205. return size;
  206. }
  207. }
  208. }
  209. return 0;
  210. }
  211. int get_calypso_node_size(const char* key, CalypsoApp* structure) {
  212. for(int i = 0; i < structure->elements_size; i++) {
  213. if(structure->elements[i].type == CALYPSO_ELEMENT_TYPE_FINAL) {
  214. if(strcmp(structure->elements[i].final->key, key) == 0) {
  215. return structure->elements[i].final->size;
  216. }
  217. } else {
  218. if(strcmp(structure->elements[i].bitmap->key, key) == 0) {
  219. return structure->elements[i].bitmap->size;
  220. }
  221. for(int j = 0; j < structure->elements[i].bitmap->size; j++) {
  222. int size =
  223. get_calypso_subnode_size(key, &structure->elements[i].bitmap->elements[j]);
  224. if(size != 0) {
  225. return size;
  226. }
  227. }
  228. }
  229. }
  230. return 0;
  231. }