]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/inc/sbuf.h
608b163c2b7ec053460d617b7deab5d65dc7e934
[apple/security.git] / SecuritySNACCRuntime / c-lib / inc / sbuf.h
1 /*
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
3 *
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
8 * using this file.
9 *
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.
16 */
17
18
19 /*
20 * sbuf.h - a buffer consisting of one contiguous block
21 * that checks for read and write range errors.
22 * MS 92
23 * Copyright (C) 1992 Michael Sample and the University of British Columbia
24 *
25 * This library is free software; you can redistribute it and/or
26 * modify it provided that this copyright/license information is retained
27 * in original form.
28 *
29 * If you modify this file, you must clearly indicate your changes.
30 *
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.
34 *
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 $
36 * $Log: sbuf.h,v $
37 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
38 * Move from private repository to open source repository
39 *
40 * Revision 1.2 2001/05/05 00:59:23 rmurphy
41 * Adding darwin license headers
42 *
43 * Revision 1.1.1.1 1999/03/16 18:06:21 aram
44 * Originals from SMIME Free Library.
45 *
46 * Revision 1.2 1995/07/27 08:54:46 rj
47 * functions used by gen-bufs or type tables merged.
48 *
49 * changed `_' to `-' in file names.
50 *
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.
53 *
54 */
55
56 #ifndef _asn_buf_h_
57 #define _asn_buf_h_
58
59 typedef struct SBuf
60 {
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 */
68 } SBuf;
69
70 #ifdef USE_GEN_BUF
71
72 /* use functions (-> src/sbuf.c) instead of cpp macros */
73
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));
89 #if TTBL
90 char *SBufPeekSeg PROTO ((SBuf *b, long int *lenPtr));
91 void SBufPeekCopy PROTO ((char *dst, SBuf *b, long int copyLen));
92 #endif
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));
97
98 #else
99
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;\
106 }
107
108 #define SBufResetInReadMode(b)\
109 { (b)->readLoc = (b)->dataStart;\
110 (b)->readError = 0;\
111 (b)->writeError = 1;\
112 }
113
114 #define SBufResetInWriteRvsMode(b)\
115 { (b)->dataStart = (b)->dataEnd = (b)->blkEnd;\
116 (b)->writeError = 0;\
117 (b)->readError = 1;\
118 }
119
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);
125
126 /* returns the number of bytes in the data portion */
127 #define SBufDataLen(b)\
128 ((b)->dataEnd - (b)->dataStart)
129
130 /* returns the pointer to the first data byte */
131 #define SBufDataPtr(b)\
132 ((b)->dataStart)
133
134 /* returns the size of block, the maximum size for data */
135 #define SBufBlkLen(b)\
136 ((b)->blkEnd - (b)->blkStart)
137
138 /* returns a pointer to the first byte of the block */
139 #define SBufBlkPtr(b)\
140 ((b)->blkStart)
141
142 /* returns true if there is no more data to be read in the SBuf */
143 #define SBufEod(b)\
144 ((b)->readLoc >= (b)->dataEnd)
145
146 /* returns true if you attempted to read past the end of data */
147 #define SBufReadError(b)\
148 ((b)->readError)
149
150 /*
151 * returns true if you attempted to write past the end of the block
152 * (remember SBufs do not expand like ExpBufs)
153 */
154 #define SBufWriteError(b)\
155 ((b)->writeError)
156
157 /* Skips the next skipLen bytes for reading */
158 #define SBufSkip(b, skipLen)\
159 { if ( ((b)->readLoc + skipLen) > (b)->dataEnd)\
160 {\
161 (b)->readLoc = (b)->dataEnd;\
162 (b)->readError = 1;\
163 }\
164 else\
165 (b)->readLoc += skipLen;\
166 }
167
168
169 /*
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.
174 */
175 #define SBufCopy(dst, b, copyLen)\
176 { if (((b)->readLoc + copyLen) > (b)->dataEnd)\
177 {\
178 memcpy (dst, (b)->readLoc, (b)->dataEnd - (b)->readLoc);\
179 (b)->readLoc = (b)->dataEnd;\
180 (b)->readError = 1;\
181 }\
182 else\
183 {\
184 memcpy (dst, (b)->readLoc, copyLen);\
185 (b)->readLoc += copyLen;\
186 }\
187 }
188
189 /*
190 * returns the next byte from the buffer without advancing the
191 * current read location.
192 */
193 #define SBufPeekByte(b)\
194 ((SBufEod (b))? ((b)->readError = 1):(unsigned char) *((b)->readLoc))
195
196 /*
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.
203 */
204 #define SBufGetSeg( b, lenPtr)\
205 ((b)->readLoc);\
206 if (((b)->readLoc + *lenPtr) > (b)->dataEnd)\
207 {\
208 *lenPtr = (b)->dataEnd - (b)->readLoc;\
209 (b)->readLoc = (b)->dataEnd;\
210 }\
211 else\
212 (b)->readLoc += *lenPtr;
213
214 /*
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
218 */
219 #define SBufPutSegRvs(b, seg, segLen)\
220 { if (((b)->dataStart - segLen) < (b)->blkStart)\
221 (b)->writeError = 1;\
222 else\
223 {\
224 (b)->dataStart -= segLen;\
225 memcpy ((b)->dataStart, seg, segLen);\
226 }\
227 }
228
229 /*
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
233 */
234 #define SBufGetByte(b)\
235 (unsigned char)((SBufEod (b))? ((b)->readError = 1):*((b)->readLoc++))
236
237 /*
238 * writes (prepends) the given byte to buffer b's data
239 */
240 #define SBufPutByteRvs(b, byte)\
241 { if ((b)->dataStart <= (b)->blkStart)\
242 (b)->writeError = 1;\
243 else\
244 *(--(b)->dataStart) = byte;\
245 }
246
247 #endif /* USE_GEN_BUF */
248
249 #endif /* conditional include */