1 /* 2 * Copyright 1992 by Jutta Degener and Carsten Bormann, Technische 3 * Universitaet Berlin. See the accompanying file "COPYRIGHT" for 4 * details. THERE IS ABSOLUTELY NO WARRANTY FOR THIS SOFTWARE. 5 */ 6 7 /* $Header: /tmp_amd/presto/export/kbs/jutta/src/gsm/RCS/toast.c,v 1.8 1996/07/02 10:41:04 jutta Exp $ */ 8 9 #include "toast.h" 10 11 /* toast -- lossy sound compression using the gsm library. 12 */ 13 14 char * progname; 15 16 int f_decode = 0; /* decode rather than encode (-d) */ 17 int f_cat = 0; /* write to stdout; implies -p (-c) */ 18 int f_force = 0; /* don't ask about replacements (-f) */ 19 int f_precious = 0; /* avoid deletion of original (-p) */ 20 int f_fast = 0; /* use faster fpt algorithm (-F) */ 21 int f_verbose = 0; /* debugging (-V) */ 22 int f_ltp_cut = 0; /* LTP cut-off margin (-C) */ 23 24 struct stat instat; /* stat (inname) */ 25 26 FILE *in, *out; 27 char *inname, *outname; 28 29 /* 30 * The function (*output)() writes a frame of 160 samples given as 31 * 160 signed 16 bit values (gsm_signals) to <out>. 32 * The function (*input)() reads one such frame from <in>. 33 * The function (*init_output)() begins output (e.g. writes a header)., 34 * The function (*init_input)() begins input (e.g. skips a header). 35 * 36 * There are different versions of input, output, init_input and init_output 37 * for different formats understood by toast; which ones are used 38 * depends on the command line arguments and, in their absence, the 39 * filename; the fallback is #defined in toast.h 40 * 41 * The specific implementations of input, output, init_input and init_output 42 * for a format `foo' live in toast_foo.c. 43 */ 44 45 int (*output ) P((gsm_signal *)), 46 (*input ) P((gsm_signal *)); 47 int (*init_input) P((void)), 48 (*init_output) P((void)); 49 50 static int generic_init P0() { return 0; } /* NOP */ 51 52 struct fmtdesc { 53 54 char * name, * longname, * suffix; 55 56 int (* init_input ) P((void)), 57 (* init_output) P((void)); 58 59 int (* input ) P((gsm_signal * )), 60 (* output) P((gsm_signal * )); 61 62 } f_audio = { 63 "audio", 64 "8 kHz, 8 bit u-law encoding with Sun audio header", ".au", 65 audio_init_input, 66 audio_init_output, 67 ulaw_input, 68 ulaw_output 69 }, f_ulaw = { 70 "u-law", "plain 8 kHz, 8 bit u-law encoding", ".u", 71 generic_init, 72 generic_init, 73 ulaw_input, 74 ulaw_output 75 76 }, f_alaw = { 77 "A-law", "8 kHz, 8 bit A-law encoding", ".A", 78 generic_init, 79 generic_init, 80 alaw_input, 81 alaw_output 82 83 }, f_linear = { 84 "linear", 85 "16 bit (13 significant) signed 8 kHz signal", ".l", 86 generic_init, 87 generic_init, 88 linear_input, 89 linear_output 90 }; 91 92 struct fmtdesc * alldescs[] = { 93 &f_audio, 94 &f_alaw, 95 &f_ulaw, 96 &f_linear, 97 (struct fmtdesc *)NULL 98 }; 99 100 #define DEFAULT_FORMAT f_ulaw /* default audio format, others */ 101 /* are: f_alaw,f_audio,f_linear */ 102 struct fmtdesc * f_format = 0; 103 104 /* 105 * basename + suffix of a pathname 106 */ 107 static char * endname P1((name), char * name) 108 { 109 if (name) { 110 char * s = strrchr(name, '/'); 111 if (s && s[1]) name = s + 1; 112 } 113 return name; 114 115 } 116 117 /* 118 * Try to figure out what we're supposed to do from the argv[0], if 119 * any, and set the parameters accordingly. 120 */ 121 static void parse_argv0 P1((av0), char * av0 ) 122 { 123 int l; 124 125 progname = av0 = endname(av0 ? av0 : "toast"); 126 127 /* If the name starts with `un', we want to decode, not code. 128 * If the name ends in `cat', we want to write to stdout, 129 * and decode as well. 130 */ 131 132 if (!strncmp(av0, "un", 2)) f_decode = 1; 133 if ( (l = strlen(av0)) >= 3 /* strlen("cat") */ 134 && !strcmp( av0 + l - 3, "cat" )) f_cat = f_decode = 1; 135 } 136 137 138 /* 139 * Check whether the name (possibly generated by appending 140 * .gsm to something else) is short enough for this system. 141 */ 142 static int length_okay P1((name), char * name) 143 { 144 long max_filename_length = 0; 145 char * end; 146 147 /* If our _pathname_ is too long, we'll usually not be 148 * able to open the file at all -- don't worry about that. 149 * 150 * But if the _filename_ is too long, there is danger of 151 * silent truncation on some systems, which results 152 * in the target replacing the source! 153 */ 154 155 if (!name) return 0; 156 end = endname(name); 157 158 #ifdef NAME_MAX 159 max_filename_length = NAME_MAX; 160 #else 161 #ifdef _PC_NAME_MAX 162 #ifdef USE_PATHCONF 163 { char * s, tmp; 164 165 /* s = dirname(name) 166 */ 167 if ((s = end) > name) { 168 if (s > name + 1) s--; 169 tmp = s; 170 *s = 0; 171 } 172 173 errno = 0; 174 max_filename_length = pathconf(s > name ? name : ".", 175 _PC_NAME_MAX); 176 if (max_filename_length == -1 && errno) { 177 perror( s > name ? name : "." ); 178 fprintf(stderr, 179 "%s: cannot get dynamic filename length limit for %s.\n", 180 progname, s > name ? name : "."); 181 return 0; 182 } 183 if (s > name) *s = tmp; 184 } 185 #endif /* USE_PATHCONF */ 186 #endif /* _PC_NAME_MAX */ 187 #endif /* !NAME_MAX */ 188 189 if (max_filename_length > 0 && strlen(end) > max_filename_length) { 190 fprintf(stderr, 191 "%s: filename \"%s\" is too long (maximum is %ld)\n", 192 progname, endname(name), max_filename_length ); 193 return 0; 194 } 195 196 return 1; 197 } 198 199 /* 200 * Return a pointer the suffix of a string, if any. 201 * A suffix alone has no suffix, an empty suffix can not be had. 202 */ 203 static char * suffix P2((name, suf), char *name, char * suf) 204 { 205 size_t nlen = strlen(name); 206 size_t slen = strlen(suf); 207 208 if (!slen || nlen <= slen) return (char *)0; 209 name += nlen - slen; 210 return memcmp(name, suf, slen) ? (char *)0 : name; 211 } 212 213 214 static void catch_signals P1((fun), SIGHANDLER_T (*fun) ()) 215 { 216 #ifdef SIGHUP 217 signal( SIGHUP, fun ); 218 #endif 219 #ifdef SIGINT 220 signal( SIGINT, fun ); 221 #endif 222 #ifdef SIGPIPE 223 signal( SIGPIPE, fun ); 224 #endif 225 #ifdef SIGTERM 226 signal( SIGTERM, fun ); 227 #endif 228 #ifdef SIGXFSZ 229 signal( SIGXFSZ, fun ); 230 #endif 231 } 232 233 static SIGHANDLER_T onintr P0() 234 { 235 char * tmp = outname; 236 237 #ifdef HAS_SYSV_SIGNALS 238 catch_signals( SIG_IGN ); 239 #endif 240 241 outname = (char *)0; 242 if (tmp) (void)unlink(tmp); 243 244 exit(1); 245 } 246 247 /* 248 * Allocate some memory and complain if it fails. 249 */ 250 static char * emalloc P1((len), size_t len) 251 { 252 char * s; 253 if (!(s = malloc(len))) { 254 fprintf(stderr, "%s: failed to malloc %d bytes -- abort\n", 255 progname, (int)len); 256 onintr(); 257 exit(1); 258 } 259 return s; 260 } 261 262 static char* normalname P3((name, want, cut), char *name, char *want,char *cut) 263 { 264 size_t maxlen; 265 char * s, * p; 266 267 p = (char *)0; 268 if (!name) return p; 269 270 maxlen = strlen(name) + 1 + strlen(want) + strlen(cut); 271 p = strcpy(emalloc(maxlen), name); 272 273 if ((s = suffix(p, cut)) != 0) strcpy(s, want); 274 else if (*want && !suffix(p, want)) strcat(p, want); 275 276 return p; 277 } 278 279 /* 280 * Generate a `plain' (non-encoded) name from a given name. 281 */ 282 static char * plainname P1((name), char *name) 283 { 284 return normalname(name, "", SUFFIX_TOASTED ); 285 } 286 287 /* 288 * Generate a `code' name from a given name. 289 */ 290 static char * codename P1((name), char *name) 291 { 292 return normalname( name, SUFFIX_TOASTED, "" ); 293 } 294 295 /* 296 * If we're supposed to ask (fileno (stderr) is a tty, and f_force not 297 * set), ask the user whether to overwrite a file or not. 298 */ 299 static int ok_to_replace P1(( name ), char * name) 300 { 301 int reply, c; 302 303 if (f_force) return 1; /* YES, do replace */ 304 if (!isatty(fileno(stderr))) return 0; /* NO, don't replace */ 305 306 fprintf(stderr, 307 "%s already exists; do you wish to overwrite %s (y or n)? ", 308 name, name); 309 fflush(stderr); 310 311 for (c = reply = getchar(); c != '\n' && c != EOF; c = getchar()) ; 312 if (reply == 'y') return 1; 313 314 fprintf(stderr, "\tnot overwritten\n"); 315 return 0; 316 } 317 318 static void update_mode P0() 319 { 320 if (!instat.st_nlink) return; /* couldn't stat in */ 321 322 #ifdef HAS_FCHMOD 323 if (fchmod(fileno(out), instat.st_mode & 07777)) { 324 perror(outname); 325 fprintf(stderr, "%s: could not change file mode of \"%s\"\n", 326 progname, outname); 327 } 328 #else 329 #ifdef HAS_CHMOD 330 if (outname && chmod(outname, instat.st_mode & 07777)) { 331 perror(outname); 332 fprintf(stderr, "%s: could not change file mode of \"%s\"\n", 333 progname, outname); 334 } 335 #endif /* HAS_CHMOD */ 336 #endif /* HAS_FCHMOD */ 337 } 338 339 static void update_own P0() 340 { 341 if (!instat.st_nlink) return; /* couldn't stat in */ 342 #ifdef HAS_FCHOWN 343 (void)fchown(fileno(out), instat.st_uid, instat.st_gid); 344 #else 345 #ifdef HAS_CHOWN 346 (void)chown(outname, instat.st_uid, instat.st_gid); 347 #endif /* HAS_CHOWN */ 348 #endif /* HAS_FCHOWN */ 349 } 350 351 static void update_times P0() 352 { 353 if (!instat.st_nlink) return; /* couldn't stat in */ 354 355 #ifdef HAS_UTIMES 356 if (outname) { 357 struct timeval tv[2]; 358 359 tv[0].tv_sec = instat.st_atime; 360 tv[1].tv_sec = instat.st_mtime; 361 tv[0].tv_usec = tv[1].tv_usec = 0; 362 (void) utimes(outname, tv); 363 } 364 #else 365 #ifdef HAS_UTIME 366 367 if (outname) { 368 369 #ifdef HAS_UTIMBUF 370 struct utimbuf ut; 371 372 ut.actime = instat.st_atime; 373 ut.modtime = instat.st_mtime; 374 375 # ifdef HAS_UTIMEUSEC 376 ut.acusec = instat.st_ausec; 377 ut.modusec = instat.st_musec; 378 # endif /* HAS_UTIMEUSEC */ 379 380 (void) utime(outname, &ut); 381 382 #else /* UTIMBUF */ 383 384 time_t ut[2]; 385 386 ut[0] = instat.st_atime; 387 ut[1] = instat.st_mtime; 388 389 (void) utime(outname, ut); 390 391 #endif /* UTIMBUF */ 392 } 393 #endif /* HAS_UTIME */ 394 #endif /* HAS_UTIMES */ 395 } 396 397 398 static int okay_as_input P3((name,f,st), char* name, FILE* f, struct stat * st) 399 { 400 # ifdef HAS_FSTAT 401 if (fstat(fileno(f), st) < 0) 402 # else 403 if (stat(name, st) < 0) 404 # endif 405 { 406 perror(name); 407 fprintf(stderr, "%s: cannot stat \"%s\"\n", progname, name); 408 return 0; 409 } 410 411 if (!S_ISREG(st->st_mode)) { 412 fprintf(stderr, 413 "%s: \"%s\" is not a regular file -- unchanged.\n", 414 progname, name); 415 return 0; 416 } 417 if (st->st_nlink > 1 && !f_cat && !f_precious) { 418 fprintf(stderr, 419 "%s: \"%s\" has %d other link%s -- unchanged.\n", 420 progname, name, (int)(st->st_nlink - 1), 421 "s" + (st->st_nlink <= 2)); 422 return 0; 423 } 424 return 1; 425 } 426 427 static void prepare_io P1(( desc), struct fmtdesc * desc) 428 { 429 output = desc->output; 430 input = desc->input; 431 432 init_input = desc->init_input; 433 init_output = desc->init_output; 434 } 435 436 static struct fmtdesc * grok_format P1((name), char * name) 437 { 438 char * c; 439 struct fmtdesc ** f; 440 441 if (name) { 442 c = plainname(name); 443 444 for (f = alldescs; *f; f++) { 445 if ( (*f)->suffix 446 && *(*f)->suffix 447 && suffix(c, (*f)->suffix)) { 448 449 free(c); 450 return *f; 451 } 452 } 453 454 free(c); 455 } 456 return (struct fmtdesc *)0; 457 } 458 459 static int open_input P2((name, st), char * name, struct stat * st) 460 { 461 struct fmtdesc * f = f_format; 462 463 st->st_nlink = 0; /* indicates `undefined' value */ 464 if (!name) { 465 inname = (char *)NULL; 466 in = stdin; 467 #ifdef HAS__FSETMODE 468 _fsetmode(in, "b"); 469 #endif 470 } 471 else { 472 if (f_decode) inname = codename(name); 473 else { 474 if (!f_cat && suffix(name, SUFFIX_TOASTED)) { 475 fprintf(stderr, 476 "%s: %s already has \"%s\" suffix -- unchanged.\n", 477 progname, name, SUFFIX_TOASTED ); 478 return 0; 479 } 480 inname = strcpy(emalloc(strlen(name)+1), name); 481 } 482 if (!(in = fopen(inname, READ))) { 483 perror(inname); /* not guaranteed to be valid here */ 484 fprintf(stderr, "%s: cannot open \"%s\" for reading\n", 485 progname, inname); 486 return 0; 487 } 488 if (!okay_as_input(inname, in, st)) return 0; 489 if (!f) f = grok_format(inname); 490 } 491 prepare_io( f ? f : & DEFAULT_FORMAT ); 492 return 1; 493 } 494 495 static int open_output P1((name), char *name) 496 { 497 if (!name || f_cat) { 498 out = stdout; 499 outname = (char *)NULL; 500 #ifdef HAS__FSETMODE 501 _fsetmode(out, "b"); 502 #endif 503 } 504 else { 505 int outfd = -1; 506 char * o; 507 508 o = (*(f_decode ? plainname : codename))(name); 509 if (!length_okay(o)) return 0; 510 if ((outfd = open(o, O_WRITE_EXCL, 0666)) >= 0) 511 out = fdopen(outfd, WRITE); 512 else if (errno != EEXIST) out = (FILE *)NULL; 513 else if (ok_to_replace(o)) out = fopen(o, WRITE); 514 else return 0; 515 516 if (!out) { 517 perror(o); 518 fprintf(stderr, 519 "%s: can't open \"%s\" for writing\n", 520 progname, o); 521 if (outfd >= 0) (void)close(outfd); 522 return 0; 523 } 524 525 outname = o; 526 } 527 return 1; 528 } 529 530 static int process_encode P0() 531 { 532 gsm r; 533 gsm_signal s[ 160 ]; 534 gsm_frame d; 535 536 int cc; 537 538 if (!(r = gsm_create())) { 539 perror(progname); 540 return -1; 541 } 542 (void)gsm_option(r, GSM_OPT_FAST, &f_fast); 543 (void)gsm_option(r, GSM_OPT_VERBOSE, &f_verbose); 544 (void)gsm_option(r, GSM_OPT_LTP_CUT, &f_ltp_cut); 545 546 while ((cc = (*input)(s)) > 0) { 547 if (cc < sizeof(s) / sizeof(*s)) 548 memset((char *)(s+cc), 0, sizeof(s)-(cc * sizeof(*s))); 549 gsm_encode(r, s, d); 550 if (fwrite((char *)d, sizeof(d), 1, out) != 1) { 551 perror(outname ? outname : "stdout"); 552 fprintf(stderr, "%s: error writing to %s\n", 553 progname, outname ? outname : "stdout"); 554 gsm_destroy(r); 555 return -1; 556 } 557 } 558 if (cc < 0) { 559 perror(inname ? inname : "stdin"); 560 fprintf(stderr, "%s: error reading from %s\n", 561 progname, inname ? inname : "stdin"); 562 gsm_destroy(r); 563 return -1; 564 } 565 gsm_destroy(r); 566 567 return 0; 568 } 569 570 static int process_decode P0() 571 { 572 gsm r; 573 gsm_frame s; 574 gsm_signal d[ 160 ]; 575 576 int cc; 577 578 if (!(r = gsm_create())) { /* malloc failed */ 579 perror(progname); 580 return -1; 581 } 582 (void)gsm_option(r, GSM_OPT_FAST, &f_fast); 583 (void)gsm_option(r, GSM_OPT_VERBOSE, &f_verbose); 584 585 while ((cc = fread(s, 1, sizeof(s), in)) > 0) { 586 587 if (cc != sizeof(s)) { 588 if (cc >= 0) fprintf(stderr, 589 "%s: incomplete frame (%d byte%s missing) from %s\n", 590 progname, (int)(sizeof(s) - cc), 591 "s" + (sizeof(s) - cc == 1), 592 inname ? inname : "stdin" ); 593 gsm_destroy(r); 594 errno = 0; 595 return -1; 596 } 597 if (gsm_decode(r, s, d)) { 598 fprintf(stderr, "%s: bad frame in %s\n", 599 progname, inname ? inname : "stdin"); 600 gsm_destroy(r); 601 errno = 0; 602 return -1; 603 } 604 605 if ((*output)(d) < 0) { 606 perror(outname); 607 fprintf(stderr, "%s: error writing to %s\n", 608 progname, outname); 609 gsm_destroy(r); 610 return -1; 611 } 612 } 613 614 if (cc < 0) { 615 perror(inname ? inname : "stdin" ); 616 fprintf(stderr, "%s: error reading from %s\n", progname, 617 inname ? inname : "stdin"); 618 gsm_destroy(r); 619 return -1; 620 } 621 622 gsm_destroy(r); 623 return 0; 624 } 625 626 static int process P1((name), char * name) 627 { 628 out = (FILE *)0; 629 in = (FILE *)0; 630 631 outname = (char *)0; 632 inname = (char *)0; 633 634 if (!open_input(name, &instat) || !open_output(name)) 635 goto err; 636 637 if ((*(f_decode ? init_output : init_input))()) { 638 fprintf(stderr, "%s: error %s %s\n", 639 progname, 640 f_decode ? "writing header to" : "reading header from", 641 f_decode ? (outname ? outname : "stdout") 642 : (inname ? inname : "stdin")); 643 goto err; 644 } 645 646 if ((*(f_decode ? process_decode : process_encode))()) 647 goto err; 648 649 if (fflush(out) < 0 || ferror(out)) { 650 perror(outname ? outname : "stdout"); 651 fprintf(stderr, "%s: error writing \"%s\"\n", progname, 652 outname ? outname:"stdout"); 653 goto err; 654 } 655 656 if (out != stdout) { 657 658 update_times(); 659 update_mode (); 660 update_own (); 661 662 if (fclose(out) < 0) { 663 perror(outname); 664 fprintf(stderr, "%s: error writing \"%s\"\n", 665 progname, outname); 666 goto err; 667 } 668 if (outname != name) free(outname); 669 outname = (char *)0; 670 } 671 out = (FILE *)0; 672 if (in != stdin) { 673 (void)fclose(in), in = (FILE *)0; 674 if (!f_cat && !f_precious) { 675 if (unlink(inname) < 0) { 676 perror(inname); 677 fprintf(stderr, 678 "%s: source \"%s\" not deleted.\n", 679 progname, inname); 680 } 681 goto err; 682 } 683 if (inname != name) free(inname); 684 inname = (char *)0; 685 } 686 return 0; 687 688 /* 689 * Error handling and cleanup. 690 */ 691 err: 692 if (out && out != stdout) { 693 (void)fclose(out), out = (FILE *)0; 694 if (unlink(outname) < 0 && errno != ENOENT && errno != EINTR) { 695 perror(outname); 696 fprintf(stderr, "%s: could not unlink \"%s\"\n", 697 progname, outname); 698 } 699 } 700 if (in && in != stdin) (void)fclose(in), in = (FILE *)0; 701 702 if (inname && inname != name) free(inname); 703 if (outname && outname != name) free(outname); 704 705 return -1; 706 } 707 708 static void version P0() 709 { 710 printf( "%s 1.0, version %s\n", 711 progname, 712 "$Id: toast.c,v 1.8 1996/07/02 10:41:04 jutta Exp $" ); 713 } 714 715 static void help P0() 716 { 717 printf("Usage: %s [-fcpdhvaulsFC] [files...]\n", progname); 718 printf("\n"); 719 720 printf(" -f force Replace existing files without asking\n"); 721 printf(" -c cat Write to stdout, do not remove source files\n"); 722 printf(" -d decode Decode data (default is encode)\n"); 723 printf(" -p precious Do not delete the source\n"); 724 printf("\n"); 725 726 printf(" -u u-law Force 8 kHz/8 bit u-law in/output format\n"); 727 printf(" -s sun .au Force Sun .au u-law in/output format\n"); 728 printf(" -a A-law Force 8 kHz/8 bit A-law in/output format\n"); 729 printf(" -l linear Force 16 bit linear in/output format\n"); 730 printf("\n"); 731 732 printf(" -F fast Sacrifice conformance to performance\n"); 733 printf(" -C cutoff Ignore most samples during LTP\n"); 734 printf(" -v version Show version information\n"); 735 printf(" -h help Print this text\n"); 736 printf("\n"); 737 } 738 739 740 static void set_format P1((f), struct fmtdesc * f) 741 { 742 if (f_format && f_format != f) { 743 fprintf( stderr, 744 "%s: only one of -[uals] is possible (%s -h for help)\n", 745 progname, progname); 746 exit(1); 747 } 748 749 f_format = f; 750 } 751 752 int main P2((ac, av), int ac, char **av) 753 { 754 int opt; 755 extern int optind; 756 757 parse_argv0(*av); 758 759 while ((opt = getopt(ac, av, "fcdpvhuaslVFC:")) != EOF) switch (opt) { 760 761 case 'd': f_decode = 1; break; 762 case 'f': f_force = 1; break; 763 case 'c': f_cat = 1; break; 764 case 'p': f_precious = 1; break; 765 case 'F': f_fast = 1; break; 766 case 'C': f_ltp_cut = 100; break; 767 #ifndef NDEBUG 768 case 'V': f_verbose = 1; break; /* undocumented */ 769 #endif 770 771 case 'u': set_format( &f_ulaw ); break; 772 case 'l': set_format( &f_linear ); break; 773 case 'a': set_format( &f_alaw ); break; 774 case 's': set_format( &f_audio ); break; 775 776 case 'v': version(); exit(0); 777 case 'h': help(); exit(0); 778 779 default: 780 fprintf(stderr, 781 "Usage: %s [-fcpdhvuaslFC] [files...] (-h for help)\n", 782 progname); 783 exit(1); 784 } 785 786 f_precious |= f_cat; 787 788 av += optind; 789 ac -= optind; 790 791 catch_signals(onintr); 792 793 if (ac <= 0) process( (char *)0 ); 794 else while (ac--) process( *av++ ); 795 796 exit(0); 797 } 798