1*7c478bd9Sstevel@tonic-gate /* 2*7c478bd9Sstevel@tonic-gate * CDDL HEADER START 3*7c478bd9Sstevel@tonic-gate * 4*7c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*7c478bd9Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only 6*7c478bd9Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance 7*7c478bd9Sstevel@tonic-gate * with the License. 8*7c478bd9Sstevel@tonic-gate * 9*7c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*7c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 11*7c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 12*7c478bd9Sstevel@tonic-gate * and limitations under the License. 13*7c478bd9Sstevel@tonic-gate * 14*7c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 15*7c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*7c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 17*7c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 18*7c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 19*7c478bd9Sstevel@tonic-gate * 20*7c478bd9Sstevel@tonic-gate * CDDL HEADER END 21*7c478bd9Sstevel@tonic-gate */ 22*7c478bd9Sstevel@tonic-gate /* 23*7c478bd9Sstevel@tonic-gate * Copyright 2004 Sun Microsystems, Inc. All rights reserved. 24*7c478bd9Sstevel@tonic-gate * Use is subject to license terms. 25*7c478bd9Sstevel@tonic-gate */ 26*7c478bd9Sstevel@tonic-gate 27*7c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate #include <fcntl.h> 30*7c478bd9Sstevel@tonic-gate #include <string.h> 31*7c478bd9Sstevel@tonic-gate #include <errno.h> 32*7c478bd9Sstevel@tonic-gate #include <sys/types.h> 33*7c478bd9Sstevel@tonic-gate #include <unistd.h> 34*7c478bd9Sstevel@tonic-gate #include <sys/stat.h> 35*7c478bd9Sstevel@tonic-gate #include <sys/statvfs.h> 36*7c478bd9Sstevel@tonic-gate #include <stdio.h> 37*7c478bd9Sstevel@tonic-gate #include <stdlib.h> 38*7c478bd9Sstevel@tonic-gate #include <libintl.h> 39*7c478bd9Sstevel@tonic-gate #include <limits.h> 40*7c478bd9Sstevel@tonic-gate #include <audio/au.h> 41*7c478bd9Sstevel@tonic-gate 42*7c478bd9Sstevel@tonic-gate #include "bstream.h" 43*7c478bd9Sstevel@tonic-gate #include "util.h" 44*7c478bd9Sstevel@tonic-gate #include "audio.h" 45*7c478bd9Sstevel@tonic-gate #include "byteorder.h" 46*7c478bd9Sstevel@tonic-gate #include "main.h" 47*7c478bd9Sstevel@tonic-gate 48*7c478bd9Sstevel@tonic-gate int str_errno; 49*7c478bd9Sstevel@tonic-gate 50*7c478bd9Sstevel@tonic-gate char * 51*7c478bd9Sstevel@tonic-gate str_errno_to_string(int serrno) 52*7c478bd9Sstevel@tonic-gate { 53*7c478bd9Sstevel@tonic-gate switch (serrno) { 54*7c478bd9Sstevel@tonic-gate case STR_ERR_NO_ERR: 55*7c478bd9Sstevel@tonic-gate return (gettext("No error")); 56*7c478bd9Sstevel@tonic-gate case STR_ERR_NO_REG_FILE: 57*7c478bd9Sstevel@tonic-gate return (gettext("Not a regular file")); 58*7c478bd9Sstevel@tonic-gate case STR_ERR_NO_READ_STDIN: 59*7c478bd9Sstevel@tonic-gate return (gettext("Stdin not open for reading")); 60*7c478bd9Sstevel@tonic-gate case STR_ERR_AU_READ_ERR: 61*7c478bd9Sstevel@tonic-gate return (gettext("Unable to read au header")); 62*7c478bd9Sstevel@tonic-gate case STR_ERR_AU_UNSUPPORTED_FORMAT: 63*7c478bd9Sstevel@tonic-gate return (gettext("Unsupported au format")); 64*7c478bd9Sstevel@tonic-gate case STR_ERR_AU_BAD_HEADER: 65*7c478bd9Sstevel@tonic-gate return (gettext("Bad au header")); 66*7c478bd9Sstevel@tonic-gate case STR_ERR_WAV_READ_ERR: 67*7c478bd9Sstevel@tonic-gate return (gettext("Unable to read wav header")); 68*7c478bd9Sstevel@tonic-gate case STR_ERR_WAV_UNSUPPORTED_FORMAT: 69*7c478bd9Sstevel@tonic-gate return (gettext("Unsupported wav format")); 70*7c478bd9Sstevel@tonic-gate case STR_ERR_WAV_BAD_HEADER: 71*7c478bd9Sstevel@tonic-gate return (gettext("Bad wav header")); 72*7c478bd9Sstevel@tonic-gate default: 73*7c478bd9Sstevel@tonic-gate return (gettext("unknown error")); 74*7c478bd9Sstevel@tonic-gate } 75*7c478bd9Sstevel@tonic-gate } 76*7c478bd9Sstevel@tonic-gate 77*7c478bd9Sstevel@tonic-gate static int 78*7c478bd9Sstevel@tonic-gate file_stream_size(bstreamhandle h, off_t *size) 79*7c478bd9Sstevel@tonic-gate { 80*7c478bd9Sstevel@tonic-gate struct stat st; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate str_errno = 0; 83*7c478bd9Sstevel@tonic-gate 84*7c478bd9Sstevel@tonic-gate if (fstat(h->bstr_fd, &st) < 0) 85*7c478bd9Sstevel@tonic-gate return (0); 86*7c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) != S_IFREG) { 87*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_REG_FILE; 88*7c478bd9Sstevel@tonic-gate return (0); 89*7c478bd9Sstevel@tonic-gate } 90*7c478bd9Sstevel@tonic-gate *size = st.st_size; 91*7c478bd9Sstevel@tonic-gate return (1); 92*7c478bd9Sstevel@tonic-gate } 93*7c478bd9Sstevel@tonic-gate 94*7c478bd9Sstevel@tonic-gate static int 95*7c478bd9Sstevel@tonic-gate audio_stream_size(bstreamhandle h, off_t *size) 96*7c478bd9Sstevel@tonic-gate { 97*7c478bd9Sstevel@tonic-gate str_errno = 0; 98*7c478bd9Sstevel@tonic-gate *size = (off_t)(uintptr_t)(h->bstr_private); 99*7c478bd9Sstevel@tonic-gate return (1); 100*7c478bd9Sstevel@tonic-gate } 101*7c478bd9Sstevel@tonic-gate 102*7c478bd9Sstevel@tonic-gate static int 103*7c478bd9Sstevel@tonic-gate file_stream_read(bstreamhandle h, uchar_t *buf, off_t size) 104*7c478bd9Sstevel@tonic-gate { 105*7c478bd9Sstevel@tonic-gate str_errno = 0; 106*7c478bd9Sstevel@tonic-gate return (read(h->bstr_fd, buf, size)); 107*7c478bd9Sstevel@tonic-gate } 108*7c478bd9Sstevel@tonic-gate 109*7c478bd9Sstevel@tonic-gate static int 110*7c478bd9Sstevel@tonic-gate file_stream_write(bstreamhandle h, uchar_t *buf, off_t size) 111*7c478bd9Sstevel@tonic-gate { 112*7c478bd9Sstevel@tonic-gate str_errno = 0; 113*7c478bd9Sstevel@tonic-gate return (write(h->bstr_fd, buf, size)); 114*7c478bd9Sstevel@tonic-gate } 115*7c478bd9Sstevel@tonic-gate 116*7c478bd9Sstevel@tonic-gate /* 117*7c478bd9Sstevel@tonic-gate * with reverse byteorder 118*7c478bd9Sstevel@tonic-gate */ 119*7c478bd9Sstevel@tonic-gate static int 120*7c478bd9Sstevel@tonic-gate file_stream_read_wrbo(bstreamhandle h, uchar_t *buf, off_t size) 121*7c478bd9Sstevel@tonic-gate { 122*7c478bd9Sstevel@tonic-gate int cnt; 123*7c478bd9Sstevel@tonic-gate 124*7c478bd9Sstevel@tonic-gate str_errno = 0; 125*7c478bd9Sstevel@tonic-gate cnt = read(h->bstr_fd, buf, size); 126*7c478bd9Sstevel@tonic-gate if (cnt > 0) { 127*7c478bd9Sstevel@tonic-gate int i; 128*7c478bd9Sstevel@tonic-gate uchar_t ch; 129*7c478bd9Sstevel@tonic-gate 130*7c478bd9Sstevel@tonic-gate for (i = 0; i < cnt; i += 2) { 131*7c478bd9Sstevel@tonic-gate ch = buf[i]; 132*7c478bd9Sstevel@tonic-gate buf[i] = buf[i+1]; 133*7c478bd9Sstevel@tonic-gate buf[i+1] = ch; 134*7c478bd9Sstevel@tonic-gate } 135*7c478bd9Sstevel@tonic-gate } 136*7c478bd9Sstevel@tonic-gate return (cnt); 137*7c478bd9Sstevel@tonic-gate } 138*7c478bd9Sstevel@tonic-gate 139*7c478bd9Sstevel@tonic-gate /* 140*7c478bd9Sstevel@tonic-gate * This will change the byteorder in the buffer but that is fine with us. 141*7c478bd9Sstevel@tonic-gate */ 142*7c478bd9Sstevel@tonic-gate static int 143*7c478bd9Sstevel@tonic-gate file_stream_write_wrbo(bstreamhandle h, uchar_t *buf, off_t size) 144*7c478bd9Sstevel@tonic-gate { 145*7c478bd9Sstevel@tonic-gate int i; 146*7c478bd9Sstevel@tonic-gate uchar_t ch; 147*7c478bd9Sstevel@tonic-gate 148*7c478bd9Sstevel@tonic-gate str_errno = 0; 149*7c478bd9Sstevel@tonic-gate if (size > 0) { 150*7c478bd9Sstevel@tonic-gate for (i = 0; i < size; i += 2) { 151*7c478bd9Sstevel@tonic-gate ch = buf[i]; 152*7c478bd9Sstevel@tonic-gate buf[i] = buf[i+1]; 153*7c478bd9Sstevel@tonic-gate buf[i+1] = ch; 154*7c478bd9Sstevel@tonic-gate } 155*7c478bd9Sstevel@tonic-gate } 156*7c478bd9Sstevel@tonic-gate return (write(h->bstr_fd, buf, size)); 157*7c478bd9Sstevel@tonic-gate } 158*7c478bd9Sstevel@tonic-gate 159*7c478bd9Sstevel@tonic-gate static int 160*7c478bd9Sstevel@tonic-gate file_stream_close(bstreamhandle h) 161*7c478bd9Sstevel@tonic-gate { 162*7c478bd9Sstevel@tonic-gate int fd; 163*7c478bd9Sstevel@tonic-gate 164*7c478bd9Sstevel@tonic-gate str_errno = 0; 165*7c478bd9Sstevel@tonic-gate fd = h->bstr_fd; 166*7c478bd9Sstevel@tonic-gate free(h); 167*7c478bd9Sstevel@tonic-gate return (close(fd)); 168*7c478bd9Sstevel@tonic-gate } 169*7c478bd9Sstevel@tonic-gate 170*7c478bd9Sstevel@tonic-gate static int 171*7c478bd9Sstevel@tonic-gate stdin_stream_close(bstreamhandle h) 172*7c478bd9Sstevel@tonic-gate { 173*7c478bd9Sstevel@tonic-gate str_errno = 0; 174*7c478bd9Sstevel@tonic-gate free(h); 175*7c478bd9Sstevel@tonic-gate return (0); 176*7c478bd9Sstevel@tonic-gate } 177*7c478bd9Sstevel@tonic-gate 178*7c478bd9Sstevel@tonic-gate static int 179*7c478bd9Sstevel@tonic-gate wav_write_stream_close(bstreamhandle h) 180*7c478bd9Sstevel@tonic-gate { 181*7c478bd9Sstevel@tonic-gate uint32_t sz; 182*7c478bd9Sstevel@tonic-gate Wave_filehdr wav; 183*7c478bd9Sstevel@tonic-gate 184*7c478bd9Sstevel@tonic-gate str_errno = 0; 185*7c478bd9Sstevel@tonic-gate (void) memset(&wav, 0, sizeof (wav)); 186*7c478bd9Sstevel@tonic-gate sz = lseek(h->bstr_fd, 0L, SEEK_END); 187*7c478bd9Sstevel@tonic-gate (void) lseek(h->bstr_fd, 0L, SEEK_SET); 188*7c478bd9Sstevel@tonic-gate if (read(h->bstr_fd, &wav, sizeof (wav)) != sizeof (wav)) { 189*7c478bd9Sstevel@tonic-gate return (1); 190*7c478bd9Sstevel@tonic-gate } 191*7c478bd9Sstevel@tonic-gate wav.total_chunk_size = CPU_TO_LE32(sz - 8); 192*7c478bd9Sstevel@tonic-gate wav.data_size = CPU_TO_LE32(sz - 44); 193*7c478bd9Sstevel@tonic-gate (void) lseek(h->bstr_fd, 0L, SEEK_SET); 194*7c478bd9Sstevel@tonic-gate if (write(h->bstr_fd, &wav, sizeof (wav)) != sizeof (wav)) { 195*7c478bd9Sstevel@tonic-gate return (1); 196*7c478bd9Sstevel@tonic-gate } 197*7c478bd9Sstevel@tonic-gate (void) close(h->bstr_fd); 198*7c478bd9Sstevel@tonic-gate free(h); 199*7c478bd9Sstevel@tonic-gate return (0); 200*7c478bd9Sstevel@tonic-gate } 201*7c478bd9Sstevel@tonic-gate 202*7c478bd9Sstevel@tonic-gate static int 203*7c478bd9Sstevel@tonic-gate au_write_stream_close(bstreamhandle h) 204*7c478bd9Sstevel@tonic-gate { 205*7c478bd9Sstevel@tonic-gate uint32_t sz; 206*7c478bd9Sstevel@tonic-gate 207*7c478bd9Sstevel@tonic-gate str_errno = 0; 208*7c478bd9Sstevel@tonic-gate sz = lseek(h->bstr_fd, 0L, SEEK_END); 209*7c478bd9Sstevel@tonic-gate sz -= PRE_DEF_AU_HDR_LEN; 210*7c478bd9Sstevel@tonic-gate sz = CPU_TO_BE32(sz); 211*7c478bd9Sstevel@tonic-gate if (lseek(h->bstr_fd, 8L, SEEK_SET) < 0) 212*7c478bd9Sstevel@tonic-gate return (1); 213*7c478bd9Sstevel@tonic-gate 214*7c478bd9Sstevel@tonic-gate if (write(h->bstr_fd, &sz, 4) < 0) 215*7c478bd9Sstevel@tonic-gate return (1); 216*7c478bd9Sstevel@tonic-gate 217*7c478bd9Sstevel@tonic-gate (void) close(h->bstr_fd); 218*7c478bd9Sstevel@tonic-gate free(h); 219*7c478bd9Sstevel@tonic-gate return (0); 220*7c478bd9Sstevel@tonic-gate } 221*7c478bd9Sstevel@tonic-gate 222*7c478bd9Sstevel@tonic-gate /* ARGSUSED */ 223*7c478bd9Sstevel@tonic-gate static void 224*7c478bd9Sstevel@tonic-gate stdin_stream_rewind(bstreamhandle h) 225*7c478bd9Sstevel@tonic-gate { 226*7c478bd9Sstevel@tonic-gate } 227*7c478bd9Sstevel@tonic-gate 228*7c478bd9Sstevel@tonic-gate static void 229*7c478bd9Sstevel@tonic-gate file_stream_rewind(bstreamhandle h) 230*7c478bd9Sstevel@tonic-gate { 231*7c478bd9Sstevel@tonic-gate (void) lseek(h->bstr_fd, 0L, SEEK_SET); 232*7c478bd9Sstevel@tonic-gate } 233*7c478bd9Sstevel@tonic-gate 234*7c478bd9Sstevel@tonic-gate static void 235*7c478bd9Sstevel@tonic-gate au_stream_rewind(bstreamhandle h) 236*7c478bd9Sstevel@tonic-gate { 237*7c478bd9Sstevel@tonic-gate au_filehdr_t au; 238*7c478bd9Sstevel@tonic-gate 239*7c478bd9Sstevel@tonic-gate (void) lseek(h->bstr_fd, 0L, SEEK_SET); 240*7c478bd9Sstevel@tonic-gate if (read(h->bstr_fd, &au, sizeof (au)) != sizeof (au)) { 241*7c478bd9Sstevel@tonic-gate return; 242*7c478bd9Sstevel@tonic-gate } 243*7c478bd9Sstevel@tonic-gate 244*7c478bd9Sstevel@tonic-gate if (lseek(h->bstr_fd, (long)(BE32_TO_CPU(au.au_offset)), 245*7c478bd9Sstevel@tonic-gate SEEK_SET) < 0) { 246*7c478bd9Sstevel@tonic-gate return; 247*7c478bd9Sstevel@tonic-gate } 248*7c478bd9Sstevel@tonic-gate } 249*7c478bd9Sstevel@tonic-gate 250*7c478bd9Sstevel@tonic-gate static void 251*7c478bd9Sstevel@tonic-gate wav_stream_rewind(bstreamhandle h) 252*7c478bd9Sstevel@tonic-gate { 253*7c478bd9Sstevel@tonic-gate (void) lseek(h->bstr_fd, (long)(sizeof (Wave_filehdr)), SEEK_SET); 254*7c478bd9Sstevel@tonic-gate } 255*7c478bd9Sstevel@tonic-gate 256*7c478bd9Sstevel@tonic-gate bstreamhandle 257*7c478bd9Sstevel@tonic-gate open_file_read_stream(char *file) 258*7c478bd9Sstevel@tonic-gate { 259*7c478bd9Sstevel@tonic-gate bstreamhandle h; 260*7c478bd9Sstevel@tonic-gate int fd; 261*7c478bd9Sstevel@tonic-gate struct stat st; 262*7c478bd9Sstevel@tonic-gate 263*7c478bd9Sstevel@tonic-gate str_errno = 0; 264*7c478bd9Sstevel@tonic-gate if (stat(file, &st) < 0) 265*7c478bd9Sstevel@tonic-gate return (NULL); 266*7c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) == S_IFDIR) { 267*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_REG_FILE; 268*7c478bd9Sstevel@tonic-gate return (NULL); 269*7c478bd9Sstevel@tonic-gate } 270*7c478bd9Sstevel@tonic-gate fd = open(file, O_RDONLY); 271*7c478bd9Sstevel@tonic-gate if (fd < 0) 272*7c478bd9Sstevel@tonic-gate return (NULL); 273*7c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 274*7c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 275*7c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read; 276*7c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 277*7c478bd9Sstevel@tonic-gate h->bstr_size = file_stream_size; 278*7c478bd9Sstevel@tonic-gate h->bstr_rewind = file_stream_rewind; 279*7c478bd9Sstevel@tonic-gate 280*7c478bd9Sstevel@tonic-gate return (h); 281*7c478bd9Sstevel@tonic-gate } 282*7c478bd9Sstevel@tonic-gate 283*7c478bd9Sstevel@tonic-gate bstreamhandle 284*7c478bd9Sstevel@tonic-gate open_stdin_read_stream(void) 285*7c478bd9Sstevel@tonic-gate { 286*7c478bd9Sstevel@tonic-gate bstreamhandle h; 287*7c478bd9Sstevel@tonic-gate int mode; 288*7c478bd9Sstevel@tonic-gate 289*7c478bd9Sstevel@tonic-gate str_errno = 0; 290*7c478bd9Sstevel@tonic-gate if ((mode = fcntl(0, F_GETFD, NULL)) < 0) { 291*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_READ_STDIN; 292*7c478bd9Sstevel@tonic-gate return (NULL); 293*7c478bd9Sstevel@tonic-gate } 294*7c478bd9Sstevel@tonic-gate mode &= 3; 295*7c478bd9Sstevel@tonic-gate if ((mode != O_RDONLY) && (mode != O_RDWR)) { 296*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_READ_STDIN; 297*7c478bd9Sstevel@tonic-gate return (NULL); 298*7c478bd9Sstevel@tonic-gate } 299*7c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 300*7c478bd9Sstevel@tonic-gate h->bstr_fd = 0; 301*7c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read; 302*7c478bd9Sstevel@tonic-gate h->bstr_close = stdin_stream_close; 303*7c478bd9Sstevel@tonic-gate h->bstr_size = file_stream_size; 304*7c478bd9Sstevel@tonic-gate h->bstr_rewind = stdin_stream_rewind; 305*7c478bd9Sstevel@tonic-gate 306*7c478bd9Sstevel@tonic-gate return (h); 307*7c478bd9Sstevel@tonic-gate } 308*7c478bd9Sstevel@tonic-gate 309*7c478bd9Sstevel@tonic-gate bstreamhandle 310*7c478bd9Sstevel@tonic-gate open_au_read_stream(char *fname) 311*7c478bd9Sstevel@tonic-gate { 312*7c478bd9Sstevel@tonic-gate bstreamhandle h; 313*7c478bd9Sstevel@tonic-gate int fd, sav; 314*7c478bd9Sstevel@tonic-gate au_filehdr_t *au; 315*7c478bd9Sstevel@tonic-gate struct stat st; 316*7c478bd9Sstevel@tonic-gate uint32_t data_size; 317*7c478bd9Sstevel@tonic-gate 318*7c478bd9Sstevel@tonic-gate au = NULL; 319*7c478bd9Sstevel@tonic-gate str_errno = 0; 320*7c478bd9Sstevel@tonic-gate fd = open(fname, O_RDONLY); 321*7c478bd9Sstevel@tonic-gate if (fd < 0) 322*7c478bd9Sstevel@tonic-gate return (NULL); 323*7c478bd9Sstevel@tonic-gate 324*7c478bd9Sstevel@tonic-gate if (fstat(fd, &st) < 0) { 325*7c478bd9Sstevel@tonic-gate goto au_open_failed; 326*7c478bd9Sstevel@tonic-gate } 327*7c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) != S_IFREG) { 328*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_REG_FILE; 329*7c478bd9Sstevel@tonic-gate goto au_open_failed; 330*7c478bd9Sstevel@tonic-gate } 331*7c478bd9Sstevel@tonic-gate au = (au_filehdr_t *)my_zalloc(sizeof (*au)); 332*7c478bd9Sstevel@tonic-gate if (read(fd, au, sizeof (*au)) != sizeof (*au)) { 333*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_AU_READ_ERR; 334*7c478bd9Sstevel@tonic-gate goto au_open_failed; 335*7c478bd9Sstevel@tonic-gate } 336*7c478bd9Sstevel@tonic-gate au->au_magic = BE32_TO_CPU(au->au_magic); 337*7c478bd9Sstevel@tonic-gate au->au_offset = BE32_TO_CPU(au->au_offset); 338*7c478bd9Sstevel@tonic-gate au->au_data_size = BE32_TO_CPU(au->au_data_size); 339*7c478bd9Sstevel@tonic-gate au->au_encoding = BE32_TO_CPU(au->au_encoding); 340*7c478bd9Sstevel@tonic-gate au->au_sample_rate = BE32_TO_CPU(au->au_sample_rate); 341*7c478bd9Sstevel@tonic-gate au->au_channels = BE32_TO_CPU(au->au_channels); 342*7c478bd9Sstevel@tonic-gate 343*7c478bd9Sstevel@tonic-gate if (au->au_magic != AUDIO_AU_FILE_MAGIC) { 344*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_AU_BAD_HEADER; 345*7c478bd9Sstevel@tonic-gate goto au_open_failed; 346*7c478bd9Sstevel@tonic-gate } 347*7c478bd9Sstevel@tonic-gate if ((au->au_encoding != AUDIO_AU_ENCODING_LINEAR_16) || 348*7c478bd9Sstevel@tonic-gate (au->au_sample_rate != 44100) || (au->au_channels != 2)) { 349*7c478bd9Sstevel@tonic-gate 350*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT; 351*7c478bd9Sstevel@tonic-gate goto au_open_failed; 352*7c478bd9Sstevel@tonic-gate } 353*7c478bd9Sstevel@tonic-gate if (au->au_data_size != AUDIO_AU_UNKNOWN_SIZE) { 354*7c478bd9Sstevel@tonic-gate if ((au->au_offset + au->au_data_size) != st.st_size) { 355*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_AU_BAD_HEADER; 356*7c478bd9Sstevel@tonic-gate goto au_open_failed; 357*7c478bd9Sstevel@tonic-gate } 358*7c478bd9Sstevel@tonic-gate data_size = au->au_data_size; 359*7c478bd9Sstevel@tonic-gate } else { 360*7c478bd9Sstevel@tonic-gate data_size = st.st_size - au->au_offset; 361*7c478bd9Sstevel@tonic-gate } 362*7c478bd9Sstevel@tonic-gate if (data_size == 0) { 363*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_AU_UNSUPPORTED_FORMAT; 364*7c478bd9Sstevel@tonic-gate goto au_open_failed; 365*7c478bd9Sstevel@tonic-gate } 366*7c478bd9Sstevel@tonic-gate if (lseek(fd, au->au_offset, SEEK_SET) < 0) { 367*7c478bd9Sstevel@tonic-gate goto au_open_failed; 368*7c478bd9Sstevel@tonic-gate } 369*7c478bd9Sstevel@tonic-gate 370*7c478bd9Sstevel@tonic-gate free(au); 371*7c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 372*7c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 373*7c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read_wrbo; 374*7c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 375*7c478bd9Sstevel@tonic-gate h->bstr_size = audio_stream_size; 376*7c478bd9Sstevel@tonic-gate h->bstr_rewind = au_stream_rewind; 377*7c478bd9Sstevel@tonic-gate h->bstr_private = (void *)data_size; 378*7c478bd9Sstevel@tonic-gate 379*7c478bd9Sstevel@tonic-gate return (h); 380*7c478bd9Sstevel@tonic-gate 381*7c478bd9Sstevel@tonic-gate au_open_failed: 382*7c478bd9Sstevel@tonic-gate sav = errno; 383*7c478bd9Sstevel@tonic-gate (void) close(fd); 384*7c478bd9Sstevel@tonic-gate if (au != NULL) 385*7c478bd9Sstevel@tonic-gate free(au); 386*7c478bd9Sstevel@tonic-gate errno = sav; 387*7c478bd9Sstevel@tonic-gate return (NULL); 388*7c478bd9Sstevel@tonic-gate } 389*7c478bd9Sstevel@tonic-gate 390*7c478bd9Sstevel@tonic-gate bstreamhandle 391*7c478bd9Sstevel@tonic-gate open_wav_read_stream(char *fname) 392*7c478bd9Sstevel@tonic-gate { 393*7c478bd9Sstevel@tonic-gate bstreamhandle h; 394*7c478bd9Sstevel@tonic-gate int fd, sav; 395*7c478bd9Sstevel@tonic-gate Wave_filehdr *wav; 396*7c478bd9Sstevel@tonic-gate struct stat st; 397*7c478bd9Sstevel@tonic-gate uint32_t data_size; 398*7c478bd9Sstevel@tonic-gate 399*7c478bd9Sstevel@tonic-gate wav = NULL; 400*7c478bd9Sstevel@tonic-gate str_errno = 0; 401*7c478bd9Sstevel@tonic-gate fd = open(fname, O_RDONLY); 402*7c478bd9Sstevel@tonic-gate if (fd < 0) 403*7c478bd9Sstevel@tonic-gate return (NULL); 404*7c478bd9Sstevel@tonic-gate 405*7c478bd9Sstevel@tonic-gate if (fstat(fd, &st) < 0) { 406*7c478bd9Sstevel@tonic-gate goto wav_open_failed; 407*7c478bd9Sstevel@tonic-gate } 408*7c478bd9Sstevel@tonic-gate if ((st.st_mode & S_IFMT) != S_IFREG) { 409*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_NO_REG_FILE; 410*7c478bd9Sstevel@tonic-gate goto wav_open_failed; 411*7c478bd9Sstevel@tonic-gate } 412*7c478bd9Sstevel@tonic-gate wav = (Wave_filehdr *)my_zalloc(sizeof (*wav)); 413*7c478bd9Sstevel@tonic-gate if (read(fd, wav, sizeof (*wav)) != sizeof (*wav)) { 414*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_WAV_READ_ERR; 415*7c478bd9Sstevel@tonic-gate goto wav_open_failed; 416*7c478bd9Sstevel@tonic-gate } 417*7c478bd9Sstevel@tonic-gate if ((strncmp(wav->riff, "RIFF", 4) != 0) || 418*7c478bd9Sstevel@tonic-gate (strncmp(wav->wave, "WAVE", 4) != 0)) { 419*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_WAV_BAD_HEADER; 420*7c478bd9Sstevel@tonic-gate goto wav_open_failed; 421*7c478bd9Sstevel@tonic-gate } 422*7c478bd9Sstevel@tonic-gate if (((CPU_TO_LE32(wav->total_chunk_size) + 8) != st.st_size) || 423*7c478bd9Sstevel@tonic-gate (strncmp(wav->fmt, "fmt ", 4) != 0) || 424*7c478bd9Sstevel@tonic-gate (CPU_TO_LE16(wav->fmt_tag) != 1) || 425*7c478bd9Sstevel@tonic-gate (CPU_TO_LE16(wav->n_channels) != 2) || 426*7c478bd9Sstevel@tonic-gate (CPU_TO_LE32(wav->sample_rate) != 44100) || 427*7c478bd9Sstevel@tonic-gate (CPU_TO_LE16(wav->bits_per_sample) != 16) || 428*7c478bd9Sstevel@tonic-gate (strncmp(wav->data, "data", 4) != 0) || 429*7c478bd9Sstevel@tonic-gate ((CPU_TO_LE32(wav->data_size) + 44) != st.st_size)) { 430*7c478bd9Sstevel@tonic-gate 431*7c478bd9Sstevel@tonic-gate str_errno = STR_ERR_WAV_UNSUPPORTED_FORMAT; 432*7c478bd9Sstevel@tonic-gate goto wav_open_failed; 433*7c478bd9Sstevel@tonic-gate } 434*7c478bd9Sstevel@tonic-gate data_size = CPU_TO_LE32(wav->data_size); 435*7c478bd9Sstevel@tonic-gate if (lseek(fd, sizeof (*wav), SEEK_SET) < 0) { 436*7c478bd9Sstevel@tonic-gate goto wav_open_failed; 437*7c478bd9Sstevel@tonic-gate } 438*7c478bd9Sstevel@tonic-gate 439*7c478bd9Sstevel@tonic-gate free(wav); 440*7c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 441*7c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 442*7c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read; 443*7c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 444*7c478bd9Sstevel@tonic-gate h->bstr_size = audio_stream_size; 445*7c478bd9Sstevel@tonic-gate h->bstr_rewind = wav_stream_rewind; 446*7c478bd9Sstevel@tonic-gate h->bstr_private = (void *)data_size; 447*7c478bd9Sstevel@tonic-gate 448*7c478bd9Sstevel@tonic-gate return (h); 449*7c478bd9Sstevel@tonic-gate 450*7c478bd9Sstevel@tonic-gate wav_open_failed: 451*7c478bd9Sstevel@tonic-gate sav = errno; 452*7c478bd9Sstevel@tonic-gate (void) close(fd); 453*7c478bd9Sstevel@tonic-gate if (wav != NULL) 454*7c478bd9Sstevel@tonic-gate free(wav); 455*7c478bd9Sstevel@tonic-gate errno = sav; 456*7c478bd9Sstevel@tonic-gate return (NULL); 457*7c478bd9Sstevel@tonic-gate } 458*7c478bd9Sstevel@tonic-gate 459*7c478bd9Sstevel@tonic-gate bstreamhandle 460*7c478bd9Sstevel@tonic-gate open_aur_read_stream(char *fname) 461*7c478bd9Sstevel@tonic-gate { 462*7c478bd9Sstevel@tonic-gate bstreamhandle h; 463*7c478bd9Sstevel@tonic-gate 464*7c478bd9Sstevel@tonic-gate h = open_file_read_stream(fname); 465*7c478bd9Sstevel@tonic-gate if (h != NULL) { 466*7c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read_wrbo; 467*7c478bd9Sstevel@tonic-gate } 468*7c478bd9Sstevel@tonic-gate return (h); 469*7c478bd9Sstevel@tonic-gate } 470*7c478bd9Sstevel@tonic-gate 471*7c478bd9Sstevel@tonic-gate bstreamhandle 472*7c478bd9Sstevel@tonic-gate open_au_write_stream(char *fname) 473*7c478bd9Sstevel@tonic-gate { 474*7c478bd9Sstevel@tonic-gate bstreamhandle h; 475*7c478bd9Sstevel@tonic-gate int esav, fd; 476*7c478bd9Sstevel@tonic-gate uchar_t head[] = PRE_DEF_AU_HDR; 477*7c478bd9Sstevel@tonic-gate 478*7c478bd9Sstevel@tonic-gate str_errno = 0; 479*7c478bd9Sstevel@tonic-gate fd = -1; 480*7c478bd9Sstevel@tonic-gate /* O_RDWR because we need to read while closing */ 481*7c478bd9Sstevel@tonic-gate fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); 482*7c478bd9Sstevel@tonic-gate if (fd < 0) 483*7c478bd9Sstevel@tonic-gate goto open_au_write_stream_failed; 484*7c478bd9Sstevel@tonic-gate if (write(fd, head, PRE_DEF_AU_HDR_LEN) != PRE_DEF_AU_HDR_LEN) { 485*7c478bd9Sstevel@tonic-gate goto open_au_write_stream_failed; 486*7c478bd9Sstevel@tonic-gate } 487*7c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 488*7c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 489*7c478bd9Sstevel@tonic-gate h->bstr_write = file_stream_write_wrbo; 490*7c478bd9Sstevel@tonic-gate h->bstr_close = au_write_stream_close; 491*7c478bd9Sstevel@tonic-gate return (h); 492*7c478bd9Sstevel@tonic-gate 493*7c478bd9Sstevel@tonic-gate open_au_write_stream_failed: 494*7c478bd9Sstevel@tonic-gate esav = errno; 495*7c478bd9Sstevel@tonic-gate if (fd != -1) 496*7c478bd9Sstevel@tonic-gate (void) close(fd); 497*7c478bd9Sstevel@tonic-gate errno = esav; 498*7c478bd9Sstevel@tonic-gate return (NULL); 499*7c478bd9Sstevel@tonic-gate } 500*7c478bd9Sstevel@tonic-gate 501*7c478bd9Sstevel@tonic-gate bstreamhandle 502*7c478bd9Sstevel@tonic-gate open_wav_write_stream(char *fname) 503*7c478bd9Sstevel@tonic-gate { 504*7c478bd9Sstevel@tonic-gate bstreamhandle h; 505*7c478bd9Sstevel@tonic-gate int esav, fd; 506*7c478bd9Sstevel@tonic-gate uchar_t head[] = PRE_DEF_WAV_HDR; 507*7c478bd9Sstevel@tonic-gate 508*7c478bd9Sstevel@tonic-gate str_errno = 0; 509*7c478bd9Sstevel@tonic-gate fd = -1; 510*7c478bd9Sstevel@tonic-gate fd = open(fname, O_RDWR|O_CREAT|O_TRUNC, 0666); 511*7c478bd9Sstevel@tonic-gate if (fd < 0) 512*7c478bd9Sstevel@tonic-gate goto open_wav_write_stream_failed; 513*7c478bd9Sstevel@tonic-gate if (write(fd, head, PRE_DEF_WAV_HDR_LEN) != PRE_DEF_WAV_HDR_LEN) { 514*7c478bd9Sstevel@tonic-gate goto open_wav_write_stream_failed; 515*7c478bd9Sstevel@tonic-gate } 516*7c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 517*7c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 518*7c478bd9Sstevel@tonic-gate h->bstr_write = file_stream_write; 519*7c478bd9Sstevel@tonic-gate h->bstr_close = wav_write_stream_close; 520*7c478bd9Sstevel@tonic-gate return (h); 521*7c478bd9Sstevel@tonic-gate 522*7c478bd9Sstevel@tonic-gate open_wav_write_stream_failed: 523*7c478bd9Sstevel@tonic-gate esav = errno; 524*7c478bd9Sstevel@tonic-gate if (fd != -1) 525*7c478bd9Sstevel@tonic-gate (void) close(fd); 526*7c478bd9Sstevel@tonic-gate errno = esav; 527*7c478bd9Sstevel@tonic-gate return (NULL); 528*7c478bd9Sstevel@tonic-gate } 529*7c478bd9Sstevel@tonic-gate 530*7c478bd9Sstevel@tonic-gate bstreamhandle 531*7c478bd9Sstevel@tonic-gate open_aur_write_stream(char *fname) 532*7c478bd9Sstevel@tonic-gate { 533*7c478bd9Sstevel@tonic-gate bstreamhandle h; 534*7c478bd9Sstevel@tonic-gate int fd; 535*7c478bd9Sstevel@tonic-gate 536*7c478bd9Sstevel@tonic-gate str_errno = 0; 537*7c478bd9Sstevel@tonic-gate fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); 538*7c478bd9Sstevel@tonic-gate if (fd < 0) 539*7c478bd9Sstevel@tonic-gate return (NULL); 540*7c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 541*7c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 542*7c478bd9Sstevel@tonic-gate h->bstr_write = file_stream_write_wrbo; 543*7c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 544*7c478bd9Sstevel@tonic-gate return (h); 545*7c478bd9Sstevel@tonic-gate } 546*7c478bd9Sstevel@tonic-gate 547*7c478bd9Sstevel@tonic-gate bstreamhandle 548*7c478bd9Sstevel@tonic-gate open_file_write_stream(char *fname) 549*7c478bd9Sstevel@tonic-gate { 550*7c478bd9Sstevel@tonic-gate bstreamhandle h; 551*7c478bd9Sstevel@tonic-gate int fd; 552*7c478bd9Sstevel@tonic-gate 553*7c478bd9Sstevel@tonic-gate str_errno = 0; 554*7c478bd9Sstevel@tonic-gate fd = open(fname, O_WRONLY|O_CREAT|O_TRUNC, 0666); 555*7c478bd9Sstevel@tonic-gate if (fd < 0) 556*7c478bd9Sstevel@tonic-gate return (NULL); 557*7c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 558*7c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 559*7c478bd9Sstevel@tonic-gate h->bstr_write = file_stream_write; 560*7c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 561*7c478bd9Sstevel@tonic-gate return (h); 562*7c478bd9Sstevel@tonic-gate } 563*7c478bd9Sstevel@tonic-gate 564*7c478bd9Sstevel@tonic-gate bstreamhandle 565*7c478bd9Sstevel@tonic-gate open_temp_file_stream(void) 566*7c478bd9Sstevel@tonic-gate { 567*7c478bd9Sstevel@tonic-gate bstreamhandle h; 568*7c478bd9Sstevel@tonic-gate char *t; 569*7c478bd9Sstevel@tonic-gate int fd; 570*7c478bd9Sstevel@tonic-gate 571*7c478bd9Sstevel@tonic-gate str_errno = 0; 572*7c478bd9Sstevel@tonic-gate 573*7c478bd9Sstevel@tonic-gate t = (char *)get_tmp_name(); 574*7c478bd9Sstevel@tonic-gate 575*7c478bd9Sstevel@tonic-gate if (strlcat(t, "/cdXXXXXX", PATH_MAX) >= PATH_MAX) 576*7c478bd9Sstevel@tonic-gate return (NULL); 577*7c478bd9Sstevel@tonic-gate 578*7c478bd9Sstevel@tonic-gate fd = mkstemp(t); 579*7c478bd9Sstevel@tonic-gate 580*7c478bd9Sstevel@tonic-gate if (debug) 581*7c478bd9Sstevel@tonic-gate (void) printf("temp is: %s length: %d\n", t, strlen(t)); 582*7c478bd9Sstevel@tonic-gate 583*7c478bd9Sstevel@tonic-gate if (fd < 0) 584*7c478bd9Sstevel@tonic-gate return (NULL); 585*7c478bd9Sstevel@tonic-gate (void) unlink(t); 586*7c478bd9Sstevel@tonic-gate 587*7c478bd9Sstevel@tonic-gate h = (bstreamhandle)my_zalloc(sizeof (*h)); 588*7c478bd9Sstevel@tonic-gate h->bstr_fd = fd; 589*7c478bd9Sstevel@tonic-gate h->bstr_read = file_stream_read; 590*7c478bd9Sstevel@tonic-gate h->bstr_write = file_stream_write; 591*7c478bd9Sstevel@tonic-gate h->bstr_close = file_stream_close; 592*7c478bd9Sstevel@tonic-gate h->bstr_size = file_stream_size; 593*7c478bd9Sstevel@tonic-gate h->bstr_rewind = file_stream_rewind; 594*7c478bd9Sstevel@tonic-gate 595*7c478bd9Sstevel@tonic-gate return (h); 596*7c478bd9Sstevel@tonic-gate } 597*7c478bd9Sstevel@tonic-gate 598*7c478bd9Sstevel@tonic-gate int 599*7c478bd9Sstevel@tonic-gate check_avail_temp_space(off_t req_size) 600*7c478bd9Sstevel@tonic-gate { 601*7c478bd9Sstevel@tonic-gate char *t; 602*7c478bd9Sstevel@tonic-gate struct statvfs buf; 603*7c478bd9Sstevel@tonic-gate uint64_t free_size; 604*7c478bd9Sstevel@tonic-gate 605*7c478bd9Sstevel@tonic-gate 606*7c478bd9Sstevel@tonic-gate t = get_tmp_name(); 607*7c478bd9Sstevel@tonic-gate 608*7c478bd9Sstevel@tonic-gate if (statvfs(t, &buf) < 0) 609*7c478bd9Sstevel@tonic-gate return (0); 610*7c478bd9Sstevel@tonic-gate 611*7c478bd9Sstevel@tonic-gate free_size = (uint64_t)buf.f_bfree; 612*7c478bd9Sstevel@tonic-gate free_size *= (uint64_t)buf.f_frsize; 613*7c478bd9Sstevel@tonic-gate 614*7c478bd9Sstevel@tonic-gate if (free_size <= ((uint64_t)req_size)) 615*7c478bd9Sstevel@tonic-gate return (0); 616*7c478bd9Sstevel@tonic-gate 617*7c478bd9Sstevel@tonic-gate /* path directory and there is enough free space */ 618*7c478bd9Sstevel@tonic-gate return (1); 619*7c478bd9Sstevel@tonic-gate } 620*7c478bd9Sstevel@tonic-gate 621*7c478bd9Sstevel@tonic-gate 622*7c478bd9Sstevel@tonic-gate char * 623*7c478bd9Sstevel@tonic-gate get_tmp_name(void) 624*7c478bd9Sstevel@tonic-gate { 625*7c478bd9Sstevel@tonic-gate char *t; 626*7c478bd9Sstevel@tonic-gate char *envptr; 627*7c478bd9Sstevel@tonic-gate 628*7c478bd9Sstevel@tonic-gate t = (char *)my_zalloc(PATH_MAX); 629*7c478bd9Sstevel@tonic-gate 630*7c478bd9Sstevel@tonic-gate /* 631*7c478bd9Sstevel@tonic-gate * generate temp directory path based on this order: 632*7c478bd9Sstevel@tonic-gate * user specified (-m option), temp env variable, 633*7c478bd9Sstevel@tonic-gate * and finally /tmp if nothing is found. 634*7c478bd9Sstevel@tonic-gate */ 635*7c478bd9Sstevel@tonic-gate 636*7c478bd9Sstevel@tonic-gate if (alt_tmp_dir) { 637*7c478bd9Sstevel@tonic-gate 638*7c478bd9Sstevel@tonic-gate /* copy and leave room for temp filename */ 639*7c478bd9Sstevel@tonic-gate 640*7c478bd9Sstevel@tonic-gate (void) strlcpy(t, alt_tmp_dir, PATH_MAX - 10); 641*7c478bd9Sstevel@tonic-gate } else { 642*7c478bd9Sstevel@tonic-gate envptr = getenv("TMPDIR"); 643*7c478bd9Sstevel@tonic-gate if (envptr != NULL) { 644*7c478bd9Sstevel@tonic-gate (void) strlcpy(t, envptr, PATH_MAX - 10); 645*7c478bd9Sstevel@tonic-gate } else { 646*7c478bd9Sstevel@tonic-gate (void) strlcpy(t, "/tmp", 5); 647*7c478bd9Sstevel@tonic-gate } 648*7c478bd9Sstevel@tonic-gate } 649*7c478bd9Sstevel@tonic-gate 650*7c478bd9Sstevel@tonic-gate /* 651*7c478bd9Sstevel@tonic-gate * no need to check if path is valid. statvfs will catch 652*7c478bd9Sstevel@tonic-gate * it later and fail with a proper error message. 653*7c478bd9Sstevel@tonic-gate */ 654*7c478bd9Sstevel@tonic-gate 655*7c478bd9Sstevel@tonic-gate return (t); 656*7c478bd9Sstevel@tonic-gate } 657