⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stream.h

📁 提供了很多种加密算法和CA认证及相关服务如CMP、OCSP等的开发
💻 H
字号:
/****************************************************************************
*																			*
*						STREAM Class Constants and Structures				*
*						  Copyright Peter Gutmann 1993-2002					*
*																			*
****************************************************************************/

#ifndef _STREAM_DEFINED

#define _STREAM_DEFINED

#if defined( INC_ALL )
  #include "crypt.h"
#elif defined( INC_CHILD )
  #include "../crypt.h"
#else
  #include "crypt.h"
#endif /* Compiler-specific includes */
#ifdef __MAC__		/* Must be after the crypt.h include */
  #include <Files.h>
#else
  #include <stdio.h>
#endif /* __MAC__ */

/****************************************************************************
*																			*
*								STREAM Constants							*
*																			*
****************************************************************************/

/* The stream types */

typedef enum {
	STREAM_TYPE_NONE,				/* No stream type */
	STREAM_TYPE_NULL,				/* Null stream (/dev/nul) */
	STREAM_TYPE_MEMORY,				/* Memory stream */
	STREAM_TYPE_FILE,				/* File stream */
	STREAM_TYPE_NETWORK,			/* Network stream */
	STREAM_TYPE_LAST				/* Last possible stream type */
	} STREAM_TYPE;

/* Flags for the stream */

#define STREAM_FLAG_DIRTY	0x01	/* Stream contains unwritten data */
#define STREAM_FLAG_READONLY 0x02	/* Stream is read-only */

/* Occasionally we want to connect a memory stream to a fixed-length buffer
   whose size is "big enough for the data it needs to hold", but of an
   unknown length.  Using the following as the length will avoid various
   checks on the input length */

#define STREAMSIZE_UNKNOWN	-1

/* When we're reading data of unknown size from an external source, we may
   need to increase the I/O buffer size during the read.  The following
   callback function is used to increase the I/O buffer size */

typedef int ( *CALLBACKFUNCTION )( void *callbackParams, void **bufPtr, \
								   const int bufSize );

/* Access/option flags for the file stream open call.  The exclusive access
   flag locks the file so that other threads/processes can't open it until 
   the current thread/process closes it.  This flag is implicitly set if the
   file R/W bits are FILE_WRITE, which creates a new file.  The difference 
   between the private and sensitive flags is that some data may be private 
   for a given user but not sensitive (eg config info) while other data may 
   be private and sensitive (eg private keys).  The sensitive flag only has 
   an effect on special systems where data can be committed to secure 
   storage, since there's usually a very limited amount of this available we 
   only use it for sensitive data but not generic private data */

#define FILE_READ			0x01	/* Open file for read access */
#define FILE_WRITE			0x02	/* Open file for write access */
#define FILE_EXCLUSIVE_ACCESS 0x04	/* Don't allow others access */
#define FILE_PRIVATE		0x08	/* Set ACL's to allow owner access only */
#define FILE_SENSITIVE		0x10	/* Use secure storage if available */
#define FILE_RW_MASK		0x03	/* Mask for R/W bits */

/* Stream IOCTL types */

typedef enum {
	STREAM_IOCTL_NONE,				/* No IOCTL type */
	STREAM_IOCTL_TIMEOUT,			/* Read/write timeout */
	STREAM_IOCTL_GETTIMEOUT,		/* Get previously set timeout */
	STREAM_IOCTL_GETCLIENTNAME,		/* Get client name */
	STREAM_IOCTL_GETCLIENTPORT,		/* Get client port */
	STREAM_IOCTL_CONTENTTYPE,		/* HTTP content type */
	STREAM_IOCTL_QUERY,				/* HTTP query portion of URL */
	STREAM_IOCTL_LASTMESSAGE,		/* CMP last message in transaction */
	STREAM_IOCTL_CALLBACKFUNCTION,	/* Callback function address */
	STREAM_IOCTL_CALLBACKPARAMS,	/* Callback function parameters */
	STREAM_IOCTL_LAST				/* Last possible IOCTL type */
	} STREAM_IOCTL_TYPE;

