section.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. /*
  2. * section and descriptor parser
  3. *
  4. * Copyright (C) 2005 Kenneth Aafloy (kenneth@linuxtv.org)
  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. #ifndef _UCSI_SECTION_H
  21. #define _UCSI_SECTION_H 1
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. #include <libucsi/endianops.h>
  27. #include <libucsi/descriptor.h>
  28. #include <libucsi/crc32.h>
  29. #include <stdint.h>
  30. #include <string.h>
  31. #define CRC_SIZE 4
  32. /**
  33. * Generic section header.
  34. */
  35. struct section {
  36. uint8_t table_id;
  37. EBIT4(uint16_t syntax_indicator : 1; ,
  38. uint16_t private_indicator : 1; , /* 2.4.4.10 */
  39. uint16_t reserved : 2; ,
  40. uint16_t length :12; );
  41. } __ucsi_packed;
  42. /**
  43. * Generic extended section header structure.
  44. */
  45. struct section_ext {
  46. uint8_t table_id;
  47. EBIT4(uint16_t syntax_indicator : 1; ,
  48. uint16_t private_indicator : 1; , /* 2.4.4.10 */
  49. uint16_t reserved : 2; ,
  50. uint16_t length :12; );
  51. uint16_t table_id_ext;
  52. EBIT3(uint8_t reserved1 : 2; ,
  53. uint8_t version_number : 5; ,
  54. uint8_t current_next_indicator : 1; );
  55. uint8_t section_number;
  56. uint8_t last_section_number;
  57. } __ucsi_packed;
  58. /**
  59. * Structure for keeping track of sections of a PSI table.
  60. */
  61. struct psi_table_state {
  62. uint8_t version_number;
  63. uint16_t next_section_number;
  64. uint8_t complete:1;
  65. uint8_t new_table:1;
  66. } __ucsi_packed;
  67. /**
  68. * Determine the total length of a section, including the header.
  69. *
  70. * @param section The parsed section structure.
  71. * @return The length.
  72. */
  73. static inline size_t section_length(struct section *section)
  74. {
  75. return section->length + sizeof(struct section);
  76. }
  77. /**
  78. * Determine the total length of an extended section, including the header,
  79. * but omitting the CRC.
  80. *
  81. * @param section The parsed section_ext structure.
  82. * @return The length.
  83. */
  84. static inline size_t section_ext_length(struct section_ext * section)
  85. {
  86. return section->length + sizeof(struct section) - CRC_SIZE;
  87. }
  88. /**
  89. * Process a section structure in-place.
  90. *
  91. * @param buf Pointer to the data.
  92. * @param len Length of data.
  93. * @return Pointer to the section structure, or NULL if invalid.
  94. */
  95. static inline struct section * section_codec(uint8_t * buf, size_t len)
  96. {
  97. struct section * ret = (struct section *)buf;
  98. if (len < 3)
  99. return NULL;
  100. bswap16(buf+1);
  101. if (len != ret->length + 3U)
  102. return NULL;
  103. return ret;
  104. }
  105. /**
  106. * Some sections have a CRC even though they are not section_exts.
  107. * This function is to allow checking of them.
  108. *
  109. * @param section Pointer to the processed section structure.
  110. * @return Nonzero on error, or 0 if the CRC was correct.
  111. */
  112. static inline int section_check_crc(struct section *section)
  113. {
  114. uint8_t * buf = (uint8_t *) section;
  115. size_t len = section_length(section);
  116. uint32_t crc;
  117. /* the crc check has to be performed on the unswapped data */
  118. bswap16(buf+1);
  119. crc = crc32(CRC32_INIT, buf, len);
  120. bswap16(buf+1);
  121. /* the crc check includes the crc value,
  122. * the result should therefore be zero.
  123. */
  124. if (crc)
  125. return -1;
  126. return 0;
  127. }
  128. /**
  129. * Decode an extended section structure.
  130. *
  131. * @param section Pointer to the processed section structure.
  132. * @param check_crc If 1, the CRC of the section will also be checked.
  133. * @return Pointer to the parsed section_ext structure, or NULL if invalid.
  134. */
  135. static inline struct section_ext * section_ext_decode(struct section * section,
  136. int check_crc)
  137. {
  138. if (section->syntax_indicator == 0)
  139. return NULL;
  140. if (check_crc) {
  141. if (section_check_crc(section))
  142. return NULL;
  143. }
  144. bswap16((uint8_t *)section + sizeof(struct section));
  145. return (struct section_ext *)section;
  146. }
  147. /**
  148. * Encode an extended section structure for transmission.
  149. *
  150. * @param section Pointer to the section_ext structure.
  151. * @param update_crc If 1, the CRC of the section will also be updated.
  152. * @return Pointer to the encoded section_ext structure, or NULL if invalid.
  153. */
  154. static inline struct section_ext * section_ext_encode(struct section_ext* section,
  155. int update_crc)
  156. {
  157. if (section->syntax_indicator == 0)
  158. return NULL;
  159. bswap16((uint8_t *)section + sizeof(struct section));
  160. if (update_crc) {
  161. uint8_t * buf = (uint8_t *) section;
  162. int len = sizeof(struct section) + section->length;
  163. uint32_t crc;
  164. /* the crc has to be performed on the swapped data */
  165. bswap16(buf+1);
  166. crc = crc32(CRC32_INIT, buf, len-4);
  167. bswap16(buf+1);
  168. /* update the CRC */
  169. *((uint32_t*) (buf+len-4)) = crc;
  170. bswap32(buf+len-4);
  171. }
  172. return (struct section_ext *)section;
  173. }
  174. /**
  175. * Reset a psi_table_state structure.
  176. *
  177. * @param tstate The structure to reset.
  178. */
  179. static inline void psi_table_state_reset(struct psi_table_state *tstate)
  180. {
  181. tstate->version_number = 0xff;
  182. }
  183. /**
  184. * Check if a supplied section_ext is something we want to process.
  185. *
  186. * @param section The parsed section_ext structure.
  187. * @param tstate The state structure for this PSI table.
  188. * @return 0=> not useful. nonzero => useful.
  189. */
  190. static inline int section_ext_useful(struct section_ext *section, struct psi_table_state *tstate)
  191. {
  192. if ((section->version_number == tstate->version_number) && tstate->complete)
  193. return 0;
  194. if (section->version_number != tstate->version_number) {
  195. if (section->section_number != 0)
  196. return 0;
  197. tstate->next_section_number = 0;
  198. tstate->complete = 0;
  199. tstate->version_number = section->version_number;
  200. tstate->new_table = 1;
  201. } else if (section->section_number == tstate->next_section_number) {
  202. tstate->new_table = 0;
  203. } else {
  204. return 0;
  205. }
  206. tstate->next_section_number++;
  207. if (section->last_section_number < tstate->next_section_number) {
  208. tstate->complete = 1;
  209. }
  210. return 1;
  211. }
  212. #ifdef __cplusplus
  213. }
  214. #endif
  215. #endif