1 /*****************************************************************************
2  * desc_48.h: ETSI EN 300 468 Descriptor 0x48: Service descriptor
3  *****************************************************************************
4  * Copyright (C) 2009-2010 VideoLAN
5  *
6  * Authors: Christophe Massiot <massiot@via.ecp.fr>
7  *          Georgi Chorbadzhiyski <georgi@unixsol.org>
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining
10  * a copy of this software and associated documentation files (the
11  * "Software"), to deal in the Software without restriction, including
12  * without limitation the rights to use, copy, modify, merge, publish,
13  * distribute, sublicense, and/or sell copies of the Software, and to
14  * permit persons to whom the Software is furnished to do so, subject
15  * to the following conditions:
16  *
17  * The above copyright notice and this permission notice shall be
18  * included in all copies or substantial portions of the Software.
19  *
20  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
23  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
24  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
25  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
26  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
27  *****************************************************************************/
28 
29 /*
30  * Normative references:
31  *  - ETSI EN 300 468 V1.11.1 (2010-04) (SI in DVB systems)
32  */
33 
34 #ifndef __BITSTREAM_DVB_DESC_48_H__
35 #define __BITSTREAM_DVB_DESC_48_H__
36 
37 #include <bitstream/common.h>
38 #include <bitstream/mpeg/psi/descriptors.h>
39 #include <bitstream/dvb/si/strings.h>
40 
41 #ifdef __cplusplus
42 extern "C"
43 {
44 #endif
45 
46 /*****************************************************************************
47  * Descriptor 0x48: Service descriptor
48  *****************************************************************************/
49 #define DESC48_HEADER_SIZE      (DESC_HEADER_SIZE + 1)
50 
desc48_init(uint8_t * p_desc)51 static inline void desc48_init(uint8_t *p_desc)
52 {
53     desc_set_tag(p_desc, 0x48);
54 }
55 
desc48_set_type(uint8_t * p_desc,uint8_t i_type)56 static inline void desc48_set_type(uint8_t *p_desc, uint8_t i_type)
57 {
58     p_desc[2] = i_type;
59 }
60 
desc48_get_type(const uint8_t * p_desc)61 static inline uint8_t desc48_get_type(const uint8_t *p_desc)
62 {
63     return p_desc[2];
64 }
65 
desc48_set_provider(uint8_t * p_desc,const uint8_t * p_provider,uint8_t i_length)66 static inline void desc48_set_provider(uint8_t *p_desc,
67                                        const uint8_t *p_provider,
68                                        uint8_t i_length)
69 {
70     uint8_t *p = p_desc + DESC48_HEADER_SIZE;
71     p[0] = i_length;
72     memcpy(p + 1, p_provider, i_length);
73 }
74 
desc48_get_provider(const uint8_t * p_desc,uint8_t * pi_length)75 static inline uint8_t *desc48_get_provider(const uint8_t *p_desc,
76                                                  uint8_t *pi_length)
77 {
78     uint8_t *p = (uint8_t *)p_desc + DESC48_HEADER_SIZE;
79     *pi_length = p[0];
80     return p + 1;
81 }
82 
desc48_set_service(uint8_t * p_desc,const uint8_t * p_service,uint8_t i_length)83 static inline void desc48_set_service(uint8_t *p_desc,
84                                       const uint8_t *p_service,
85                                       uint8_t i_length)
86 {
87     uint8_t *p = p_desc + DESC48_HEADER_SIZE + 1 + p_desc[3];
88     p[0] = i_length;
89     memcpy(p + 1, p_service, i_length);
90 }
91 
desc48_get_service(const uint8_t * p_desc,uint8_t * pi_length)92 static inline uint8_t *desc48_get_service(const uint8_t *p_desc,
93                                           uint8_t *pi_length)
94 {
95     uint8_t *p = (uint8_t *)p_desc + DESC48_HEADER_SIZE + 1 + p_desc[3];
96     *pi_length = p[0];
97     return p + 1;
98 }
99 
desc48_set_length(uint8_t * p_desc)100 static inline void desc48_set_length(uint8_t *p_desc)
101 {
102     uint8_t i_provider_len, i_service_len;
103     desc48_get_provider(p_desc, &i_provider_len);
104     desc48_get_service(p_desc, &i_service_len);
105     desc_set_length(p_desc, DESC48_HEADER_SIZE - DESC_HEADER_SIZE
106                             + 1 + i_provider_len
107                             + 1 + i_service_len);
108 }
109 
desc48_validate(const uint8_t * p_desc)110 static inline bool desc48_validate(const uint8_t *p_desc)
111 {
112     uint8_t i_length = desc_get_length(p_desc);
113     const uint8_t *p = p_desc + DESC48_HEADER_SIZE;
114 
115     p += *p + 1;
116     if (DESC48_HEADER_SIZE + 2 > i_length + DESC_HEADER_SIZE ||
117         p + 1 - p_desc > i_length + DESC_HEADER_SIZE)
118         return false;
119 
120     p += *p + 1;
121     if (p - p_desc > i_length + DESC_HEADER_SIZE)
122         return false;
123 
124     return true;
125 }
126 
desc48_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)127 static inline void desc48_print(const uint8_t *p_desc,
128                                 f_print pf_print, void *print_opaque,
129                                 f_iconv pf_iconv, void *iconv_opaque,
130                                 print_type_t i_print_type)
131 {
132     uint8_t i_provider_length, i_service_length;
133     const uint8_t *p_provider = desc48_get_provider(p_desc, &i_provider_length);
134     const uint8_t *p_service = desc48_get_service(p_desc, &i_service_length);
135     char *psz_provider = dvb_string_get(p_provider, i_provider_length,
136                                         pf_iconv, iconv_opaque);
137     char *psz_service = dvb_string_get(p_service, i_service_length,
138                                        pf_iconv, iconv_opaque);
139     switch (i_print_type) {
140     case PRINT_XML:
141         psz_provider = dvb_string_xml_escape(psz_provider);
142         psz_service = dvb_string_xml_escape(psz_service);
143         pf_print(print_opaque,
144                  "<SERVICE_DESC type=\"0x%hhx\" provider=\"%s\" service=\"%s\"/>",
145                  desc48_get_type(p_desc), psz_provider, psz_service);
146         break;
147     default:
148         pf_print(print_opaque,
149                  "    - desc 48 service type=0x%hhx provider=\"%s\" service=\"%s\"",
150                  desc48_get_type(p_desc), psz_provider, psz_service);
151     }
152     free(psz_provider);
153     free(psz_service);
154 }
155 
156 #ifdef __cplusplus
157 }
158 #endif
159 
160 #endif
161