descriptor.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_DESCRIPTOR_H
  21. #define _UCSI_DESCRIPTOR_H 1
  22. #ifdef __cplusplus
  23. extern "C"
  24. {
  25. #endif
  26. #include <libucsi/endianops.h>
  27. #include <stdint.h>
  28. #include <stdlib.h>
  29. /**
  30. * Generic descriptor header.
  31. */
  32. struct descriptor {
  33. uint8_t tag;
  34. uint8_t len;
  35. } __ucsi_packed;
  36. /**
  37. * Retreive pointer to the next descriptor structure.
  38. *
  39. * @param buf The buffer of descriptors.
  40. * @param len Size of the buffer.
  41. * @param pos Current descriptor.
  42. * @return Pointer to next descriptor, or NULL if there are none.
  43. */
  44. static inline struct descriptor *
  45. next_descriptor(uint8_t * buf, size_t len, struct descriptor * pos)
  46. {
  47. uint8_t* next;
  48. if (pos == NULL)
  49. return NULL;
  50. next = (uint8_t*) pos + 2 + pos->len;
  51. if (next >= buf + len)
  52. return NULL;
  53. return (struct descriptor *) next;
  54. }
  55. /**
  56. * The unknown descriptor.
  57. */
  58. struct unknown_descriptor {
  59. struct descriptor d;
  60. /* uint8_t data [] */
  61. } __ucsi_packed;
  62. /**
  63. * Retrieve pointer to the unknown descriptor's data field.
  64. *
  65. * @param d The descriptor.
  66. * @return Pointer to the data field.
  67. */
  68. static inline uint8_t *
  69. unknown_descriptor_data(struct unknown_descriptor *d)
  70. {
  71. return (uint8_t *) d + sizeof(struct unknown_descriptor);
  72. }
  73. /**
  74. * Retrieve size of unknown descriptor's data field.
  75. *
  76. * @param d The descriptor.
  77. * @return Size of data field in bytes.
  78. */
  79. static inline int
  80. unknown_descriptor_data_size(struct unknown_descriptor *d)
  81. {
  82. return d->d.len;
  83. }
  84. /******************************** PRIVATE CODE ********************************/
  85. static inline int verify_descriptors(uint8_t * buf, size_t len)
  86. {
  87. size_t pos = 0;
  88. while (pos < len) {
  89. if ((pos + 2) > len)
  90. return -1;
  91. pos += 2 + buf[pos+1];
  92. }
  93. if (pos != len)
  94. return -1;
  95. return 0;
  96. }
  97. #ifdef __cplusplus
  98. }
  99. #endif
  100. #endif