home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / crypl200.zip / KEYMGMT / STREAM.H < prev    next >
Text File  |  1996-09-24  |  5KB  |  133 lines

  1. /****************************************************************************
  2. *                                                                            *
  3. *                        STREAM Class Constants and Structures                *
  4. *                          Copyright Peter Gutmann 1993-1996                    *
  5. *                                                                            *
  6. ****************************************************************************/
  7.  
  8. #ifndef _STREAM_DEFINED
  9.  
  10. #define _STREAM_DEFINED
  11.  
  12. #include <stdio.h>
  13. #if defined( INC_CHILD )
  14.   #include "../crypt.h"
  15. #else
  16.   #include "crypt.h"
  17. #endif /* Compiler-specific includes */
  18.  
  19. /* There are a few OS's broken enough to not define the standard seek codes
  20.    which are needed by sFileSeek() (SunOS springs to mind), so we define
  21.    them here just in case */
  22.  
  23. #ifndef SEEK_SET
  24.   #define SEEK_SET    0
  25.   #define SEEK_CUR    1
  26.   #define SEEK_END    2
  27. #endif /* SEEK_SET */
  28.  
  29. /****************************************************************************
  30. *                                                                            *
  31. *                                STREAM Constants                            *
  32. *                                                                            *
  33. ****************************************************************************/
  34.  
  35. /* STREAM error types */
  36.  
  37. #define STREAM_OK                0    /* No error */
  38. #define STREAM_ERROR            -1    /* General error */
  39. #define STREAM_BADPARAM            -2    /* Bad parameter passed to function */
  40. #define STREAM_OPEN                -3    /* Cannot open stream */
  41. #define STREAM_CLOSE            -4    /* Cannot close stream */
  42. #define STREAM_READ                -5    /* Read error on stream */
  43. #define STREAM_WRITE            -6    /* Write error on stream */
  44. #define STREAM_SEEK                -7    /* Seek error on stream */
  45. #define STREAM_FULL                -8    /* No space left on stream */
  46. #define STREAM_EMPTY            -9    /* No data left in stream */
  47. #define STREAM_BADDATA            -10    /* User-defined data error in stream */
  48.  
  49. /* Occasionally we want to connect a memory stream to a fixed-length buffer
  50.    whose size is "big enough for the data it needs to hold", but of an
  51.    unknown length.  Using the following as the length will avoid various
  52.    checks on the input length */
  53.  
  54. #define STREAMSIZE_UNKNOWN        -1
  55.  
  56. /****************************************************************************
  57. *                                                                            *
  58. *                            STREAM Class Structures                            *
  59. *                                                                            *
  60. ****************************************************************************/
  61.  
  62. /* The STREAM data type */
  63.  
  64. typedef struct {
  65.     /* Information for memory I/O */
  66.     BYTE *buffer;                /* Buffer to R/W to */
  67.     int bufSize;                /* Total size of buffer */
  68.     int bufPos;                    /* Current position in buffer */
  69.     int bufEnd;                    /* Last buffer position with valid data */
  70.  
  71.     /* Information for file I/O */
  72.     FILE *filePtr;                /* The file associated with this stream */
  73.  
  74.     /* General information for the stream */
  75.     BOOLEAN isNull;                /* Whether this is a null stream */
  76.     int status;                    /* Current stream status */
  77.     int lastChar;                /* Last char read, for ungetc() function */
  78.     int ungetChar;                /* Whether we need to return lastChar next */
  79.     } STREAM;
  80.  
  81. /****************************************************************************
  82. *                                                                            *
  83. *                            STREAM Class Function Prototypes                *
  84. *                                                                            *
  85. ****************************************************************************/
  86.  
  87. /* Read and write a byte or block of data to/from a stream */
  88.  
  89. int sputc( STREAM *stream, int data );
  90. int sgetc( STREAM *stream );
  91. int sungetc( STREAM *stream );
  92. int sread( STREAM *stream, void *buffer, int length );
  93. int swrite( STREAM *stream, const void *buffer, const int length );
  94.  
  95. /* Skip a number of bytes in a stream */
  96.  
  97. int sSkip( STREAM *stream, const int length );
  98.  
  99. /* Inquire as to the health of a stream */
  100.  
  101. #define sGetStatus( stream )            ( stream )->status
  102.  
  103. /* Set/clear user-defined error state for the stream */
  104.  
  105. #define sSetError( stream, error )        if( ( stream )->status == STREAM_OK ) \
  106.                                             ( stream )->status = ( error )
  107. #define sClearError( stream )            ( stream )->status = STREAM_OK
  108.  
  109. /* Clear the unget buffer for a stream */
  110.  
  111. #define sClearUnget( stream )            sungetc( stream ); sgetc( stream );
  112.  
  113. /* Functions to work with memory streams */
  114.  
  115. int sMemOpen( STREAM *stream, void *buffer, const int length );
  116. int sMemNullOpen( STREAM *stream );
  117. int sMemClose( STREAM *stream );
  118. int sMemConnect( STREAM *stream, void *buffer, const int length );
  119. int sMemDisconnect( STREAM *stream );
  120. int sMemSeek( STREAM *stream, const size_t position );
  121. int sMemReset( STREAM *stream );
  122. int sMemSize( STREAM *stream );
  123.  
  124. /* Functions to work with file streams */
  125.  
  126. int sFileOpen( STREAM *stream, const char *fileName, const char *mode );
  127. int sFileClose( STREAM *stream );
  128. int sFileConnect( STREAM *stream, FILE *filePtr );
  129. int sFileDisconnect( STREAM *stream );
  130. int sFileSeek( STREAM *stream, const long position );
  131.  
  132. #endif /* _STREAM_DEFINED */
  133.