container.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. /*
  2. * ESG parser
  3. *
  4. * Copyright (C) 2006 Stephane Este-Gracias (sestegra@free.fr)
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 2.1 of the License, or (at your option) any later version.
  10. *
  11. * This library is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public
  17. * License along with this library; if not, write to the Free Software
  18. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. */
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <libesg/encapsulation/container.h>
  23. #include <libesg/encapsulation/fragment_management_information.h>
  24. #include <libesg/encapsulation/data_repository.h>
  25. #include <libesg/encapsulation/string_repository.h>
  26. #include <libesg/representation/init_message.h>
  27. #include <libesg/transport/session_partition_declaration.h>
  28. struct esg_container *esg_container_decode(uint8_t *buffer, uint32_t size) {
  29. uint32_t pos;
  30. struct esg_container *container;
  31. struct esg_container_structure *structure;
  32. struct esg_container_structure *last_structure;
  33. uint8_t structure_index;
  34. if ((buffer == NULL) || (size <= 1)) {
  35. return NULL;
  36. }
  37. pos = 0;
  38. container = (struct esg_container *) malloc(sizeof(struct esg_container));
  39. memset(container, 0, sizeof(struct esg_container));
  40. // Container header
  41. container->header = (struct esg_container_header *) malloc(sizeof(struct esg_container_header));
  42. memset(container->header, 0, sizeof(struct esg_container_header));
  43. container->header->num_structures = buffer[pos];
  44. pos += 1;
  45. if (size < pos + (container->header->num_structures * 8)) {
  46. esg_container_free(container);
  47. return NULL;
  48. }
  49. last_structure = NULL;
  50. for (structure_index = 0; structure_index < container->header->num_structures; structure_index++) {
  51. structure = (struct esg_container_structure *) malloc(sizeof(struct esg_container_structure));
  52. memset(structure, 0, sizeof(struct esg_container_structure));
  53. structure->_next = NULL;
  54. if (last_structure == NULL) {
  55. container->header->structure_list = structure;
  56. } else {
  57. last_structure->_next = structure;
  58. }
  59. last_structure = structure;
  60. structure->type = buffer[pos];
  61. pos += 1;
  62. structure->id = buffer[pos];
  63. pos += 1;
  64. structure->ptr = (buffer[pos] << 16) | (buffer[pos+1] << 8) | buffer[pos+2];
  65. pos += 3;
  66. structure->length = (buffer[pos] << 16) | (buffer[pos+1] << 8) | buffer[pos+2];
  67. pos += 3;
  68. if (size < (structure->ptr + structure->length)) {
  69. esg_container_free(container);
  70. return NULL;
  71. }
  72. // Decode structure
  73. switch (structure->type) {
  74. case 0x01: {
  75. switch (structure->id) {
  76. case 0x00: {
  77. structure->data = (void *) esg_encapsulation_structure_decode(buffer + structure->ptr, structure->length);
  78. break;
  79. }
  80. default: {
  81. esg_container_free(container);
  82. return NULL;
  83. }
  84. }
  85. break;
  86. }
  87. case 0x02: {
  88. switch (structure->id) {
  89. case 0x00: {
  90. structure->data = (void *) esg_string_repository_decode(buffer + structure->ptr, structure->length);
  91. break;
  92. }
  93. default: {
  94. esg_container_free(container);
  95. return NULL;
  96. }
  97. }
  98. break;
  99. }
  100. case 0x03: {
  101. //TODO
  102. break;
  103. }
  104. case 0x04: {
  105. //TODO
  106. break;
  107. }
  108. case 0x05: {
  109. //TODO
  110. break;
  111. }
  112. case 0xE0: {
  113. switch (structure->id) {
  114. case 0x00: {
  115. structure->data = (void *) esg_data_repository_decode(buffer + structure->ptr, structure->length);
  116. break;
  117. }
  118. default: {
  119. esg_container_free(container);
  120. return NULL;
  121. }
  122. }
  123. break;
  124. }
  125. case 0xE1: {
  126. switch (structure->id) {
  127. case 0xFF: {
  128. structure->data = (void *) esg_session_partition_declaration_decode(buffer + structure->ptr, structure->length);
  129. break;
  130. }
  131. default: {
  132. esg_container_free(container);
  133. return NULL;
  134. }
  135. }
  136. break;
  137. }
  138. case 0xE2: {
  139. switch (structure->id) {
  140. case 0x00: {
  141. structure->data = (void *) esg_init_message_decode(buffer + structure->ptr, structure->length);
  142. break;
  143. }
  144. default: {
  145. esg_container_free(container);
  146. return NULL;
  147. }
  148. }
  149. break;
  150. }
  151. default: {
  152. esg_container_free(container);
  153. return NULL;
  154. }
  155. }
  156. }
  157. // Container structure body
  158. container->structure_body_ptr = pos;
  159. container->structure_body_length = size - pos;
  160. container->structure_body = (uint8_t *) malloc(size - pos);
  161. memcpy(container->structure_body, buffer + pos, size - pos);
  162. return container;
  163. }
  164. void esg_container_free(struct esg_container *container) {
  165. struct esg_container_structure *structure;
  166. struct esg_container_structure *next_structure;
  167. if (container == NULL) {
  168. return;
  169. }
  170. if (container->header) {
  171. for(structure = container->header->structure_list; structure; structure = next_structure) {
  172. next_structure = structure->_next;
  173. free(structure);
  174. }
  175. free(container->header);
  176. }
  177. if (container->structure_body) {
  178. free(container->structure_body);
  179. }
  180. free(container);
  181. }