17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 57c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 67c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 77c478bd9Sstevel@tonic-gate * with the License. 87c478bd9Sstevel@tonic-gate * 97c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 107c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 117c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 127c478bd9Sstevel@tonic-gate * and limitations under the License. 137c478bd9Sstevel@tonic-gate * 147c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 157c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 167c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 177c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 187c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 197c478bd9Sstevel@tonic-gate * 207c478bd9Sstevel@tonic-gate * CDDL HEADER END 217c478bd9Sstevel@tonic-gate */ 227c478bd9Sstevel@tonic-gate /* 23*416baccdSec158148 * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 247c478bd9Sstevel@tonic-gate * Use is subject to license terms. 257c478bd9Sstevel@tonic-gate */ 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <fcntl.h> 307c478bd9Sstevel@tonic-gate #include <string.h> 317c478bd9Sstevel@tonic-gate #include <errno.h> 327c478bd9Sstevel@tonic-gate #include <sys/types.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <sys/stat.h> 357c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 367c478bd9Sstevel@tonic-gate #include <stdio.h> 377c478bd9Sstevel@tonic-gate #include <stdlib.h> 387c478bd9Sstevel@tonic-gate #include <libintl.h> 397c478bd9Sstevel@tonic-gate #include <limits.h> 407c478bd9Sstevel@tonic-gate #include <audio/au.h> 417c478bd9Sstevel@tonic-gate 427c478bd9Sstevel@tonic-gate #include "bstream.h" 437c478bd9Sstevel@tonic-gate #include "util.h" 447c478bd9Sstevel@tonic-gate #include "audio.h" 457c478bd9Sstevel@tonic-gate #include "byteorder.h" 467c478bd9Sstevel@tonic-gate #include "main.h" 477c478bd9Sstevel@tonic-gate 487c478bd9Sstevel@tonic-gate int str_errno; 497c478bd9Sstevel@tonic-gate 507c478bd9Sstevel@tonic-gate char * 517c478bd9Sstevel@tonic-gate str_errno_to_string(int serrno) 527c478bd9Sstevel@tonic-gate { 537c478bd9Sstevel@tonic-gate switch (serrno) { 547c478bd9Sstevel@tonic-gate case STR_ERR_NO_ERR: 557c478bd9Sstevel@tonic-gate return (gettext("No error")); 567c478bd9Sstevel@tonic-gate case STR_ERR_NO_REG_FILE: 577c478bd9Sstevel@tonic-gate return (gettext("Not a regular file")); 587c478bd9Sstevel@tonic-gate case STR_ERR_NO_READ_STDIN: 597c478bd9Sstevel@tonic-gate return (gettext("Stdin not open for reading")); 607c478bd9Sstevel@tonic-gate case STR_ERR_AU_READ_ERR: 617c478bd9Sstevel@tonic-gate return (gettext("Unable to read au header")); 627c478bd9Sstevel@tonic-gate case STR_ERR_AU_UNSUPPORTED_FORMAT: 637c478bd9Sstevel@tonic-gate return (gettext("Unsupported au format")); 647c478bd9Sstevel@tonic-gate case STR_ERR_AU_BAD_HEADER: 657c478bd9Sstevel@tonic-gate return (gettext("Bad au header")); 667c478bd9Sstevel@tonic-gate case STR_ERR_WAV_READ_ERR: 677c478bd9Sstevel@tonic-gate return (gettext("Unable to read wav header")); 687c478bd9Sstevel@tonic-gate case STR_ERR_WAV_UNSUPPORTED_FORMAT: 697c478bd9Sstevel@tonic-gate return (gettext("Unsupported wav format")); 707c478bd9Sstevel@tonic-gate case STR_ERR_WAV_BAD_HEADER: 717c478bd9Sstevel@tonic-gate return (gettext("Bad wav header")); 727c478bd9Sstevel@tonic-gate default: 737c478bd9Sstevel@tonic-gate return (gettext("unknown error")); 747c478bd9Sstevel@tonic-gate } 757c478bd9Sstevel@tonic-gate } 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate static int 787c478bd9Sstevel@tonic-gate file_stream_size(bstreamhandle h, off_t *size) 797c478bd9Sstevel@tonic-gate { 807c478bd9Sstevel@tonic-gate struct stat st; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate str_errno = 0; 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate if (fstat(h->bstr_fd, &st) < 0) 857c478bd9Sstevel@tonic-gate return (0); 867c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) != S_IFREG) { 877c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_REG_FILE; 887c478bd9Sstevel@tonic-gate return (0); 897c478bd9Sstevel@tonic-gate } 907c478bd9Sstevel@tonic-gate *size = st.st_size; 917c478bd9Sstevel@tonic-gate return (1); 927c478bd9Sstevel@tonic-gate } 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate static int 957c478bd9Sstevel@tonic-gate audio_stream_size(bstreamhandle h, off_t *size) 967c478bd9Sstevel@tonic-gate { 977c478bd9Sstevel@tonic-gate str_errno = 0; 987c478bd9Sstevel@tonic-gate *size = (off_t)(uintptr_t)(h->bstr_private); 997c478bd9Sstevel@tonic-gate return (1); 1007c478bd9Sstevel@tonic-gate } 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate static int 1037c478bd9Sstevel@tonic-gate file_stream_read(bstreamhandle h, uchar_t *buf, off_t size) 1047c478bd9Sstevel@tonic-gate { 1057c478bd9Sstevel@tonic-gate str_errno = 0; 1067c478bd9Sstevel@tonic-gate return (read(h->bstr_fd, buf, size)); 1077c478bd9Sstevel@tonic-gate } 1087c478bd9Sstevel@tonic-gate 1097c478bd9Sstevel@tonic-gate static int 1107c478bd9Sstevel@tonic-gate file_stream_write(bstreamhandle h, uchar_t *buf, off_t size) 1117c478bd9Sstevel@tonic-gate { 1127c478bd9Sstevel@tonic-gate str_errno = 0; 1137c478bd9Sstevel@tonic-gate return (write(h->bstr_fd, buf, size)); 1147c478bd9Sstevel@tonic-gate } 1157c478bd9Sstevel@tonic-gate 1167c478bd9Sstevel@tonic-gate /* 1177c478bd9Sstevel@tonic-gate * with reverse byteorder 1187c478bd9Sstevel@tonic-gate */ 1197c478bd9Sstevel@tonic-gate static int 1207c478bd9Sstevel@tonic-gate file_stream_read_wrbo(bstreamhandle h, uchar_t *buf, off_t size) 1217c478bd9Sstevel@tonic-gate { 1227c478bd9Sstevel@tonic-gate int cnt; 1237c478bd9Sstevel@tonic-gate 1247c478bd9Sstevel@tonic-gate str_errno = 0; 1257c478bd9Sstevel@tonic-gate cnt = read(h->bstr_fd, buf, size); 1267c478bd9Sstevel@tonic-gate if (cnt > 0) { 1277c478bd9Sstevel@tonic-gate int i; 1287c478bd9Sstevel@tonic-gate uchar_t ch; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate for (i = 0; i < cnt; i += 2) { 1317c478bd9Sstevel@tonic-gate ch = buf[i]; 1327c478bd9Sstevel@tonic-gate buf[i] = buf[i+1]; 1337c478bd9Sstevel@tonic-gate buf[i+1] = ch; 1347c478bd9Sstevel@tonic-gate } 1357c478bd9Sstevel@tonic-gate } 1367c478bd9Sstevel@tonic-gate return (cnt); 1377c478bd9Sstevel@tonic-gate } 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate /* 1407c478bd9Sstevel@tonic-gate * This will change the byteorder in the buffer but that is fine with us. 1417c478bd9Sstevel@tonic-gate */ 1427c478bd9Sstevel@tonic-gate static int 1437c478bd9Sstevel@tonic-gate file_stream_write_wrbo(bstreamhandle h, uchar_t *buf, off_t size) 1447c478bd9Sstevel@tonic-gate { 1457c478bd9Sstevel@tonic-gate int i; 1467c478bd9Sstevel@tonic-gate uchar_t ch; 1477c478bd9Sstevel@tonic-gate 1487c478bd9Sstevel@tonic-gate str_errno = 0; 1497c478bd9Sstevel@tonic-gate if (size > 0) { 1507c478bd9Sstevel@tonic-gate for (i = 0; i < size; i += 2) { 1517c478bd9Sstevel@tonic-gate ch = buf[i]; 1527c478bd9Sstevel@tonic-gate buf[i] = buf[i+1]; 1537c478bd9Sstevel@tonic-gate buf[i+1] = ch; 1547c478bd9Sstevel@tonic-gate } 1557c478bd9Sstevel@tonic-gate } 1567c478bd9Sstevel@tonic-gate return (write(h->bstr_fd, buf, size)); 1577c478bd9Sstevel@tonic-gate } 1587c478bd9Sstevel@tonic-gate 1597c478bd9Sstevel@tonic-gate static int 1607c478bd9Sstevel@tonic-gate file_stream_close(bstreamhandle h) 1617c478bd9Sstevel@tonic-gate { 1627c478bd9Sstevel@tonic-gate int fd; 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate str_errno = 0; 1657c478bd9Sstevel@tonic-gate fd = h->bstr_fd; 1667c478bd9Sstevel@tonic-gate free(h); 1677c478bd9Sstevel@tonic-gate return (close(fd)); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate static int 1717c478bd9Sstevel@tonic-gate stdin_stream_close(bstreamhandle h) 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate str_errno = 0; 1747c478bd9Sstevel@tonic-gate free(h); 1757c478bd9Sstevel@tonic-gate return (0); 1767c478bd9Sstevel@tonic-gate } 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate static int 1797c478bd9Sstevel@tonic-gate wav_write_stream_close(bstreamhandle h) 1807c478bd9Sstevel@tonic-gate { 1817c478bd9Sstevel@tonic-gate uint32_t sz; 1827c478bd9Sstevel@tonic-gate Wave_filehdr wav; 1837c478bd9Sstevel@tonic-gate 1847c478bd9Sstevel@tonic-gate str_errno = 0; 1857c478bd9Sstevel@tonic-gate (void) memset(&wav, 0, sizeof (wav)); 1867c478bd9Sstevel@tonic-gate sz = lseek(h->bstr_fd, 0L, SEEK_END); 1877c478bd9Sstevel@tonic-gate (void) lseek(h->bstr_fd, 0L, SEEK_SET); 1887c478bd9Sstevel@tonic-gate if (read(h->bstr_fd, &wav, sizeof (wav)) != sizeof (wav)) { 1897c478bd9Sstevel@tonic-gate return (1); 1907c478bd9Sstevel@tonic-gate } 1917c478bd9Sstevel@tonic-gate wav.total_chunk_size = CPU_TO_LE32(sz - 8); 1927c478bd9Sstevel@tonic-gate wav.data_size = CPU_TO_LE32(sz - 44); 1937c478bd9Sstevel@tonic-gate (void) lseek(h->bstr_fd, 0L, SEEK_SET); 1947c478bd9Sstevel@tonic-gate if (write(h->bstr_fd, &wav, sizeof (wav)) != sizeof (wav)) { 1957c478bd9Sstevel@tonic-gate return (1); 1967c478bd9Sstevel@tonic-gate } 1977c478bd9Sstevel@tonic-gate (void) close(h->bstr_fd); 1987c478bd9Sstevel@tonic-gate free(h); 1997c478bd9Sstevel@tonic-gate return (0); 2007c478bd9Sstevel@tonic-gate } 2017c478bd9Sstevel@tonic-gate 2027c478bd9Sstevel@tonic-gate static int 2037c478bd9Sstevel@tonic-gate au_write_stream_close(bstreamhandle h) 2047c478bd9Sstevel@tonic-gate { 2057c478bd9Sstevel@tonic-gate uint32_t sz; 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate str_errno = 0; 2087c478bd9Sstevel@tonic-gate sz = lseek(h->bstr_fd, 0L, SEEK_END); 2097c478bd9Sstevel@tonic-gate sz -= PRE_DEF_AU_HDR_LEN; 2107c478bd9Sstevel@tonic-gate sz = CPU_TO_BE32(sz); 2117c478bd9Sstevel@tonic-gate if (lseek(h->bstr_fd, 8L, SEEK_SET) < 0) 2127c478bd9Sstevel@tonic-gate return (1); 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate if (write(h->bstr_fd, &sz, 4) < 0) 2157c478bd9Sstevel@tonic-gate return (1); 2167c478bd9Sstevel@tonic-gate 2177c478bd9Sstevel@tonic-gate (void) close(h->bstr_fd); 2187c478bd9Sstevel@tonic-gate free(h); 2197c478bd9Sstevel@tonic-gate return (0); 2207c478bd9Sstevel@tonic-gate } 2217c478bd9Sstevel@tonic-gate 2227c478bd9Sstevel@tonic-gate /* ARGSUSED */ 2237c478bd9Sstevel@tonic-gate static void 2247c478bd9Sstevel@tonic-gate stdin_stream_rewind(bstreamhandle h) 2257c478bd9Sstevel@tonic-gate { 2267c478bd9Sstevel@tonic-gate } 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate static void 2297c478bd9Sstevel@tonic-gate file_stream_rewind(bstreamhandle h) 2307c478bd9Sstevel@tonic-gate { 2317c478bd9Sstevel@tonic-gate (void) lseek(h->bstr_fd, 0L, SEEK_SET); 2327c478bd9Sstevel@tonic-gate } 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate static void 2357c478bd9Sstevel@tonic-gate au_stream_rewind(bstreamhandle h) 2367c478bd9Sstevel@tonic-gate { 2377c478bd9Sstevel@tonic-gate au_filehdr_t au; 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate (void) lseek(h->bstr_fd, 0L, SEEK_SET); 2407c478bd9Sstevel@tonic-gate if (read(h->bstr_fd, &au, sizeof (au)) != sizeof (au)) { 2417c478bd9Sstevel@tonic-gate return; 2427c478bd9Sstevel@tonic-gate } 2437c478bd9Sstevel@tonic-gate 2447c478bd9Sstevel@tonic-gate if (lseek(h->bstr_fd, (long)(BE32_TO_CPU(au.au_offset)), 2457c478bd9Sstevel@tonic-gate SEEK_SET) < 0) { 2467c478bd9Sstevel@tonic-gate return; 2477c478bd9Sstevel@tonic-gate } 2487c478bd9Sstevel@tonic-gate } 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate static void 2517c478bd9Sstevel@tonic-gate wav_stream_rewind(bstreamhandle h) 2527c478bd9Sstevel@tonic-gate { 2537c478bd9Sstevel@tonic-gate (void) lseek(h->bstr_fd, (long)(sizeof (Wave_filehdr)), SEEK_SET); 2547c478bd9Sstevel@tonic-gate } 2557c478bd9Sstevel@tonic-gate 2567c478bd9Sstevel@tonic-gate bstreamhandle 2577c478bd9Sstevel@tonic-gate open_file_read_stream(char *file) 2587c478bd9Sstevel@tonic-gate { 2597c478bd9Sstevel@tonic-gate bstreamhandle h; 2607c478bd9Sstevel@tonic-gate int fd; 2617c478bd9Sstevel@tonic-gate struct stat st; 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate str_errno = 0; 2647c478bd9Sstevel@tonic-gate if (stat(file, &st) < 0) 2657c478bd9Sstevel@tonic-gate return (NULL); 2667c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) == S_IFDIR) { 2677c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_REG_FILE; 2687c478bd9Sstevel@tonic-gate return (NULL); 2697c478bd9Sstevel@tonic-gate } 2707c478bd9Sstevel@tonic-gate fd = open(file, O_RDONLY); 2717c478bd9Sstevel@tonic-gate if (fd < 0) 2727c478bd9Sstevel@tonic-gate return (NULL); 2737c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 2747c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 2757c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read; 2767c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 2777c478bd9Sstevel@tonic-gate h->bstr_size = file_stream_size; 2787c478bd9Sstevel@tonic-gate h->bstr_rewind = file_stream_rewind; 2797c478bd9Sstevel@tonic-gate 2807c478bd9Sstevel@tonic-gate return (h); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate bstreamhandle 2847c478bd9Sstevel@tonic-gate open_stdin_read_stream(void) 2857c478bd9Sstevel@tonic-gate { 2867c478bd9Sstevel@tonic-gate bstreamhandle h; 2877c478bd9Sstevel@tonic-gate int mode; 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate str_errno = 0; 2907c478bd9Sstevel@tonic-gate if ((mode = fcntl(0, F_GETFD, NULL)) < 0) { 2917c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_READ_STDIN; 2927c478bd9Sstevel@tonic-gate return (NULL); 2937c478bd9Sstevel@tonic-gate } 2947c478bd9Sstevel@tonic-gate mode &= 3; 2957c478bd9Sstevel@tonic-gate if ((mode != O_RDONLY) && (mode != O_RDWR)) { 2967c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_READ_STDIN; 2977c478bd9Sstevel@tonic-gate return (NULL); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 3007c478bd9Sstevel@tonic-gate h->bstr_fd = 0; 3017c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read; 3027c478bd9Sstevel@tonic-gate h->bstr_close = stdin_stream_close; 3037c478bd9Sstevel@tonic-gate h->bstr_size = file_stream_size; 3047c478bd9Sstevel@tonic-gate h->bstr_rewind = stdin_stream_rewind; 3057c478bd9Sstevel@tonic-gate 3067c478bd9Sstevel@tonic-gate return (h); 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate bstreamhandle 3107c478bd9Sstevel@tonic-gate open_au_read_stream(char *fname) 3117c478bd9Sstevel@tonic-gate { 3127c478bd9Sstevel@tonic-gate bstreamhandle h; 3137c478bd9Sstevel@tonic-gate int fd, sav; 3147c478bd9Sstevel@tonic-gate au_filehdr_t *au; 3157c478bd9Sstevel@tonic-gate struct stat st; 3167c478bd9Sstevel@tonic-gate uint32_t data_size; 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate au = NULL; 3197c478bd9Sstevel@tonic-gate str_errno = 0; 3207c478bd9Sstevel@tonic-gate fd = open(fname, O_RDONLY); 3217c478bd9Sstevel@tonic-gate if (fd < 0) 3227c478bd9Sstevel@tonic-gate return (NULL); 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate if (fstat(fd, &st) < 0) { 3257c478bd9Sstevel@tonic-gate goto au_open_failed; 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) != S_IFREG) { 3287c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_REG_FILE; 3297c478bd9Sstevel@tonic-gate goto au_open_failed; 3307c478bd9Sstevel@tonic-gate } 3317c478bd9Sstevel@tonic-gate au = (au_filehdr_t *)my_zalloc(sizeof (*au)); 3327c478bd9Sstevel@tonic-gate if (read(fd, au, sizeof (*au)) != sizeof (*au)) { 3337c478bd9Sstevel@tonic-gate str_errno = STR_ERR_AU_READ_ERR; 3347c478bd9Sstevel@tonic-gate goto au_open_failed; 3357c478bd9Sstevel@tonic-gate } 3367c478bd9Sstevel@tonic-gate au->au_magic = BE32_TO_CPU(au->au_magic); 3377c478bd9Sstevel@tonic-gate au->au_offset = BE32_TO_CPU(au->au_offset); 3387c478bd9Sstevel@tonic-gate au->au_data_size = BE32_TO_CPU(au->au_data_size); 3397c478bd9Sstevel@tonic-gate au->au_encoding = BE32_TO_CPU(au->au_encoding); 3407c478bd9Sstevel@tonic-gate au->au_sample_rate = BE32_TO_CPU(au->au_sample_rate); 3417c478bd9Sstevel@tonic-gate au->au_channels = BE32_TO_CPU(au->au_channels); 3427c478bd9Sstevel@tonic-gate 3437c478bd9Sstevel@tonic-gate if (au->au_magic != AUDIO_AU_FILE_MAGIC) { 3447c478bd9Sstevel@tonic-gate str_errno = STR_ERR_AU_BAD_HEADER; 3457c478bd9Sstevel@tonic-gate goto au_open_failed; 3467c478bd9Sstevel@tonic-gate } 3477c478bd9Sstevel@tonic-gate if ((au->au_encoding != AUDIO_AU_ENCODING_LINEAR_16) || 3487c478bd9Sstevel@tonic-gate (au->au_sample_rate != 44100) || (au->au_channels != 2)) { 3497c478bd9Sstevel@tonic-gate 3507c478bd9Sstevel@tonic-gate str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT; 3517c478bd9Sstevel@tonic-gate goto au_open_failed; 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate if (au->au_data_size != AUDIO_AU_UNKNOWN_SIZE) { 3547c478bd9Sstevel@tonic-gate if ((au->au_offset + au->au_data_size) != st.st_size) { 3557c478bd9Sstevel@tonic-gate str_errno = STR_ERR_AU_BAD_HEADER; 3567c478bd9Sstevel@tonic-gate goto au_open_failed; 3577c478bd9Sstevel@tonic-gate } 3587c478bd9Sstevel@tonic-gate data_size = au->au_data_size; 3597c478bd9Sstevel@tonic-gate } else { 3607c478bd9Sstevel@tonic-gate data_size = st.st_size - au->au_offset; 3617c478bd9Sstevel@tonic-gate } 3627c478bd9Sstevel@tonic-gate if (data_size == 0) { 3637c478bd9Sstevel@tonic-gate str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT; 3647c478bd9Sstevel@tonic-gate goto au_open_failed; 3657c478bd9Sstevel@tonic-gate } 3667c478bd9Sstevel@tonic-gate if (lseek(fd, au->au_offset, SEEK_SET) < 0) { 3677c478bd9Sstevel@tonic-gate goto au_open_failed; 3687c478bd9Sstevel@tonic-gate } 3697c478bd9Sstevel@tonic-gate 3707c478bd9Sstevel@tonic-gate free(au); 3717c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 3727c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 3737c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read_wrbo; 3747c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 3757c478bd9Sstevel@tonic-gate h->bstr_size = audio_stream_size; 3767c478bd9Sstevel@tonic-gate h->bstr_rewind = au_stream_rewind; 3777c478bd9Sstevel@tonic-gate h->bstr_private = (void *)data_size; 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate return (h); 3807c478bd9Sstevel@tonic-gate 3817c478bd9Sstevel@tonic-gate au_open_failed: 3827c478bd9Sstevel@tonic-gate sav = errno; 3837c478bd9Sstevel@tonic-gate (void) close(fd); 3847c478bd9Sstevel@tonic-gate if (au != NULL) 3857c478bd9Sstevel@tonic-gate free(au); 3867c478bd9Sstevel@tonic-gate errno = sav; 3877c478bd9Sstevel@tonic-gate return (NULL); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate bstreamhandle 3917c478bd9Sstevel@tonic-gate open_wav_read_stream(char *fname) 3927c478bd9Sstevel@tonic-gate { 3937c478bd9Sstevel@tonic-gate bstreamhandle h; 3947c478bd9Sstevel@tonic-gate int fd, sav; 3957c478bd9Sstevel@tonic-gate Wave_filehdr *wav; 3967c478bd9Sstevel@tonic-gate struct stat st; 3977c478bd9Sstevel@tonic-gate uint32_t data_size; 3987c478bd9Sstevel@tonic-gate 3997c478bd9Sstevel@tonic-gate wav = NULL; 4007c478bd9Sstevel@tonic-gate str_errno = 0; 4017c478bd9Sstevel@tonic-gate fd = open(fname, O_RDONLY); 4027c478bd9Sstevel@tonic-gate if (fd < 0) 4037c478bd9Sstevel@tonic-gate return (NULL); 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate if (fstat(fd, &st) < 0) { 4067c478bd9Sstevel@tonic-gate goto wav_open_failed; 4077c478bd9Sstevel@tonic-gate } 4087c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) != S_IFREG) { 4097c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_REG_FILE; 4107c478bd9Sstevel@tonic-gate goto wav_open_failed; 4117c478bd9Sstevel@tonic-gate } 4127c478bd9Sstevel@tonic-gate wav = (Wave_filehdr *)my_zalloc(sizeof (*wav)); 4137c478bd9Sstevel@tonic-gate if (read(fd, wav, sizeof (*wav)) != sizeof (*wav)) { 4147c478bd9Sstevel@tonic-gate str_errno = STR_ERR_WAV_READ_ERR; 4157c478bd9Sstevel@tonic-gate goto wav_open_failed; 4167c478bd9Sstevel@tonic-gate } 4177c478bd9Sstevel@tonic-gate if ((strncmp(wav->riff, "RIFF", 4) != 0) || 4187c478bd9Sstevel@tonic-gate (strncmp(wav->wave, "WAVE", 4) != 0)) { 4197c478bd9Sstevel@tonic-gate str_errno = STR_ERR_WAV_BAD_HEADER; 4207c478bd9Sstevel@tonic-gate goto wav_open_failed; 4217c478bd9Sstevel@tonic-gate } 4227c478bd9Sstevel@tonic-gate if (((CPU_TO_LE32(wav->total_chunk_size) + 8) != st.st_size) || 4237c478bd9Sstevel@tonic-gate (strncmp(wav->fmt, "fmt ", 4) != 0) || 4247c478bd9Sstevel@tonic-gate (CPU_TO_LE16(wav->fmt_tag) != 1) || 4257c478bd9Sstevel@tonic-gate (CPU_TO_LE16(wav->n_channels) != 2) || 4267c478bd9Sstevel@tonic-gate (CPU_TO_LE32(wav->sample_rate) != 44100) || 4277c478bd9Sstevel@tonic-gate (CPU_TO_LE16(wav->bits_per_sample) != 16) || 4287c478bd9Sstevel@tonic-gate (strncmp(wav->data, "data", 4) != 0) || 4297c478bd9Sstevel@tonic-gate ((CPU_TO_LE32(wav->data_size) + 44) != st.st_size)) { 4307c478bd9Sstevel@tonic-gate 4317c478bd9Sstevel@tonic-gate str_errno = STR_ERR_WAV_UNSUPPORTED_FORMAT; 4327c478bd9Sstevel@tonic-gate goto wav_open_failed; 4337c478bd9Sstevel@tonic-gate } 4347c478bd9Sstevel@tonic-gate data_size = CPU_TO_LE32(wav->data_size); 4357c478bd9Sstevel@tonic-gate if (lseek(fd, sizeof (*wav), SEEK_SET) < 0) { 4367c478bd9Sstevel@tonic-gate goto wav_open_failed; 4377c478bd9Sstevel@tonic-gate } 4387c478bd9Sstevel@tonic-gate 4397c478bd9Sstevel@tonic-gate free(wav); 4407c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 4417c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 4427c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read; 4437c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 4447c478bd9Sstevel@tonic-gate h->bstr_size = audio_stream_size; 4457c478bd9Sstevel@tonic-gate h->bstr_rewind = wav_stream_rewind; 4467c478bd9Sstevel@tonic-gate h->bstr_private = (void *)data_size; 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate return (h); 4497c478bd9Sstevel@tonic-gate 4507c478bd9Sstevel@tonic-gate wav_open_failed: 4517c478bd9Sstevel@tonic-gate sav = errno; 4527c478bd9Sstevel@tonic-gate (void) close(fd); 4537c478bd9Sstevel@tonic-gate if (wav != NULL) 4547c478bd9Sstevel@tonic-gate free(wav); 4557c478bd9Sstevel@tonic-gate errno = sav; 4567c478bd9Sstevel@tonic-gate return (NULL); 4577c478bd9Sstevel@tonic-gate } 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate bstreamhandle 4607c478bd9Sstevel@tonic-gate open_aur_read_stream(char *fname) 4617c478bd9Sstevel@tonic-gate { 4627c478bd9Sstevel@tonic-gate bstreamhandle h; 4637c478bd9Sstevel@tonic-gate 4647c478bd9Sstevel@tonic-gate h = open_file_read_stream(fname); 4657c478bd9Sstevel@tonic-gate if (h != NULL) { 4667c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read_wrbo; 4677c478bd9Sstevel@tonic-gate } 4687c478bd9Sstevel@tonic-gate return (h); 4697c478bd9Sstevel@tonic-gate } 4707c478bd9Sstevel@tonic-gate 4717c478bd9Sstevel@tonic-gate bstreamhandle 4727c478bd9Sstevel@tonic-gate open_au_write_stream(char *fname) 4737c478bd9Sstevel@tonic-gate { 4747c478bd9Sstevel@tonic-gate bstreamhandle h; 4757c478bd9Sstevel@tonic-gate int esav, fd; 4767c478bd9Sstevel@tonic-gate uchar_t head[] = PRE_DEF_AU_HDR; 4777c478bd9Sstevel@tonic-gate 4787c478bd9Sstevel@tonic-gate str_errno = 0; 4797c478bd9Sstevel@tonic-gate fd = -1; 4807c478bd9Sstevel@tonic-gate /* O_RDWR because we need to read while closing */ 4817c478bd9Sstevel@tonic-gate fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); 4827c478bd9Sstevel@tonic-gate if (fd < 0) 4837c478bd9Sstevel@tonic-gate goto open_au_write_stream_failed; 4847c478bd9Sstevel@tonic-gate if (write(fd, head, PRE_DEF_AU_HDR_LEN) != PRE_DEF_AU_HDR_LEN) { 4857c478bd9Sstevel@tonic-gate goto open_au_write_stream_failed; 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 4887c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 4897c478bd9Sstevel@tonic-gate h->bstr_write = file_stream_write_wrbo; 4907c478bd9Sstevel@tonic-gate h->bstr_close = au_write_stream_close; 4917c478bd9Sstevel@tonic-gate return (h); 4927c478bd9Sstevel@tonic-gate 4937c478bd9Sstevel@tonic-gate open_au_write_stream_failed: 4947c478bd9Sstevel@tonic-gate esav = errno; 4957c478bd9Sstevel@tonic-gate if (fd != -1) 4967c478bd9Sstevel@tonic-gate (void) close(fd); 4977c478bd9Sstevel@tonic-gate errno = esav; 4987c478bd9Sstevel@tonic-gate return (NULL); 4997c478bd9Sstevel@tonic-gate } 5007c478bd9Sstevel@tonic-gate 5017c478bd9Sstevel@tonic-gate bstreamhandle 5027c478bd9Sstevel@tonic-gate open_wav_write_stream(char *fname) 5037c478bd9Sstevel@tonic-gate { 5047c478bd9Sstevel@tonic-gate bstreamhandle h; 5057c478bd9Sstevel@tonic-gate int esav, fd; 5067c478bd9Sstevel@tonic-gate uchar_t head[] = PRE_DEF_WAV_HDR; 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate str_errno = 0; 5097c478bd9Sstevel@tonic-gate fd = -1; 5107c478bd9Sstevel@tonic-gate fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); 5117c478bd9Sstevel@tonic-gate if (fd < 0) 5127c478bd9Sstevel@tonic-gate goto open_wav_write_stream_failed; 5137c478bd9Sstevel@tonic-gate if (write(fd, head, PRE_DEF_WAV_HDR_LEN) != PRE_DEF_WAV_HDR_LEN) { 5147c478bd9Sstevel@tonic-gate goto open_wav_write_stream_failed; 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 5177c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 5187c478bd9Sstevel@tonic-gate h->bstr_write = file_stream_write; 5197c478bd9Sstevel@tonic-gate h->bstr_close = wav_write_stream_close; 5207c478bd9Sstevel@tonic-gate return (h); 5217c478bd9Sstevel@tonic-gate 5227c478bd9Sstevel@tonic-gate open_wav_write_stream_failed: 5237c478bd9Sstevel@tonic-gate esav = errno; 5247c478bd9Sstevel@tonic-gate if (fd != -1) 5257c478bd9Sstevel@tonic-gate (void) close(fd); 5267c478bd9Sstevel@tonic-gate errno = esav; 5277c478bd9Sstevel@tonic-gate return (NULL); 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate bstreamhandle 5317c478bd9Sstevel@tonic-gate open_aur_write_stream(char *fname) 5327c478bd9Sstevel@tonic-gate { 5337c478bd9Sstevel@tonic-gate bstreamhandle h; 5347c478bd9Sstevel@tonic-gate int fd; 5357c478bd9Sstevel@tonic-gate 5367c478bd9Sstevel@tonic-gate str_errno = 0; 5377c478bd9Sstevel@tonic-gate fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); 5387c478bd9Sstevel@tonic-gate if (fd < 0) 5397c478bd9Sstevel@tonic-gate return (NULL); 5407c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 5417c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 5427c478bd9Sstevel@tonic-gate h->bstr_write = file_stream_write_wrbo; 5437c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 5447c478bd9Sstevel@tonic-gate return (h); 5457c478bd9Sstevel@tonic-gate } 5467c478bd9Sstevel@tonic-gate 5477c478bd9Sstevel@tonic-gate bstreamhandle 5487c478bd9Sstevel@tonic-gate open_file_write_stream(char *fname) 5497c478bd9Sstevel@tonic-gate { 5507c478bd9Sstevel@tonic-gate bstreamhandle h; 5517c478bd9Sstevel@tonic-gate int fd; 5527c478bd9Sstevel@tonic-gate 5537c478bd9Sstevel@tonic-gate str_errno = 0; 5547c478bd9Sstevel@tonic-gate fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); 5557c478bd9Sstevel@tonic-gate if (fd < 0) 5567c478bd9Sstevel@tonic-gate return (NULL); 5577c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 5587c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 5597c478bd9Sstevel@tonic-gate h->bstr_write = file_stream_write; 5607c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 5617c478bd9Sstevel@tonic-gate return (h); 5627c478bd9Sstevel@tonic-gate } 5637c478bd9Sstevel@tonic-gate 5647c478bd9Sstevel@tonic-gate bstreamhandle 5657c478bd9Sstevel@tonic-gate open_temp_file_stream(void) 5667c478bd9Sstevel@tonic-gate { 5677c478bd9Sstevel@tonic-gate bstreamhandle h; 5687c478bd9Sstevel@tonic-gate char *t; 5697c478bd9Sstevel@tonic-gate int fd; 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate str_errno = 0; 5727c478bd9Sstevel@tonic-gate 5737c478bd9Sstevel@tonic-gate t = (char *)get_tmp_name(); 5747c478bd9Sstevel@tonic-gate 5757c478bd9Sstevel@tonic-gate if (strlcat(t, "/cdXXXXXX", PATH_MAX) >= PATH_MAX) 5767c478bd9Sstevel@tonic-gate return (NULL); 5777c478bd9Sstevel@tonic-gate 5787c478bd9Sstevel@tonic-gate fd = mkstemp(t); 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate if (debug) 5817c478bd9Sstevel@tonic-gate (void) printf("temp is: %s length: %d\n", t, strlen(t)); 5827c478bd9Sstevel@tonic-gate 5837c478bd9Sstevel@tonic-gate if (fd < 0) 5847c478bd9Sstevel@tonic-gate return (NULL); 5857c478bd9Sstevel@tonic-gate (void) unlink(t); 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 5887c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 5897c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read; 5907c478bd9Sstevel@tonic-gate h->bstr_write = file_stream_write; 5917c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 5927c478bd9Sstevel@tonic-gate h->bstr_size = file_stream_size; 5937c478bd9Sstevel@tonic-gate h->bstr_rewind = file_stream_rewind; 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate return (h); 5967c478bd9Sstevel@tonic-gate } 5977c478bd9Sstevel@tonic-gate 598*416baccdSec158148 /* 599*416baccdSec158148 * check_avail_temp_space returns 0 if there is adequate space 600*416baccdSec158148 * in the temporary directory, or a non-zero error code if 601*416baccdSec158148 * something goes wrong 602*416baccdSec158148 */ 6037c478bd9Sstevel@tonic-gate int 604*416baccdSec158148 check_avail_temp_space(size_t req_size) 6057c478bd9Sstevel@tonic-gate { 6067c478bd9Sstevel@tonic-gate struct statvfs buf; 607*416baccdSec158148 size_t free_size = 0; 6087c478bd9Sstevel@tonic-gate 609*416baccdSec158148 if (statvfs(get_tmp_name(), &buf) < 0) { 610*416baccdSec158148 return (errno); 611*416baccdSec158148 } 6127c478bd9Sstevel@tonic-gate 613*416baccdSec158148 free_size = buf.f_bfree * buf.f_frsize; 6147c478bd9Sstevel@tonic-gate 615*416baccdSec158148 if (free_size <= req_size) 616*416baccdSec158148 return (ENOMEM); 617*416baccdSec158148 6187c478bd9Sstevel@tonic-gate return (0); 6197c478bd9Sstevel@tonic-gate } 6207c478bd9Sstevel@tonic-gate 6217c478bd9Sstevel@tonic-gate 6227c478bd9Sstevel@tonic-gate char * 6237c478bd9Sstevel@tonic-gate get_tmp_name(void) 6247c478bd9Sstevel@tonic-gate { 6257c478bd9Sstevel@tonic-gate char *t; 6267c478bd9Sstevel@tonic-gate char *envptr; 6277c478bd9Sstevel@tonic-gate 6287c478bd9Sstevel@tonic-gate t = (char *)my_zalloc(PATH_MAX); 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate /* 6317c478bd9Sstevel@tonic-gate * generate temp directory path based on this order: 6327c478bd9Sstevel@tonic-gate * user specified (-m option), temp env variable, 6337c478bd9Sstevel@tonic-gate * and finally /tmp if nothing is found. 6347c478bd9Sstevel@tonic-gate */ 6357c478bd9Sstevel@tonic-gate 6367c478bd9Sstevel@tonic-gate if (alt_tmp_dir) { 6377c478bd9Sstevel@tonic-gate 6387c478bd9Sstevel@tonic-gate /* copy and leave room for temp filename */ 6397c478bd9Sstevel@tonic-gate 6407c478bd9Sstevel@tonic-gate (void) strlcpy(t, alt_tmp_dir, PATH_MAX - 10); 6417c478bd9Sstevel@tonic-gate } else { 6427c478bd9Sstevel@tonic-gate envptr = getenv("TMPDIR"); 6437c478bd9Sstevel@tonic-gate if (envptr != NULL) { 6447c478bd9Sstevel@tonic-gate (void) strlcpy(t, envptr, PATH_MAX - 10); 6457c478bd9Sstevel@tonic-gate } else { 6467c478bd9Sstevel@tonic-gate (void) strlcpy(t, "/tmp", 5); 6477c478bd9Sstevel@tonic-gate } 6487c478bd9Sstevel@tonic-gate } 6497c478bd9Sstevel@tonic-gate 6507c478bd9Sstevel@tonic-gate /* 6517c478bd9Sstevel@tonic-gate * no need to check if path is valid. statvfs will catch 6527c478bd9Sstevel@tonic-gate * it later and fail with a proper error message. 6537c478bd9Sstevel@tonic-gate */ 6547c478bd9Sstevel@tonic-gate 6557c478bd9Sstevel@tonic-gate return (t); 6567c478bd9Sstevel@tonic-gate } 657