]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/c++/asn-len.cpp
Security-54.1.9.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c++-lib / c++ / asn-len.cpp
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/src/asn-len.C - ASN.1 Length manipluation routines
20 //
21 // MS 92/06/18
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 //
34
35 #include "asn-config.h"
36 #include "asn-len.h"
37
38
39 /*
40 * Encodes the given length to the given buffer.
41 * returns the number of octets written to the buffer.
42 */
43 AsnLen
44 BEncDefLen (BUF_TYPE b, AsnLen len)
45 {
46 /*
47 * unrolled for efficiency
48 * (check each possibitlity of the 4 byte integer)
49 */
50 if (len < 128)
51 {
52 b.PutByteRvs (len);
53 return 1;
54 }
55 else if (len < 256)
56 {
57 b.PutByteRvs (len);
58 b.PutByteRvs (0x81);
59 return 2;
60 }
61 else if (len < 65536)
62 {
63 b.PutByteRvs (len);
64 b.PutByteRvs (len >> 8);
65 b.PutByteRvs (0x82);
66 return 3;
67 }
68 else if (len < 16777126)
69 {
70 b.PutByteRvs (len);
71 b.PutByteRvs (len >> 8);
72 b.PutByteRvs (len >> 16);
73 b.PutByteRvs (0x83);
74 return 4;
75 }
76 else
77 {
78 b.PutByteRvs (len);
79 b.PutByteRvs (len >> 8);
80 b.PutByteRvs (len >> 16);
81 b.PutByteRvs (len >> 24);
82 b.PutByteRvs (0x84);
83 return 5;
84 }
85 } /* EncodeDefLen */
86
87 /*
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
91 */
92 AsnLen
93 BDecLen (BUF_TYPE b, AsnLen &bytesDecoded, ENV_TYPE env)
94 {
95 AsnLen len;
96 unsigned char byte;
97 unsigned lenBytes;
98
99 byte = b.GetByte();
100
101 if (b.ReadError())
102 {
103 Asn1Error << "BDecLen: decoded past end of data" << endl;
104 #if SNACC_EXCEPTION_ENABLE
105 SnaccExcep::throwMe(-9);
106 #else
107 longjmp (env, -9);
108 #endif
109 }
110
111 bytesDecoded++;
112 if (byte < 128) /* short length */
113 return byte;
114
115 else if (byte == (unsigned char) 0x080) /* indef len indicator */
116 return INDEFINITE_LEN;
117
118 else /* long len form */
119 {
120 /*
121 * strip high bit to get # bytes left in len
122 */
123 lenBytes = byte & (unsigned char) 0x7f;
124
125 if (lenBytes > sizeof (long int))
126 {
127 Asn1Error << "BDecLen: ERROR - length overflow" << endl;
128 #if SNACC_EXCEPTION_ENABLE
129 SnaccExcep::throwMe(-10);
130 #else
131 longjmp (env, -10);
132 #endif
133 }
134
135 bytesDecoded += lenBytes;
136
137 for (len = 0; lenBytes > 0; lenBytes--)
138 len = (len << 8) | (unsigned long int) b.GetByte();
139
140
141 if (b.ReadError())
142 {
143 Asn1Error << "BDecLen: decoded past end of data" << endl;
144 #if SNACC_EXCEPTION_ENABLE
145 SnaccExcep::throwMe(-11);
146 #else
147 longjmp (env, -11);
148 #endif
149 }
150
151 return len;
152 }
153 /* not reached */
154 }
155
156
157 /*
158 * Encodes an End of Contents (EOC) to the given buffer.
159 * Returns the encoded length.
160 */
161 AsnLen
162 BEncEoc (BUF_TYPE b)
163 {
164
165 b.PutByteRvs (0);
166 b.PutByteRvs (0);
167 return 2;
168 } /* BEncEoc */
169
170 /*
171 * Decodes an EOC from the given buffer. flags an error if the
172 * octets are non-zero or if read error occured.
173 */
174 void
175 BDecEoc (BUF_TYPE b, AsnLen &bytesDecoded, ENV_TYPE env)
176 {
177
178 if ((b.GetByte() != 0) || (b.GetByte() != 0) || b.ReadError())
179 {
180 Asn1Error << "BDecEoc: ERROR - non zero byte in EOC or end of data reached" << endl;
181 #if SNACC_EXCEPTION_ENABLE
182 SnaccExcep::throwMe(-12);
183 #else
184 longjmp (env, -12);
185 #endif
186 }
187 bytesDecoded += 2;
188 } /* BDecEoc */