]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/c++/asn-len.cpp
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.
19 // file: .../c++-lib/src/asn-len.C - ASN.1 Length manipluation routines
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.
35 #include "asn-config.h"
40 * Encodes the given length to the given buffer.
41 * returns the number of octets written to the buffer.
44 BEncDefLen (BUF_TYPE b
, AsnLen len
)
47 * unrolled for efficiency
48 * (check each possibitlity of the 4 byte integer)
64 b
.PutByteRvs (len
>> 8);
68 else if (len
< 16777126)
71 b
.PutByteRvs (len
>> 8);
72 b
.PutByteRvs (len
>> 16);
79 b
.PutByteRvs (len
>> 8);
80 b
.PutByteRvs (len
>> 16);
81 b
.PutByteRvs (len
>> 24);
88 * Decode a BER length from the given buffer. Increments bytesDecoded
89 * by the number of octets of the encoded length. Flags an
90 * error if the length is too large or a read error occurs
93 BDecLen (BUF_TYPE b
, AsnLen
&bytesDecoded
, ENV_TYPE env
)
103 Asn1Error
<< "BDecLen: decoded past end of data" << endl
;
104 #if SNACC_EXCEPTION_ENABLE
105 SnaccExcep::throwMe(-9);
112 if (byte
< 128) /* short length */
115 else if (byte
== (unsigned char) 0x080) /* indef len indicator */
116 return INDEFINITE_LEN
;
118 else /* long len form */
121 * strip high bit to get # bytes left in len
123 lenBytes
= byte
& (unsigned char) 0x7f;
125 if (lenBytes
> sizeof (long int))
127 Asn1Error
<< "BDecLen: ERROR - length overflow" << endl
;
128 #if SNACC_EXCEPTION_ENABLE
129 SnaccExcep::throwMe(-10);
135 bytesDecoded
+= lenBytes
;
137 for (len
= 0; lenBytes
> 0; lenBytes
--)
138 len
= (len
<< 8) | (unsigned long int) b
.GetByte();
143 Asn1Error
<< "BDecLen: decoded past end of data" << endl
;
144 #if SNACC_EXCEPTION_ENABLE
145 SnaccExcep::throwMe(-11);
158 * Encodes an End of Contents (EOC) to the given buffer.
159 * Returns the encoded length.
171 * Decodes an EOC from the given buffer. flags an error if the
172 * octets are non-zero or if read error occured.
175 BDecEoc (BUF_TYPE b
, AsnLen
&bytesDecoded
, ENV_TYPE env
)
178 if ((b
.GetByte() != 0) || (b
.GetByte() != 0) || b
.ReadError())
180 Asn1Error
<< "BDecEoc: ERROR - non zero byte in EOC or end of data reached" << endl
;
181 #if SNACC_EXCEPTION_ENABLE
182 SnaccExcep::throwMe(-12);