]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/inc/sbuf.h
608b163c2b7ec053460d617b7deab5d65dc7e934
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 * sbuf.h - a buffer consisting of one contiguous block
21 * that checks for read and write range errors.
23 * Copyright (C) 1992 Michael Sample and the University of British Columbia
25 * This library is free software; you can redistribute it and/or
26 * modify it provided that this copyright/license information is retained
29 * If you modify this file, you must clearly indicate your changes.
31 * This source code is distributed in the hope that it will be
32 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
33 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35 * $Header: /cvs/root/Security/SecuritySNACCRuntime/c-lib/inc/Attic/sbuf.h,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
37 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
38 * Move from private repository to open source repository
40 * Revision 1.2 2001/05/05 00:59:23 rmurphy
41 * Adding darwin license headers
43 * Revision 1.1.1.1 1999/03/16 18:06:21 aram
44 * Originals from SMIME Free Library.
46 * Revision 1.2 1995/07/27 08:54:46 rj
47 * functions used by gen-bufs or type tables merged.
49 * changed `_' to `-' in file names.
51 * Revision 1.1 1994/08/28 09:45:39 rj
52 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
61 char *dataStart
; /* byte last written (or end) */
62 char *dataEnd
; /* ptr to first byte after last valid data byte */
63 char *blkStart
; /* ptr to first byte of the buffer */
64 char *blkEnd
; /* ptr to first byte past end of the buffer */
65 char *readLoc
; /* next byte to read (or end) */
66 int writeError
; /* whether write error occurred */
67 int readError
; /* whether read error occurred */
72 /* use functions (-> src/sbuf.c) instead of cpp macros */
74 void PutSBufInGenBuf
PROTO ((SBuf
*sb
, GenBuf
*gb
));
75 void SBufInit
PROTO ((SBuf
*b
, char *data
, long int dataLen
));
76 void SBufResetInReadMode
PROTO ((SBuf
*b
));
77 void SBufResetInWriteRvsMode
PROTO ((SBuf
*b
));
78 void SBufInstallData
PROTO ((SBuf
*b
, char *data
, long int dataLen
));
79 long int SBufDataLen
PROTO ((SBuf
*b
));
80 char *SBufDataPtr
PROTO ((SBuf
*b
));
81 long int SBufBlkLen
PROTO ((SBuf
*b
));
82 char *SBufBlkPtr
PROTO ((SBuf
*b
));
83 int SBufEod
PROTO ((SBuf
*b
));
84 int SBufReadError
PROTO ((SBuf
*b
));
85 int SBufWriteError
PROTO ((SBuf
*b
));
86 void SBufSkip
PROTO ((SBuf
*b
, long int skipLen
));
87 void SBufCopy
PROTO ((char *dst
, SBuf
*b
, long int copyLen
));
88 unsigned char SBufPeekByte
PROTO ((SBuf
*b
));
90 char *SBufPeekSeg
PROTO ((SBuf
*b
, long int *lenPtr
));
91 void SBufPeekCopy
PROTO ((char *dst
, SBuf
*b
, long int copyLen
));
93 char *SBufGetSeg
PROTO ((SBuf
*b
,long int *lenPtr
));
94 void SBufPutSegRvs
PROTO ((SBuf
*b
, char *seg
, long int segLen
));
95 unsigned char SBufGetByte
PROTO ((SBuf
*b
));
96 void SBufPutByteRvs
PROTO ((SBuf
*b
, unsigned char byte
));
100 /* initializes a buffer into an 'empty' state */
101 #define SBufInit(b, data, dataLen)\
102 { (b)->readError = (b)->writeError = 1;\
103 (b)->blkStart = data;\
104 (b)->blkEnd = data + dataLen;\
105 (b)->dataStart = (b)->dataEnd = (b)->readLoc = (b)->blkEnd;\
108 #define SBufResetInReadMode(b)\
109 { (b)->readLoc = (b)->dataStart;\
111 (b)->writeError = 1;\
114 #define SBufResetInWriteRvsMode(b)\
115 { (b)->dataStart = (b)->dataEnd = (b)->blkEnd;\
116 (b)->writeError = 0;\
120 /* installs given block of data into a buffer and sets it up for reading */
121 #define SBufInstallData(b, data, dataLen)\
122 SBufInit (b, data, dataLen);\
123 (b)->dataStart = (b)->blkStart;\
124 SBufResetInReadMode (b);
126 /* returns the number of bytes in the data portion */
127 #define SBufDataLen(b)\
128 ((b)->dataEnd - (b)->dataStart)
130 /* returns the pointer to the first data byte */
131 #define SBufDataPtr(b)\
134 /* returns the size of block, the maximum size for data */
135 #define SBufBlkLen(b)\
136 ((b)->blkEnd - (b)->blkStart)
138 /* returns a pointer to the first byte of the block */
139 #define SBufBlkPtr(b)\
142 /* returns true if there is no more data to be read in the SBuf */
144 ((b)->readLoc >= (b)->dataEnd)
146 /* returns true if you attempted to read past the end of data */
147 #define SBufReadError(b)\
151 * returns true if you attempted to write past the end of the block
152 * (remember SBufs do not expand like ExpBufs)
154 #define SBufWriteError(b)\
157 /* Skips the next skipLen bytes for reading */
158 #define SBufSkip(b, skipLen)\
159 { if ( ((b)->readLoc + skipLen) > (b)->dataEnd)\
161 (b)->readLoc = (b)->dataEnd;\
165 (b)->readLoc += skipLen;\
170 * copies copyLen bytes from buffer b into char *dst.
171 * assumes dst is pre-allocated and is large enough.
172 * Will set the read error flag is you attempt to copy
173 * more than the number of unread bytes available.
175 #define SBufCopy(dst, b, copyLen)\
176 { if (((b)->readLoc + copyLen) > (b)->dataEnd)\
178 memcpy (dst, (b)->readLoc, (b)->dataEnd - (b)->readLoc);\
179 (b)->readLoc = (b)->dataEnd;\
184 memcpy (dst, (b)->readLoc, copyLen);\
185 (b)->readLoc += copyLen;\
190 * returns the next byte from the buffer without advancing the
191 * current read location.
193 #define SBufPeekByte(b)\
194 ((SBufEod (b))? ((b)->readError = 1):(unsigned char) *((b)->readLoc))
197 * WARNING: this is a fragile macro. be careful where you use it.
198 * return a pointer into the buffer for the next bytes to be read
199 * if *lenPtr uread bytes are not available, *lenPtr will be set
200 * to the number of byte that are available. The current read location
201 * is advance by the number of bytes returned in *lenPtr. The read error
202 * flag will NOT set, ever, by this routine.
204 #define SBufGetSeg( b, lenPtr)\
206 if (((b)->readLoc + *lenPtr) > (b)->dataEnd)\
208 *lenPtr = (b)->dataEnd - (b)->readLoc;\
209 (b)->readLoc = (b)->dataEnd;\
212 (b)->readLoc += *lenPtr;
215 * Write in reverse the char *seg of segLen bytes to the buffer b.
216 * A reverse write of segement really just prepends the given seg
217 * (in original order) to the buffers existing data
219 #define SBufPutSegRvs(b, seg, segLen)\
220 { if (((b)->dataStart - segLen) < (b)->blkStart)\
221 (b)->writeError = 1;\
224 (b)->dataStart -= segLen;\
225 memcpy ((b)->dataStart, seg, segLen);\
230 * returns the next byte from buffer b's data and advances the
231 * current read location by one byte. This will set the read error
232 * flag if you attempt to read past the end of the SBuf
234 #define SBufGetByte(b)\
235 (unsigned char)((SBufEod (b))? ((b)->readError = 1):*((b)->readLoc++))
238 * writes (prepends) the given byte to buffer b's data
240 #define SBufPutByteRvs(b, byte)\
241 { if ((b)->dataStart <= (b)->blkStart)\
242 (b)->writeError = 1;\
244 *(--(b)->dataStart) = byte;\
247 #endif /* USE_GEN_BUF */
249 #endif /* conditional include */