double STREAMSeek(STREAM *S, double offset, int whence) { double pos; int wherefrom; if (S->OutEnd > 0) STREAMFlush(S); if (whence==SEEK_CUR) { pos=STREAMTell(S); pos+=offset; wherefrom=SEEK_SET; } else { pos=offset; wherefrom=whence; } S->InStart=0; S->InEnd=0; #ifdef _LARGEFILE64_SOURCE pos=(double) lseek64(S->in_fd,(off64_t) pos, wherefrom); #else pos=(double) lseek(S->in_fd,(off_t) pos, wherefrom); #endif return(pos); }
void STREAMResetInputBuffers(STREAM *S) { double pos; pos=STREAMTell(S); S->InStart=0; S->InEnd=0; STREAMSeek(S,pos,SEEK_SET); }
int MultipartReadFile(STREAM *S,char *FName,char *Boundary, int BoundaryLen) { char *Tempstr=NULL, *ptr; int result, RetVal=FALSE; STREAM *FOut=NULL; off_t fsize; FOut=STREAMOpenFile(FName,SF_CREAT | SF_TRUNC | SF_WRONLY); if (FOut) { Tempstr=SetStrLen(Tempstr,4096); result=STREAMReadBytesToTerm(S, Tempstr, 4096, '\n'); while (result > -1) { if ( (result < (BoundaryLen + 6)) && (strncmp(Tempstr,Boundary,BoundaryLen)==0)) { //As we read to a '\n' we may have left its '\r' partner attached to //the end of the data ptr=Tempstr+result-2; if (strcmp(ptr,"\r\n")==0) result-=2; if ((result >= BoundaryLen) && (strncmp(Tempstr+BoundaryLen,"--\r\n",4)==0)) { //must remove '\r\n' from end of file (it's the start of the boundary) RetVal=UPLOAD_DONE; } break; } else if (FOut) STREAMWriteBytes(FOut,Tempstr,result); result=STREAMReadBytesToTerm(S, Tempstr, 4096, '\n'); } //If we read to a boundary then there will always be a \r\n on the end of the file, fsize=(off_t) STREAMTell(FOut); if (fsize > 0) ftruncate(FOut->out_fd,fsize-2); STREAMClose(FOut); } if (result==-1) RetVal=UPLOAD_DONE; DestroyString(Tempstr); return(RetVal); }
int STREAMCheckForBytes(STREAM *S) { off_t pos; struct stat Stat; if (! S) return(FALSE); if (S->State & SS_EMBARGOED) return(FALSE); if (S->InEnd > S->InStart) return(TRUE); if (S->in_fd==-1) return(FALSE); if (S->Flags & SF_FOLLOW) { while (1) { pos=STREAMTell(S); fstat(S->in_fd,&Stat); if (Stat.st_size > pos) return(TRUE); } } return(FDCheckForBytes(S->in_fd)); }