content_advisory_descriptor.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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_ATSC_CONTENT_ADVISORY_DESCRIPTOR
  22. #define _UCSI_ATSC_CONTENT_ADVISORY_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. * atsc_content_advisory_descriptor structure.
  32. */
  33. struct atsc_content_advisory_descriptor {
  34. struct descriptor d;
  35. EBIT2(uint8_t reserved : 2; ,
  36. uint8_t rating_region_count : 6; );
  37. /* struct atsc_content_advisory_entry entries[] */
  38. } __ucsi_packed;
  39. /**
  40. * An entry in the entries field of a atsc_content_advisory_descriptor.
  41. */
  42. struct atsc_content_advisory_entry {
  43. uint8_t rating_region;
  44. uint8_t rated_dimensions;
  45. /* struct atsc_content_advisory_entry_dimension dimensions[] */
  46. /* struct atsc_content_advisory_entry_part2 part2 */
  47. } __ucsi_packed;
  48. /**
  49. * An entry in the entries field of a atsc_content_advisory_descriptor.
  50. */
  51. struct atsc_content_advisory_entry_dimension {
  52. uint8_t rating_dimension_j;
  53. EBIT2(uint8_t reserved : 4; ,
  54. uint8_t rating_value : 4; );
  55. } __ucsi_packed;
  56. /**
  57. * Part2 of an atsc_content_advisory_entry.
  58. */
  59. struct atsc_content_advisory_entry_part2 {
  60. uint8_t rating_description_length;
  61. /* struct atsc_text description */
  62. } __ucsi_packed;
  63. /**
  64. * Process an atsc_content_advisory_descriptor.
  65. *
  66. * @param d Generic descriptor pointer.
  67. * @return atsc_content_advisory_descriptor pointer, or NULL on error.
  68. */
  69. static inline struct atsc_content_advisory_descriptor*
  70. atsc_content_advisory_descriptor_codec(struct descriptor* d)
  71. {
  72. struct atsc_content_advisory_descriptor *ret =
  73. (struct atsc_content_advisory_descriptor *) d;
  74. uint8_t *buf = (uint8_t*) d + 2;
  75. int pos = 0;
  76. int idx;
  77. if (d->len < 1)
  78. return NULL;
  79. pos++;
  80. for(idx = 0; idx < ret->rating_region_count; idx++) {
  81. if (d->len < (pos + sizeof(struct atsc_content_advisory_entry)))
  82. return NULL;
  83. struct atsc_content_advisory_entry *entry =
  84. (struct atsc_content_advisory_entry *) (buf + pos);
  85. pos += sizeof(struct atsc_content_advisory_entry);
  86. if (d->len < (pos + (sizeof(struct atsc_content_advisory_entry_dimension) *
  87. entry->rated_dimensions)))
  88. return NULL;
  89. pos += sizeof(struct atsc_content_advisory_entry_dimension) * entry->rated_dimensions;
  90. if (d->len < (pos + sizeof(struct atsc_content_advisory_entry_part2)))
  91. return NULL;
  92. struct atsc_content_advisory_entry_part2 *part2 =
  93. (struct atsc_content_advisory_entry_part2 *) (buf + pos);
  94. pos += sizeof(struct atsc_content_advisory_entry_part2);
  95. if (d->len < (pos + part2->rating_description_length))
  96. return NULL;
  97. if (atsc_text_validate(buf+pos, part2->rating_description_length))
  98. return NULL;
  99. pos += part2->rating_description_length;
  100. }
  101. return (struct atsc_content_advisory_descriptor*) d;
  102. }
  103. /**
  104. * Iterator for entries field of a atsc_content_advisory_descriptor.
  105. *
  106. * @param d atsc_content_advisory_descriptor pointer.
  107. * @param pos Variable holding a pointer to the current atsc_content_advisory_entry.
  108. * @param idx Integer used to count which entry we are in.
  109. */
  110. #define atsc_content_advisory_descriptor_entries_for_each(d, pos, idx) \
  111. for ((pos) = atsc_content_advisory_descriptor_entries_first(d), idx=0; \
  112. (pos); \
  113. (pos) = atsc_content_advisory_descriptor_entries_next(d, pos, ++idx))
  114. /**
  115. * Iterator for dimensions field of a atsc_content_advisory_entry.
  116. *
  117. * @param d atsc_content_advisory_entry pointer.
  118. * @param pos Variable holding a pointer to the current atsc_content_advisory_entry_dimension.
  119. * @param idx Integer used to count which dimension we are in.
  120. */
  121. #define atsc_content_advisory_entry_dimensions_for_each(d, pos, idx) \
  122. for ((pos) = atsc_content_advisory_entry_dimensions_first(d), idx=0; \
  123. (pos); \
  124. (pos) = atsc_content_advisory_entry_dimensions_next(d, pos, ++idx))
  125. /**
  126. * Accessor for the part2 field of an atsc_content_advisory_entry.
  127. *
  128. * @param entry atsc_content_advisory_entry pointer.
  129. * @return struct atsc_content_advisory_entry_part2 pointer.
  130. */
  131. static inline struct atsc_content_advisory_entry_part2 *
  132. atsc_content_advisory_entry_part2(struct atsc_content_advisory_entry *entry)
  133. {
  134. int pos = sizeof(struct atsc_content_advisory_entry);
  135. pos += entry->rated_dimensions * sizeof(struct atsc_content_advisory_entry_dimension);
  136. return (struct atsc_content_advisory_entry_part2 *) (((uint8_t*) entry) + pos);
  137. }
  138. /**
  139. * Accessor for the description field of an atsc_content_advisory_entry_part2.
  140. *
  141. * @param part2 atsc_content_advisory_entry_part2 pointer.
  142. * @return Pointer to the atsc_text data, or NULL on error.
  143. */
  144. static inline struct atsc_text*
  145. atsc_content_advisory_entry_part2_description(struct atsc_content_advisory_entry_part2 *part2)
  146. {
  147. uint8_t *txt = ((uint8_t*) part2) + sizeof(struct atsc_content_advisory_entry_part2);
  148. return (struct atsc_text *) txt;
  149. }
  150. /******************************** PRIVATE CODE ********************************/
  151. static inline struct atsc_content_advisory_entry*
  152. atsc_content_advisory_descriptor_entries_first(struct atsc_content_advisory_descriptor *d)
  153. {
  154. if (d->rating_region_count == 0)
  155. return NULL;
  156. return (struct atsc_content_advisory_entry *)
  157. ((uint8_t*) d + sizeof(struct atsc_content_advisory_descriptor));
  158. }
  159. static inline struct atsc_content_advisory_entry*
  160. atsc_content_advisory_descriptor_entries_next(struct atsc_content_advisory_descriptor *d,
  161. struct atsc_content_advisory_entry *pos,
  162. int idx)
  163. {
  164. if (idx >= d->rating_region_count)
  165. return NULL;
  166. struct atsc_content_advisory_entry_part2 *part2 =
  167. atsc_content_advisory_entry_part2(pos);
  168. return (struct atsc_content_advisory_entry *)
  169. ((uint8_t *) part2 +
  170. sizeof(struct atsc_content_advisory_entry_part2) +
  171. part2->rating_description_length);
  172. }
  173. static inline struct atsc_content_advisory_entry_dimension*
  174. atsc_content_advisory_entry_dimensions_first(struct atsc_content_advisory_entry *e)
  175. {
  176. if (e->rated_dimensions == 0)
  177. return NULL;
  178. return (struct atsc_content_advisory_entry_dimension *)
  179. ((uint8_t*) e + sizeof(struct atsc_content_advisory_entry));
  180. }
  181. static inline struct atsc_content_advisory_entry_dimension*
  182. atsc_content_advisory_entry_dimensions_next(struct atsc_content_advisory_entry *e,
  183. struct atsc_content_advisory_entry_dimension *pos,
  184. int idx)
  185. {
  186. uint8_t *next = (uint8_t *) pos + sizeof(struct atsc_content_advisory_entry_dimension);
  187. if (idx >= e->rated_dimensions)
  188. return NULL;
  189. return (struct atsc_content_advisory_entry_dimension *) next;
  190. }
  191. #ifdef __cplusplus
  192. }
  193. #endif
  194. #endif