10 #if defined( INC_ALL )
24 int fileWrite(
STREAM *stream,
28 int fileFlush(
STREAM *stream );
46 switch( stream->
type )
78 if( netStream->timeout < 0 || netStream->timeout > 300 )
96 if( stream->buffer != NULL || stream->
bufSize != 0 )
115 if( stream->buffer == NULL )
120 if( netStream->writeBuffer != NULL || \
121 netStream->writeBufSize != 0 || \
122 netStream->writeBufEnd != 0 )
130 if( netStream->writeBuffer == NULL || \
131 netStream->writeBufSize <= 0 || \
134 if( netStream->writeBufEnd < 0 || \
135 netStream->writeBufEnd > netStream->writeBufSize )
145 if( stream->buffer == NULL )
149 stream->bufPos == 0 && stream->
bufEnd == 0 && \
150 stream->bufSize == 0 )
193 REQUIRES_S( stream->bufPos >= stream->bufEnd || \
213 status = fileSeek( stream, stream->bufCount * stream->bufSize );
215 return( sSetError( stream, status ) );
219 status = fileRead( stream, stream->buffer, stream->bufSize, &length );
221 return( sSetError( stream, status ) );
249 STREAM_FFLAG_POSCHANGED_NOSKIP );
273 status = fileSeek( stream, 0 );
275 return( sSetError( stream, status ) );
279 status = fileWrite( stream, stream->buffer, stream->bufPos );
281 return( sSetError( stream, status ) );
299 #ifdef VIRTUAL_FILE_STREAM
305 static
int expandVirtualFileStream(
INOUT STREAM *stream,
333 if( ( newBuffer =
clDynAlloc(
"expandVirtualFileStream", \
336 memcpy( newBuffer, stream->buffer, stream->bufEnd );
337 zeroise( stream->buffer, stream->bufEnd );
338 clFree(
"expandVirtualFileStream", stream->buffer );
339 stream->buffer = newBuffer;
340 stream->bufSize = newSize;
362 assert(
isReadPtr( stream->buffer, stream->bufSize ) );
374 return( stream->status );
376 switch( stream->type )
380 if( stream->bufPos >= stream->bufEnd )
382 ch =
byteToInt( stream->buffer[ stream->bufPos++ ] );
389 if( stream->bufPos >= stream->bufEnd || \
392 int status = refillStream( stream );
394 return( ( status ==
OK_SPECIAL ) ? 0 : status );
396 ch =
byteToInt( stream->buffer[ stream->bufPos++ ] );
417 isReadPtr( stream->buffer, stream->bufSize ) );
434 return( stream->status );
436 switch( stream->type )
442 #ifdef VIRTUAL_FILE_STREAM
450 localLength = stream->bufEnd - stream->bufPos;
451 if( localLength > length )
457 if( stream->bufPos + localLength > stream->bufEnd )
459 memset( buffer, 0,
min( 16, length ) );
464 memcpy( buffer, stream->buffer + stream->bufPos, localLength );
465 stream->bufPos += localLength;
483 for( dataLength = length, iterationCount = 0;
491 if( stream->bufPos >= stream->bufEnd || \
494 status = refillStream( stream );
497 bytesCopied : status );
501 bytesToCopy =
min( dataLength, \
502 stream->bufEnd - stream->bufPos );
505 memcpy( bufPtr, stream->buffer + stream->bufPos,
507 stream->bufPos += bytesToCopy;
508 bufPtr += bytesToCopy;
509 bytesCopied += bytesToCopy;
510 dataLength -= bytesToCopy;
513 ENSURES_S( iterationCount < FAILSAFE_ITERATIONS_LARGE );
537 status = netStream->readFunction( stream, buffer, length,
544 stream->status = netStream->persistentStatus;
569 if( bytesRead < length && \
588 "Read timed out with %d of %d bytes read",
594 "Read timed out with %d of %d bytes read",
595 bytesRead, length ) );
632 isWritePtr( stream->buffer, stream->bufSize ) );
648 return( stream->status );
650 switch( stream->type )
655 if( stream->bufEnd < stream->bufPos )
656 stream->bufEnd = stream->bufPos;
661 if( stream->bufPos >= stream->bufSize )
663 #ifdef VIRTUAL_FILE_STREAM
668 status = expandVirtualFileStream( stream, 1 );
676 stream->buffer[ stream->bufPos++ ] =
intToByte( ch );
677 if( stream->bufEnd < stream->bufPos )
678 stream->bufEnd = stream->bufPos;
679 #ifdef VIRTUAL_FILE_STREAM
693 if( stream->bufPos >= stream->bufSize )
697 status = emptyStream( stream,
FALSE );
701 stream->buffer[ stream->bufPos++ ] =
intToByte( ch );
724 isWritePtr( stream->buffer, stream->bufSize ) );
744 return( stream->status );
746 switch( stream->type )
751 if( stream->bufEnd < stream->bufPos )
752 stream->bufEnd = stream->bufPos;
758 if( stream->bufPos + length > stream->bufSize )
760 #ifdef VIRTUAL_FILE_STREAM
763 status = expandVirtualFileStream( stream, length );
773 memcpy( stream->buffer + stream->bufPos, buffer, length );
775 if( stream->bufEnd < stream->bufPos )
776 stream->bufEnd = stream->bufPos;
777 #ifdef VIRTUAL_FILE_STREAM
796 for( dataLength = length, iterationCount = 0;
800 const int bytesToCopy = \
801 min( dataLength, stream->bufSize - stream->bufPos );
803 if( bytesToCopy > 0 )
807 memcpy( stream->buffer + stream->bufPos, bufPtr,
809 stream->bufPos += bytesToCopy;
810 bufPtr += bytesToCopy;
811 dataLength -= bytesToCopy;
813 if( stream->bufPos >= stream->bufSize )
815 status = emptyStream( stream,
FALSE );
820 ENSURES_S( iterationCount < FAILSAFE_ITERATIONS_LARGE );
840 status = netStream->writeFunction( stream, buffer, length,
847 stream->status = netStream->persistentStatus;
851 if( bytesWritten < length && \
869 "Write timed out with %d of %d bytes written",
875 "Write timed out with %d of %d bytes written",
876 bytesWritten, length ) );
878 status = bytesWritten;
931 status = emptyStream( stream,
TRUE );
934 flushStatus = fileFlush( stream );
963 return( stream->status );
1016 return( stream->status );
1018 switch( stream->type )
1024 stream->bufPos = ( int ) position;
1025 if( stream->bufEnd < stream->bufPos )
1026 stream->bufEnd = stream->bufPos;
1031 if( (
int ) position > stream->bufSize )
1033 stream->bufPos = stream->bufSize;
1036 stream->bufPos = ( int ) position;
1037 if( stream->bufEnd < stream->bufPos )
1038 stream->bufEnd = stream->bufPos;
1059 newBufCount = position / stream->bufSize;
1060 if( newBufCount != stream->bufCount )
1069 if( newBufCount == stream->bufCount + 1 )
1072 stream->bufCount = newBufCount;
1074 stream->bufPos = position % stream->bufSize;
1110 DEBUG_DIAG((
"Stream is in invalid state" ));
1115 switch( stream->type )
1119 return( stream->bufPos );
1122 return( ( stream->bufCount * stream->bufSize ) + \
1148 return( stream->status );
1150 return( sseek( stream, stream->bufPos + offset ) );
1159 assert(
isReadPtr( stream->buffer, stream->bufSize ) );
1172 return( stream->status );
1176 switch( stream->type )
1180 if( stream->bufPos >= stream->bufEnd )
1182 return( stream->buffer[ stream->bufPos ] );
1188 if( stream->bufPos >= stream->bufEnd || \
1191 int status = refillStream( stream );
1193 return( ( status ==
OK_SPECIAL ) ? 0 : status );
1195 return( stream->buffer[ stream->bufPos ] );
1217 assert( ( data == NULL && dataLen == 0 ) || \
1220 REQUIRES_S( ( data == NULL && dataLen == 0 ) || \
1221 ( data != NULL && dataLen > 0 && dataLen <
MAX_INTLENGTH ) );
1223 dataLen == 512 || dataLen == 1024 || \
1224 dataLen == 2048 || dataLen == 4096 || \
1225 dataLen == 8192 || dataLen == 16384 );
1227 #ifdef VIRTUAL_FILE_STREAM
1236 stream->buffer = (
void * ) data;
1237 stream->bufSize = stream->bufEnd =
dataLen;
1245 stream->bufPos = stream->bufCount = 0;
1246 sClearError( stream );
1248 STREAM_FFLAG_EOF | \
1249 STREAM_FFLAG_POSCHANGED_NOSKIP );
1287 return( setStreamBuffer( stream, NULL, 0 ) );
1314 netStream->timeout =
value;
1331 REQUIRES_S( netStream->savedTimeout >= 0 && \
1337 netStream->timeout = netStream->savedTimeout;
1376 STREAM_NFLAG_HTTPPOST;
1413 netStream->transportDisconnectFunction( netStream,
FALSE );
1466 dataLen == 512 || dataLen == 1024 || \
1467 dataLen == 2048 || dataLen == 4096 || \
1468 dataLen == 8192 || dataLen == 16384 );
1470 return( setStreamBuffer( stream, data, dataLen ) );
1509 *( (
int * ) data ) = netStream->timeout;
1515 *( (
int * ) data ) = \
1522 if( netStream->clientAddressLen <= 0 )
1524 if( netStream->clientAddressLen > dataMaxLen )
1526 memcpy( data, netStream->clientAddress, netStream->clientAddressLen );
1533 if( netStream->clientAddressLen <= 0 )
1535 *( (
int * ) data ) = netStream->clientAddressLen;
1542 if( netStream->clientPort <= 0 )
1544 *( (
int * ) data ) = netStream->clientPort;
1575 assert(
isWritePtr( bufPtrPtr,
sizeof(
void * ) ) );
1589 REQUIRES( sanityCheck( fileStream ) && \
1595 memset( memStream, 0,
sizeof(
STREAM ) );
1598 #ifdef VIRTUAL_FILE_STREAM
1601 if( ( fileStream->flags & \
1607 if( length > fileStream->bufSize - fileStream->bufPos )
1613 status = sMemConnect( memStream, fileStream->buffer + \
1614 fileStream->bufPos, length );
1617 status = sSkip( fileStream, length );
1620 sMemDisconnect( memStream );
1629 if( ( bufPtr =
clAlloc(
"sFileToMemStream", length ) ) == NULL )
1631 status = sread( fileStream, bufPtr, length );
1633 status = sMemConnect( memStream, bufPtr, length );
1636 clFree(
"sFileToMemStream", bufPtr );
1639 *bufPtrPtr = bufPtr;