2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 * exp_buf.h - read/write/alloc/free routines for a simple buffer structure
22 * MACROS are gross but execution speed is important
24 * NOTE: replacing the malloc and free with a allocs/frees
25 * from/to buffer pools or similar tuned/fixed size
26 * mem mgmt will improve performance.
28 * You should tune the buffer management to your environment
32 * Copyright (C) 1992 Michael Sample and the University of British Columbia
34 * This library is free software; you can redistribute it and/or
35 * modify it provided that this copyright/license information is retained
38 * If you modify this file, you must clearly indicate your changes.
40 * This source code is distributed in the hope that it will be
41 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
42 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
44 * $Header: /cvs/Darwin/Security/SecuritySNACCRuntime/c-lib/inc/exp-buf.h,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
46 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
47 * Move from private repository to open source repository
49 * Revision 1.2 2001/05/05 00:59:23 rmurphy
50 * Adding darwin license headers
52 * Revision 1.1.1.1 1999/03/16 18:06:21 aram
53 * Originals from SMIME Free Library.
55 * Revision 1.2 1995/07/27 08:54:45 rj
56 * functions used by gen-bufs or type tables merged.
58 * changed `_' to `-' in file names.
60 * Revision 1.1 1994/08/28 09:21:40 rj
61 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
70 char *dataStart
; /* points to first valid data byte */
71 /* when empty, 1 byte past blk end (rvs write)*/
72 char *dataEnd
; /* pts to first byte AFTER last valid data byte*/
73 char *curr
; /* current location to read form */
74 /* points to next byte to read */
75 struct ExpBuf
*next
; /* next buf (NULL if no next buffer)*/
76 struct ExpBuf
*prev
; /* prev buf (NULL if no prev buffer)*/
77 char *blkStart
; /* points to first byte of the blk */
78 char *blkEnd
; /* points the first byte AFTER blks last byte */
79 int readError
; /* non-zero is attempt to read past end of data*/
80 int writeError
;/* non-zero is attempt write fails (no mor bufs)*/
85 /* init, alloc and free routines */
86 #if defined (DEBUG) /* use fcns when debugging/macros later */ || defined (USE_GEN_BUF)
89 void PutExpBufInGenBuf
PROTO ((ExpBuf
*eb
,GenBuf
*gb
));
92 void ExpBufInit
PROTO ((unsigned long dataBlkSize
));
93 ExpBuf
*ExpBufAllocBuf();
94 void ExpBufFreeBuf
PROTO ((ExpBuf
*ptr
));
95 char *ExpBufAllocData();
96 void ExpBufFreeData
PROTO ((char *ptr
));
97 void ExpBufFreeBufAndData
PROTO (( ExpBuf
*b
));
99 ExpBuf
*ExpBufNext
PROTO ((ExpBuf
*b
));
100 ExpBuf
*ExpBufPrev
PROTO ((ExpBuf
*b
));
101 void ExpBufResetInReadMode
PROTO ((ExpBuf
*b
));
102 void ExpBufResetInWriteRvsMode
PROTO ((ExpBuf
*b
));
104 int ExpBufAtEod
PROTO ((ExpBuf
*b
));
105 int ExpBufFull
PROTO ((ExpBuf
*b
));
106 int ExpBufHasNoData
PROTO ((ExpBuf
*b
));
107 unsigned long ExpBufDataSize
PROTO ((ExpBuf
*b
));
108 unsigned long ExpBufDataBlkSize
PROTO ((ExpBuf
*b
));
109 char *ExpBufDataPtr
PROTO ((ExpBuf
*b
));
113 extern unsigned long expBufDataBlkSizeG
;
115 #define ExpBufInit( size) expBufDataBlkSizeG = size;
116 #define ExpBufAllocBuf() ((ExpBuf *)malloc (sizeof (ExpBuf)))
117 #define ExpBufFreeBuf( ptr) free (ptr)
118 #define ExpBufAllocData() ((void *)malloc (expBufDataBlkSizeG))
119 #define ExpBufFreeData( ptr) free (ptr)
120 #define ExpBufFreeBufAndData( b) { ExpBufFreeData ((b)->blkStart); ExpBufFreeBuf (b); }
121 #define ExpBufNext( b) ((b)->next)
122 #define ExpBufPrev( b) ((b)->prev)
123 #define ExpBufResetInReadMode( b) { (b)->curr = (b)->dataStart; (b)->readError = 0; (b)->writeError = 1; }
124 #define ExpBufResetInWriteRvsMode( b) { (b)->dataStart = (b)->dataEnd = (b)->blkEnd; (b)->writeError = 0; (b)->readError = 1; }
126 /* ExpBufAtEod only valid during reads (fwd) */
127 #define ExpBufAtEod( b) ((b)->curr == (b)->dataEnd)
129 /* ExpBufFull only valid during write (reverse) */
130 #define ExpBufFull( b) ((b)->dataStart == (b)->blkStart)
131 #define ExpBufHasNoData( b) ((b)->dataStart == (b)->dataEnd)
132 #define ExpBufDataSize( b) ((b)->dataEnd - (b)->dataStart)
133 #define ExpBufDataBlkSize( b) ((b)->blkEnd - (b)->blkStart)
134 #define ExpBufDataPtr( b) (ExpBufHasNoData (b)? NULL: (b)->dataStart)
136 #endif /* DEBUG || USE_GEN_BUF */
139 int ExpBufReadError
PROTO ((ExpBuf
**b
));
140 int ExpBufWriteError
PROTO ((ExpBuf
**b
));
142 #define ExpBufReadError( b) ((*b)->readError)
143 #define ExpBufWriteError( b) ((*b)->writeError)
146 ExpBuf
*ExpBufAllocBufAndData();
147 void ExpBufInstallDataInBuf
PROTO ((ExpBuf
*b
, char *data
, unsigned long int len
));
148 void ExpBufFreeBufAndDataList
PROTO (( ExpBuf
*b
));
149 ExpBuf
*ExpBufListLastBuf
PROTO ((ExpBuf
*b
));
150 ExpBuf
*ExpBufListFirstBuf
PROTO ((ExpBuf
*b
));
152 void ExpBufCopyToFile
PROTO ((ExpBuf
*b
, FILE *f
));
154 /* reading and writing routines */
156 void ExpBufSkip
PROTO (( ExpBuf
**, unsigned long len
));
157 int ExpBufCopy
PROTO (( char *dst
, ExpBuf
**b
, unsigned long len
));
158 unsigned char ExpBufPeekByte
PROTO (( ExpBuf
**b
));
160 int ExpBufPeekCopy
PROTO ((char *dst
, ExpBuf
**b
, unsigned long len
));
161 char *ExpBufPeekSeg
PROTO ((ExpBuf
**b
, unsigned long *len
));
163 char *ExpBufGetSeg
PROTO ((ExpBuf
**b
, unsigned long *len
));
164 void ExpBufPutSegRvs
PROTO ((ExpBuf
**b
, char *data
, unsigned long len
));
165 unsigned char ExpBufGetByte
PROTO ((ExpBuf
**b
));
166 void ExpBufPutByteRvs
PROTO ((ExpBuf
**b
, unsigned char byte
));
168 #endif /* conditional include */