/* Stream network protocol types */

typedef enum {
	STREAM_PROTOCOL_NONE,			/* No protocol type */
	STREAM_PROTOCOL_TCPIP,			/* TCP/IP */
	STREAM_PROTOCOL_HTTP,			/* HTTP fetch */
	STREAM_PROTOCOL_HTTP_TRANSACTION,/* HTTP POST+fetch */
	STREAM_PROTOCOL_CMP,			/* TCP/IP with CMP packets */
	STREAM_PROTOCOL_LAST			/* Last possible protocol type */
	} STREAM_PROTOCOL_TYPE;

/* Stream protocol state for stateless protocols which only allow a single
   fetch request followed by a fetch */

typedef enum {
	STREAM_PROTOCOLSTATE_NONE,		/* No protocol state */
	STREAM_PROTOCOLSTATE_FETCHSENT,	/* Fetch request sent to server */
	STREAM_PROTOCOLSTATE_DATAFETCHED,/* Reply fetched from server */
	STREAM_PROTOCOLSTATE_LAST		/* Last possible protocol type */
	} STREAM_PROTOCOLSTATE_TYPE;

/****************************************************************************
*																			*
*							STREAM Class Structures							*
*																			*
****************************************************************************/

/* The STREAM data type */

typedef struct {
	/* General information for the stream */
	STREAM_TYPE type;			/* The stream type */
	int flags;					/* Stream flags */
	int status;					/* Current stream status (clib error code) */
	int lastChar;				/* Last char read, for ungetc() function */
	int ungetChar;				/* Whether we need to return lastChar next */

	/* Information for memory I/O */
	BYTE *buffer;				/* Buffer to R/W to */
	int bufSize;				/* Total size of buffer */
	int bufPos;					/* Current position in buffer */
	int bufEnd;					/* Last buffer position with valid data */

	/* Information for file I/O */
#if defined( __WIN32__ )
	HANDLE hFile;				/* The file associated with this stream */
#elif defined( __IBM4758__ ) || defined( __VMCMS__ )
	char name[ FILENAME_MAX ];	/* Data item associated with stream */
	BOOLEAN isIOStream;			/* Whether stream is an emulated I/O stream */
	BOOLEAN isSensitive;		/* Whether stream contains sensitive data */
#elif defined( __MAC__ )
	short refNum;				/* File stream reference number */
	FSSpec fsspec;				/* File system specification */
#else
	FILE *filePtr;				/* The file associated with this stream */
#endif /* __WIN32__ */

	/* Information for network I/O.  The server FQDN is held in dynamically-
	   allocated storage, the optional path for HTTP is a pointer into the 
	   host string at the appropriate location.  For a server, the 
	   listenSocket is the (possibly shared) common socket which the server 
	   is listening on, the netSocket is the ephemeral socket used for 
	   communications */
	STREAM_PROTOCOL_TYPE protocol;/* Network protocol type */
	char *host;
	int port;					/* Host name and port */
	char *path;					/* Path on host */
	int netSocket, listenSocket;/* Network socket */
	int timeout;				/* Network comms timeout */
	BOOLEAN isServer;			/* Whether stream is client or server */
	char clientAddress[ 32 ];	/* Client IP address (dotted-decimal) */
	int clientPort;				/* Client port */

	/* Protocol-specific infomation for network I/O */
	char contentType[ CRYPT_MAX_TEXTSIZE + 1 ];	/* HTTP content type */
	char *query;				/* HTTP query URI portion */
	int queryLen;				/* Size of query buffer */
	STREAM_PROTOCOLSTATE_TYPE protocolState;	/* HTTP put/get state */
	BOOLEAN lastMessage;		/* CMP last message flag */
	CALLBACKFUNCTION callbackFunction;
	void *callbackParams;		/* Callback to change I/O buffer size */

	/* Last-error information returned from lower-level code.  Unlike its
	   use in high-level objects like keysets and devices, we dynamically
	   allocate the error message storage since it's only used for network
	   streams and would lead to a lot of wasted memory in memory streams 
	   which are used constantly throughout cryptlib */
	int errorCode;
	char *errorMessage;
	} STREAM;

/****************************************************************************
*																			*
*							STREAM Class Function Prototypes				*
*																			*
****************************************************************************/

/* Functions corresponding to traditional/stdio-type I/O */

int sputc( STREAM *stream, int data );
int sgetc( STREAM *stream );
int sungetc( STREAM *stream );
int sread( STREAM *stream, void *buffer, int length );
int swrite( STREAM *stream, const void *buffer, const int length );
int sflush( STREAM *stream );
int sseek( STREAM *stream, const long position );
long stell( const STREAM *stream );
int seof( const STREAM *stream );
int sioctl( STREAM *stream, const STREAM_IOCTL_TYPE type, void *data, 
			const int dataLen );

/* Skip a number of bytes in a stream */

int sSkip( STREAM *stream, const long length );

/* Inquire as to the health of a stream */

#define sGetStatus( stream )		( stream )->status
#define sStatusOK( stream )			cryptStatusOK( ( stream )->status )

/* Set/clear user-defined error state for the stream.  The reason for the
   slightly convoluted code in sSetError() is because a conventional if
   statement would cause problems with dangling elses */

#define sSetError( stream, error )	( stream )->status = \
										( ( stream )->status == CRYPT_OK ) ? \
										( error ) : ( stream )->status
#define sClearError( stream )		( stream )->status = CRYPT_OK

/* Stream query functions to determine whether a stream is a null stream */

#define sIsNullStream( stream )		( ( stream )->type == STREAM_TYPE_NULL )

/* Determine the total size of a memory stream, the amount of data left to be
   read, and return a pointer to the current position in a streams internal 
   memory buffer.  The latter is used by some routines which need to process 
   data in a stream buffer after it's been written to the wire format */

#define sMemBufSize( stream )	( ( stream )->bufSize )
#define sMemDataLeft( stream )	( ( stream )->bufSize - \
									( ( stream )->bufPos + ( stream )->ungetChar ) )
#define sMemBufPtr( stream )	( ( stream )->type == STREAM_TYPE_NULL ? NULL : \
								  ( stream )->buffer + ( stream )->bufPos - \
									( stream )->ungetChar )

/* Functions to work with memory streams */

int sMemOpen( STREAM *stream, void *buffer, const int length );
int sMemClose( STREAM *stream );
int sMemConnect( STREAM *stream, const void *buffer, const int length );
int sMemDisconnect( STREAM *stream );

/* Functions to work with file streams */

int sFileOpen( STREAM *stream, const char *fileName, const int mode );
int sFileClose( STREAM *stream );

/* Functions to work with network streams */

int sNetConnect( STREAM *stream, const STREAM_PROTOCOL_TYPE protocol,
				 const char *location, const int port, const int timeout,
				 char *errorMessage, int *errorCode );
int sNetListenSocket( STREAM *stream, const STREAM_PROTOCOL_TYPE protocol,
					  const char *interface, const int port, 
					  char *errorMessage, int *errorCode );
int sNetDisconnect( STREAM *stream );
void sNetGetErrorInfo( STREAM *stream, char *errorString, int *errorCode );

/* Special-case file I/O calls */

BOOLEAN fileReadonly( const char *fileName );
void fileClearToEOF( const STREAM *stream );
void fileUnlink( const char *fileName );
void fileBuildCryptlibPath( char *path, const char *fileName, 
							const BOOLEAN createPath );

#endif /* _STREAM_DEFINED */

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -