Debian Codegraph @

gsm/toast.c

raw symbols SCIP
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: /usr/local/cvsroot/rplay/gsm/toast.c,v 1.1.1.1 1998/07/14 22:35:24 mrb 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, not foo.gsm (-c) */
18 int f_force = 0; /* force deletion (-f) */
19 int f_precious = 0; /* avoid deletion (-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, 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)) 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 if (outname && chmod(outname, instat.st_mode & 07777)) {
330 perror(outname);
331 fprintf(stderr, "%s: could not change file mode of \"%s\"\n",
332 progname, outname);
333 }
334 #endif /* HAS_FCHMOD */
335 }
336
337 static void update_own P0()
338 {
339 if (!instat.st_nlink) return; /* couldn't stat in */
340 #ifdef HAS_FCHOWN
341 (void)fchown(fileno(out), instat.st_uid, instat.st_gid);
342 #else
343 (void)chown(outname, instat.st_uid, instat.st_gid);
344 #endif /* HAS_FCHOWN */
345 }
346
347 static void update_times P0()
348 {
349 if (!instat.st_nlink) return; /* couldn't stat in */
350
351 #ifdef HAS_UTIMES
352 if (outname) {
353 struct timeval tv[2];
354
355 tv[0].tv_sec = instat.st_atime;
356 tv[1].tv_sec = instat.st_mtime;
357 tv[0].tv_usec = tv[1].tv_usec = 0;
358 (void) utimes(outname, tv);
359 }
360 #else
361 #ifdef HAS_UTIME
362
363 if (outname) {
364
365 #ifdef HAS_UTIMBUF
366 struct utimbuf ut;
367
368 ut.actime = instat.st_atime;
369 ut.modtime = instat.st_mtime;
370
371 # ifdef HAS_UTIMEUSEC
372 ut.acusec = instat.st_ausec;
373 ut.modusec = instat.st_musec;
374 # endif /* HAS_UTIMEUSEC */
375
376 (void) utime(outname, &ut);
377
378 #else /* UTIMBUF */
379
380 time_t ut[2];
381
382 ut[0] = instat.st_atime;
383 ut[1] = instat.st_mtime;
384
385 (void) utime(outname, ut);
386
387 #endif /* UTIMBUF */
388 }
389 #endif /* HAS_UTIME */
390 #endif /* HAS_UTIMES */
391 }
392
393
394 static int okay_as_input P3((name,f,st), char* name, FILE* f, struct stat * st)
395 {
396 # ifdef HAS_FSTAT
397 if (fstat(fileno(f), st) < 0)
398 # else
399 if (stat(name, st) < 0)
400 # endif
401 {
402 perror(name);
403 fprintf(stderr, "%s: cannot stat \"%s\"\n", progname, name);
404 return 0;
405 }
406
407 if (!S_ISREG(st->st_mode)) {
408 fprintf(stderr,
409 "%s: \"%s\" is not a regular file -- unchanged.\n",
410 progname, name);
411 return 0;
412 }
413 if (st->st_nlink > 1 && !f_cat && !f_precious) {
414 fprintf(stderr,
415 "%s: \"%s\" has %s other link%s -- unchanged.\n",
416 progname,name,st->st_nlink - 1,"s" + (st->st_nlink<=2));
417 return 0;
418 }
419 return 1;
420 }
421
422 static void prepare_io P1(( desc), struct fmtdesc * desc)
423 {
424 output = desc->output;
425 input = desc->input;
426
427 init_input = desc->init_input;
428 init_output = desc->init_output;
429 }
430
431 static struct fmtdesc * grok_format P1((name), char * name)
432 {
433 char * c;
434 struct fmtdesc ** f;
435
436 if (name) {
437 c = plainname(name);
438
439 for (f = alldescs; *f; f++) {
440 if ( (*f)->suffix
441 && *(*f)->suffix
442 && suffix(c, (*f)->suffix)) {
443
444 free(c);
445 return *f;
446 }
447 }
448
449 free(c);
450 }
451 return (struct fmtdesc *)0;
452 }
453
454 static int open_input P2((name, st), char * name, struct stat * st)
455 {
456 struct fmtdesc * f = f_format;
457
458 st->st_nlink = 0; /* indicates `undefined' value */
459 if (!name) {
460 inname = (char *)NULL;
461 in = stdin;
462 }
463 else {
464 if (f_decode) inname = codename(name);
465 else {
466 if (!f_cat && suffix(name, SUFFIX_TOASTED)) {
467 fprintf(stderr,
468 "%s: %s already has \"%s\" suffix -- unchanged.\n",
469 progname, name, SUFFIX_TOASTED );
470 return 0;
471 }
472 inname = strcpy(emalloc(strlen(name)+1), name);
473 }
474 if (!(in = fopen(inname, READ))) {
475 perror(inname); /* not guaranteed to be valid here */
476 fprintf(stderr, "%s: cannot open \"%s\" for reading\n",
477 progname, inname);
478 return 0;
479 }
480 if (!okay_as_input(inname, in, st)) return 0;
481 if (!f) f = grok_format(inname);
482 }
483 prepare_io( f ? f : & DEFAULT_FORMAT );
484 return 1;
485 }
486
487 static int open_output P1((name), char *name)
488 {
489 if (!name || f_cat) {
490 out = stdout;
491 outname = (char *)NULL;
492 }
493 else {
494 int outfd = -1;
495 char * o;
496
497 o = (*(f_decode ? plainname : codename))(name);
498 if (!length_okay(o)) return 0;
499 if ((outfd = open(o, O_WRITE_EXCL, 0666)) >= 0)
500 out = fdopen(outfd, WRITE);
501 else if (errno != EEXIST) out = (FILE *)NULL;
502 else if (ok_to_replace(o)) out = fopen(o, WRITE);
503 else return 0;
504
505 if (!out) {
506 perror(o);
507 fprintf(stderr,
508 "%s: can't open \"%s\" for writing\n",
509 progname, o);
510 if (outfd >= 0) (void)close(outfd);
511 return 0;
512 }
513
514 outname = o;
515 }
516 return 1;
517 }
518
519 static int process_encode P0()
520 {
521 gsm r;
522 gsm_signal s[ 160 ];
523 gsm_frame d;
524
525 int cc;
526
527 if (!(r = gsm_create())) {
528 perror(progname);
529 return -1;
530 }
531 (void)gsm_option(r, GSM_OPT_FAST, &f_fast);
532 (void)gsm_option(r, GSM_OPT_VERBOSE, &f_verbose);
533 (void)gsm_option(r, GSM_OPT_LTP_CUT, &f_ltp_cut);
534
535 while ((cc = (*input)(s)) > 0) {
536 if (cc < sizeof(s) / sizeof(*s))
537 memset((char *)(s+cc), 0, sizeof(s)-(cc * sizeof(*s)));
538 gsm_encode(r, s, d);
539 if (fwrite((char *)d, sizeof(d), 1, out) != 1) {
540 perror(outname ? outname : "stdout");
541 fprintf(stderr, "%s: error writing to %s\n",
542 progname, outname ? outname : "stdout");
543 gsm_destroy(r);
544 return -1;
545 }
546 }
547 if (cc < 0) {
548 perror(inname ? inname : "stdin");
549 fprintf(stderr, "%s: error reading from %s\n",
550 progname, inname ? inname : "stdin");
551 gsm_destroy(r);
552 return -1;
553 }
554 gsm_destroy(r);
555
556 return 0;
557 }
558
559 static int process_decode P0()
560 {
561 gsm r;
562 gsm_frame s;
563 gsm_signal d[ 160 ];
564
565 int cc;
566
567 if (!(r = gsm_create())) { /* malloc failed */
568 perror(progname);
569 return -1;
570 }
571 (void)gsm_option(r, GSM_OPT_FAST, &f_fast);
572 (void)gsm_option(r, GSM_OPT_VERBOSE, &f_verbose);
573
574 while ((cc = fread(s, 1, sizeof(s), in)) > 0) {
575
576 if (cc != sizeof(s)) {
577 if (cc >= 0) fprintf(stderr,
578 "%s: incomplete frame (%d byte%s missing) from %s\n",
579 progname, sizeof(s) - cc,
580 "s" + (sizeof(s) - cc == 1),
581 inname ? inname : "stdin" );
582 gsm_destroy(r);
583 errno = 0;
584 return -1;
585 }
586 if (gsm_decode(r, s, d)) {
587 fprintf(stderr, "%s: bad frame in %s\n",
588 progname, inname ? inname : "stdin");
589 gsm_destroy(r);
590 errno = 0;
591 return -1;
592 }
593
594 if ((*output)(d) < 0) {
595 perror(outname);
596 fprintf(stderr, "%s: error writing to %s\n",
597 progname, outname);
598 gsm_destroy(r);
599 return -1;
600 }
601 }
602
603 if (cc < 0) {
604 perror(inname ? inname : "stdin" );
605 fprintf(stderr, "%s: error reading from %s\n", progname,
606 inname ? inname : "stdin");
607 gsm_destroy(r);
608 return -1;
609 }
610
611 gsm_destroy(r);
612 return 0;
613 }
614
615 static int process P1((name), char * name)
616 {
617 int step = 0;
618
619 out = (FILE *)0;
620 in = (FILE *)0;
621
622 outname = (char *)0;
623 inname = (char *)0;
624
625 if (!open_input(name, &instat) || !open_output(name))
626 goto err;
627
628 if ((*(f_decode ? init_output : init_input))()) {
629 fprintf(stderr, "%s: error %s %s\n",
630 progname,
631 f_decode ? "writing header to" : "reading header from",
632 f_decode ? (outname ? outname : "stdout")
633 : (inname ? inname : "stdin"));
634 goto err;
635 }
636
637 if ((*(f_decode ? process_decode : process_encode))())
638 goto err;
639
640 if (fflush(out) < 0 || ferror(out)) {
641 perror(outname ? outname : "stdout");
642 fprintf(stderr, "%s: error writing \"%s\"\n", progname,
643 outname ? outname:"stdout");
644 goto err;
645 }
646
647 if (out != stdout) {
648
649 update_times();
650 update_mode ();
651 update_own ();
652
653 if (fclose(out) < 0) {
654 perror(outname);
655 fprintf(stderr, "%s: error writing \"%s\"\n",
656 progname, outname);
657 goto err;
658 }
659 if (outname != name) free(outname);
660 outname = (char *)0;
661 }
662 out = (FILE *)0;
663 if (in != stdin) {
664 (void)fclose(in), in = (FILE *)0;
665 if (!f_cat && !f_precious) {
666 if (unlink(inname) < 0) {
667 perror(inname);
668 fprintf(stderr,
669 "%s: source \"%s\" not deleted.\n",
670 progname, inname);
671 }
672 goto err;
673 }
674 if (inname != name) free(inname);
675 inname = (char *)0;
676 }
677 return 0;
678
679 /*
680 * Error handling and cleanup:
681 * - error out: close out, unlink it, close in, free the names.
682 * -
683 */
684 err:
685 if (out && out != stdout) {
686 (void)fclose(out), out = (FILE *)0;
687 if (unlink(outname) < 0 && errno != ENOENT && errno != EINTR) {
688 perror(outname);
689 fprintf(stderr, "%s: could not unlink \"%s\"\n",
690 progname, outname);
691 }
692 }
693 if (in && in != stdin) (void)fclose(in), in = (FILE *)0;
694
695 if (inname && inname != name) free(inname);
696 if (outname && outname != name) free(outname);
697
698 return -1;
699 }
700
701 static void version P0()
702 {
703 printf( "%s 1.0, version %s\n",
704 progname,
705 "$Id: toast.c,v 1.1.1.1 1998/07/14 22:35:24 mrb Exp $" );
706 }
707
708 static void help P0()
709 {
710 printf("Usage: %s [-fcpdhvaulsFC] [files...]\n", progname);
711 printf("\n");
712
713 printf(" -f force Replace existing files without asking\n");
714 printf(" -c cat Write to stdout, do not remove source files\n");
715 printf(" -d decode Decode data (default is encode)\n");
716 printf(" -p precious Do not delete the source\n");
717 printf("\n");
718
719 printf(" -u u-law Force 8 kHz/8 bit u-law in/output format\n");
720 printf(" -s sun .au Force Sun .au u-law in/output format\n");
721 printf(" -a A-law Force 8 kHz/8 bit A-law in/output format\n");
722 printf(" -l linear Force 16 bit linear in/output format\n");
723 printf("\n");
724
725 printf(" -F fast Sacrifice conformance to performance\n");
726 printf(" -C cutoff Ignore most samples during LTP\n");
727 printf(" -v version Show version information\n");
728 printf(" -h help Print this text\n");
729 printf("\n");
730 }
731
732
733 static void set_format P1((f), struct fmtdesc * f)
734 {
735 if (f_format && f_format != f) {
736 fprintf( stderr,
737 "%s: only one of -[uals] is possible (%s -h for help)\n",
738 progname, progname);
739 exit(1);
740 }
741
742 f_format = f;
743 }
744
745 int main P2((ac, av), int ac, char **av)
746 {
747 int opt;
748 extern int optind;
749 extern char * optarg;
750
751 parse_argv0(*av);
752
753 while ((opt = getopt(ac, av, "fcdpvhuaslVFC:")) != EOF) switch (opt) {
754
755 case 'd': f_decode = 1; break;
756 case 'f': f_force = 1; break;
757 case 'c': f_cat = 1; break;
758 case 'p': f_precious = 1; break;
759 case 'F': f_fast = 1; break;
760 case 'C': f_ltp_cut = 100; break;
761 #ifndef NDEBUG
762 case 'V': f_verbose = 1; break; /* undocumented */
763 #endif
764
765 case 'u': set_format( &f_ulaw ); break;
766 case 'l': set_format( &f_linear ); break;
767 case 'a': set_format( &f_alaw ); break;
768 case 's': set_format( &f_audio ); break;
769
770 case 'v': version(); exit(0);
771 case 'h': help(); exit(0);
772
773 default:
774 usage:
775 fprintf(stderr,
776 "Usage: %s [-fcpdhvuaslFC] [files...] (-h for help)\n",
777 progname);
778 exit(1);
779 }
780
781 f_precious |= f_cat;
782
783 av += optind;
784 ac -= optind;
785
786 catch_signals(onintr);
787
788 if (ac <= 0) process( (char *)0 );
789 else while (ac--) process( *av++ );
790
791 exit(0);
792 }