]> git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c-lib/src/asn-tag.c
Security-54.1.3.tar.gz
[apple/security.git] / SecuritySNACCRuntime / c-lib / src / asn-tag.c
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 /*
20 * asn_tag.c - BER encode, decode and untility routines for ASN.1 Tags.
21 *
22 * MS 92
23 * Copyright (C) 1992 Michael Sample and the University of British Columbia
24 *
25 * This library is free software; you can redistribute it and/or
26 * modify it provided that this copyright/license information is retained
27 * in original form.
28 *
29 * If you modify this file, you must clearly indicate your changes.
30 *
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.
34 *
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 $
36 * $Log: asn-tag.c,v $
37 * Revision 1.1.1.1 2001/05/18 23:14:08 mb
38 * Move from private repository to open source repository
39 *
40 * Revision 1.2 2001/05/05 00:59:25 rmurphy
41 * Adding darwin license headers
42 *
43 * Revision 1.1.1.1 1999/03/16 18:06:32 aram
44 * Originals from SMIME Free Library.
45 *
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
49 *
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>).
55 *
56 * Revision 1.3 1997/02/28 13:39:50 wan
57 * Modifications collected for new version 1.3: Bug fixes, tk4.2.
58 *
59 * Revision 1.2 1995/07/27 09:01:25 rj
60 * merged PeekTag(), a function used only by the type table code.
61 *
62 * changed `_' to `-' in file names.
63 *
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.
66 *
67 */
68
69 #include "asn-config.h"
70 #include "asn-len.h"
71 #include "asn-tag.h"
72
73
74 /*
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)
81 *
82 * RETURNS 0 if decoded a 0 byte (ie first byte of an EOC)
83 */
84 AsnTag
85 BDecTag PARAMS ((b, bytesDecoded, env),
86 BUF_TYPE b _AND_
87 AsnLen *bytesDecoded _AND_
88 jmp_buf env)
89 {
90 AsnTag tagId;
91 AsnTag tmpTagId;
92 int i;
93
94 tagId = ((AsnTag)BufGetByte (b)) << ((sizeof (AsnTag)-1)*8);
95 (*bytesDecoded)++;
96
97 /* check if long tag format (ie code > 31) */
98 if ((tagId & (((AsnTag) 0x1f) << ((sizeof (AsnTag)-1)*8))) == (((AsnTag)0x1f) << ((sizeof (AsnTag)-1)*8)))
99 {
100 i = 2;
101 do
102 {
103 tmpTagId = (AsnTag) BufGetByte (b);
104 tagId |= (tmpTagId << ((sizeof (AsnTag)-i)*8));
105 (*bytesDecoded)++;
106 i++;
107 }
108 while ((tmpTagId & (AsnTag)0x80) && (i <= sizeof (AsnTag)));
109
110 /*
111 * check for tag that is too long
112 */
113 if (i > (sizeof (AsnTag)+1))
114 {
115 Asn1Error ("BDecTag: ERROR - tag value overflow\n");
116 longjmp (env, -25);
117 }
118 }
119
120 if (BufReadError (b))
121 {
122 Asn1Error ("BDecTag: ERROR - decoded past the end of data\n");
123 longjmp (env, -26);
124 }
125
126 return tagId;
127
128 } /* BDecTag */
129
130
131 #if TTBL
132 AsnTag PeekTag PARAMS ((b, env),
133 BUF_TYPE b _AND_
134 ENV_TYPE env)
135 {
136 AsnTag tagId, tmpTagId;
137 int i;
138 unsigned char buf[sizeof(AsnTag)];
139 unsigned char* p = buf;
140
141 /*
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.
145 */
146
147 BufPeekCopy ((char*)buf, b, 1);
148 tagId = ((AsnTag)*p++) << ((sizeof (AsnTag)-1)*8);
149
150 /* check if long tag format (ie code > 31) */
151 if ((tagId & (((AsnTag) 0x1f) << ((sizeof (AsnTag)-1)*8))) == (((AsnTag)0x1f) << ((sizeof (AsnTag)-1)*8)))
152 {
153 i = 2;
154 do
155 {
156 BufPeekCopy ((char*)buf, b, i);
157 tmpTagId = (AsnTag) *p++;
158 tagId |= (tmpTagId << ((sizeof (AsnTag)-i)*8));
159 i++;
160 }
161 while ((tmpTagId & (AsnTag)0x80) && (i <= sizeof (AsnTag)));
162
163 /*
164 * check for tag that is too long
165 */
166 if (i > (sizeof (AsnTag)+1))
167 {
168 Asn1Error ("BDecTag: ERROR - tag value overflow\n");
169 longjmp (env, -1004);
170 }
171 }
172
173 return tagId;
174
175 } /* PeekTag */
176 #endif /* TTBL */