]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/asn-tag.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_tag.c - BER encode, decode and untility routines for ASN.1 Tags.
23 * Copyright (C) 1992 Michael Sample and the University of British Columbia
25 * This library is free software; you can redistribute it and/or
26 * modify it provided that this copyright/license information is retained
29 * If you modify this file, you must clearly indicate your changes.
31 * This source code is distributed in the hope that it will be
32 * useful, but WITHOUT ANY WARRANTY; without even the implied warranty
33 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
35 * $Header: /cvs/Darwin/src/live/Security/SecuritySNACCRuntime/c-lib/src/asn-tag.c,v 1.1.1.1 2001/05/18 23:14:08 mb Exp $
37 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
38 * Move from private repository to open source repository
40 * Revision 1.2 2001/05/05 00:59:25 rmurphy
41 * Adding darwin license headers
43 * Revision 1.1.1.1 1999/03/16 18:06:32 aram
44 * Originals from SMIME Free Library.
46 * Revision 1.5 1997/09/03 12:11:41 wan
47 * Patch to tag decoding for tags > 2^14 (thanks to Enrico Badella)
48 * Patch to TblEncTag to emit final 0x00 if previous octet signals continuation
50 * Revision 1.4 1997/03/13 09:15:18 wan
51 * Improved dependency generation for stupid makedepends.
52 * Corrected PeekTag to peek into buffer only as far as necessary.
53 * Added installable error handler.
54 * Fixed small glitch in idl-code generator (Markku Savela <msa@msa.tte.vtt.fi>).
56 * Revision 1.3 1997/02/28 13:39:50 wan
57 * Modifications collected for new version 1.3: Bug fixes, tk4.2.
59 * Revision 1.2 1995/07/27 09:01:25 rj
60 * merged PeekTag(), a function used only by the type table code.
62 * changed `_' to `-' in file names.
64 * Revision 1.1 1994/08/28 09:46:01 rj
65 * first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
69 #include "asn-config.h"
75 * Returns an AsnTag. An AsnTag is simply an encoded tag
76 * shifted to fill up an unsigned long int (first tag byte
77 * in most sig byte of long int)
78 * This rep permits easy case stmt comparison of tags.
79 * NOTE: The unsigned long rep for tag BREAKS if the
80 * the tag's code is over 2^21 (very unlikely)
82 * RETURNS 0 if decoded a 0 byte (ie first byte of an EOC)
85 BDecTag
PARAMS ((b
, bytesDecoded
, env
),
87 AsnLen
*bytesDecoded _AND_
94 tagId
= ((AsnTag
)BufGetByte (b
)) << ((sizeof (AsnTag
)-1)*8);
97 /* check if long tag format (ie code > 31) */
98 if ((tagId
& (((AsnTag
) 0x1f) << ((sizeof (AsnTag
)-1)*8))) == (((AsnTag
)0x1f) << ((sizeof (AsnTag
)-1)*8)))
103 tmpTagId
= (AsnTag
) BufGetByte (b
);
104 tagId
|= (tmpTagId
<< ((sizeof (AsnTag
)-i
)*8));
108 while ((tmpTagId
& (AsnTag
)0x80) && (i
<= sizeof (AsnTag
)));
111 * check for tag that is too long
113 if (i
> (sizeof (AsnTag
)+1))
115 Asn1Error ("BDecTag: ERROR - tag value overflow\n");
120 if (BufReadError (b
))
122 Asn1Error ("BDecTag: ERROR - decoded past the end of data\n");
132 AsnTag PeekTag
PARAMS ((b
, env
),
136 AsnTag tagId
, tmpTagId
;
138 unsigned char buf
[sizeof(AsnTag
)];
139 unsigned char* p
= buf
;
142 * peek/copy the next (max size of tag) bytes
143 * to get the tag info. The Peek buffer routines
144 * were added to the standard set for this function.
147 BufPeekCopy ((char*)buf
, b
, 1);
148 tagId
= ((AsnTag
)*p
++) << ((sizeof (AsnTag
)-1)*8);
150 /* check if long tag format (ie code > 31) */
151 if ((tagId
& (((AsnTag
) 0x1f) << ((sizeof (AsnTag
)-1)*8))) == (((AsnTag
)0x1f) << ((sizeof (AsnTag
)-1)*8)))
156 BufPeekCopy ((char*)buf
, b
, i
);
157 tmpTagId
= (AsnTag
) *p
++;
158 tagId
|= (tmpTagId
<< ((sizeof (AsnTag
)-i
)*8));
161 while ((tmpTagId
& (AsnTag
)0x80) && (i
<= sizeof (AsnTag
)));
164 * check for tag that is too long
166 if (i
> (sizeof (AsnTag
)+1))
168 Asn1Error ("BDecTag: ERROR - tag value overflow\n");
169 longjmp (env
, -1004);