]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/asn-len.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 * asn_len.c - BER encode, decode and utilities for ASN.1 lengths.
22 * indefinite lens are representd by the highest AsnLen
25 * Copyright (C) 1992 Michael Sample and the University of British Columbia
27 * This library is free software; you can redistribute it and/or
28 * modify it provided that this copyright/license information is retained
31 * If you modify this file, you must clearly indicate your changes.
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.
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 $
39 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
40 * Move from private repository to open source repository
42 * Revision 1.2 2001/05/05 00:59:25 rmurphy
43 * Adding darwin license headers
45 * Revision 1.1.1.1 1999/03/16 18:06:30 aram
46 * Originals from SMIME Free Library.
48 * Revision 1.2 1995/07/27 08:58:36 rj
49 * merged PeekEoc(), a function used only by the type table code.
51 * changed `_' to `-' in file names.
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.
58 #include "asn-config.h"
63 BEncDefLen
PARAMS ((b
, len
),
68 * unrolled for efficiency
69 * check each possibitlity of the 4 byte integer
73 BufPutByteRvs (b
, len
);
78 BufPutByteRvs (b
, len
);
79 BufPutByteRvs (b
, 0x81);
84 BufPutByteRvs (b
, len
);
85 BufPutByteRvs (b
, len
>> 8);
86 BufPutByteRvs (b
, 0x82);
89 else if (len
< 16777126)
91 BufPutByteRvs (b
, len
);
92 BufPutByteRvs (b
, len
>> 8);
93 BufPutByteRvs (b
, len
>> 16);
94 BufPutByteRvs (b
, 0x83);
99 BufPutByteRvs (b
, len
);
100 BufPutByteRvs (b
, len
>> 8);
101 BufPutByteRvs (b
, len
>> 16);
102 BufPutByteRvs (b
, len
>> 24);
103 BufPutByteRvs (b
, 0x84);
110 * non unrolled version
113 BEncDefLen2
PARAMS ((b
, len
),
122 BufPutByteRvs (b
, len
);
127 for (i
= 0, j
= len
; j
> 0; j
>>= 8, i
++)
128 BufPutByteRvs (b
, j
);
130 BufPutByteRvs (b
, 0x80 | i
);
138 * decodes and returns an ASN.1 length
141 BDecLen
PARAMS ((b
, bytesDecoded
, env
),
143 unsigned long int *bytesDecoded _AND_
150 byte
= (unsigned long int) BufGetByte (b
);
152 if (BufReadError (b
))
154 Asn1Error ("BDecLen: ERROR - decoded past end of data\n");
159 if (byte
< 128) /* short length */
162 else if (byte
== (AsnLen
) 0x080) /* indef len indicator */
163 return INDEFINITE_LEN
;
165 else /* long len form */
168 * strip high bit to get # bytes left in len
170 lenBytes
= byte
& (AsnLen
) 0x7f;
172 if (lenBytes
> sizeof (AsnLen
))
174 Asn1Error ("BDecLen: ERROR - length overflow\n");
178 (*bytesDecoded
) += lenBytes
;
180 for (len
= 0; lenBytes
> 0; lenBytes
--)
181 len
= (len
<< 8) | (AsnLen
) BufGetByte (b
);
184 if (BufReadError (b
))
186 Asn1Error ("BDecLen: ERROR - decoded past end of data\n");
201 BufPutByteRvs (b, 0);
202 BufPutByteRvs (b, 0);
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.
212 BDecEoc
PARAMS ((b
, bytesDecoded
, env
),
214 AsnLen
*bytesDecoded _AND_
217 if ((BufGetByte (b
) != 0) || (BufGetByte (b
) != 0) || BufReadError (b
))
219 Asn1Error ("BDecEoc: ERROR - non zero byte in EOC or end of data reached\n");
222 (*bytesDecoded
) += 2;
227 /* returns true if the next tag is actually and EOC */
228 int PeekEoc
PARAMS ((b
),
231 return BufPeekByte (b
) == 0;