1 /*****************************************************************************
2  * desc_5d.h: ETSI EN 300 468 Descriptor 0x5d: Multilingual service name
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_5D_H__
35 #define __BITSTREAM_DVB_DESC_5D_H__
36 
37 #include <bitstream/common.h>
38 #include <bitstream/mpeg/psi/descriptors.h>
39 #include <bitstream/mpeg/psi/desc_0a.h>
40 #include <bitstream/dvb/si/strings.h>
41 
42 #ifdef __cplusplus
43 extern "C"
44 {
45 #endif
46 
47 /*****************************************************************************
48  * Descriptor 0x5d: Multilingual service name
49  *****************************************************************************/
50 #define DESC5D_HEADER_SIZE      DESC_HEADER_SIZE
51 #define DESC5D_DATA_SIZE        5
52 
desc5d_init(uint8_t * p_desc)53 static inline void desc5d_init(uint8_t *p_desc)
54 {
55     desc_set_tag(p_desc, 0x5d);
56 }
57 
58 #define desc5dn_set_code desc0an_set_code
59 #define desc5dn_get_code desc0an_get_code
60 
desc5dn_get_provider_name_length(const uint8_t * p_desc_n)61 static inline uint8_t desc5dn_get_provider_name_length(const uint8_t *p_desc_n)
62 {
63     return p_desc_n[3];
64 }
65 
desc5dn_get_provider_name(const uint8_t * p_desc_n,uint8_t * pi_length)66 static inline const uint8_t *desc5dn_get_provider_name(const uint8_t *p_desc_n, uint8_t *pi_length)
67 {
68     *pi_length = desc5dn_get_provider_name_length(p_desc_n);
69     return p_desc_n + 4;
70 }
71 
desc5dn_set_provider_name(uint8_t * p_desc_n,const uint8_t * p_network_name,uint8_t i_length)72 static inline void desc5dn_set_provider_name(uint8_t *p_desc_n, const uint8_t *p_network_name, uint8_t i_length)
73 {
74     p_desc_n[3] = i_length;
75     memcpy(p_desc_n + 4, p_network_name, i_length);
76 }
77 
desc5dn_get_service_name_length(const uint8_t * p_desc_n)78 static inline uint8_t desc5dn_get_service_name_length(const uint8_t *p_desc_n)
79 {
80     return p_desc_n[4 + desc5dn_get_provider_name_length(p_desc_n)];
81 }
82 
desc5dn_get_service_name(const uint8_t * p_desc_n,uint8_t * pi_length)83 static inline const uint8_t *desc5dn_get_service_name(const uint8_t *p_desc_n, uint8_t *pi_length)
84 {
85     *pi_length = desc5dn_get_service_name_length(p_desc_n);
86     return p_desc_n + 5 + desc5dn_get_provider_name_length(p_desc_n);
87 }
88 
desc5dn_set_service_name(uint8_t * p_desc_n,const uint8_t * p_network_name,uint8_t i_length)89 static inline void desc5dn_set_service_name(uint8_t *p_desc_n, const uint8_t *p_network_name, uint8_t i_length)
90 {
91     p_desc_n[4 + desc5dn_get_provider_name_length(p_desc_n)] = i_length;
92     memcpy(p_desc_n + 5 + desc5dn_get_provider_name_length(p_desc_n),
93         p_network_name, i_length);
94 }
95 
desc5d_get_data(const uint8_t * p_desc,uint8_t n)96 static inline uint8_t *desc5d_get_data(const uint8_t *p_desc, uint8_t n)
97 {
98     const uint8_t *p_desc_n = p_desc + DESC5D_HEADER_SIZE;
99     uint8_t i_desc_size = desc_get_length(p_desc);
100 
101     while (n) {
102         if (p_desc_n + DESC5D_DATA_SIZE - p_desc > i_desc_size)
103             return NULL;
104         p_desc_n += DESC5D_DATA_SIZE
105             + desc5dn_get_provider_name_length(p_desc_n)
106             + desc5dn_get_service_name_length(p_desc_n);
107         n--;
108     }
109     if (p_desc_n - p_desc > i_desc_size)
110         return NULL;
111     return (uint8_t *)p_desc_n;
112 }
113 
desc5d_validate(const uint8_t * p_desc)114 static inline bool desc5d_validate(const uint8_t *p_desc)
115 {
116     const uint8_t *p_desc_n = p_desc + DESC5D_HEADER_SIZE;
117     int i_desc_size = desc_get_length(p_desc);
118     while (i_desc_size > 0)
119     {
120         uint8_t i_data_sz = DESC5D_DATA_SIZE
121             + desc5dn_get_provider_name_length(p_desc_n)
122             + desc5dn_get_service_name_length(p_desc_n);
123         i_desc_size -= i_data_sz;
124         p_desc_n    += i_data_sz;
125     }
126     return i_desc_size == 0;
127 }
128 
desc5d_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)129 static inline void desc5d_print(const uint8_t *p_desc,
130                                 f_print pf_print, void *print_opaque,
131                                 f_iconv pf_iconv, void *iconv_opaque,
132                                 print_type_t i_print_type)
133 {
134     const uint8_t *p_desc_n;
135     uint8_t j = 0;
136 
137     while ((p_desc_n = desc5d_get_data(p_desc, j++)) != NULL) {
138         uint8_t i_provider_name_length, i_service_name_length;
139         const uint8_t *p_provider_name = desc5dn_get_provider_name(p_desc_n, &i_provider_name_length);
140         const uint8_t *p_service_name  = desc5dn_get_service_name(p_desc_n, &i_service_name_length);
141         char *psz_provider_name = dvb_string_get(p_provider_name, i_provider_name_length, pf_iconv, iconv_opaque);
142         char *psz_service_name = dvb_string_get(p_service_name, i_service_name_length, pf_iconv, iconv_opaque);
143         switch (i_print_type) {
144         case PRINT_XML:
145             psz_provider_name = dvb_string_xml_escape(psz_provider_name);
146             pf_print(print_opaque,
147                      "<MULTILINGUAL_SERVICE_NAME_DESC code=\"%3.3s\" provider=\"%s\" service=\"%s\"/>",
148                      desc5dn_get_code(p_desc_n),
149                      psz_provider_name,
150                      psz_service_name
151                     );
152             break;
153         default:
154             pf_print(print_opaque,
155                      "    - desc 5d multilingual_service_name code=\"%3.3s\" provider=\"%s\" service=\"%s\"",
156                      desc5dn_get_code(p_desc_n),
157                      psz_provider_name,
158                      psz_service_name
159                     );
160         }
161         free(psz_provider_name);
162         free(psz_service_name);
163     }
164 }
165 
166 #ifdef __cplusplus
167 }
168 #endif
169 
170 #endif
171