]> git.sesse.net Git - vlc/blob - modules/demux/nsc.c
Removes trailing spaces. Removes tabs.
[vlc] / modules / demux / nsc.c
1 /*****************************************************************************
2  * nsc.c: NSC file demux and encoding decoder
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Jon Lech Johansen <jon@nanocrew.net>
8  *          Derk-Jan Hartman <hartman at videolan dot org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <vlc/vlc.h>
29 #include <vlc_demux.h>
30 #include <vlc_playlist.h>
31
32 #include <ctype.h>
33 #define MAX_LINE 16024
34
35 /*****************************************************************************
36  * Module descriptor
37  *****************************************************************************/
38 static int  DemuxOpen  ( vlc_object_t * );
39 static void DemuxClose ( vlc_object_t * );
40
41 vlc_module_begin();
42     set_description( _("Windows Media NSC metademux") );
43     set_category( CAT_INPUT );
44     set_subcategory( SUBCAT_INPUT_DEMUX );
45     set_capability( "demux2", 2 );
46     set_callbacks( DemuxOpen, DemuxClose );
47 vlc_module_end();
48
49 static int Demux ( demux_t *p_demux );
50 static int Control( demux_t *p_demux, int i_query, va_list args );
51
52 static const unsigned char inverse[ 128 ] =
53 {
54     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
55     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
56     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
57     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
58     0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF,
59     0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10,
60     0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C,
61     0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
62     0xFF, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E,
63     0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A,
64     0x3B, 0x3C, 0x3D, 0x3E, 0xFF, 0x3F, 0xFF, 0xFF
65 };
66
67 static int load_byte( unsigned char encoding_type,
68                       unsigned char *output, char **input,
69                       unsigned char *j, unsigned char *k )
70 {
71     *output = 0;
72
73     if( encoding_type == 1 )
74     {
75         if( isxdigit( **input ) == 0 )
76             return -1;
77
78         if( isdigit( **input ) == 0 )
79             *output = (toupper( **input ) - 7) * 16;
80         else
81             *output = **input * 16;
82
83         (*input)++;
84
85         if( isxdigit( **input ) == 0 )
86             return -1;
87
88         if( isdigit( **input ) == 0 )
89             *output |= toupper( **input ) - 0x37;
90         else
91             *output |= **input - 0x30;
92
93         (*input)++;
94     }
95     else if( encoding_type == 2 )
96     {
97         unsigned char **uinput = (unsigned char **)input;
98
99         if( **uinput > 127 || inverse[ **uinput ] == 0xFF )
100             return -1;
101
102         if( *k == 0 )
103         {
104             if( (*uinput)[ 1 ] > 127 || inverse[ (*uinput)[ 1 ] ] == 0xFF )
105                 return -1;
106
107             *output = (inverse[ (*uinput)[ 0 ] ] * 4) |
108                         (inverse[ (*uinput)[ 1 ] ] / 16);
109
110             *j = inverse[ (*uinput)[ 1 ] ] * 16;
111             *k = 4;
112
113             (*uinput) += 2;
114         }
115         else if( *k == 2 )
116         {
117             *output = *j | inverse[ **uinput ];
118
119             *j = 0;
120             *k = 0;
121
122             (*uinput)++;
123         }
124         else if( *k == 4 )
125         {
126             *output = (inverse[ **uinput ] / 4) | *j;
127
128             *j = inverse[ **uinput ] * 64;
129             *k = 2;
130
131             (*uinput)++;
132         }
133     }
134
135     return 0;
136 }
137
138 static char *nscdec( vlc_object_t *p_demux, char* p_encoded )
139 {
140     unsigned int i;
141     unsigned char tmp;
142     unsigned char j, k;
143     unsigned int length;
144     unsigned char encoding_type;
145
146     vlc_iconv_t conv;
147     size_t buf16_size;
148     unsigned char *buf16;
149     const char *p_buf16;
150     size_t buf8_size;
151     char *buf8;
152     char *p_buf8;
153
154     char *p_input = p_encoded;
155
156     if( strlen( p_input ) < 15 )
157     {
158         msg_Err( p_demux, "input string less than 15 characters" );
159         return NULL;
160     }
161
162     if( load_byte( 1, &encoding_type, &p_input, NULL, NULL ) )
163     {
164         msg_Err( p_demux, "unable to get NSC encoding type" );
165         return NULL;
166     }
167
168     if( encoding_type != 1 && encoding_type != 2 )
169     {
170         msg_Err( p_demux, "encoding type %d is not supported",
171                  encoding_type );
172         return NULL;
173     }
174
175     j = k = 0;
176
177     if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
178     {
179         msg_Err( p_demux, "load_byte failed" );
180         return NULL;
181     }
182
183     for( i = 0; i < 4; i++ )
184     {
185         if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
186         {
187             msg_Err( p_demux, "load_byte failed" );
188             return NULL;
189         }
190     }
191
192     length = 0;
193     for( i = 4; i; i-- )
194     {
195         if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) )
196         {
197             msg_Err( p_demux, "load_byte failed" );
198             return NULL;
199         }
200         length |= tmp << ((i - 1) * 8);
201     }
202
203     if( length == 0 )
204     {
205         msg_Err( p_demux, "Length is 0" );
206         return NULL;
207     }
208
209     buf16_size = length;
210     buf16 = malloc( buf16_size );
211     if( buf16 == NULL )
212     {
213         msg_Err( p_demux, "out of memory" );
214         return NULL;
215     }
216
217     for( i = 0; i < length; i++ )
218     {
219         if( load_byte( encoding_type, &buf16[ i ], &p_input, &j, &k ) )
220         {
221             msg_Err( p_demux, "load_byte failed" );
222             free( buf16 );
223             return NULL;
224         }
225     }
226
227     buf8_size = length;
228     buf8 = malloc( buf8_size + 1 );
229     if( buf8 == NULL )
230     {
231         msg_Err( p_demux, "out of memory" );
232         free( buf16 );
233         return NULL;
234     }
235
236     conv = vlc_iconv_open( "UTF-8", "UTF-16LE" );
237     if( conv == (vlc_iconv_t)(-1) )
238     {
239         msg_Err( p_demux, "iconv_open failed" );
240         free( buf16 );
241         free( buf8 );
242         return NULL;
243     }
244
245     p_buf8 = buf8;
246     p_buf16 = (const char *)buf16;
247
248     if( vlc_iconv( conv, &p_buf16, &buf16_size, &p_buf8, &buf8_size ) == (size_t)(-1) )
249     {
250         msg_Err( p_demux, "iconv failed" );
251         return NULL;
252     }
253     else
254     {
255         buf8[ length - buf8_size ] = '\0';
256     }
257
258     vlc_iconv_close( conv );
259
260     free( buf16 );
261     return buf8;
262 }
263
264 static int DemuxOpen( vlc_object_t * p_this )
265 {
266     demux_t *p_demux = (demux_t *)p_this;
267     byte_t *p_peek;
268     int i_size;
269
270     /* Lets check the content to see if this is a NSC file */
271     i_size = stream_Peek( p_demux->s, &p_peek, MAX_LINE );
272     i_size -= sizeof("NSC Format Version=") - 1;
273
274     if ( i_size > 0 )
275     {
276         while ( i_size && strncasecmp( (char *)p_peek, "NSC Format Version=",
277                                        (int) sizeof("NSC Format Version=") - 1 ) )
278         {
279             p_peek++;
280             i_size--;
281         }
282         if ( !strncasecmp( (char *)p_peek, "NSC Format Version=",
283                            (int) sizeof("NSC Format Version=") -1 ) )
284         {
285             p_demux->pf_demux = Demux;
286             p_demux->pf_control = Control;
287             return VLC_SUCCESS;
288         }
289     }
290     return VLC_EGENERIC;
291 }
292
293
294 /*****************************************************************************
295  * Deactivate: frees unused data
296  *****************************************************************************/
297 static void DemuxClose( vlc_object_t *p_this )
298 {
299     return;
300 }
301
302 static int ParseLine ( demux_t *p_demux, char *psz_line )
303 {
304     char        *psz_bol;
305     char        *psz_value;
306
307     psz_bol = psz_line;
308     /* Remove unnecessary tabs or spaces at the beginning of line */
309     while( *psz_bol == ' ' || *psz_bol == '\t' ||
310            *psz_bol == '\n' || *psz_bol == '\r' )
311     {
312         psz_bol++;
313     }
314     psz_value = strchr( psz_bol, '=' );
315     if( psz_value == NULL )
316     {
317         return 0; /* a [Address] or [Formats] line or something else we will ignore */
318     }
319     *psz_value = '\0';
320     psz_value++;
321  
322     if( !strncasecmp( psz_value, "0x", 2 ) )
323     {
324         int i_value;
325         sscanf( psz_value, "%x", &i_value );
326         msg_Dbg( p_demux, "%s = %d", psz_bol, i_value );
327     }
328     else if( !strncasecmp( psz_bol, "Format", 6 ) )
329     {
330         msg_Dbg( p_demux, "%s = asf header", psz_bol );
331     }
332     else
333     {
334         /* This should be NSC encoded strings in the values */
335         char *psz_out;
336         psz_out = nscdec( (vlc_object_t *)p_demux, psz_value );
337         if( psz_out )
338         {
339             msg_Dbg( p_demux, "%s = %s", psz_bol, psz_out );
340             if( psz_out) free( psz_out );
341         }
342     }
343     return VLC_SUCCESS;
344 }
345
346 /*****************************************************************************
347  * Demux: reads and demuxes data packets
348  *****************************************************************************
349  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
350  *****************************************************************************/
351 static int Demux ( demux_t *p_demux )
352 {
353     char            *psz_line;
354
355     while( ( psz_line = stream_ReadLine( p_demux->s ) ) )
356     {
357         ParseLine( p_demux, psz_line );
358         if( psz_line ) free( psz_line );
359     }
360     return VLC_SUCCESS;
361 }
362
363 static int Control( demux_t *p_demux, int i_query, va_list args )
364 {
365     return VLC_EGENERIC;
366 }