| 1 |
/* Copyright (C) 2008, 2009 Vincent Penquerc'h. |
| 2 |
This file is part of the Kate codec library. |
| 3 |
Written by Vincent Penquerc'h. |
| 4 |
|
| 5 |
Use, distribution and reproduction of this library is governed |
| 6 |
by a BSD style source license included with this source in the |
| 7 |
file 'COPYING'. Please read these terms before distributing. */ |
| 8 |
|
| 9 |
|
| 10 |
#ifdef HAVE_CONFIG_H |
| 11 |
#include "config.h" |
| 12 |
#endif |
| 13 |
#ifdef HAVE_UNISTD_H |
| 14 |
#include <unistd.h> |
| 15 |
#endif |
| 16 |
#include <stdarg.h> |
| 17 |
#include <stdio.h> |
| 18 |
#ifdef HAVE_STDLIB_H |
| 19 |
#include <stdlib.h> |
| 20 |
#endif |
| 21 |
#include <errno.h> |
| 22 |
#ifdef HAVE_STRING_H |
| 23 |
#include <string.h> |
| 24 |
#endif |
| 25 |
#ifdef HAVE_CTYPE_H |
| 26 |
#include <ctype.h> |
| 27 |
#endif |
| 28 |
#include <ogg/ogg.h> |
| 29 |
#include "kate/oggkate.h" |
| 30 |
#include "kstream.h" |
| 31 |
#include "kread.h" |
| 32 |
#include "kutil.h" |
| 33 |
#include "kstrings.h" |
| 34 |
#include "kstats.h" |
| 35 |
|
| 36 |
typedef struct { |
| 37 |
kate_stream_set kate_streams; |
| 38 |
int n_streams; |
| 39 |
|
| 40 |
katalyzer_stats **stats; |
| 41 |
size_t nstats; |
| 42 |
} ogg_parser_data; |
| 43 |
|
| 44 |
typedef enum { |
| 45 |
klt_misc, |
| 46 |
klt_error, |
| 47 |
klt_packet, |
| 48 |
klt_container, |
| 49 |
klt_timing, |
| 50 |
klt_text, |
| 51 |
klt_event, |
| 52 |
klt_dump, |
| 53 |
klt_stats, |
| 54 |
} katalyzer_log_type; |
| 55 |
|
| 56 |
static const struct { |
| 57 |
katalyzer_log_type type; |
| 58 |
char id; |
| 59 |
int by_default; |
| 60 |
const char *tag; |
| 61 |
const char *desc; |
| 62 |
} log_types_info[]={ |
| 63 |
{ klt_misc, '-', 1, "misc", "All the rest" }, |
| 64 |
{ klt_error, 'e', 1, "error", "Errors" }, |
| 65 |
{ klt_packet, 'p', 1, "pack", "Packet information" }, |
| 66 |
{ klt_container, 'c', 0, "cont", "Container specific information (eg, Ogg)" }, |
| 67 |
{ klt_timing, 'T', 1, "time", "Timing information" }, |
| 68 |
{ klt_text, 't', 1, "text", "Text information" }, |
| 69 |
{ klt_event, 'v', 0, "event", "Event information" }, |
| 70 |
{ klt_dump, 'd', 0, "dump", "Dump pages and packets" }, |
| 71 |
{ klt_stats, 'S', 0, "stats", "Statistics about the Kate stream" }, |
| 72 |
}; |
| 73 |
static char *log_types=NULL; |
| 74 |
|
| 75 |
static int is_log_type_enabled(katalyzer_log_type type) |
| 76 |
{ |
| 77 |
return (strchr(log_types,log_types_info[type].id)!=NULL); |
| 78 |
} |
| 79 |
|
| 80 |
static void print_log_header(const kate_stream *ks,katalyzer_log_type type) |
| 81 |
{ |
| 82 |
printf("[%-5s] ",log_types_info[type].tag); |
| 83 |
if (ks) { |
| 84 |
printf("[%08x] ",(int)ks->os.serialno); |
| 85 |
} |
| 86 |
else { |
| 87 |
printf("[--------] "); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
static void kprintf(const kate_stream *ks,katalyzer_log_type type,const char *format,...) |
| 92 |
{ |
| 93 |
va_list ap; |
| 94 |
|
| 95 |
if (!is_log_type_enabled(type)) return; |
| 96 |
|
| 97 |
print_log_header(ks,type); |
| 98 |
va_start(ap,format); |
| 99 |
vprintf(format,ap); |
| 100 |
va_end(ap); |
| 101 |
fflush(stdout); |
| 102 |
} |
| 103 |
|
| 104 |
static const char *packet_type_to_string(unsigned char type) |
| 105 |
{ |
| 106 |
switch (type) { |
| 107 |
case 0x80: return "ID header"; |
| 108 |
case 0x81: return "Vorbis comment header"; |
| 109 |
case 0x82: return "regions header"; |
| 110 |
case 0x83: return "styles header"; |
| 111 |
case 0x84: return "curves header"; |
| 112 |
case 0x85: return "motions header"; |
| 113 |
case 0x86: return "palettes header"; |
| 114 |
case 0x87: return "bitmaps header"; |
| 115 |
case 0x88: return "font ranges"; |
| 116 |
|
| 117 |
case 0x00: return "text data"; |
| 118 |
case 0x01: return "keepalive"; |
| 119 |
case 0x02: return "repeat"; |
| 120 |
case 0x7f: return "eos packet"; |
| 121 |
|
| 122 |
default: return "unknown"; |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
static void katalyzer_dump_data(kate_stream *ks,katalyzer_log_type type,const char *header,const unsigned char *data,size_t len) |
| 127 |
{ |
| 128 |
size_t offset=0; |
| 129 |
|
| 130 |
if (!strchr(log_types,log_types_info[type].id)) return; |
| 131 |
|
| 132 |
print_log_header(ks,type); |
| 133 |
printf("%s (%lu bytes):\n",header?header:"data",(unsigned long)len); |
| 134 |
|
| 135 |
while (len>0) { |
| 136 |
size_t n,limit; |
| 137 |
print_log_header(ks,type); |
| 138 |
printf("%08lx: ",(unsigned long)offset); |
| 139 |
limit=len>16?16:len; |
| 140 |
for (n=0;n<16;++n) { |
| 141 |
if (n<limit) |
| 142 |
printf("%02x ",data[n]); |
| 143 |
else |
| 144 |
printf(" "); |
| 145 |
if (n==7) printf(" "); |
| 146 |
} |
| 147 |
printf(" "); |
| 148 |
for (n=0;n<limit;++n) printf("%c",isprint(data[n])?data[n]:'.'); |
| 149 |
printf("\n"); |
| 150 |
len-=limit; |
| 151 |
offset+=limit; |
| 152 |
data+=limit; |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
static void katalyzer_on_ogg_packet(kate_stream *ks,ogg_packet *op,int is_kate) |
| 157 |
{ |
| 158 |
char stype[64]; |
| 159 |
|
| 160 |
if (is_kate && op->bytes>0) { |
| 161 |
snprintf(stype,sizeof(stype),", type %02x (%s)",op->packet[0],packet_type_to_string(op->packet[0])); |
| 162 |
} |
| 163 |
else { |
| 164 |
strcpy(stype,""); |
| 165 |
} |
| 166 |
kprintf(ks,klt_packet,"packet %"PRId64", %ld bytes%s\n", |
| 167 |
op->packetno, |
| 168 |
(long)op->bytes, |
| 169 |
stype); |
| 170 |
|
| 171 |
katalyzer_dump_data(ks,klt_dump,"Packet data",op->packet,op->bytes); |
| 172 |
} |
| 173 |
|
| 174 |
static size_t get_num_glyphs(const kate_event *ev) |
| 175 |
{ |
| 176 |
size_t nglyphs=0; |
| 177 |
const char *text=ev->text; |
| 178 |
size_t rlen0=ev->len0; |
| 179 |
while (kate_text_get_character(ev->text_encoding,&text,&rlen0)>0) |
| 180 |
++nglyphs; |
| 181 |
return nglyphs; |
| 182 |
} |
| 183 |
|
| 184 |
static void katalyzer_on_event(kate_stream *ks,const kate_event *ev) |
| 185 |
{ |
| 186 |
int ret; |
| 187 |
|
| 188 |
kprintf(ks,klt_misc,"An event was found\n"); |
| 189 |
|
| 190 |
kprintf(ks,klt_timing,"event start: %f (%"PRId64")\n",ev->start_time,ev->start); |
| 191 |
kprintf(ks,klt_timing,"event end: %f\n",ev->end_time); |
| 192 |
kprintf(ks,klt_timing,"event duration: %f (%"PRId64")\n",ev->end_time-ev->start_time,ev->duration); |
| 193 |
kprintf(ks,klt_timing,"event backlink: %f (%"PRId64")\n",kate_granule_duration(ev->ki,ev->backlink),ev->backlink); |
| 194 |
|
| 195 |
kprintf(ks,klt_text,"Text encoding %d (%s)\n",ev->text_encoding,encoding2text(ev->text_encoding)); |
| 196 |
kprintf(ks,klt_text,"Text language \"%s\" (%s)\n",ev->language?ev->language:ev->ki->language,ev->language?"overridden":"default"); |
| 197 |
kprintf(ks,klt_text,"Text directionality %d (%s)\n",ev->text_directionality,directionality2text(ev->text_directionality)); |
| 198 |
|
| 199 |
ret=kate_text_validate(ev->text_encoding,ev->text,ev->len0); |
| 200 |
if (ret<0) { |
| 201 |
kprintf(ks,klt_error,"Text length %lu bytes, invalid\n",(unsigned long)ev->len); |
| 202 |
} |
| 203 |
else { |
| 204 |
kprintf(ks,klt_text,"Text length %lu bytes, %lu glyphs\n",(unsigned long)ev->len,(unsigned long)get_num_glyphs(ev)); |
| 205 |
} |
| 206 |
|
| 207 |
katalyzer_dump_data(ks,klt_text,"Event text",(const unsigned char*)ev->text,ev->len); |
| 208 |
} |
| 209 |
|
| 210 |
static void add_stats(ogg_parser_data *opd,kate_stream *ks) |
| 211 |
{ |
| 212 |
katalyzer_stats *stats=katalyzer_stats_create(ks); |
| 213 |
if (stats) { |
| 214 |
opd->stats=(katalyzer_stats**)kate_realloc(opd->stats,(opd->nstats+1)*sizeof(katalyzer_stats*)); |
| 215 |
if (opd->stats) { |
| 216 |
opd->stats[opd->nstats++]=stats; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
static katalyzer_stats *find_stats(ogg_parser_data *opd,const kate_stream *ks) |
| 222 |
{ |
| 223 |
size_t n; |
| 224 |
|
| 225 |
for (n=0;n<opd->nstats;++n) { |
| 226 |
katalyzer_stats *stats=opd->stats[n]; |
| 227 |
if (katalyzer_stats_matches(stats,ks)) { |
| 228 |
return stats; |
| 229 |
} |
| 230 |
} |
| 231 |
|
| 232 |
return NULL; |
| 233 |
} |
| 234 |
|
| 235 |
static void add_to_stats(ogg_parser_data *opd,kate_stream *ks,ogg_packet *op) |
| 236 |
{ |
| 237 |
katalyzer_stats *stats=find_stats(opd,ks); |
| 238 |
if (stats) { |
| 239 |
kate_packet kp; |
| 240 |
kate_packet_wrap(&kp,op->bytes,op->packet); |
| 241 |
katalyzer_stats_add_packet(stats,&kp); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
static int ogg_parser_on_page(kate_uintptr_t data,long offset,ogg_page *og) |
| 246 |
{ |
| 247 |
ogg_parser_data *opd=(ogg_parser_data*)data; |
| 248 |
ogg_packet op; |
| 249 |
int ret; |
| 250 |
kate_stream *ks; |
| 251 |
int is_kate; |
| 252 |
|
| 253 |
/* add the page in the correct stream */ |
| 254 |
if (ogg_page_bos(og)) { |
| 255 |
add_kate_stream(&opd->kate_streams,og,opd->n_streams++); |
| 256 |
ks=find_kate_stream_for_page(&opd->kate_streams,og); |
| 257 |
if (ks) add_stats(opd,ks); |
| 258 |
} |
| 259 |
else { |
| 260 |
ks=find_kate_stream_for_page(&opd->kate_streams,og); |
| 261 |
} |
| 262 |
|
| 263 |
if (ks) { |
| 264 |
kprintf(NULL,klt_container,"Ogg page at offset %lx, granpos %016"PRIx64", %d bytes, %d packets on this page\n", |
| 265 |
offset,ogg_page_granulepos(og), |
| 266 |
og->header_len+og->body_len,ogg_page_packets(og)); |
| 267 |
|
| 268 |
katalyzer_dump_data(ks,klt_dump,"Page header",og->header,og->header_len); |
| 269 |
katalyzer_dump_data(ks,klt_dump,"Page body",og->body,og->body_len); |
| 270 |
|
| 271 |
while ((ret=ogg_stream_packetout(&ks->os,&op))) { |
| 272 |
if (ret<0) { |
| 273 |
kprintf(ks,klt_error,"Hole in data\n"); |
| 274 |
continue; |
| 275 |
} |
| 276 |
is_kate=(ks->init>=kstream_header_info); |
| 277 |
if (!is_kate) { |
| 278 |
kate_packet kp; |
| 279 |
kate_packet_wrap(&kp,op.bytes,op.packet); |
| 280 |
if (kate_decode_is_idheader(&kp)) is_kate=1; |
| 281 |
} |
| 282 |
katalyzer_on_ogg_packet(ks,&op,is_kate); |
| 283 |
|
| 284 |
add_to_stats(opd,ks,&op); |
| 285 |
|
| 286 |
if (ks->init<kstream_data) { |
| 287 |
ret=kate_ogg_decode_headerin(&ks->ki,&ks->kc,&op); |
| 288 |
if (ret>=0) { |
| 289 |
/* we found a Kate bitstream */ |
| 290 |
if (op.packetno==0) { |
| 291 |
kprintf(ks,klt_misc,"Bitstream looks like Kate\n"); |
| 292 |
ks->init=kstream_header_info; |
| 293 |
} |
| 294 |
if (ret>0) { |
| 295 |
/* we're done parsing headers, go for data */ |
| 296 |
kprintf(ks,klt_misc,"Bitstream is Kate (bitstream v%d.%d, language \"%s\" category \"%s\", encoding %d (%s))\n", |
| 297 |
ks->ki.bitstream_version_major,ks->ki.bitstream_version_minor, |
| 298 |
ks->ki.language, |
| 299 |
ks->ki.category, |
| 300 |
ks->ki.text_encoding, |
| 301 |
encoding2text(ks->ki.text_encoding)); |
| 302 |
|
| 303 |
ret=kate_decode_init(&ks->k,&ks->ki); |
| 304 |
if (ret<0) { |
| 305 |
kprintf(ks,klt_error,"kate_decode_init failed: %d\n",ret); |
| 306 |
exit(-1); |
| 307 |
} |
| 308 |
ks->init=kstream_data; |
| 309 |
kprintf(ks,klt_misc,"headers parsed\n"); |
| 310 |
} |
| 311 |
} |
| 312 |
else { |
| 313 |
if (ret==KATE_E_NOT_KATE) { |
| 314 |
kprintf(ks,klt_misc,"Bitstream is not Kate\n"); |
| 315 |
} |
| 316 |
else { |
| 317 |
kprintf(ks,klt_error,"kate_decode_headerin: packetno %"PRId64": %d\n",op.packetno,ret); |
| 318 |
ks->ret=ret; |
| 319 |
} |
| 320 |
clear_and_remove_kate_stream(ks,&opd->kate_streams); |
| 321 |
break; |
| 322 |
} |
| 323 |
} |
| 324 |
else { |
| 325 |
ret=kate_ogg_decode_packetin(&ks->k,&op); |
| 326 |
if (ret<0) { |
| 327 |
kprintf(ks,klt_error,"error in kate_decode_packetin: %d\n",ret); |
| 328 |
ks->ret=ret; |
| 329 |
} |
| 330 |
else if (ret>0) { |
| 331 |
/* we're done */ |
| 332 |
kprintf(ks,klt_misc,"eof packet\n"); |
| 333 |
break; |
| 334 |
} |
| 335 |
else { |
| 336 |
const kate_event *ev=NULL; |
| 337 |
ret=kate_decode_eventout(&ks->k,&ev); |
| 338 |
if (ret<0) { |
| 339 |
kprintf(ks,klt_error,"error in kate_decode_eventout: %d\n",ret); |
| 340 |
ks->ret=ret; |
| 341 |
} |
| 342 |
else if (ret>0) { |
| 343 |
kprintf(ks,klt_event,"No event to go with this packet\n"); |
| 344 |
} |
| 345 |
else if (ret==0) { |
| 346 |
katalyzer_on_event(ks,ev); |
| 347 |
} |
| 348 |
} |
| 349 |
} |
| 350 |
} |
| 351 |
} |
| 352 |
|
| 353 |
return 0; |
| 354 |
} |
| 355 |
|
| 356 |
static int ogg_parser_on_hole(kate_uintptr_t data,long offset,long size) |
| 357 |
{ |
| 358 |
(void)data; |
| 359 |
kprintf(NULL,klt_error,"Hole in data at offset %lx: %ld bytes\n",offset,size); |
| 360 |
return 0; |
| 361 |
} |
| 362 |
|
| 363 |
static void katalyzer_output_stats(const katalyzer_stats *stats) |
| 364 |
{ |
| 365 |
const kate_stream *ks=stats->ks; |
| 366 |
int n; |
| 367 |
|
| 368 |
kprintf(ks,klt_stats,"Statistics:\n"); |
| 369 |
kprintf(ks,klt_stats,"Stream: %"PRId64" bytes, %"PRId64" packets\n",stats->stream_length,stats->n_packets_total); |
| 370 |
for (n=0;n<256;++n) { |
| 371 |
int type=n^0x80; |
| 372 |
if (stats->n_packets[type]==0) continue; |
| 373 |
kprintf(ks,klt_stats,"Packet type %02x: %"PRId64" packets, size %"PRId64" - %"PRId64" (acc %"PRId64", %.2f%% total)\n", |
| 374 |
type,stats->n_packets[type], |
| 375 |
stats->min_size[type],stats->max_size[type], |
| 376 |
stats->total_size[type],stats->total_size[type]*100.0f/stats->stream_length); |
| 377 |
} |
| 378 |
} |
| 379 |
|
| 380 |
#if 0 /* UNUSED METHOD */ |
| 381 |
static void katalyzer_output_stats_for_stream(ogg_parser_data *opd,kate_stream *ks) |
| 382 |
{ |
| 383 |
const katalyzer_stats *stats=find_stats(opd,ks); |
| 384 |
if (stats) { |
| 385 |
katalyzer_output_stats(stats); |
| 386 |
} |
| 387 |
} |
| 388 |
#endif |
| 389 |
|
| 390 |
static void output_all_stats(ogg_parser_data *opd) |
| 391 |
{ |
| 392 |
size_t n; |
| 393 |
|
| 394 |
for (n=0;n<opd->nstats;++n) { |
| 395 |
const katalyzer_stats *stats=opd->stats[n]; |
| 396 |
const kate_stream *ks=stats->ks; |
| 397 |
int is_kate=(ks->init>=kstream_header_info); |
| 398 |
if (is_kate) { |
| 399 |
katalyzer_output_stats(opd->stats[n]); |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
static int type_sorter(const void *e1,const void *e2) |
| 405 |
{ |
| 406 |
const char *c1=(const char*)e1; |
| 407 |
const char *c2=(const char*)e2; |
| 408 |
return *c1-*c2; |
| 409 |
} |
| 410 |
|
| 411 |
static void add_log_types(char **log_types,const char *new_log_types) |
| 412 |
{ |
| 413 |
char *ptr,*w,prevc=0; |
| 414 |
|
| 415 |
if (*log_types) { |
| 416 |
*log_types=(char*)kate_realloc(*log_types,strlen(*log_types)+strlen(new_log_types)+1); |
| 417 |
} |
| 418 |
else { |
| 419 |
*log_types=(char*)kate_malloc(strlen(new_log_types)+1); |
| 420 |
if (*log_types) **log_types=0; |
| 421 |
} |
| 422 |
|
| 423 |
if (!*log_types) { |
| 424 |
fprintf(stderr,"Failed to allocate memory for log types\n"); |
| 425 |
exit(-1); |
| 426 |
} |
| 427 |
strcat(*log_types,new_log_types); |
| 428 |
|
| 429 |
/* now sort them */ |
| 430 |
qsort(*log_types,strlen(*log_types),1,&type_sorter); |
| 431 |
|
| 432 |
/* uniq */ |
| 433 |
ptr=w=*log_types; |
| 434 |
while (*ptr) { |
| 435 |
if (*ptr!=prevc) *w++=*ptr; |
| 436 |
prevc=*ptr++; |
| 437 |
} |
| 438 |
*w=0; |
| 439 |
} |
| 440 |
|
| 441 |
static char *get_default_log_types(void) |
| 442 |
{ |
| 443 |
size_t n,idx; |
| 444 |
char *types=(char*)kate_malloc(sizeof(log_types_info)/sizeof(log_types_info[0])+1); |
| 445 |
if (!types) { |
| 446 |
fprintf(stderr,"Failed to allocate memory for default log types\n"); |
| 447 |
exit(-1); |
| 448 |
} |
| 449 |
for (n=0,idx=0;n<sizeof(log_types_info)/sizeof(log_types_info[0]);++n) { |
| 450 |
if (log_types_info[n].by_default) { |
| 451 |
types[idx++]=log_types_info[n].id; |
| 452 |
} |
| 453 |
} |
| 454 |
types[idx]=0; |
| 455 |
return types; |
| 456 |
} |
| 457 |
|
| 458 |
static char *get_all_log_types(void) |
| 459 |
{ |
| 460 |
size_t n; |
| 461 |
char *types=(char*)kate_malloc(sizeof(log_types_info)/sizeof(log_types_info[0])+1); |
| 462 |
if (!types) { |
| 463 |
fprintf(stderr,"Failed to allocate memory for all log types\n"); |
| 464 |
exit(-1); |
| 465 |
} |
| 466 |
for (n=0;n<sizeof(log_types_info)/sizeof(log_types_info[0]);++n) { |
| 467 |
types[n]=log_types_info[n].id; |
| 468 |
} |
| 469 |
types[n]=0; |
| 470 |
return types; |
| 471 |
} |
| 472 |
|
| 473 |
static void init_ogg_parser_data(ogg_parser_data *opd) |
| 474 |
{ |
| 475 |
init_kate_stream_set(&opd->kate_streams); |
| 476 |
opd->n_streams=0; |
| 477 |
opd->stats=NULL; |
| 478 |
opd->nstats=0; |
| 479 |
} |
| 480 |
|
| 481 |
static void clear_ogg_parser_data(ogg_parser_data *opd) |
| 482 |
{ |
| 483 |
size_t n; |
| 484 |
|
| 485 |
clear_kate_stream_set(&opd->kate_streams); |
| 486 |
for (n=0;n<opd->nstats;++n) katalyzer_stats_destroy(opd->stats[n]); |
| 487 |
kate_free(opd->stats); |
| 488 |
} |
| 489 |
|
| 490 |
static void print_version(void) |
| 491 |
{ |
| 492 |
printf("Katalyzer, a Kate stream analyzer - %s\n",kate_get_version_string()); |
| 493 |
} |
| 494 |
|
| 495 |
static void print_help(const char *argv0) |
| 496 |
{ |
| 497 |
size_t n; |
| 498 |
|
| 499 |
printf("usage: %s [options] [filename]\n",argv0); |
| 500 |
printf(" -V version\n"); |
| 501 |
printf(" -h help\n"); |
| 502 |
printf(" -l <types> select information to log\n"); |
| 503 |
printf(" -l! log everything\n"); |
| 504 |
for (n=0;n<sizeof(log_types_info)/sizeof(log_types_info[0]);++n) { |
| 505 |
printf(" %c %s (default %s)\n", |
| 506 |
log_types_info[n].id, |
| 507 |
log_types_info[n].desc, |
| 508 |
log_types_info[n].by_default?"on":"off"); |
| 509 |
} |
| 510 |
} |
| 511 |
|
| 512 |
int main(int argc,char **argv) |
| 513 |
{ |
| 514 |
size_t bytes_read; |
| 515 |
const char *input_filename=NULL; |
| 516 |
const char *new_log_types; |
| 517 |
FILE *fin; |
| 518 |
int arg; |
| 519 |
char signature[64]; /* matches the size of the Kate ID header */ |
| 520 |
size_t signature_size; |
| 521 |
char *buffer=NULL; |
| 522 |
ogg_int64_t bytes; |
| 523 |
int headers_written=0; |
| 524 |
ogg_parser_data opd; |
| 525 |
ogg_parser_funcs opf; |
| 526 |
|
| 527 |
for (arg=1;arg<argc;++arg) { |
| 528 |
if (argv[arg][0]=='-') { |
| 529 |
switch (argv[arg][1]) { |
| 530 |
case '-': |
| 531 |
if (!strcmp(argv[arg],"--version")) { |
| 532 |
print_version(); |
| 533 |
exit(0); |
| 534 |
} |
| 535 |
else if (!strcmp(argv[arg],"--help")) { |
| 536 |
print_help(argv[0]); |
| 537 |
exit(0); |
| 538 |
} |
| 539 |
else { |
| 540 |
fprintf(stderr,"Invalid option: %s\n",argv[arg]); |
| 541 |
exit(-1); |
| 542 |
} |
| 543 |
break; |
| 544 |
case 'V': |
| 545 |
print_version(); |
| 546 |
exit(0); |
| 547 |
case 'h': |
| 548 |
print_version(); |
| 549 |
print_help(argv[0]); |
| 550 |
exit(0); |
| 551 |
case 'l': |
| 552 |
switch (argv[arg][2]) { |
| 553 |
case '!': |
| 554 |
if (log_types) kate_free(log_types); |
| 555 |
log_types=get_all_log_types(); |
| 556 |
break; |
| 557 |
case 0: |
| 558 |
new_log_types=eat_arg(argc,argv,&arg); |
| 559 |
add_log_types(&log_types,new_log_types); |
| 560 |
break; |
| 561 |
default: |
| 562 |
add_log_types(&log_types,argv[arg]+2); |
| 563 |
break; |
| 564 |
} |
| 565 |
break; |
| 566 |
default: |
| 567 |
fprintf(stderr,"Invalid option: %s\n",argv[arg]); |
| 568 |
exit(-1); |
| 569 |
} |
| 570 |
} |
| 571 |
else { |
| 572 |
if (!input_filename) { |
| 573 |
input_filename=argv[arg]; |
| 574 |
} |
| 575 |
else { |
| 576 |
fprintf(stderr,"Only one input filename may be converted at a time\n"); |
| 577 |
exit(-1); |
| 578 |
} |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
/* defaults if no log types are specified */ |
| 583 |
if (!log_types) log_types=get_default_log_types(); |
| 584 |
|
| 585 |
fin=open_and_probe_stream(input_filename); |
| 586 |
if (!fin) exit(-1); |
| 587 |
|
| 588 |
/* first, read the first few bytes to know if we have a raw Kate stream |
| 589 |
or a Kate-in-Ogg stream */ |
| 590 |
bytes_read=fread(signature,1,sizeof(signature),fin); |
| 591 |
if (bytes_read!=sizeof(signature)) { |
| 592 |
/* A Kate stream's first packet is 64 bytes, so this cannot be one */ |
| 593 |
fprintf(stderr,"Failed to read first %lu bytes of stream\n",(unsigned long)sizeof(signature)); |
| 594 |
exit(-1); |
| 595 |
} |
| 596 |
|
| 597 |
if (!memcmp(signature,"\200kate\0\0\0",8)) { |
| 598 |
/* raw Kate stream */ |
| 599 |
kate_state k; |
| 600 |
int ret; |
| 601 |
|
| 602 |
bytes=64; |
| 603 |
buffer=(char*)kate_malloc(bytes); |
| 604 |
if (!buffer) { |
| 605 |
fprintf(stderr,"failed to allocate %"PRId64" bytes\n",bytes); |
| 606 |
exit(-1); |
| 607 |
} |
| 608 |
memcpy(buffer,signature,bytes); |
| 609 |
|
| 610 |
ret=kate_high_decode_init(&k); |
| 611 |
if (ret<0) { |
| 612 |
fprintf(stderr,"failed to init raw kate packet decoding (%d)\n",ret); |
| 613 |
exit(-1); |
| 614 |
} |
| 615 |
|
| 616 |
while (1) { |
| 617 |
const kate_event *ev=NULL; |
| 618 |
kate_packet kp; |
| 619 |
kate_packet_wrap(&kp,bytes,buffer); |
| 620 |
ret=kate_high_decode_packetin(&k,&kp,&ev); |
| 621 |
if (ret<0) { |
| 622 |
fprintf(stderr,"failed to decode raw kate packet (%d)\n",ret); |
| 623 |
exit(-1); |
| 624 |
} |
| 625 |
if (k.ki->probe<0 && !headers_written) { |
| 626 |
kprintf(NULL,klt_misc,"headers parsed\n"); |
| 627 |
headers_written=1; |
| 628 |
} |
| 629 |
if (ret>0) { |
| 630 |
kprintf(NULL,klt_misc,"eos\n"); |
| 631 |
break; /* last packet decoded */ |
| 632 |
} |
| 633 |
if (ev) { |
| 634 |
kprintf(NULL,klt_misc,"event found\n"); |
| 635 |
} |
| 636 |
|
| 637 |
/* all subsequent packets are prefixed with 64 bits (signed) of the packet length in bytes */ |
| 638 |
ret=read_raw_size_and_packet(fin,&buffer,&bytes); |
| 639 |
if (ret<0) exit(-1); |
| 640 |
} |
| 641 |
|
| 642 |
ret=kate_high_decode_clear(&k); |
| 643 |
if (ret<0) { |
| 644 |
fprintf(stderr,"kate_high_decode_clear failed: %d\n",ret); |
| 645 |
/* continue anyway */ |
| 646 |
} |
| 647 |
kate_free(buffer); |
| 648 |
} |
| 649 |
else { |
| 650 |
int ret=-1; |
| 651 |
signature_size=bytes_read; |
| 652 |
|
| 653 |
init_ogg_parser_data(&opd); |
| 654 |
|
| 655 |
opf.on_page=&ogg_parser_on_page; |
| 656 |
opf.on_hole=&ogg_parser_on_hole; |
| 657 |
ret=parse_ogg_stream(fin,signature,signature_size,opf,(kate_uintptr_t)&opd); |
| 658 |
if (ret<0) { |
| 659 |
fprintf(stderr,"parse_ogg_stream failed: %d\n",ret); |
| 660 |
/* continue anyway */ |
| 661 |
} |
| 662 |
|
| 663 |
output_all_stats(&opd); |
| 664 |
clear_ogg_parser_data(&opd); |
| 665 |
} |
| 666 |
|
| 667 |
if (log_types) kate_free(log_types); |
| 668 |
if (fin!=stdin) fclose(fin); |
| 669 |
|
| 670 |
return 0; |
| 671 |
} |