mgt_section.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include <libucsi/atsc/mgt_section.h>
  22. struct atsc_mgt_section *atsc_mgt_section_codec(struct atsc_section_psip *psip)
  23. {
  24. uint8_t * buf = (uint8_t *) psip;
  25. size_t pos = sizeof(struct atsc_section_psip);
  26. size_t len = section_ext_length(&(psip->ext_head));
  27. struct atsc_mgt_section *mgt = (struct atsc_mgt_section *) psip;
  28. int i;
  29. if (len < sizeof(struct atsc_mgt_section))
  30. return NULL;
  31. bswap16(buf + pos);
  32. pos += 2;
  33. // we cannot use the tables_defined value here because of the braindead ATSC spec!
  34. for(i=0; i < mgt->tables_defined; i++) {
  35. // we think we're still in the tables - process as normal
  36. if ((pos + sizeof(struct atsc_mgt_table)) > len)
  37. return NULL;
  38. struct atsc_mgt_table *table = (struct atsc_mgt_table *) (buf+pos);
  39. bswap16(buf+pos);
  40. bswap16(buf+pos+2);
  41. bswap32(buf+pos+5);
  42. bswap16(buf+pos+9);
  43. pos += sizeof(struct atsc_mgt_table);
  44. if ((pos + table->table_type_descriptors_length) > len)
  45. return NULL;
  46. if (verify_descriptors(buf + pos, table->table_type_descriptors_length))
  47. return NULL;
  48. pos += table->table_type_descriptors_length;
  49. }
  50. if ((pos + sizeof(struct atsc_mgt_section_part2)) > len)
  51. return NULL;
  52. struct atsc_mgt_section_part2 *part2 = (struct atsc_mgt_section_part2 *) (buf+pos);
  53. bswap16(buf+pos);
  54. pos += sizeof(struct atsc_mgt_section_part2);
  55. if ((pos + part2->descriptors_length) > len)
  56. return NULL;
  57. if (verify_descriptors(buf + pos, part2->descriptors_length))
  58. return NULL;
  59. pos += part2->descriptors_length;
  60. if (pos != len)
  61. return NULL;
  62. return (struct atsc_mgt_section *) psip;
  63. }