73 stream->netStreamInfo == NULL )
75 if( netStream->timeout < 0 || netStream->timeout > 300 )
79 if( stream->buffer != NULL || stream->
bufPos != 0 || \
82 if( netStream->writeBuffer != NULL || netStream->writeBufSize != 0 || \
83 netStream->writeBufEnd != 0 )
90 static
BOOLEAN sanityCheckBufferedFunction(
const STREAM *stream )
102 stream->netStreamInfo == NULL )
104 if( netStream->timeout < 0 || netStream->timeout > 300 )
125 if( netStream->writeBuffer == NULL || \
126 netStream->writeBufSize <= 0 || \
129 if( netStream->writeBufEnd < 0 || \
130 netStream->writeBufEnd > netStream->writeBufSize )
147 static
int transportDirectReadFunction(
INOUT STREAM *stream,
157 assert(
isWritePtr( length,
sizeof(
int ) ) );
161 REQUIRES_S( netStream->sanityCheckFunction( stream ) );
164 return( netStream->transportReadFunction( stream, buffer, maxLength,
170 static
int transportDirectWriteFunction(
INOUT STREAM *stream,
171 IN_BUFFER( maxLength )
const void *buffer,
178 assert(
isReadPtr( buffer, maxLength ) );
179 assert(
isWritePtr( length,
sizeof(
int ) ) );
183 REQUIRES_S( netStream->sanityCheckFunction( stream ) );
186 return( netStream->transportWriteFunction( stream, buffer, maxLength,
196 netStream->writeFunction = transportDirectWriteFunction;
197 netStream->readFunction = transportDirectReadFunction;
217 static
int transportSessionConnectFunction(
INOUT STREAM *stream,
229 REQUIRES_S( netStream->sanityCheckFunction( stream ) );
246 return( getSessionErrorInfo( stream,
status ) );
251 static
void transportSessionDisconnectFunction(
INOUT STREAM *stream,
260 REQUIRES_V( netStream->sanityCheckFunction( stream ) );
266 static
BOOLEAN transportSessionOKFunction(
void )
272 static
int transportSessionReadFunction(
INOUT STREAM *stream,
285 assert(
isWritePtr( length,
sizeof(
int ) ) );
289 REQUIRES_S( stream->sanityCheckFunction( stream ) );
318 return( getSessionErrorInfo( stream,
status ) );
323 "Only read %d out of %d bytes via cryptlib session "
332 static
int transportSessionWriteFunction(
INOUT STREAM *stream,
344 assert(
isReadPtr( buffer, maxLength ) );
348 REQUIRES_S( netStream->sanityCheckFunction( stream ) );
366 return( getSessionErrorInfo( stream,
status ) );
382 netStream->transportConnectFunction = transportSessionConnectFunction;
383 netStream->transportDisconnectFunction = transportSessionDisconnectFunction;
384 netStream->transportWriteFunction = transportSessionWriteFunction;
385 netStream->transportReadFunction = transportSessionReadFunction;
386 netStream->transportOKFunction = transportSessionOKFunction;
422 static
int bufferedTransportReadFunction(
INOUT STREAM *stream,
431 const int bytesLeft = stream->bufEnd - stream->bufPos;
432 int bufferBytesRead, bytesRead,
status;
436 assert(
isWritePtr( length,
sizeof(
int ) ) );
440 REQUIRES_S( netStream->sanityCheckFunction( stream ) );
451 if( maxLength <= bytesLeft )
456 *buffer = stream->buffer[ stream->bufPos++ ];
460 memcpy( buffer, stream->buffer + stream->bufPos, maxLength );
465 ENSURES_S( netStream->sanityCheckFunction( stream ) );
472 if( stream->bufPos > 0 )
478 memmove( stream->buffer, stream->buffer + stream->bufPos,
481 stream->bufEnd = bytesLeft;
489 if( stream->bufEnd < stream->bufSize )
498 bytesToRead = stream->bufSize - stream->bufEnd;
499 if( bytesToRead > maxLength )
506 status = netStream->transportReadFunction( stream,
507 stream->buffer + stream->bufEnd,
508 bytesToRead, &bytesRead,
512 stream->bufEnd += bytesRead;
517 if( stream->bufEnd < stream->bufSize )
519 status = netStream->transportReadFunction( stream,
520 stream->buffer + stream->bufEnd,
521 stream->bufSize - stream->bufEnd,
522 &bytesRead, TRANSPORT_FLAG_NONBLOCKING );
524 stream->bufEnd += bytesRead;
527 ENSURES_S( netStream->sanityCheckFunction( stream ) );
530 bufferBytesRead =
min( maxLength, stream->bufEnd );
531 memcpy( buffer, stream->buffer, bufferBytesRead );
532 stream->bufPos = bufferBytesRead;
533 *length = bufferBytesRead;
536 if( maxLength <= bufferBytesRead )
538 ENSURES_S( netStream->sanityCheckFunction( stream ) );
550 status = netStream->transportReadFunction( stream,
551 buffer + bufferBytesRead, maxLength - bufferBytesRead,
555 *length += bytesRead;
557 ENSURES_S( netStream->sanityCheckFunction( stream ) );
618 const int bytesLeftToWrite = netStream->writeBufEnd - bytesWritten;
622 REQUIRES( bytesWritten > 0 && bytesWritten < netStream->writeBufEnd && \
647 if( bytesLeftToWrite < newDataToWrite )
648 *newDataWritten = newDataToWrite - bytesLeftToWrite;
655 netStream->writeBufEnd ) );
656 memmove( netStream->writeBuffer, netStream->writeBuffer + bytesWritten,
658 netStream->writeBufEnd = bytesLeftToWrite;
664 static
int bufferedTransportWriteFunction(
INOUT STREAM *stream,
672 const BYTE *bufPtr = buffer;
673 int byteCount = maxLength, bytesWritten,
status;
676 assert(
isReadPtr( buffer, maxLength ) );
680 REQUIRES_S( netStream->sanityCheckFunction( stream ) );
691 netStream->writeBufEnd + byteCount <= netStream->writeBufSize )
693 memcpy( netStream->writeBuffer + netStream->writeBufEnd, buffer,
695 netStream->writeBufEnd += byteCount;
698 ENSURES_S( netStream->sanityCheckFunction( stream ) );
705 if( netStream->writeBufEnd > 0 )
711 bytesToCopy = netStream->writeBufSize - netStream->writeBufEnd;
712 if( bytesToCopy > byteCount )
713 bytesToCopy = byteCount;
714 if( bytesToCopy > 0 )
716 memcpy( netStream->writeBuffer + netStream->writeBufEnd, buffer,
718 netStream->writeBufEnd += bytesToCopy;
720 status = netStream->transportWriteFunction( stream,
721 netStream->writeBuffer, netStream->writeBufEnd,
725 if( bytesWritten < netStream->writeBufEnd )
727 status = processIncompleteWrite( netStream, bytesWritten,
728 bytesToCopy, length );
732 ENSURES_S( netStream->sanityCheckFunction( stream ) );
736 netStream->writeBufEnd = 0;
737 if( bytesToCopy > 0 )
739 bufPtr += bytesToCopy;
740 byteCount -= bytesToCopy;
746 ENSURES_S( netStream->sanityCheckFunction( stream ) );
752 ENSURES( netStream->writeBufEnd == 0 );
755 status = netStream->transportWriteFunction( stream, bufPtr, byteCount,
760 if( bytesWritten < byteCount )
767 byteCount -= bytesWritten;
768 *length = maxLength - byteCount;
776 ENSURES_S( netStream->sanityCheckFunction( stream ) );
787 if( useTransportBuffering )
789 netStream->sanityCheckFunction = sanityCheckBufferedFunction;
790 netStream->bufferedTransportReadFunction = bufferedTransportReadFunction;
791 netStream->bufferedTransportWriteFunction = bufferedTransportWriteFunction;
795 netStream->sanityCheckFunction = sanityCheckFunction;
796 netStream->bufferedTransportReadFunction = netStream->transportReadFunction;
797 netStream->bufferedTransportWriteFunction = netStream->transportWriteFunction;