]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/asn-int.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_int.c - BER encode, decode, print and free routines for the
24 * Copyright (C) 1992 Michael Sample and the University of British Columbia
26 * This library is free software; you can redistribute it and/or
27 * modify it provided that this copyright/license information is retained
30 * If you modify this file, you must clearly indicate your changes.
32 * This source code is distributed in the hope that it will be
33 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
34 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
36 * $Header: /cvs/root/Security/SecuritySNACCRuntime/c-lib/src/Attic/asn-int.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
38 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
39 * Move from private repository to open source repository
41 * Revision 1.2 2001/05/05 00:59:25 rmurphy
42 * Adding darwin license headers
44 * Revision 1.1.1.1 1999/03/16 18:06:30 aram
45 * Originals from SMIME Free Library.
47 * Revision 1.3 1995/07/24 21:04:51 rj
48 * changed `_' to `-' in file names.
50 * Revision 1.2 1994/09/01 00:05:05 rj
51 * reduce the risk of unwanted surprises with macro expansion by properly separating the C tokens.
53 * Revision 1.1 1994/08/28 09:45:53 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"
64 * encodes universal TAG LENGTH and Contents of and ASN.1 INTEGER
67 BEncAsnInt
PARAMS ((b
, data
),
73 len
= BEncAsnIntContent (b
, data
);
74 len
+= BEncDefLen (b
, len
);
75 len
+= BEncTag1 (b
, UNIV
, PRIM
, INTEGER_TAG_CODE
);
81 * decodes universal TAG LENGTH and Contents of and ASN.1 INTEGER
84 BDecAsnInt
PARAMS ((b
, result
, bytesDecoded
, env
),
87 AsnLen
*bytesDecoded _AND_
93 if ((tag
= BDecTag (b
, bytesDecoded
, env
)) != MAKE_TAG_ID (UNIV
, PRIM
, INTEGER_TAG_CODE
))
95 Asn1Error ("BDecAsnInt: ERROR wrong tag on INTEGER.\n");
99 elmtLen
= BDecLen (b
, bytesDecoded
, env
);
100 BDecAsnIntContent (b
, tag
, elmtLen
, result
, bytesDecoded
, env
);
106 * encodes signed long integer's contents
109 BEncAsnIntContent
PARAMS ((b
, data
),
115 unsigned long int mask
;
116 unsigned long int dataCpy
;
118 #define INT_MASK (0x7f80 << ((sizeof(AsnInt) - 2) * 8))
123 * calculate encoded length of the integer (content)
126 if ((long int)dataCpy
< 0)
127 for (len
= sizeof (AsnInt
); len
> 1; --len
)
129 if ((dataCpy
& mask
) == mask
)
135 for (len
= sizeof (AsnInt
); len
> 1; --len
)
137 if ((dataCpy
& mask
) == 0)
144 * write the BER integer
146 for (i
= 0; i
< len
; i
++)
148 BufPutByteRvs (b
, dataCpy
);
154 } /* BEncAsnIntContent */
158 * Decodes content of BER a INTEGER value. The given tag is ignored.
161 BDecAsnIntContent
PARAMS ((b
, tagId
, len
, result
, bytesDecoded
, env
),
166 AsnLen
*bytesDecoded _AND_
171 unsigned long int byte
;
174 if (len
> sizeof (AsnInt
))
176 Asn1Error ("BDecAsnIntContent: ERROR - integer to big to decode.\n");
181 * look at integer value
183 byte
= (unsigned long int) BufGetByte (b
);
185 if (byte
& 0x80) /* top bit of first byte is sign bit */
186 retVal
= (-1 << 8) | byte
;
191 * write from buffer into long int
193 for (i
= 1; i
< len
; i
++)
194 retVal
= (retVal
<< 8) | (unsigned long int)(BufGetByte (b
));
196 if (BufReadError (b
))
198 Asn1Error ("BDecAsnIntContent: ERROR - decoded past end of data \n");
201 (*bytesDecoded
) += len
;
205 } /* BDecAsnIntContent */
209 * Prints the given integer to the given FILE * in Value Notation.
213 PrintAsnInt
PARAMS ((f
, v
, indent
),
216 unsigned short int indent
)
218 fprintf (f
,"%d", *v
);
223 * The following deal with UNSIGNED long ints.
224 * They do the same as the above routines for unsigned values.
226 * The compiler generated code does not call them. (It should
227 * based on subtype info but it does not).
232 * encodes universal TAG LENGTH and Contents of and ASN.1 INTEGER
235 BEncUAsnInt
PARAMS ((b
, data
),
241 len
= BEncUAsnIntContent (b
, data
);
242 len
+= BEncDefLen (b
, len
);
243 len
+= BEncTag1 (b
, UNIV
, PRIM
, INTEGER_TAG_CODE
);
249 * decodes universal TAG LENGTH and Contents of and ASN.1 INTEGER
252 BDecUAsnInt
PARAMS ((b
, result
, bytesDecoded
, env
),
254 UAsnInt
*result _AND_
255 AsnLen
*bytesDecoded _AND_
261 if ((tag
= BDecTag (b
, bytesDecoded
, env
)) != MAKE_TAG_ID (UNIV
, PRIM
, INTEGER_TAG_CODE
))
263 Asn1Error ("BDecAsnInt: ERROR wrong tag on INTGER.\n");
267 elmtLen
= BDecLen (b
, bytesDecoded
, env
);
268 BDecUAsnIntContent (b
, tag
, elmtLen
, result
, bytesDecoded
, env
);
274 * encodes unsigned long integer. This allows you to correctly
275 * handle unsiged values that used the most significant (sign) bit.
278 BEncUAsnIntContent
PARAMS ((b
, data
),
284 unsigned long int mask
;
285 unsigned long int dataCpy
;
290 * calculate encoded length of the integer (content)
293 if ((long int)dataCpy
< 0)
295 /*write integer as normal (remember writing in reverse) */
296 for (i
= 0; i
< sizeof (UAsnInt
); i
++)
298 BufPutByteRvs (b
, dataCpy
);
302 * write zero byte at beginning of int, since high bit
303 * is set and need to differentiate between sign
304 * bit and high bit in unsigned case.
305 * (this code follows the prev for loop since writing
308 BufPutByteRvs (b
, 0);
310 return sizeof (UAsnInt
)+1;
314 for (len
= sizeof (UAsnInt
); len
> 1; --len
)
316 if ((dataCpy
& mask
) == 0)
322 /* write the BER integer */
323 for (i
= 0; i
< len
; i
++)
325 BufPutByteRvs (b
, dataCpy
);
331 } /* BEncUAsnIntContent */
335 * decode integer portion - no tag or length expected or decoded
336 * assumes unsigned integer - This routine is useful for
337 * integer subtyped to > 0 eg Guage ::= INTEGER (0..4294967295)
340 BDecUAsnIntContent
PARAMS ((b
, tag
, len
, result
, bytesDecoded
, env
),
344 UAsnInt
*result _AND_
345 AsnLen
*bytesDecoded _AND_
349 unsigned long int retVal
;
351 retVal
= (unsigned long int) BufGetByte (b
);
353 if (len
> (sizeof (UAsnInt
)+1))
355 Asn1Error ("BDecUAsnIntContent: ERROR - integer to big to decode.\n");
358 else if (retVal
& 0x80) /* top bit of first byte is sign bit */
360 Asn1Error ("BDecUAsnIntContent: ERROR - integer is negative.\n");
363 else if ((len
== (sizeof (UAsnInt
)+1)) && (retVal
!= 0))
366 * first octet must be zero 5 octets long - extra 0 octet
367 * at beginning is only used for value > 0 that need the
370 Asn1Error ("BDecUAsnIntContent: ERROR - integer is negative.\n");
375 * write from buffer into long int
377 for (i
= 1; i
< len
; i
++)
378 retVal
= (retVal
<< 8) | (unsigned long int)(BufGetByte (b
));
380 if (BufReadError (b
))
382 Asn1Error ("BDecUIntegerContent: ERROR - decoded past end of data\n");
385 (*bytesDecoded
) += len
;
389 } /* BDecUAsnIntContent */
393 PrintUAsnInt
PARAMS ((f
, v
, indent
),
396 unsigned short int indent
)
398 fprintf (f
, "%u", *v
);