void seek_by_jump(const archive_handle_t *archive_handle, const unsigned int amount) { if (lseek(archive_handle->src_fd, (off_t) amount, SEEK_CUR) == (off_t) -1) { #if ENABLE_FEATURE_UNARCHIVE_TAPE if (errno == ESPIPE) { seek_by_read(archive_handle, amount); } else #endif bb_perror_msg_and_die("seek failure"); } }
void FAST_FUNC seek_by_jump(int fd, off_t amount) { if (amount && lseek(fd, amount, SEEK_CUR) == (off_t) -1 ) { if (errno == ESPIPE) seek_by_read(fd, amount); else bb_perror_msg_and_die("seek failure"); } }
void FAST_FUNC seek_by_jump(const archive_handle_t *archive_handle, unsigned amount) { if (amount && lseek(archive_handle->src_fd, (off_t) amount, SEEK_CUR) == (off_t) -1 ) { if (errno == ESPIPE) seek_by_read(archive_handle, amount); else bb_perror_msg_and_die("seek failure"); } }
static void seek_sub_file(FILE *fd, const int count) { archive_offset += count; /* Do not use fseek() on a pipe. It may fail with ESPIPE, leaving the * stream at an undefined location. */ seek_by_read(fd, count); return; }
int SFReadHdr(FILE *file, SFSTRUCT *snd, int infoBmax) /* utility to sample the header of an opened file, at most infobmax info byts */ { #define LBLEN 256 char lbuf[LBLEN]; int nhdread = 0; int hdtotlen = 0; int done = 0; int schan = 0; long samps = 0; int srate = 0; int snbyt = 0; int sbits = 0; int basefmt = _LINEAR; long ft; char *ib_ptr; int bytemode = BM_INORDER; int seekable = TRUE; int skipmagic = 0; ft = ftell(file); if( ft < 0 ) { /* don't try seek on stdin */ /* pipe - skip magic if already ID'd */ if (GetSFFType(file) != NULL) { #ifdef HAVE_FPUSH /* If we have fpush, we'll have pushed back to zero anyway */ skipmagic = 0; #else /* !HAVE_FPUSH */ /* we'll guess that the type came from SFIdentify, although it *could* have come from SFSetFileType, in which case we'll fail */ skipmagic = sizeof(INT32); #endif /* HAVE_FPUSH */ } seekable = FALSE; } else if (ft == sizeof(INT32)) { /* assume we already read the magic & fake it up */ skipmagic = ft; } else { if(fseek(file,(long)0,SEEK_SET) != 0 ) /* move to start of file */ return(SFerror = SFE_RDERR); } if(skipmagic) strncpy(lbuf, "NIST", skipmagic); /* fgets(lbuf+skipmagic, LBLEN-skipmagic, file); nhdread += strlen(lbuf); chop(lbuf); */ /* Use fread rather than fgets for this first read because the 4 bytes 'pushed back' by SFIdentify are only restored in my_fread - not in my_fgets */ nhdread = skipmagic; nhdread += fread(lbuf+skipmagic, 1, strlen("NIST_1A/") - skipmagic, file); /* if we're at EOF, it's not a sound file (lame attempt to readon strem) */ if (feof(file)) { return (SFerror = SFE_EOF); } lbuf[8] = '\0'; chop(lbuf); if(strcmp(lbuf, "NIST_1A")!=0) { fprintf(stderr, "Error: NIST SFRdHdr: id of '%s' is not 'NIST_1A'\n", lbuf); return SFE_NSF; /* Not a Sound File */ } /* read header values */ /* first line is number of bytes in header */ fgets(lbuf, LBLEN, file); nhdread += strlen(lbuf); chop(lbuf); hdtotlen = atoi(lbuf); /* rest are token/value pairs */ /* reset the info_buf used to store unknown fields */ /* 2002-10-08: allocate if not yet, or too small */ if (ib_len < hdtotlen) { if (info_buf) free(info_buf); info_buf = NULL; } if (info_buf == NULL) { ib_len = hdtotlen; info_buf = malloc(ib_len); } info_buf[0] = '\0'; ib_ptr = info_buf; while(!done && !feof(file)) { char tok[LBLEN], *t; int tklen; t = lbuf; fgets(lbuf, LBLEN, file); nhdread += strlen(lbuf); chop(lbuf); /* fprintf(stderr, "HDR: got '%s'\n", lbuf); */ tklen = getNextTok(&t, tok); if(strcmp(tok, "end_head")==0) { done = 1; } else if(strcmp(tok, "channel_count")==0) { tklen = getNextTok(&t, tok); /* format spec - -i */ tklen = getNextTok(&t, tok); /* actual count */ schan = atoi(tok); } else if(strcmp(tok, "sample_coding")==0) { tklen = getNextTok(&t, tok); /* format spec - -sN */ tklen = getNextTok(&t, tok); /* actual coding */ if(strcmp(tok, "pcm")==0 || strcmp(tok, "linear")==0) { basefmt |= _LINEAR; } else if(strncmp(tok, "pcm,",4)==0) { basefmt |= _LINEAR; /* check for shorten (any version 2.xx) */ if (strncmp(tok+4, "embedded-shorten-v2.", 20)==0) { /* this is embedded shorten - hope we are decoding it */ } else { fprintf(stderr, "Warn: NIST Hdr: Extra coding '%s' ignored\n", tok+4); } } else if(strcmp(tok, "alaw")==0) { basefmt |= _ALAW; } else if(strcmp(tok, "ulaw")==0) { basefmt |= _ULAW; } else if(strncmp(tok, "ulaw",4)==0) { basefmt |= _ULAW; fprintf(stderr, "Warn: NIST Hdr: Extra coding '%s' ignored\n", tok+4); } else if(strcmp(tok, "mu-law")==0) { basefmt |= _ULAW; } else if(strcmp(tok, "float")==0) { basefmt |= _FLOAT; } else if(strcmp(tok, "raw")==0) { /* 2005-03-15: SRI's hacked format holds floats as 'raw' */ basefmt |= _FLOAT; /* .. and has an implied byte order */ bytemode = BM_BYWDREV; } else { fprintf(stderr, "Warn: NIST Hdr: '%s' not pcm, alaw, ulaw or mu-law (assume pcm)\n", tok); } } else if(strcmp(tok, "sample_count")==0) { tklen = getNextTok(&t, tok); /* format spec - -i */ tklen = getNextTok(&t, tok); /* actual count */ samps = atol(tok); } else if(strcmp(tok, "sample_rate")==0) { tklen = getNextTok(&t, tok); /* format spec - -i */ tklen = getNextTok(&t, tok); /* actual count */ srate = atol(tok); } else if(strcmp(tok, "sample_n_bytes")==0) { tklen = getNextTok(&t, tok); /* format spec - -i */ tklen = getNextTok(&t, tok); /* actual count */ snbyt = atoi(tok); } else if(strcmp(tok, "sample_byte_format")==0) { tklen = getNextTok(&t, tok); tklen = getNextTok(&t, tok); if(strcmp(tok, "01")==0 || strcmp(tok, "0123")==0) { bytemode = BM_BYTEREV; /* fprintf(stderr,"this data is byteswapped, but I can't handle it\n"); */ } else if(strcmp(tok, "10")==0 || strcmp(tok, "3210")==0) { bytemode = BM_INORDER; } else if(strcmp(tok, "1")==0) { /* mu-law reported like this? */ bytemode = BM_INORDER; } else if(strcmp(tok, "mu-law")==0) { bytemode = BM_INORDER; basefmt |= _ULAW; /* fprintf(stderr, "NIST SFRdHdr: funky mu-law sample_byte_format\n"); */ } else { fprintf(stderr, "Warn: NIST Hdr: sample_byte_format '%s' is not mu-law, 10 or 01 (assume 10)\n", tok); } } else if(strcmp(tok, "sample_sig_bits")==0) { tklen = getNextTok(&t, tok); /* format spec - -i */ tklen = getNextTok(&t, tok); /* actual count */ sbits = atoi(tok); } else if(strcmp(tok, "sample_checksum")==0) { long cksum; tklen = getNextTok(&t, tok); /* format spec - -i */ tklen = getNextTok(&t, tok); /* actual count */ cksum = atoi(tok); /* just consume & ignore */ } else { /* not an anticipated field - write into the info_buf */ if(strlen(lbuf)) { sprintf(ib_ptr, "%s\n", lbuf); ib_ptr += strlen(ib_ptr); /* hope info_buf never fills up! */ } } } if(!done) { /* hit eof before completing */ fprintf(stderr, "Error: NIST Hdr: looked good, but no end_head\n"); return SFE_NSF; } if(sbits != 0 && sbits != 8 * snbyt) { fprintf(stderr, "Warn: NIST Hdr: %d bits a surprise for %d bytes/samp (using %d bytes/samp)\n", sbits, snbyt, snbyt); } /* handle whatever happened to bytemode */ if(snbyt>1 && (bytemode & BM_WORDMASK) != (HostByteMode() & BM_WORDMASK)) { if (snbyt > 2) SetBytemode(file, BM_BYWDREV); else SetBytemode(file, BM_BYTEREV); } else { /* set the flag that no byteswapping is needed */ SetBytemode(file, BM_INORDER); } if(nhdread > hdtotlen) { fprintf(stderr, "Error: NIST Hdr: overran header len of %d bytes - expect stack corruption\n", hdtotlen); return SFE_NSF; } if(seekable) { fseek(file, hdtotlen, SEEK_SET); } else { seek_by_read(file, hdtotlen-nhdread); } /* now at start of data */ /* set up fields */ snd->magic = SFMAGIC; snd->headBsize = sizeof(SFSTRUCT); assert(snbyt==8 || snbyt==4 ||snbyt==2 || snbyt==1); /* ulw, lin8 or lin16, float or double */ /* snd->format = basefmt | ((snbyt==1)?SFMT_CHAR:SFMT_SHORT); */ snd->format = basefmt | snbyt; /* luckily, _EIGHTBYTE==8 */ snd->channels = schan; snd->samplingRate = (float)srate; snd->dataBsize = snbyt*samps*schan; /* samps is frames now 1999sep28 */ { /* handle whatever we put into info_buf */ int ibSize = ib_ptr - info_buf; int lastwd = ibSize % 4; if(infoBmax <= 0) infoBmax = SFDFLTBYTS; snd->info[0] = '\0'; if(lastwd) { /* pad last word to 4-byte boundary with zeros */ int xtra = 4-lastwd; long l = 0; memcpy(info_buf+ibSize, &l, xtra); ibSize += xtra; } memcpy(snd->info, info_buf, MIN(infoBmax, ibSize)); snd->headBsize += MAX(0, ibSize-SFDFLTBYTS); } /* record the indicated seek points for start and end of sound data */ SetSeekEnds(file, hdtotlen, hdtotlen+snd->dataBsize); return SFE_OK; }