]> git.saurik.com Git - apple/security.git/blame - SecuritySNACCRuntime/c++-lib/inc/asn-buf.h
Security-54.1.3.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c++-lib / inc / asn-buf.h
CommitLineData
bac41a7b
A
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// file: .../c++-lib/inc/asn-buf.h - buffer class
20//
21// MS 92
22// Copyright (C) 1992 Michael Sample and the University of British Columbia
23//
24// This library is free software; you can redistribute it and/or
25// modify it provided that this copyright/license information is retained
26// in original form.
27//
28// If you modify this file, you must clearly indicate your changes.
29//
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.
33//
5a719ac8 34// $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c++-lib/inc/asn-buf.h,v 1.1.1.1 2001/05/18 23:14:06 mb Exp $
bac41a7b
A
35// $Log: asn-buf.h,v $
36// Revision 1.1.1.1 2001/05/18 23:14:06 mb
37// Move from private repository to open source repository
38//
39// Revision 1.3 2001/05/05 00:59:17 rmurphy
40// Adding darwin license headers
41//
42// Revision 1.2 2000/06/15 18:48:23 dmitch
43// Snacc-generated source files, now part of CVS tree to allow for cross-platform build of snaccRuntime.
44//
45// Revision 1.1.1.1 2000/03/09 01:00:05 rmurphy
46// Base Fortissimo Tree
47//
48// Revision 1.4 1999/08/06 16:13:18 mb
49// Set readError when doing a GetSeg past the end of the buffer. This fixes many potential bugs and hangs when doing streaming decodes with embedded data.
50//
51// Revision 1.3 1999/07/14 23:53:55 aram
52// Made const correct so things build with CW 5.0
53//
54// Revision 1.2 1999/03/04 00:43:20 mb
55// Made buffer full check work for NULL buffer in an unsigned int context
56//
57// Revision 1.1 1999/02/25 05:21:41 mb
58// Added snacc c++ library
59//
60// Revision 1.5 1997/02/16 20:25:35 rj
61// check-in of a few cosmetic changes
62//
63// Revision 1.4 1995/07/25 20:18:58 rj
64// changed `_' to `-' in file names.
65//
66// Revision 1.3 1994/10/08 04:15:38 rj
67// fixed both Copy()'s name and implementation to CopyOut() that always returns the number of bytes copied out instead of 0 in case less than the requested amount is available.
68//
69// several `unsigned long int' turned into `size_t'.
70//
71// Revision 1.2 1994/08/28 10:00:46 rj
72// comment leader fixed.
73//
74// Revision 1.1 1994/08/28 09:20:28 rj
75// first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
76
77#ifndef _asn_buf_h_
78#define _asn_buf_h_
79
80class AsnBuf
81{
82protected:
83 char *dataStart;
84 char *dataEnd;
85 char *blkStart;
86 char *blkEnd;
87 char *readLoc;
88 bool writeError;
89 bool readError;
90
91public:
92 // install data for reading or blank blk for writing in buffer
93 // must be followed by 'mode' setting method call
94 void Init (char *data, size_t dataLen)
95 {
96 readError = writeError = 1;
97 blkStart = data;
98 blkEnd = data + dataLen;
99 dataStart = dataEnd = readLoc = blkEnd;
100 }
101
102 void ResetInReadMode()
103 {
104 readLoc = dataStart;
105 readError = false;
106 writeError = true;
107 }
108
109 void ResetInWriteRvsMode()
110 {
111 dataStart = dataEnd = blkEnd;
112 writeError = false;
113 readError = true;
114 }
115
116 void InstallData (const char *data, size_t dataLen)
117 {
118 Init (const_cast<char *>(data), dataLen);
119 dataStart = blkStart;
120 ResetInReadMode();
121 }
122
123 size_t DataLen() { return dataEnd - dataStart; }
124 char *DataPtr() { return dataStart; }
125 size_t BlkLen() { return blkEnd - blkStart; }
126 char *BlkPtr() { return blkStart; }
127 bool Eod() { return readLoc >= dataEnd; }
128 bool ReadError() { return readError; }
129 bool WriteError() { return writeError; }
130
131 void Skip (size_t skipLen)
132 {
133 if ((readLoc + skipLen) > dataEnd)
134 {
135 readLoc = dataEnd;
136 readError = true;
137 }
138 else
139 readLoc += skipLen;
140 }
141
142 size_t CopyOut (char *dst, size_t copyLen)
143 {
144 if (readLoc + copyLen > dataEnd)
145 {
146 copyLen = dataEnd - readLoc;
147 readError = true;
148 }
149 memcpy (dst, readLoc, copyLen);
150 readLoc += copyLen;
151 return copyLen;
152 }
153
154 unsigned char PeekByte()
155 {
156 if (Eod())
157 {
158 readError = true;
159 return 0;
160 }
161 else
162 return *readLoc;
163 }
164
165 char *GetSeg (size_t *lenPtr)
166 {
167 char *retVal = readLoc;
168 if ((readLoc + *lenPtr) > dataEnd)
169 {
170 *lenPtr = dataEnd - readLoc;
171 readLoc = dataEnd;
172
173 /* Attempting to read more bytes than left in the buffer is a read error --Michael. */
174 readError = true;
175
176 return retVal;
177 }
178 else
179 {
180 readLoc += *lenPtr;
181 return retVal;
182 }
183 }
184
185 void PutSegRvs (char *seg, size_t segLen)
186 {
187 if (dataStart < (blkStart + segLen))
188 writeError = true;
189 else
190 {
191 dataStart -= segLen;
192 memcpy (dataStart, seg, segLen);
193 }
194 }
195
196 unsigned char GetByte()
197 {
198 if (Eod())
199 {
200 readError = true;
201 return 0;
202 }
203 else
204 return *(readLoc++);
205 }
206
207 void PutByteRvs (unsigned char byte)
208 {
209 if (dataStart <= blkStart)
210 writeError = true;
211 else
212 *(--dataStart) = byte;
213 }
214};
215
216#endif /* conditional include */