]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/sbuf.c
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 * .../c-lib/src/sbuf.c
22 * Copyright (C) 1992 Michael Sample and the University of British Columbia
24 * This library is free software; you can redistribute it and/or
25 * modify it provided that this copyright/license information is retained
28 * If you modify this file, you must clearly indicate your changes.
30 * This source code is distributed in the hope that it will be
31 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
32 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
37 #include "asn-config.h"
42 * casts are used to overcome void * - SBuf * conflict
43 * be careful if you modify param lists etc.
45 static struct GenBuf sBufOpsG
=
47 (BufGetByteFcn
) SBufGetByte
,
48 (BufGetSegFcn
) SBufGetSeg
,
49 (BufCopyFcn
) SBufCopy
,
50 (BufSkipFcn
) SBufSkip
,
51 (BufPeekByteFcn
) SBufPeekByte
,
52 (BufPeekSegFcn
) SBufPeekSeg
,
53 (BufPeekCopyFcn
) SBufPeekCopy
,
54 (BufPutByteRvsFcn
) SBufPutByteRvs
,
55 (BufPutSegRvsFcn
) SBufPutSegRvs
,
56 (BufReadErrorFcn
) SBufReadError
,
57 (BufWriteErrorFcn
) SBufWriteError
,
63 PutSBufInGenBuf
PARAMS ((sb
, gb
),
67 *gb
= sBufOpsG
; /* structure assignemnt */
72 * given an SBuf,b, and a block of data
73 * and its length this initializes a the SBuf
74 * to point to the data block. The data
75 * block is assumed to contain no valid data-
76 * ie it is empty and ready for writing
79 SBufInit
PARAMS ((b
, data
, dataLen
),
84 b
->readError
= b
->writeError
= 1;
86 b
->blkEnd
= data
+ dataLen
;
87 b
->dataStart
= b
->dataEnd
= b
->readLoc
= b
->blkEnd
;
92 * puts the given buffer in read mode and sets
93 * the current read location to the beginning of
95 * The read error flag is cleared.
96 * The writeError flag is set so that attempted writes
97 * will be fail and be detectable via a call to
101 SBufResetInReadMode
PARAMS ((b
),
104 b
->readLoc
= b
->dataStart
;
107 } /* SBufResetInnReadMode */
111 * puts the given buffer in reverse writing mode and sets
112 * the current write location to the end of the
113 * buffer's data block.
114 * The data start and end pointers are set to point to
115 * the end of the block - ie no data.
116 * The write error flag is cleared.
117 * The readError flag is set so that attempted reads
118 * will be fail and be detectable via a call to
122 SBufResetInWriteRvsMode
PARAMS ((b
),
125 b
->dataStart
= b
->dataEnd
= b
->blkEnd
;
128 } /* SBufResetInWriteRvsMode */
131 * installs given block of data into a buffer
132 * and sets it up for reading
135 SBufInstallData
PARAMS ((b
, data
, dataLen
),
140 SBufInit (b
, data
, dataLen
);
141 b
->dataStart
= b
->blkStart
;
142 SBufResetInReadMode (b
);
143 } /* SBufInstallData */
146 * returns the number of bytes in the data portion
149 SBufDataLen
PARAMS ((b
),
152 return b
->dataEnd
- b
->dataStart
;
156 * returns the pointer to the first data byte
159 SBufDataPtr
PARAMS ((b
),
166 * returns the size of block, the maximum size for data
167 * (does not look at how much data is present, just the
168 * max size if the block were empty)
171 SBufBlkLen
PARAMS ((b
),
174 return b
->blkEnd
- b
->blkStart
;
178 * returns a pointer to the first byte of the block
181 SBufBlkPtr
PARAMS ((b
),
188 * returns true if there is no more data
189 * to be read in the SBuf
195 return b
->readLoc
>= b
->dataEnd
;
199 /* returns true if you attempted to read past the end of data */
201 SBufReadError
PARAMS ((b
),
205 } /* SBufReadError */
208 * returns true if you attempted to write past the end of the block
209 * (remember SBufs do not expand like ExpBufs)
212 SBufWriteError
PARAMS ((b
),
215 return b
->writeError
;
216 } /* SBufWriteError */
219 * Skips the next skipLen bytes for reading
222 SBufSkip
PARAMS ((b
, skipLen
),
226 if (b
->readLoc
+ skipLen
> b
->dataEnd
)
228 b
->readLoc
= b
->dataEnd
;
232 b
->readLoc
+= skipLen
;
237 * copies copyLen bytes from buffer b into char *dst.
238 * Advances the curr read loc by copyLen
239 * Assumes dst is pre-allocated and is large enough.
240 * Will set the read error flag is you attempt to copy
241 * more than the number of unread bytes available.
244 SBufCopy
PARAMS ((dst
, b
, copyLen
),
249 if (b
->readLoc
+ copyLen
> b
->dataEnd
)
251 memcpy (dst
, b
->readLoc
, b
->dataEnd
- b
->readLoc
);
252 b
->readLoc
= b
->dataEnd
;
257 memcpy (dst
, b
->readLoc
, copyLen
);
258 b
->readLoc
+= copyLen
;
264 * returns the next byte from the buffer without advancing the
265 * current read location.
268 SBufPeekByte
PARAMS ((b
),
274 return (unsigned char)0;
277 return (unsigned char) *b
->readLoc
;
283 * returns a pointer into the buffer to the next bytes to be read.
284 * If *lenPtr unread bytes are not available, *lenPtr will be set
285 * to the number of bytes that are available. The current read location
286 * is *NOT* advanced at all. The read error flag will NOT be set
290 SBufPeekSeg
PARAMS ((b
, lenPtr
),
294 if (b
->readLoc
+ *lenPtr
> b
->dataEnd
)
295 *lenPtr
= b
->dataEnd
- b
->readLoc
;
303 * copies copyLen bytes from buffer b into char *dst.
304 * Does NOT advance the curr read location.
305 * assumes dst is pre-allocated and is large enough.
306 * Will set the read error flag is you attempt to copy
307 * more than the number of unread bytes available.
310 SBufPeekCopy
PARAMS ((dst
, b
, copyLen
),
315 if (b
->readLoc
+ copyLen
> b
->dataEnd
)
317 memcpy (dst
, b
->readLoc
, b
->dataEnd
- b
->readLoc
);
321 memcpy (dst
, b
->readLoc
, copyLen
);
328 * returns a pointer into the buffer to the next bytes to be read.
329 * If *lenPtr unread bytes are not available, *lenPtr will be set
330 * to the number of bytes that are available. The current read location
331 * is advance by the number of bytes returned in *lenPtr. The read error
332 * flag will NOT be set, ever, by this routine.
335 SBufGetSeg
PARAMS ((b
, lenPtr
),
342 if (b
->readLoc
+ *lenPtr
> b
->dataEnd
)
344 *lenPtr
= b
->dataEnd
- b
->readLoc
;
345 b
->readLoc
= b
->dataEnd
;
348 b
->readLoc
+= *lenPtr
;
354 * Write in reverse the char *seg of segLen bytes to the buffer b.
355 * A reverse write of segement really just prepends the given seg
356 * (in original order) to the buffers existing data.
357 * If the SBuf does not have enough room for the segment,
358 * the writeError flag is set and *NO* copying is done at all.
361 SBufPutSegRvs
PARAMS ((b
, seg
, segLen
),
366 if (b
->dataStart
- segLen
< b
->blkStart
)
370 b
->dataStart
-= segLen
;
371 memcpy (b
->dataStart
, seg
, segLen
);
373 } /* SBufPutSegRvs */
376 * returns the next byte from buffer b's data and advances the
377 * current read location by one byte. This will set the read error
378 * flag if you attempt to read past the end of the SBuf
381 SBufGetByte
PARAMS ((b
),
387 return (unsigned char)(*(b
->readLoc
++));
392 * writes (prepends) the given byte to buffer b's data
395 SBufPutByteRvs
PARAMS ((b
, byte
),
399 if (b
->dataStart
<= b
->blkStart
)
402 *--b
->dataStart
= byte
;
403 } /* SBufPutByteRvs */
405 #endif /* USE_GEN_BUF */