]>
Commit | Line | Data |
---|---|---|
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 | /* | |
20 | * asn_len.c - BER encode, decode and utilities for ASN.1 lengths. | |
21 | * | |
22 | * indefinite lens are representd by the highest AsnLen | |
23 | * | |
24 | * MS 92 | |
25 | * Copyright (C) 1992 Michael Sample and the University of British Columbia | |
26 | * | |
27 | * This library is free software; you can redistribute it and/or | |
28 | * modify it provided that this copyright/license information is retained | |
29 | * in original form. | |
30 | * | |
31 | * If you modify this file, you must clearly indicate your changes. | |
32 | * | |
33 | * This source code is distributed in the hope that it will be | |
34 | * useful, but WITHOUT ANY WARRANTY; without even the implied warranty | |
35 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. | |
36 | * | |
5a719ac8 | 37 | * $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c-lib/src/asn-len.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $ |
bac41a7b A |
38 | * $Log: asn-len.c,v $ |
39 | * Revision 1.1.1.1 2001/05/18 23:14:08 mb | |
40 | * Move from private repository to open source repository | |
41 | * | |
42 | * Revision 1.2 2001/05/05 00:59:25 rmurphy | |
43 | * Adding darwin license headers | |
44 | * | |
45 | * Revision 1.1.1.1 1999/03/16 18:06:30 aram | |
46 | * Originals from SMIME Free Library. | |
47 | * | |
48 | * Revision 1.2 1995/07/27 08:58:36 rj | |
49 | * merged PeekEoc(), a function used only by the type table code. | |
50 | * | |
51 | * changed `_' to `-' in file names. | |
52 | * | |
53 | * Revision 1.1 1994/08/28 09:45:54 rj | |
54 | * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog. | |
55 | * | |
56 | */ | |
57 | ||
58 | #include "asn-config.h" | |
59 | #include "asn-len.h" | |
60 | ||
61 | ||
62 | AsnLen | |
63 | BEncDefLen PARAMS ((b, len), | |
64 | BUF_TYPE b _AND_ | |
65 | AsnLen len) | |
66 | { | |
67 | /* | |
68 | * unrolled for efficiency | |
69 | * check each possibitlity of the 4 byte integer | |
70 | */ | |
71 | if (len < 128) | |
72 | { | |
73 | BufPutByteRvs (b, len); | |
74 | return 1; | |
75 | } | |
76 | else if (len < 256) | |
77 | { | |
78 | BufPutByteRvs (b, len); | |
79 | BufPutByteRvs (b, 0x81); | |
80 | return 2; | |
81 | } | |
82 | else if (len < 65536) | |
83 | { | |
84 | BufPutByteRvs (b, len); | |
85 | BufPutByteRvs (b, len >> 8); | |
86 | BufPutByteRvs (b, 0x82); | |
87 | return 3; | |
88 | } | |
89 | else if (len < 16777126) | |
90 | { | |
91 | BufPutByteRvs (b, len); | |
92 | BufPutByteRvs (b, len >> 8); | |
93 | BufPutByteRvs (b, len >> 16); | |
94 | BufPutByteRvs (b, 0x83); | |
95 | return 4; | |
96 | } | |
97 | else | |
98 | { | |
99 | BufPutByteRvs (b, len); | |
100 | BufPutByteRvs (b, len >> 8); | |
101 | BufPutByteRvs (b, len >> 16); | |
102 | BufPutByteRvs (b, len >> 24); | |
103 | BufPutByteRvs (b, 0x84); | |
104 | return 5; | |
105 | } | |
106 | } /* BEncDefLen */ | |
107 | ||
108 | ||
109 | /* | |
110 | * non unrolled version | |
111 | */ | |
112 | AsnLen | |
113 | BEncDefLen2 PARAMS ((b, len), | |
114 | BUF_TYPE b _AND_ | |
115 | long int len) | |
116 | { | |
117 | int i; | |
118 | unsigned long int j; | |
119 | ||
120 | if (len < 128) | |
121 | { | |
122 | BufPutByteRvs (b, len); | |
123 | return 1; | |
124 | } | |
125 | else | |
126 | { | |
127 | for (i = 0, j = len; j > 0; j >>= 8, i++) | |
128 | BufPutByteRvs (b, j); | |
129 | ||
130 | BufPutByteRvs (b, 0x80 | i); | |
131 | return i + 1; | |
132 | } | |
133 | ||
134 | } /* BEncDefLen2 */ | |
135 | ||
136 | ||
137 | /* | |
138 | * decodes and returns an ASN.1 length | |
139 | */ | |
140 | AsnLen | |
141 | BDecLen PARAMS ((b, bytesDecoded, env), | |
142 | BUF_TYPE b _AND_ | |
143 | unsigned long int *bytesDecoded _AND_ | |
144 | jmp_buf env) | |
145 | { | |
146 | AsnLen len; | |
147 | AsnLen byte; | |
148 | int lenBytes; | |
149 | ||
150 | byte = (unsigned long int) BufGetByte (b); | |
151 | ||
152 | if (BufReadError (b)) | |
153 | { | |
154 | Asn1Error ("BDecLen: ERROR - decoded past end of data\n"); | |
155 | longjmp (env, -13); | |
156 | } | |
157 | ||
158 | (*bytesDecoded)++; | |
159 | if (byte < 128) /* short length */ | |
160 | return byte; | |
161 | ||
162 | else if (byte == (AsnLen) 0x080) /* indef len indicator */ | |
163 | return INDEFINITE_LEN; | |
164 | ||
165 | else /* long len form */ | |
166 | { | |
167 | /* | |
168 | * strip high bit to get # bytes left in len | |
169 | */ | |
170 | lenBytes = byte & (AsnLen) 0x7f; | |
171 | ||
172 | if (lenBytes > sizeof (AsnLen)) | |
173 | { | |
174 | Asn1Error ("BDecLen: ERROR - length overflow\n"); | |
175 | longjmp (env, -14); | |
176 | } | |
177 | ||
178 | (*bytesDecoded) += lenBytes; | |
179 | ||
180 | for (len = 0; lenBytes > 0; lenBytes--) | |
181 | len = (len << 8) | (AsnLen) BufGetByte (b); | |
182 | ||
183 | ||
184 | if (BufReadError (b)) | |
185 | { | |
186 | Asn1Error ("BDecLen: ERROR - decoded past end of data\n"); | |
187 | longjmp (env, -15); | |
188 | } | |
189 | ||
190 | return len; | |
191 | } | |
192 | /* not reached */ | |
193 | } /* BDecLen */ | |
194 | ||
195 | ||
196 | /* MACRO | |
197 | AsnLen | |
198 | BEncEoc PARAMS ((b), | |
199 | BUF_TYPE b) | |
200 | { | |
201 | BufPutByteRvs (b, 0); | |
202 | BufPutByteRvs (b, 0); | |
203 | return 2; | |
204 | } BEncEoc */ | |
205 | ||
206 | /* | |
207 | * Decodes an End of Contents (EOC) marker from the given buffer. | |
208 | * Flags and error if the octets are non-zero or if a read error | |
209 | * occurs. Increments bytesDecoded by the length of the EOC marker. | |
210 | */ | |
211 | void | |
212 | BDecEoc PARAMS ((b, bytesDecoded, env), | |
213 | BUF_TYPE b _AND_ | |
214 | AsnLen *bytesDecoded _AND_ | |
215 | jmp_buf env) | |
216 | { | |
217 | if ((BufGetByte (b) != 0) || (BufGetByte (b) != 0) || BufReadError (b)) | |
218 | { | |
219 | Asn1Error ("BDecEoc: ERROR - non zero byte in EOC or end of data reached\n"); | |
220 | longjmp (env, -16); | |
221 | } | |
222 | (*bytesDecoded) += 2; | |
223 | ||
224 | } /* BDecEoc */ | |
225 | ||
226 | #if TTBL | |
227 | /* returns true if the next tag is actually and EOC */ | |
228 | int PeekEoc PARAMS ((b), | |
229 | BUF_TYPE b) | |
230 | { | |
231 | return BufPeekByte (b) == 0; | |
232 | } /* PeekEoc */ | |
233 | #endif |