| 1 |
|
|---|
| 2 |
|
|---|
| 3 |
|
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
|
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
|
|---|
| 10 |
|
|---|
| 11 |
|
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
|
|---|
| 20 |
|
|---|
| 21 |
|
|---|
| 22 |
|
|---|
| 23 |
|
|---|
| 24 |
|
|---|
| 25 |
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 |
#ifdef HAVE_CONFIG_H |
|---|
| 29 |
# include "config.h" |
|---|
| 30 |
#endif |
|---|
| 31 |
|
|---|
| 32 |
#include <vlc_common.h> |
|---|
| 33 |
#include <vlc_plugin.h> |
|---|
| 34 |
#include <vlc_demux.h> |
|---|
| 35 |
|
|---|
| 36 |
#include <ctype.h> |
|---|
| 37 |
#define MAX_LINE 16024 |
|---|
| 38 |
|
|---|
| 39 |
|
|---|
| 40 |
|
|---|
| 41 |
|
|---|
| 42 |
static int DemuxOpen ( vlc_object_t * ); |
|---|
| 43 |
static void DemuxClose ( vlc_object_t * ); |
|---|
| 44 |
|
|---|
| 45 |
vlc_module_begin(); |
|---|
| 46 |
set_description( N_("Windows Media NSC metademux") ); |
|---|
| 47 |
set_category( CAT_INPUT ); |
|---|
| 48 |
set_subcategory( SUBCAT_INPUT_DEMUX ); |
|---|
| 49 |
set_capability( "demux", 3 ); |
|---|
| 50 |
set_callbacks( DemuxOpen, DemuxClose ); |
|---|
| 51 |
vlc_module_end(); |
|---|
| 52 |
|
|---|
| 53 |
static int Demux ( demux_t *p_demux ); |
|---|
| 54 |
static int Control( demux_t *p_demux, int i_query, va_list args ); |
|---|
| 55 |
|
|---|
| 56 |
static const unsigned char inverse[ 128 ] = |
|---|
| 57 |
{ |
|---|
| 58 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
|---|
| 59 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
|---|
| 60 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
|---|
| 61 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
|---|
| 62 |
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0xFF, 0xFF, |
|---|
| 63 |
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, |
|---|
| 64 |
0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, |
|---|
| 65 |
0x1D, 0x1E, 0x1F, 0x20, 0x21, 0x22, 0x23, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, |
|---|
| 66 |
0xFF, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2A, 0x2B, 0x2C, 0x2D, 0x2E, |
|---|
| 67 |
0x2F, 0x30, 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x3A, |
|---|
| 68 |
0x3B, 0x3C, 0x3D, 0x3E, 0xFF, 0x3F, 0xFF, 0xFF |
|---|
| 69 |
}; |
|---|
| 70 |
|
|---|
| 71 |
static int load_byte( unsigned char encoding_type, |
|---|
| 72 |
unsigned char *output, char **input, |
|---|
| 73 |
unsigned char *j, unsigned char *k ) |
|---|
| 74 |
{ |
|---|
| 75 |
*output = 0; |
|---|
| 76 |
|
|---|
| 77 |
if( encoding_type == 1 ) |
|---|
| 78 |
{ |
|---|
| 79 |
if( isxdigit( **input ) == 0 ) |
|---|
| 80 |
return -1; |
|---|
| 81 |
|
|---|
| 82 |
if( isdigit( **input ) == 0 ) |
|---|
| 83 |
*output = (toupper( **input ) - 7) * 16; |
|---|
| 84 |
else |
|---|
| 85 |
*output = **input * 16; |
|---|
| 86 |
|
|---|
| 87 |
(*input)++; |
|---|
| 88 |
|
|---|
| 89 |
if( isxdigit( **input ) == 0 ) |
|---|
| 90 |
return -1; |
|---|
| 91 |
|
|---|
| 92 |
if( isdigit( **input ) == 0 ) |
|---|
| 93 |
*output |= toupper( **input ) - 0x37; |
|---|
| 94 |
else |
|---|
| 95 |
*output |= **input - 0x30; |
|---|
| 96 |
|
|---|
| 97 |
(*input)++; |
|---|
| 98 |
} |
|---|
| 99 |
else if( encoding_type == 2 ) |
|---|
| 100 |
{ |
|---|
| 101 |
unsigned char **uinput = (unsigned char **)input; |
|---|
| 102 |
|
|---|
| 103 |
if( **uinput > 127 || inverse[ **uinput ] == 0xFF ) |
|---|
| 104 |
return -1; |
|---|
| 105 |
|
|---|
| 106 |
if( *k == 0 ) |
|---|
| 107 |
{ |
|---|
| 108 |
if( (*uinput)[ 1 ] > 127 || inverse[ (*uinput)[ 1 ] ] == 0xFF ) |
|---|
| 109 |
return -1; |
|---|
| 110 |
|
|---|
| 111 |
*output = (inverse[ (*uinput)[ 0 ] ] * 4) | |
|---|
| 112 |
(inverse[ (*uinput)[ 1 ] ] / 16); |
|---|
| 113 |
|
|---|
| 114 |
*j = inverse[ (*uinput)[ 1 ] ] * 16; |
|---|
| 115 |
*k = 4; |
|---|
| 116 |
|
|---|
| 117 |
(*uinput) += 2; |
|---|
| 118 |
} |
|---|
| 119 |
else if( *k == 2 ) |
|---|
| 120 |
{ |
|---|
| 121 |
*output = *j | inverse[ **uinput ]; |
|---|
| 122 |
|
|---|
| 123 |
*j = 0; |
|---|
| 124 |
*k = 0; |
|---|
| 125 |
|
|---|
| 126 |
(*uinput)++; |
|---|
| 127 |
} |
|---|
| 128 |
else if( *k == 4 ) |
|---|
| 129 |
{ |
|---|
| 130 |
*output = (inverse[ **uinput ] / 4) | *j; |
|---|
| 131 |
|
|---|
| 132 |
*j = inverse[ **uinput ] * 64; |
|---|
| 133 |
*k = 2; |
|---|
| 134 |
|
|---|
| 135 |
(*uinput)++; |
|---|
| 136 |
} |
|---|
| 137 |
} |
|---|
| 138 |
|
|---|
| 139 |
return 0; |
|---|
| 140 |
} |
|---|
| 141 |
|
|---|
| 142 |
static char *nscdec( vlc_object_t *p_demux, char* p_encoded ) |
|---|
| 143 |
{ |
|---|
| 144 |
unsigned int i; |
|---|
| 145 |
unsigned char tmp; |
|---|
| 146 |
unsigned char j, k; |
|---|
| 147 |
unsigned int length; |
|---|
| 148 |
unsigned char encoding_type; |
|---|
| 149 |
|
|---|
| 150 |
vlc_iconv_t conv; |
|---|
| 151 |
size_t buf16_size; |
|---|
| 152 |
unsigned char *buf16; |
|---|
| 153 |
const char *p_buf16; |
|---|
| 154 |
size_t buf8_size; |
|---|
| 155 |
char *buf8; |
|---|
| 156 |
char *p_buf8; |
|---|
| 157 |
|
|---|
| 158 |
char *p_input = p_encoded; |
|---|
| 159 |
|
|---|
| 160 |
if( strlen( p_input ) < 15 ) |
|---|
| 161 |
{ |
|---|
| 162 |
msg_Err( p_demux, "input string less than 15 characters" ); |
|---|
| 163 |
return NULL; |
|---|
| 164 |
} |
|---|
| 165 |
|
|---|
| 166 |
if( load_byte( 1, &encoding_type, &p_input, NULL, NULL ) ) |
|---|
| 167 |
{ |
|---|
| 168 |
msg_Err( p_demux, "unable to get NSC encoding type" ); |
|---|
| 169 |
return NULL; |
|---|
| 170 |
} |
|---|
| 171 |
|
|---|
| 172 |
if( encoding_type != 1 && encoding_type != 2 ) |
|---|
| 173 |
{ |
|---|
| 174 |
msg_Err( p_demux, "encoding type %d is not supported", |
|---|
| 175 |
encoding_type ); |
|---|
| 176 |
return NULL; |
|---|
| 177 |
} |
|---|
| 178 |
|
|---|
| 179 |
j = k = 0; |
|---|
| 180 |
|
|---|
| 181 |
if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) ) |
|---|
| 182 |
{ |
|---|
| 183 |
msg_Err( p_demux, "load_byte failed" ); |
|---|
| 184 |
return NULL; |
|---|
| 185 |
} |
|---|
| 186 |
|
|---|
| 187 |
for( i = 0; i < 4; i++ ) |
|---|
| 188 |
{ |
|---|
| 189 |
if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) ) |
|---|
| 190 |
{ |
|---|
| 191 |
msg_Err( p_demux, "load_byte failed" ); |
|---|
| 192 |
return NULL; |
|---|
| 193 |
} |
|---|
| 194 |
} |
|---|
| 195 |
|
|---|
| 196 |
length = 0; |
|---|
| 197 |
for( i = 4; i; i-- ) |
|---|
| 198 |
{ |
|---|
| 199 |
if( load_byte( encoding_type, &tmp, &p_input, &j, &k ) ) |
|---|
| 200 |
{ |
|---|
| 201 |
msg_Err( p_demux, "load_byte failed" ); |
|---|
| 202 |
return NULL; |
|---|
| 203 |
} |
|---|
| 204 |
length |= tmp << ((i - 1) * 8); |
|---|
| 205 |
} |
|---|
| 206 |
|
|---|
| 207 |
if( length == 0 ) |
|---|
| 208 |
{ |
|---|
| 209 |
msg_Err( p_demux, "Length is 0" ); |
|---|
| 210 |
return NULL; |
|---|
| 211 |
} |
|---|
| 212 |
|
|---|
| 213 |
buf16_size = length; |
|---|
| 214 |
buf16 = malloc( buf16_size ); |
|---|
| 215 |
if( buf16 == NULL ) |
|---|
| 216 |
return NULL; |
|---|
| 217 |
|
|---|
| 218 |
for( i = 0; i < length; i++ ) |
|---|
| 219 |
{ |
|---|
| 220 |
if( load_byte( encoding_type, &buf16[ i ], &p_input, &j, &k ) ) |
|---|
| 221 |
{ |
|---|
| 222 |
msg_Err( p_demux, "load_byte failed" ); |
|---|
| 223 |
free( buf16 ); |
|---|
| 224 |
return NULL; |
|---|
| 225 |
} |
|---|
| 226 |
} |
|---|
| 227 |
|
|---|
| 228 |
buf8_size = length; |
|---|
| 229 |
buf8 = malloc( buf8_size + 1 ); |
|---|
| 230 |
if( buf8 == NULL ) |
|---|
| 231 |
{ |
|---|
| 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 |
const uint8_t *p_peek; |
|---|
| 268 |
int i_size; |
|---|
| 269 |
|
|---|
| 270 |
|
|---|
| 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 |
|
|---|
| 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 |
|
|---|
| 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; |
|---|
| 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 |
|
|---|
| 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 |
free( psz_out ); |
|---|
| 341 |
} |
|---|
| 342 |
} |
|---|
| 343 |
return VLC_SUCCESS; |
|---|
| 344 |
} |
|---|
| 345 |
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
| 348 |
|
|---|
| 349 |
|
|---|
| 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 |
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 |
} |
|---|