content_identifier_descriptor.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  1. /*
  2. * section and descriptor parser
  3. *
  4. * Copyright (C) 2005 Kenneth Aafloy (kenneth@linuxtv.org)
  5. * Copyright (C) 2005 Andrew de Quincey (adq_dvb@lidskialf.net)
  6. *
  7. * This library is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * This library is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with this library; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #ifndef _UCSI_DVB_CONTENT_IDENTIFIER_DESCRIPTOR
  22. #define _UCSI_DVB_CONTENT_IDENTIFIER_DESCRIPTOR 1
  23. #ifdef __cplusplus
  24. extern "C"
  25. {
  26. #endif
  27. #include <libucsi/descriptor.h>
  28. #include <libucsi/endianops.h>
  29. #include <libucsi/types.h>
  30. /**
  31. * Possible values for the crid_type.
  32. */
  33. enum {
  34. DVB_CRID_TYPE_NONE = 0x00,
  35. DVB_CRID_TYPE_ITEM = 0x01,
  36. DVB_CRID_TYPE_SERIES = 0x02,
  37. DVB_CRID_TYPE_RECOMMENDATION = 0x03,
  38. };
  39. /**
  40. * Possible values for the crid_location.
  41. */
  42. enum {
  43. DVB_CRID_LOCATION_THIS_DESCRIPTOR = 0x00,
  44. DVB_CRID_LOCATION_CIT = 0x01,
  45. };
  46. /**
  47. * dvb_content_identifier_descriptor structure.
  48. */
  49. struct dvb_content_identifier_descriptor {
  50. struct descriptor d;
  51. /* struct dvb_content_identifier_entry entries[] */
  52. } __ucsi_packed;
  53. /**
  54. * An entry in the entries field of a dvb_content_identifier_descriptor.
  55. */
  56. struct dvb_content_identifier_entry {
  57. EBIT2(uint8_t crid_type : 6; ,
  58. uint8_t crid_location : 2; );
  59. /* struct dvb_content_identifier_data_00 data0 */
  60. /* struct dvb_content_identifier_data_01 data1 */
  61. } __ucsi_packed;
  62. /**
  63. * The data if crid_location == 0
  64. */
  65. struct dvb_content_identifier_entry_data_0 {
  66. uint8_t crid_length;
  67. /* uint8_t data[] */
  68. } __ucsi_packed;
  69. /**
  70. * The data if crid_location == 1
  71. */
  72. struct dvb_content_identifier_entry_data_1 {
  73. uint16_t crid_ref;
  74. } __ucsi_packed;
  75. /**
  76. * Process a dvb_content_identifier_descriptor.
  77. *
  78. * @param d Generic descriptor.
  79. * @return dvb_content_identifier_descriptor pointer, or NULL on error.
  80. */
  81. static inline struct dvb_content_identifier_descriptor*
  82. dvb_content_identifier_descriptor_codec(struct descriptor* d)
  83. {
  84. uint32_t len = d->len + 2;
  85. uint32_t pos = 2;
  86. uint8_t *buf = (uint8_t*) d;
  87. while(pos < len) {
  88. struct dvb_content_identifier_entry *e =
  89. (struct dvb_content_identifier_entry*) (buf + pos);
  90. if (len < (pos+1))
  91. return NULL;
  92. pos++;
  93. switch(e->crid_location) {
  94. case 0:
  95. if (len < (pos + 1))
  96. return NULL;
  97. if (len < (pos + 1 + buf[pos]))
  98. return NULL;
  99. pos += 1 + buf[pos];
  100. break;
  101. case 1:
  102. if (len < (pos+2))
  103. return NULL;
  104. bswap16(buf+pos);
  105. break;
  106. }
  107. }
  108. if (pos != len)
  109. return NULL;
  110. return (struct dvb_content_identifier_descriptor*) d;
  111. }
  112. /**
  113. * Iterator for entries field of a dvb_content_identifier_descriptor.
  114. *
  115. * @param d dvb_content_identifier_descriptor pointer.
  116. * @param pos Variable holding a pointer to the current dvb_content_identifier_entry.
  117. */
  118. #define dvb_content_identifier_descriptor_entries_for_each(d, pos) \
  119. for ((pos) = dvb_content_identifier_descriptor_entries_first(d); \
  120. (pos); \
  121. (pos) = dvb_content_identifier_descriptor_entries_next(d, pos))
  122. /**
  123. * Accessor for the data0 field of a dvb_content_identifier_entry.
  124. *
  125. * @param d dvb_content_identifier_entry pointer.
  126. * @return Pointer, or NULL on error.
  127. */
  128. static inline struct dvb_content_identifier_entry_data_0*
  129. dvb_content_identifier_entry_data_0(struct dvb_content_identifier_entry *d)
  130. {
  131. if (d->crid_location != 0)
  132. return NULL;
  133. return (struct dvb_content_identifier_entry_data_0*)
  134. ((uint8_t*) d + sizeof(struct dvb_content_identifier_entry));
  135. }
  136. /**
  137. * Accessor for the data field of a dvb_content_identifier_entry_data_0.
  138. *
  139. * @param d dvb_content_identifier_entry_data_0 pointer.
  140. * @return Pointer, or NULL on error.
  141. */
  142. static inline uint8_t*
  143. dvb_content_identifier_entry_data_0_data(struct dvb_content_identifier_entry_data_0 *d)
  144. {
  145. return ((uint8_t*) d + sizeof(struct dvb_content_identifier_entry_data_0));
  146. }
  147. /**
  148. * Accessor for the data1 field of a dvb_content_identifier_entry.
  149. *
  150. * @param d dvb_content_identifier_entry pointer.
  151. * @return Pointer, or NULL on error.
  152. */
  153. static inline struct dvb_content_identifier_entry_data_1*
  154. dvb_content_identifier_entry_data_1(struct dvb_content_identifier_entry *d)
  155. {
  156. if (d->crid_location != 1)
  157. return NULL;
  158. return (struct dvb_content_identifier_entry_data_1*)
  159. ((uint8_t*) d + sizeof(struct dvb_content_identifier_entry));
  160. }
  161. /******************************** PRIVATE CODE ********************************/
  162. static inline struct dvb_content_identifier_entry*
  163. dvb_content_identifier_descriptor_entries_first(struct dvb_content_identifier_descriptor *d)
  164. {
  165. if (d->d.len == 0)
  166. return NULL;
  167. return (struct dvb_content_identifier_entry *)
  168. ((uint8_t*) d + sizeof(struct dvb_content_identifier_descriptor));
  169. }
  170. static inline struct dvb_content_identifier_entry*
  171. dvb_content_identifier_descriptor_entries_next(struct dvb_content_identifier_descriptor *d,
  172. struct dvb_content_identifier_entry *pos)
  173. {
  174. uint8_t *end = (uint8_t*) d + 2 + d->d.len;
  175. uint8_t *next = (uint8_t *) pos + sizeof(struct dvb_content_identifier_entry);
  176. if (next >= end)
  177. return NULL;
  178. switch(pos->crid_location) {
  179. case 0:
  180. if ((next+2) >= end)
  181. return NULL;
  182. if ((next+2+next[1]) >= end)
  183. return NULL;
  184. break;
  185. case 1:
  186. if ((next+3) >= end)
  187. return NULL;
  188. break;
  189. }
  190. return (struct dvb_content_identifier_entry*) next;
  191. }
  192. #ifdef __cplusplus
  193. }
  194. #endif
  195. #endif