1 /*****************************************************************************
2  * desc_64.h: ETSI EN 300 468 Descriptor 0x64: Data broadcast descriptor
3  *****************************************************************************
4  * Copyright (C) 2011 Unix Solutions Ltd.
5  *
6  * Authors: Georgi Chorbadzhiyski <georgi@unixsol.org>
7  *
8  * Permission is hereby granted, free of charge, to any person obtaining
9  * a copy of this software and associated documentation files (the
10  * "Software"), to deal in the Software without restriction, including
11  * without limitation the rights to use, copy, modify, merge, publish,
12  * distribute, sublicense, and/or sell copies of the Software, and to
13  * permit persons to whom the Software is furnished to do so, subject
14  * to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be
17  * included in all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
22  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
23  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
24  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
25  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26  *****************************************************************************/
27 
28 /*
29  * Normative references:
30  *  - ETSI EN 300 468 V1.11.1 (2010-04) (SI in DVB systems)
31  */
32 
33 #ifndef __BITSTREAM_DVB_DESC_64_H__
34 #define __BITSTREAM_DVB_DESC_64_H__
35 
36 #include <bitstream/common.h>
37 #include <bitstream/mpeg/psi/descriptors.h>
38 #include <bitstream/dvb/si/strings.h>
39 
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44 
45 /*****************************************************************************
46  * Descriptor 0x64: Data broadcast descriptor
47  *****************************************************************************/
48 #define DESC64_HEADER_SIZE      (DESC_HEADER_SIZE + 8)
49 
desc64_init(uint8_t * p_desc)50 static inline void desc64_init(uint8_t *p_desc)
51 {
52     desc_set_tag(p_desc, 0x64);
53     desc_set_length(p_desc, DESC64_HEADER_SIZE - DESC_HEADER_SIZE);
54 }
55 
desc64_get_broadcast_id(const uint8_t * p_desc)56 static inline uint16_t desc64_get_broadcast_id(const uint8_t *p_desc)
57 {
58     return (p_desc[2] << 8) | p_desc[3];
59 }
60 
desc64_set_broadcast_id(uint8_t * p_desc,uint16_t i_broadcast_id)61 static inline void desc64_set_broadcast_id(uint8_t *p_desc, uint16_t i_broadcast_id)
62 {
63     p_desc[2] = (i_broadcast_id >> 8) & 0xff;
64     p_desc[3] =  i_broadcast_id       & 0xff;
65 }
66 
desc64_get_component_tag(const uint8_t * p_desc)67 static inline uint8_t desc64_get_component_tag(const uint8_t *p_desc)
68 {
69     return p_desc[4];
70 }
71 
desc64_set_component_tag(uint8_t * p_desc,uint8_t i_component_tag)72 static inline void desc64_set_component_tag(uint8_t *p_desc, uint8_t i_component_tag)
73 {
74     p_desc[4] = i_component_tag;
75 }
76 
desc64_get_selector_byte_length(const uint8_t * p_desc)77 static inline uint8_t desc64_get_selector_byte_length(const uint8_t *p_desc)
78 {
79     return p_desc[5];
80 }
81 
desc64_get_selector_byte(const uint8_t * p_desc,uint8_t * pi_length)82 static inline const uint8_t *desc64_get_selector_byte(const uint8_t *p_desc, uint8_t *pi_length)
83 {
84     *pi_length = desc64_get_selector_byte_length(p_desc);
85     return p_desc + 6;
86 }
87 
desc64_set_selector_byte(uint8_t * p_desc,const uint8_t * p_selector_byte,uint8_t i_length)88 static inline void desc64_set_selector_byte(uint8_t *p_desc, const uint8_t *p_selector_byte, uint8_t i_length)
89 {
90     p_desc[5] = i_length;
91     memcpy(p_desc + 6, p_selector_byte, i_length);
92 }
93 
desc64_get_lang(const uint8_t * p_desc)94 static inline const uint8_t *desc64_get_lang(const uint8_t *p_desc)
95 {
96     return p_desc + 6 + desc64_get_selector_byte_length(p_desc);
97 }
98 
desc64_set_lang(uint8_t * p_desc,const uint8_t p_lang[3])99 static inline void desc64_set_lang(uint8_t *p_desc, const uint8_t p_lang[3])
100 {
101     uint8_t *p = p_desc + 6 + desc64_get_selector_byte_length(p_desc);
102     p[0] = p_lang[0];
103     p[1] = p_lang[1];
104     p[2] = p_lang[2];
105 }
106 
desc64_get_text_length(const uint8_t * p_desc)107 static inline uint8_t desc64_get_text_length(const uint8_t *p_desc)
108 {
109     return p_desc[6 + desc64_get_selector_byte_length(p_desc) + 3];
110 }
111 
desc64_get_text(const uint8_t * p_desc,uint8_t * pi_length)112 static inline const uint8_t *desc64_get_text(const uint8_t *p_desc, uint8_t *pi_length)
113 {
114     *pi_length = desc64_get_text_length(p_desc);
115     return p_desc + 6 + desc64_get_selector_byte_length(p_desc) + 3 + 1;
116 }
117 
desc64_set_text(uint8_t * p_desc,const uint8_t * p_text,uint8_t i_length)118 static inline void desc64_set_text(uint8_t *p_desc, const uint8_t *p_text, uint8_t i_length)
119 {
120     uint8_t *p = p_desc + 6 + desc64_get_selector_byte_length(p_desc) + 3;
121     p[0] = i_length;
122     memcpy(p + 1, p_text, i_length);
123 }
124 
desc64_set_length(uint8_t * p_desc)125 static inline void desc64_set_length(uint8_t *p_desc)
126 {
127     desc_set_length(p_desc, DESC64_HEADER_SIZE - DESC_HEADER_SIZE
128                             + desc64_get_selector_byte_length(p_desc)
129                             + desc64_get_text_length(p_desc));
130 }
131 
desc64_validate(const uint8_t * p_desc)132 static inline bool desc64_validate(const uint8_t *p_desc)
133 {
134     int i_length = desc_get_length(p_desc) - (DESC64_HEADER_SIZE - DESC_HEADER_SIZE);
135     i_length -= desc64_get_selector_byte_length(p_desc);
136     i_length -= desc64_get_text_length(p_desc);
137     return i_length == 0;
138 }
139 
desc64_print(const uint8_t * p_desc,f_print pf_print,void * print_opaque,f_iconv pf_iconv,void * iconv_opaque,print_type_t i_print_type)140 static inline void desc64_print(const uint8_t *p_desc,
141                                 f_print pf_print, void *print_opaque,
142                                 f_iconv pf_iconv, void *iconv_opaque,
143                                 print_type_t i_print_type)
144 {
145     uint8_t i_selector_byte_length, i_text_length, i;
146     const uint8_t *p_selector_byte = desc64_get_selector_byte(p_desc, &i_selector_byte_length);
147 
148     const uint8_t *p_text = desc64_get_text(p_desc, &i_text_length);
149     char *psz_text = dvb_string_get(p_text, i_text_length, pf_iconv, iconv_opaque);
150 
151     char psz_selector_byte[2 * 255 + 1];
152     char psz_selector_byte_txt[255 + 1];
153 
154     for (i = 0; i < i_selector_byte_length; i++) {
155         sprintf(psz_selector_byte + 2 * i, "%02x", p_selector_byte[i]);
156         if (p_selector_byte[i] >= 32 && p_selector_byte[i] <= 127 && p_selector_byte[i] != '"')
157             psz_selector_byte_txt[i] = p_selector_byte[i];
158         else
159             psz_selector_byte_txt[i] = '.';
160     }
161     psz_selector_byte[2 * i] = '\0';
162     psz_selector_byte_txt[i] = '\0';
163 
164     switch (i_print_type) {
165     case PRINT_XML:
166         psz_text = dvb_string_xml_escape(psz_text);
167         pf_print(print_opaque,
168                  "<DATA_BROADCAST_DESC broadcast_id=\"0x%04x\" component_tag=\"%u\""
169                  " selector_byte=\"%s\" lang=\"%3.3s\""
170                  " text=\"%s\"/>",
171                  desc64_get_broadcast_id(p_desc),
172                  desc64_get_component_tag(p_desc),
173                  psz_selector_byte,
174                  (char *)desc64_get_lang(p_desc),
175                  psz_text);
176         break;
177     default:
178         pf_print(print_opaque,
179                  "    - desc 64 data_broadcast broadcast_id=0x%04x component_tag=%u"
180                  " selector_byte=\"%s\" selector_byte_txt=\"%s\" lang=%3.3s"
181                  " text=\"%s\"",
182                  desc64_get_broadcast_id(p_desc),
183                  desc64_get_component_tag(p_desc),
184                  psz_selector_byte,
185                  psz_selector_byte_txt,
186                  (char *)desc64_get_lang(p_desc),
187                  psz_text);
188     }
189     free(psz_text);
190 }
191 
192 #ifdef __cplusplus
193 }
194 #endif
195 
196 #endif
197