📄 stream.h
字号:
{ \
memset( netConnectInfo, 0, sizeof( NET_CONNECT_INFO ) ); \
( netConnectInfo )->networkSocket = CRYPT_ERROR; \
( netConnectInfo )->iCryptSession = CRYPT_ERROR; \
( netConnectInfo )->iUserObject = netUserObject; \
( netConnectInfo )->timeout = netTimeout; \
( netConnectInfo )->connectTimeout = netConnectTimeout; \
( netConnectInfo )->options = netOption; \
}
/* Information required when reading from/writing to an HTTP stream.
Although we're in theory just using HTTP as a universal substrate,
there's a pile of additional HTTP-related data that we have to convey,
so when we perform a read or write to an HTTP stream we use a composite
data parameter */
typedef struct {
/* Data payload informtion. On read the { buffer, bufSize } is the
amount of buffer space available to read data, with bytesAvail being
the length of the data item being read into the buffer and
bytesTransferred being the amount of data actually transferred. On
write the { buffer, bufSize } is the data to write and
bytesTransferred is the amount actually transferred. We have to
store this information here because the write call is passed the
HTTP_DATA_INFO structure rather than the data buffer so we can't
return a bytes-read or written count as the return value */
BUFFER_UNSPECIFIED( bufSize ) \
void *buffer; /* Data buffer */
int bufSize; /* Size of data buffer */
int bytesAvail, bytesTransferred; /* Actual data bytes on read */
BUFFER_FIXED( contentTypeLen ) \
const char *contentType; /* HTTP content type */
int contentTypeLen;
/* HTTP read/write control flags. If the bufferResize flag is set then
the HTTP read code can dynamically resize the buffer in order to read
arbitrary-length input. If the buffer was resized during the read
then the flag is returned set and the caller has to reset their read
buffer to { buffer, bufSize }. If no resize took place then the flag
is returned cleared.
If the softErrors flag is set then errors like a CRYPT_ERROR_NOTFOUND
(= HTTP 404) are treated as non-fatal because while (say) a
CRYPT_ERROR_BADDATA means that the stream has been corrupted and we
can't continue, the former merely indicates that the requested item
wasn't found but we can still submit further requests */
BOOLEAN bufferResize; /* Buffer is resizeable */
BOOLEAN softErrors; /* Treat soft errors as nonfatal */
/* The client's request type and request info (for HTTP GET), and the
server's status in response to a client GET request */
STREAM_HTTPREQTYPE_TYPE reqType;/* HTTP request type */
HTTP_URI_INFO *reqInfo;
int reqStatus; /* HTTP status in response to request */
} HTTP_DATA_INFO;
#define initHttpDataInfo( httpDataInfo, dataBuffer, dataLength ) \
{ \
memset( httpDataInfo, 0, sizeof( HTTP_DATA_INFO ) ); \
( httpDataInfo )->buffer= dataBuffer; \
( httpDataInfo )->bufSize = dataLength; \
}
#define initHttpDataInfoEx( httpDataInfo, dataBuffer, dataLength, uriInfo ) \
{ \
memset( httpDataInfo, 0, sizeof( HTTP_DATA_INFO ) ); \
memset( uriInfo, 0, sizeof( HTTP_URI_INFO ) ); \
( httpDataInfo )->buffer= dataBuffer; \
( httpDataInfo )->bufSize = dataLength; \
( httpDataInfo )->reqInfo = uriInfo; \
}
/****************************************************************************
* *
* Stream Function Prototypes *
* *
****************************************************************************/
/* Functions corresponding to traditional/stdio-type I/O */
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sputc( INOUT STREAM *stream, IN_BYTE const int ch );
RETVAL_RANGE( MAX_ERROR, 0xFF ) STDC_NONNULL_ARG( ( 1 ) ) \
int sgetc( INOUT STREAM *stream );
RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
int sread( INOUT STREAM *stream,
OUT_BUFFER_FIXED( length ) void *buffer,
IN_LENGTH const int length );
RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
int swrite( INOUT STREAM *stream,
IN_BUFFER( length ) const void *buffer,
IN_LENGTH const int length );
CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sflush( INOUT STREAM *stream );
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sseek( INOUT STREAM *stream, IN_LENGTH_Z const long position );
CHECK_RETVAL_RANGE( 0, MAX_INTLENGTH ) STDC_NONNULL_ARG( ( 1 ) ) \
int stell( const STREAM *stream );
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sioctl( INOUT STREAM *stream, \
IN_ENUM( STREAM_IOCTL ) const STREAM_IOCTL_TYPE type,
IN_OPT void *data, IN_INT const int dataLen );
/* Nonstandard functions: Skip a number of bytes in a stream, peek at the
next value in the stream */
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sSkip( INOUT STREAM *stream, IN_LENGTH const long offset );
CHECK_RETVAL_RANGE( MAX_ERROR, 0xFF ) STDC_NONNULL_ARG( ( 1 ) ) \
int sPeek( INOUT STREAM *stream );
/* Inquire as to the health of a stream. Currently these are only used in
debugging assertions, in int_api.c when exporting attributes to a stream,
and as a safety check in ssl_rw.c/ssh2_rw.c when wrapping a packet that
needs direct access to a memory stream */
#define sGetStatus( stream ) ( stream )->status
#define sStatusOK( stream ) cryptStatusOK( ( stream )->status )
/* Set/clear a user-defined error state for the stream */
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sSetError( INOUT STREAM *stream, IN_ERROR const int status );
STDC_NONNULL_ARG( ( 1 ) ) \
void sClearError( STREAM *stream );
/* Stream query functions to determine whether a stream is a null stream,
a memory-mapped file stream, or a virtual file stream. The null stream
check is used to short-circuit unnecessary data transfers in higher-level
code where writing to a null stream is used to determine overall data
sizes. The memory-mapped stream check is used when we can eliminate
extra buffer allocation if all data is available in memory. The virtual
file stream check is used where the low-level access routines have
converted a file on a CONFIG_NO_STDIO system to a memory stream that acts
like a file stream */
CHECK_RETVAL_BOOL STDC_NONNULL_ARG( ( 1 ) ) \
BOOLEAN sIsNullStream( const STREAM *stream );
/* Functions to work with memory streams. A null stream is a special case of a
memory stream that just acts as a data sink. In some cases we may want to
open either a null stream or a standard memory stream depending on whether
the caller has specified an output buffer or not, in this case we provide a
function sMemOpenEx() to indicate that it's OK for the buffer value to be
NULL */
RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
int sMemOpen( INOUT STREAM *stream,
IN_BUFFER( length ) void *buffer,
IN_LENGTH const int length );
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sMemOpenOpt( INOUT STREAM *stream,
IN_BUFFER_OPT( length ) void *buffer,
IN_LENGTH_Z const int length );
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sMemNullOpen( INOUT STREAM *stream );
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sMemClose( INOUT STREAM *stream );
RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
int sMemConnect( INOUT STREAM *stream,
IN_BUFFER( length ) const void *buffer,
IN_LENGTH const int length );
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sMemDisconnect( INOUT STREAM *stream );
/* Memory stream direct-access functions, used when the contents of a memory
stream need to be encrypted/decrypted/signed/MACd. The basic
sMemGetDataBlock() returns a data block of a given size from the current
stream position, sMemGetDataBlockAbs() returns a data block from the
given stream position, and sMemGetDataBlockRemaining() returns a data
block containing all remaining data available in the stream. The stream
parameter is given as an INOUT even though the stream contents aren't
strictly affected because the functions can set the stream error state
in the case of a failure */
CHECK_RETVAL_RANGE( 0, MAX_INTLENGTH ) STDC_NONNULL_ARG( ( 1 ) ) \
int sMemDataLeft( const STREAM *stream );
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
int sMemGetDataBlock( INOUT STREAM *stream,
OUT_BUFFER_ALLOC( dataSize ) void **dataPtrPtr,
IN_LENGTH const int dataSize );
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3 ) ) \
int sMemGetDataBlockAbs( INOUT STREAM *stream,
IN_LENGTH_Z const int position,
OUT_BUFFER_ALLOC( dataSize ) void **dataPtrPtr,
IN_LENGTH const int dataSize );
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2, 3 ) ) \
int sMemGetDataBlockRemaining( INOUT STREAM *stream,
OUT_BUFFER_ALLOC( *length ) void **dataPtrPtr,
OUT_LENGTH_Z int *length );
/* Functions to work with file streams */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
int sFileOpen( INOUT STREAM *stream, IN_STRING const char *fileName,
IN_FLAGS( FILE ) const int mode );
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sFileClose( INOUT STREAM *stream );
/* Convert a file stream to a memory stream */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2, 3 ) ) \
int sFileToMemStream( INOUT STREAM *memStream, INOUT STREAM *fileStream,
OUT_BUFFER_ALLOC( length ) void **bufPtrPtr,
IN_LENGTH const int length );
/* Functions to work with network streams */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 2 ) ) \
int sNetParseURL( INOUT URL_INFO *urlInfo,
IN_BUFFER( urlLen ) const char *url,
IN_LENGTH_SHORT const int urlLen,
IN_ENUM_OPT( URL_TYPE ) const URL_TYPE urlTypeHint );
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3, 4 ) ) \
int sNetConnect( INOUT STREAM *stream,
IN_ENUM( STREAM_PROTOCOL ) \
const STREAM_PROTOCOL_TYPE protocol,
const NET_CONNECT_INFO *connectInfo,
INOUT ERROR_INFO *errorInfo );
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3, 4 ) ) \
int sNetListen( INOUT STREAM *stream,
IN_ENUM( STREAM_PROTOCOL ) \
const STREAM_PROTOCOL_TYPE protocol,
const NET_CONNECT_INFO *connectInfo,
INOUT ERROR_INFO *errorInfo );
RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
int sNetDisconnect( INOUT STREAM *stream );
STDC_NONNULL_ARG( ( 1, 2 ) ) \
void sNetGetErrorInfo( INOUT STREAM *stream, INOUT ERROR_INFO *errorInfo );
/* Special-case file I/O calls */
CHECK_RETVAL STDC_NONNULL_ARG( ( 1 ) ) \
BOOLEAN fileReadonly( IN_STRING const char *fileName );
STDC_NONNULL_ARG( ( 1 ) ) \
void fileClearToEOF( const STREAM *stream );
STDC_NONNULL_ARG( ( 1 ) ) \
void fileErase( IN_STRING const char *fileName );
CHECK_RETVAL STDC_NONNULL_ARG( ( 1, 3, 4 ) ) \
int fileBuildCryptlibPath( OUT_BUFFER( pathMaxLen, pathLen ) char *path,
IN_LENGTH_SHORT const int pathMaxLen,
OUT_LENGTH_SHORT_Z int *pathLen,
IN_BUFFER( fileNameLen ) const char *fileName,
IN_LENGTH_SHORT const int fileNameLen,
IN_ENUM( BUILDPATH_OPTION ) \
const BUILDPATH_OPTION_TYPE option );
/* Initialisation/shutdown functions for network stream interfaces */
#ifdef USE_TCP
RETVAL \
int netInitTCP( void );
void netSignalShutdown( void );
void netEndTCP( void );
#else
#define netInitTCP() CRYPT_OK
#define netSignalShutdown()
#define netEndTCP()
#endif /* NET_TCP */
#endif /* _STREAM_DEFINED */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -