📄 stream.h
字号:
/****************************************************************************
* *
* STREAM Class Constants and Structures *
* Copyright Peter Gutmann 1993-2007 *
* *
****************************************************************************/
#ifndef _STREAM_DEFINED
#define _STREAM_DEFINED
#if defined( INC_ALL )
#include "crypt.h"
#else
#include "crypt.h"
#endif /* Compiler-specific includes */
#if defined( __WIN32__ ) || defined( __WINCE__ )
/* Includes are always handled via the normal system includes */
#elif defined( __UNIX__ ) || defined( __BEOS__ ) || defined( __XMK__ )
#include <unistd.h> /* For lseek() codes */
#elif defined( __MAC__ )
#include <Files.h>
#elif defined( __PALMOS__ )
#include <VFSMgr.h>
#elif defined( __UCOSII__ )
#include <fs_api.h>
#elif !defined( CONFIG_NO_STDIO )
#include <stdio.h>
#endif /* System-specific file I/O headers */
/****************************************************************************
* *
* Stream Constants *
* *
****************************************************************************/
/* 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 (e.g.config info) while other data may
be private and sensitive (e.g.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_FLAG_NONE 0x00 /* No file flag */
#define FILE_FLAG_READ 0x01 /* Open file for read access */
#define FILE_FLAG_WRITE 0x02 /* Open file for write access */
#define FILE_FLAG_EXCLUSIVE_ACCESS 0x04 /* Don't allow others access */
#define FILE_FLAG_PRIVATE 0x08 /* Set ACL's to allow owner access only */
#define FILE_FLAG_SENSITIVE 0x10 /* Use secure storage if available */
#define FILE_FLAG_RW_MASK 0x03 /* Mask for R/W bits */
#define FILE_FLAG_MAX 0x1F /* Maximum possible flag value */
/* Options for the build-path call */
typedef enum {
BUILDPATH_NONE, /* No option type */
BUILDPATH_CREATEPATH, /* Get path to config file, create if nec.*/
BUILDPATH_GETPATH, /* Get path to config file */
BUILDPATH_RNDSEEDFILE, /* Get path to random seed file */
BUILDPATH_LAST /* Last valid option type */
} BUILDPATH_OPTION_TYPE;
/* Stream IOCTL types */
typedef enum {
STREAM_IOCTL_NONE, /* No IOCTL type */
STREAM_IOCTL_IOBUFFER, /* Working buffer for file streams */
STREAM_IOCTL_PARTIALREAD, /* Allow read of less than req.amount */
STREAM_IOCTL_PARTIALWRITE, /* Allow write of less then req.amount */
STREAM_IOCTL_READTIMEOUT, /* Network read timeout */
STREAM_IOCTL_WRITETIMEOUT, /* Network write timeout */
STREAM_IOCTL_HANDSHAKECOMPLETE, /* Toggle handshake vs.data timeout */
STREAM_IOCTL_CONNSTATE, /* Connection state (open/closed) */
STREAM_IOCTL_GETCLIENTNAME, /* Get client name */
STREAM_IOCTL_GETCLIENTNAMELEN, /* Get client name length */
STREAM_IOCTL_GETCLIENTPORT, /* Get client port */
STREAM_IOCTL_HTTPREQTYPES, /* Permitted HTTP request types */
STREAM_IOCTL_LASTMESSAGE, /* CMP last message in transaction */
STREAM_IOCTL_CLOSESENDCHANNEL, /* Close send side of channel */
STREAM_IOCTL_ERRORINFO, /* Set stream extended error info */
STREAM_IOCTL_LAST /* Last possible IOCTL type */
} STREAM_IOCTL_TYPE;
/* Options for STREAM_IOCTL_HTTPREQTYPES */
typedef enum {
STREAM_HTTPREQTYPE_NONE, /* No HTTP request type */
STREAM_HTTPREQTYPE_GET, /* HTTP GET only */
STREAM_HTTPREQTYPE_POST, /* HTTP POST only */
STREAM_HTTPREQTYPE_ANY, /* HTTP GET or POST */
STREAM_HTTPREQTYPE_LAST /* Last possible HTTP request type */
} STREAM_HTTPREQTYPE_TYPE;
/* Stream network protocol types */
typedef enum {
STREAM_PROTOCOL_NONE, /* No protocol type */
STREAM_PROTOCOL_TCPIP, /* TCP/IP */
STREAM_PROTOCOL_HTTP, /* HTTP */
STREAM_PROTOCOL_CMP, /* TCP/IP with CMP packets */
STREAM_PROTOCOL_LAST /* Last possible protocol type */
} STREAM_PROTOCOL_TYPE;
/* The size of the I/O buffer used to read/write data from/to streams backed
by persistent files. These are allocated on-demand on the stack, so they
shouldn't be made too big. In addition since they may correspond
directly to underlying storage media blocks (e.g. disk sectors or flash
memory segments) they shouldn't be made smaller than the underlying
blocksize either. Finally, they should be a power of two (this isn't a
strict requirement of the code, but is in a good idea in general because
of storage media constraints) */
#ifdef CONFIG_CONSERVE_MEMORY
#define STREAM_BUFSIZE 512
#else
#define STREAM_BUFSIZE 4096
#endif /* CONFIG_CONSERVE_MEMORY */
/****************************************************************************
* *
* Stream Structures *
* *
****************************************************************************/
/* The STREAM data type */
typedef struct ST {
/* General information for the stream */
int type; /* The stream type, of type STREAM_TYPE */
int flags; /* Stream flags */
int status; /* Current stream status (clib error code) */
/* Information for memory I/O */
BUFFER_OPT( bufSize, bufEnd ) \
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 */
int bufCount; /* File position quantised by buffer size */
#if defined( __WIN32__ ) || defined( __WINCE__ )
HANDLE hFile; /* Backing file for the stream */
#ifdef __TESTIO__
char name[ MAX_PATH_LENGTH + 8 ];/* Data item associated with stream */
#endif /* __TESTIO__ */
#elif defined( __AMX__ ) || defined( __BEOS__ ) || defined( __ECOS__ ) || \
defined( __MVS__ ) || defined( __RTEMS__ ) || \
defined( __SYMBIAN32__ ) || defined( __TANDEM_NSK__ ) || \
defined( __TANDEM_OSS__ ) || defined( __UNIX__ ) || \
defined( __VXWORKS__ ) || defined( __XMK__ )
int fd; /* Backing file for the stream */
#ifdef __TESTIO__
char name[ MAX_PATH_LENGTH + 8 ];/* Data item associated with stream */
#endif /* __TESTIO__ */
#elif defined( __MAC__ )
short refNum; /* File stream reference number */
FSSpec fsspec; /* File system specification */
#elif defined( __PALMOS__ )
FileRef fileRef; /* File reference number */
#elif defined( __FileX__ )
FX_FILE filePtr; /* File associated with this stream */
long position; /* Position in file */
#elif defined( __UCOSII__ )
FS_FILE *pFile; /* File associated with this stream */
#elif defined( CONFIG_NO_STDIO )
#if defined( __IBM4758__ )
char name[ 8 + 1 ]; /* Data item associated with stream */
BOOLEAN isSensitive; /* Whether stream contains sensitive data */
#elif defined( __VMCMS__ ) || defined( __TESTIO__ )
char name[ MAX_PATH_LENGTH + 8 ];/* Data item associated with stream */
#endif /* Nonstandard I/O enviroments */
#else
FILE *filePtr; /* The file associated with this stream */
#endif /* System-specific file I/O information */
/* Network stream information. This is dynamically allocated since it's
only used for (relatively rare) network streams and would lead to a
lot of wasted memory in the memory streams that are used constantly
throughout cryptlib */
#ifdef USE_TCP
void *netStreamInfo;
#endif /* USE_TCP */
} STREAM;
/* Parsed URL information: schema://user@host:port/location. This is used
to parse URL data from an in-memory string and encodes pointers to
locations in the string data */
typedef enum { URL_TYPE_NONE, URL_TYPE_HTTP, URL_TYPE_HTTPS, URL_TYPE_SSH,
URL_TYPE_CMP, URL_TYPE_TSP, URL_TYPE_LAST } URL_TYPE;
typedef struct {
URL_TYPE type;
BUFFER_OPT_FIXED( schemaLen ) \
const char *schema;
int schemaLen;
BUFFER_OPT_FIXED( userInfoLen ) \
const char *userInfo;
int userInfoLen;
BUFFER_OPT_FIXED( hostLen ) \
const char *host;
int hostLen;
BUFFER_OPT_FIXED( locationLen ) \
const char *location;
int locationLen;
int port;
} URL_INFO;
/* Parsed HTTP URI information: location?attribute=value. The contents of a
string-form URI are broken down into the following fields by the HTTP
read code */
typedef struct {
BUFFER( CRYPT_MAX_TEXTSIZE, locationLen ) \
char location[ CRYPT_MAX_TEXTSIZE + 8 ];
BUFFER( CRYPT_MAX_TEXTSIZE, attributeLen ) \
char attribute[ CRYPT_MAX_TEXTSIZE + 8 ];
BUFFER( CRYPT_MAX_TEXTSIZE, valueLen ) \
char value[ CRYPT_MAX_TEXTSIZE + 8 ];
BUFFER( CRYPT_MAX_TEXTSIZE, extraDataLen ) \
char extraData[ CRYPT_MAX_TEXTSIZE + 8 ];
int locationLen, attributeLen, valueLen, extraDataLen;
} HTTP_URI_INFO;
/* Information required when connecting a network stream. There are so many
parameters required that we pack them into a struct to keep the interface
more manageable */
typedef enum {
NET_OPTION_NONE, /* No connect option type */
NET_OPTION_HOSTNAME, /* Use host/interface name + port */
NET_OPTION_HOSTNAME_TUNNEL, /* Use host + port tunnelled via proxy */
NET_OPTION_TRANSPORTSESSION,/* Use network transport session */
NET_OPTION_NETWORKSOCKET, /* Use user-supplied network socket */
NET_OPTION_NETWORKSOCKET_DUMMY, /* Dummy open to check socket OK */
NET_OPTION_LAST /* Last possible connect option type */
} NET_OPTION_TYPE;
typedef struct {
/* Network link information, either a remote host and port, a pre-
connected network socket, or a cryptlib transport session */
BUFFER_OPT_FIXED( nameLength ) \
const char *name;
int nameLength;
int port; /* Host info */
int networkSocket; /* Pre-connected network socket */
CRYPT_SESSION iCryptSession;/* cryptlib transport session */
/* Auxiliary information: Owning user object, network status
information, general option type */
CRYPT_USER iUserObject; /* Owning user object */
int timeout, connectTimeout;/* Connect and data xfer.timeouts */
NET_OPTION_TYPE options; /* Connect options */
} NET_CONNECT_INFO;
#define initNetConnectInfo( netConnectInfo, netUserObject, netTimeout, \
netConnectTimeout, netOption ) \
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -