fragment_management_information.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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/fragment_management_information.h>
  23. struct esg_encapsulation_structure *esg_encapsulation_structure_decode(uint8_t *buffer, uint32_t size) {
  24. uint32_t pos;
  25. struct esg_encapsulation_structure *structure;
  26. struct esg_encapsulation_entry *entry;
  27. struct esg_encapsulation_entry *last_entry;
  28. if ((buffer == NULL) || (size <= 2)) {
  29. return NULL;
  30. }
  31. pos = 0;
  32. structure = (struct esg_encapsulation_structure *) malloc(sizeof(struct esg_encapsulation_structure));
  33. memset(structure, 0, sizeof(struct esg_encapsulation_structure));
  34. structure->entry_list = NULL;
  35. // Encapsulation header
  36. structure->header = (struct esg_encapsulation_header *) malloc(sizeof(struct esg_encapsulation_header));
  37. // buffer[pos] reserved
  38. structure->header->fragment_reference_format = buffer[pos+1];
  39. pos += 2;
  40. // Encapsulation entry list
  41. last_entry = NULL;
  42. while (size > pos) {
  43. entry = (struct esg_encapsulation_entry *) malloc(sizeof(struct esg_encapsulation_entry));
  44. memset(entry, 0, sizeof(struct esg_encapsulation_entry));
  45. entry->_next = NULL;
  46. if (last_entry == NULL) {
  47. structure->entry_list = entry;
  48. } else {
  49. last_entry->_next = entry;
  50. }
  51. last_entry = entry;
  52. // Fragment reference
  53. switch (structure->header->fragment_reference_format) {
  54. case 0x21: {
  55. entry->fragment_reference = (struct esg_fragment_reference *) malloc(sizeof(struct esg_fragment_reference));
  56. memset(entry->fragment_reference, 0, sizeof(struct esg_fragment_reference));
  57. entry->fragment_reference->fragment_type = buffer[pos];
  58. pos += 1;
  59. entry->fragment_reference->data_repository_offset = (buffer[pos] << 16) | (buffer[pos+1] << 8) | buffer[pos+2];
  60. pos += 3;
  61. break;
  62. }
  63. default: {
  64. esg_encapsulation_structure_free(structure);
  65. return NULL;
  66. }
  67. }
  68. // Fragment version & id
  69. entry->fragment_version = buffer[pos];
  70. pos += 1;
  71. entry->fragment_id = (buffer[pos] << 16) | (buffer[pos+1] << 8) | buffer[pos+2];
  72. pos += 3;
  73. }
  74. return structure;
  75. }
  76. void esg_encapsulation_structure_free(struct esg_encapsulation_structure *structure) {
  77. struct esg_encapsulation_entry *entry;
  78. struct esg_encapsulation_entry *next_entry;
  79. if (structure == NULL) {
  80. return;
  81. }
  82. if (structure->header) {
  83. free(structure->header);
  84. }
  85. if (structure->entry_list) {
  86. for(entry = structure->entry_list; entry; entry = next_entry) {
  87. next_entry = entry->_next;
  88. if (entry->fragment_reference) {
  89. free(entry->fragment_reference);
  90. }
  91. free(entry);
  92. }
  93. free(structure->entry_list);
  94. }
  95. free(structure);
  96. }