]>
git.saurik.com Git - apple/security.git/blob - SecuritySNACCRuntime/c++-lib/c++/asn-octs.cpp
250b198fb9f8c788df698a792a947547ef9f3d32
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-octs.C - ASN.1 OCTET STRING methods for AsnOcts Class
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/Security/SecuritySNACCRuntime/c++-lib/c++/asn-octs.cpp,v 1.3 2002/03/21 05:38:44 dmitch Exp $
36 // $Log: asn-octs.cpp,v $
37 // Revision 1.3 2002/03/21 05:38:44 dmitch
38 // Radar 2868524: no more setjmp/longjmp in SNACC-generated code.
40 // Revision 1.2.44.1 2002/03/20 00:36:49 dmitch
41 // Radar 2868524: SNACC-generated code now uses throw/catch instead of setjmp/longjmp.
43 // Revision 1.2 2001/06/27 23:09:15 dmitch
44 // Pusuant to Radar 2664258, avoid all cerr-based output in NDEBUG configuration.
46 // Revision 1.1.1.1 2001/05/18 23:14:06 mb
47 // Move from private repository to open source repository
49 // Revision 1.3 2001/05/05 00:59:17 rmurphy
50 // Adding darwin license headers
52 // Revision 1.2 2000/12/07 22:16:14 dmitch
53 // Thread-safe mods: added a local StrStk strStkG to the routines which need it.
55 // Revision 1.1 2000/06/15 18:44:57 dmitch
56 // These snacc-generated source files are now checked in to allow cross-platform build.
58 // Revision 1.2 2000/06/08 20:05:35 dmitch
59 // Mods for X port. These files are actually machine generated and probably don't need to be in CVS....
61 // Revision 1.1.1.1 2000/03/09 01:00:06 rmurphy
62 // Base Fortissimo Tree
64 // Revision 1.4 1999/04/08 21:21:25 aram
65 // Fixed AsnOcts destructor.
67 // Revision 1.3 1999/03/21 02:07:36 mb
68 // Added Copy to every AsnType.
70 // Revision 1.2 1999/02/26 00:23:40 mb
73 // Revision 1.1 1999/02/25 05:21:53 mb
74 // Added snacc c++ library
76 // Revision 1.6 1997/02/28 13:39:45 wan
77 // Modifications collected for new version 1.3: Bug fixes, tk4.2.
79 // Revision 1.5 1995/08/17 15:33:33 rj
80 // augment binify()'s error message
82 // Revision 1.4 1995/07/24 20:23:32 rj
83 // use memcmpeq that is defined in .../snacc.h to use either memcmp or bcmp.
85 // code extracted from AsnOcts::TclGetVal and AsnOcts::TclSetVal into ::debinify and ::binify in meta.C
87 // #if TCL ... #endif wrapped into #if META ... #endif
89 // call constructor with additional pdu and create arguments.
91 // changed `_' to `-' in file names.
93 // Revision 1.3 1994/10/08 04:18:27 rj
94 // code for meta structures added (provides information about the generated code itself).
96 // code for Tcl interface added (makes use of the above mentioned meta code).
98 // virtual inline functions (the destructor, the Clone() function, BEnc(), BDec() and Print()) moved from inc/*.h to src/*.C because g++ turns every one of them into a static non-inline function in every file where the .h file gets included.
100 // made Print() const (and some other, mainly comparison functions).
102 // several `unsigned long int' turned into `size_t'.
104 // Revision 1.2 1994/08/28 10:01:16 rj
105 // comment leader fixed.
107 // Revision 1.1 1994/08/28 09:21:05 rj
108 // first check-in. for a list of changes to the snacc-1.1 distribution please refer to the ChangeLog.
110 #include "asn-config.h"
113 #include "asn-type.h"
114 #include "asn-bits.h" /* for TO_HEX */
115 #include "asn-octs.h"
119 extern StrStk strStkG
;
127 AsnType
* AsnOcts :: Clone () const
132 AsnType
* AsnOcts :: Copy () const
134 return new AsnOcts (* this );
137 // Initialize the AsnOcts with a char * and length.
138 // copies the string str.
139 void AsnOcts :: Set ( const char * str
, size_t len
)
143 octs
= new char [ octetLen
];
145 octs
= ( char *) mem_mgr_ptr
-> Get ( octetLen
+ 1 ); /* Guido Grassel, 11.8.93 */
146 #endif /* _IBM_ENC_ */
147 memcpy ( octs
, str
, octetLen
);
150 // Initialize the AsnOcts from another AsnOcts
151 // copies the string from o.
152 void AsnOcts :: Set ( const AsnOcts
& o
)
154 if (& o
!= this ) // avoid o = o;
156 octetLen
= o
. octetLen
;
158 octs
= new char [ octetLen
];
160 octs
= ( char *) mem_mgr_ptr
-> Get ( octetLen
+ 1 ); /* Guido Grassel, 11.8.93 */
161 #endif /* _IBM_ENC_ */
162 memcpy ( octs
, o
. octs
, octetLen
);
166 // Initialize the AsnOcts from a Null terminated string.
167 // copies the string str.
168 void AsnOcts :: Set ( const char * str
)
170 octetLen
= strlen ( str
);
172 octs
= new char [ octetLen
];
174 octs
= ( char *) mem_mgr_ptr
-> Get ( octetLen
+ 1 ); /* Guido Grassel, 11.8.93 */
175 #endif /* _IBM_ENC_ */
176 memcpy ( octs
, str
, octetLen
);
180 // free old octs value and tehn set the new
181 // octs and octetLen values with the given char *and length.
182 // copies the string str.
183 void AsnOcts :: ReSet ( const char * str
, size_t len
)
191 mem_mgr_ptr
-> Put (( void *) octs
); /* Guido Grassel, 11.8.93 */
193 #endif /* _IBM_ENC_ */
197 // Free old octs value and then set the new
198 // octs and octetLen from the given AsnOcts
199 // copies the string in o.
200 void AsnOcts :: ReSet ( const AsnOcts
& o
)
202 if (& o
!= this ) // avoid s = s;
208 mem_mgr_ptr
-> Put (( void *) octs
); /* Guido Grassel, 11.8.93 */
210 #endif /* _IBM_ENC_ */
214 // Free old octs value and then set the new
215 // octs and octetLen values from a null terminated string.
216 // copies the string str.
217 void AsnOcts :: ReSet ( const char * str
)
225 mem_mgr_ptr
-> Put (( void *) octs
); /* Guido Grassel, 11.8.93 */
227 #endif /* _IBM_ENC_ */
232 // Prints the AsnOcts to the given ostream in Value Notation.
233 void AsnOcts :: Print ( ostream
& os
) const
238 for ( i
= 0 ; i
< octetLen
; i
++)
239 os
<< TO_HEX ( octs
[ i
] >> 4 ) << ( TO_HEX ( octs
[ i
]));
243 /* put printable parts in ASN.1 comment */
244 for ( i
= 0 ; i
< octetLen
; i
++)
246 if ( isspace ( octs
[ i
]))
247 os
<< " " ; /* newlines->space (so don't screw up ASN.1 comment) */
248 else if ( isprint ( octs
[ i
]))
255 } /* AsnOcts::Print */
258 AsnLen
AsnOcts :: BEncContent ( BUF_TYPE b
)
260 b
. PutSegRvs ( octs
, octetLen
);
264 // Decodes a BER OCTET STRING value and puts it in this object.
265 // Constructed OCTET STRINGs are always concatenated into primitive ones.
266 void AsnOcts :: BDecContent ( BUF_TYPE b
, AsnTag tagId
, AsnLen elmtLen
, AsnLen
& bytesDecoded
, ENV_TYPE env
)
269 * tagId is encoded tag shifted into long int.
270 * if CONS bit is set then constructed octet string
272 if ( tagId
& 0x20000000 )
273 BDecConsOcts ( b
, elmtLen
, bytesDecoded
, env
);
275 else /* primitive octet string */
279 octs
= Asn1Alloc ( elmtLen
+ 1 );
281 octs
= ( char *) mem_mgr_ptr
-> Get ( elmtLen
+ 1 ); /* Guido Grassel, 11.8.93 */
282 #endif /* _IBM_ENC_ */
283 b
. CopyOut ( octs
, elmtLen
);
287 Asn1Error
<< "BDecOctetString: ERROR - decoded past end of data" << endl
;
288 #if SNACC_EXCEPTION_ENABLE
289 SnaccExcep :: throwMe (- 14 );
295 /* add null terminator - this is not included in the str's len */
296 octs
[ elmtLen
] = '\0' ;
297 bytesDecoded
+= elmtLen
;
300 } /* AsnOcts::BDecContent */
302 AsnLen
AsnOcts :: BEnc ( BUF_TYPE b
)
306 l
+= BEncDefLen ( b
, l
);
307 l
+= BEncTag1 ( b
, UNIV
, PRIM
, OCTETSTRING_TAG_CODE
);
311 void AsnOcts :: BDec ( BUF_TYPE b
, AsnLen
& bytesDecoded
, ENV_TYPE env
)
316 tag
= BDecTag ( b
, bytesDecoded
, env
);
317 if (( tag
!= MAKE_TAG_ID ( UNIV
, PRIM
, OCTETSTRING_TAG_CODE
)) &&
318 ( tag
!= MAKE_TAG_ID ( UNIV
, CONS
, OCTETSTRING_TAG_CODE
)))
320 Asn1Error
<< "AsnOcts::BDec: ERROR tag on OCTET STRING is wrong." << endl
;
321 #if SNACC_EXCEPTION_ENABLE
322 SnaccExcep :: throwMe (- 56 );
327 elmtLen
= BDecLen ( b
, bytesDecoded
, env
);
328 BDecContent ( b
, tag
, elmtLen
, bytesDecoded
, env
);
334 * Used for concatenating constructed OCTET STRING values when decoding
336 * fills string stack with references to the pieces of a
337 * construced octet string
341 FillOctetStringStk ( BUF_TYPE b
, AsnLen elmtLen0
, AsnLen
& bytesDecoded
, ENV_TYPE env
, StrStk
& strStkG
)
344 FillOctetStringStk ( BUF_TYPE b
, AsnLen elmtLen0
, AsnLen
& bytesDecoded
, ENV_TYPE env
)
350 AsnLen totalElmtsLen1
= 0 ;
351 unsigned long int tagId1
;
354 for (; ( totalElmtsLen1
< elmtLen0
) || ( elmtLen0
== INDEFINITE_LEN
);)
356 tagId1
= BDecTag ( b
, totalElmtsLen1
, env
);
358 if (( tagId1
== EOC_TAG_ID
) && ( elmtLen0
== INDEFINITE_LEN
))
360 BDEC_2ND_EOC_OCTET ( b
, totalElmtsLen1
, env
);
364 elmtLen1
= BDecLen ( b
, totalElmtsLen1
, env
);
365 if ( tagId1
== MAKE_TAG_ID ( UNIV
, PRIM
, OCTETSTRING_TAG_CODE
))
368 * primitive part of string, put references to piece (s) in
375 strPtr
= b
. GetSeg (& refdLen
);
377 strStkG
. Push ( strPtr
, refdLen
);
378 totalRefdLen
+= refdLen
;
379 if ( totalRefdLen
== elmtLen1
)
380 break ; /* exit this while loop */
382 if ( refdLen
== 0 ) /* end of data */
384 Asn1Error
<< "BDecConsOctetString: ERROR - attempt to decode past end of data" << endl
;
385 #if SNACC_EXCEPTION_ENABLE
386 SnaccExcep :: throwMe (- 15 );
391 refdLen
= elmtLen1
- totalRefdLen
;
393 totalElmtsLen1
+= elmtLen1
;
397 else if ( tagId1
== MAKE_TAG_ID ( UNIV
, CONS
, OCTETSTRING_TAG_CODE
))
400 * constructed octets string embedding in this constructed
401 * octet string. decode it.
404 FillOctetStringStk ( b
, elmtLen1
, totalElmtsLen1
, env
,
407 FillOctetStringStk ( b
, elmtLen1
, totalElmtsLen1
, env
);
412 Asn1Error
<< "BDecConsOctetString: ERROR - decoded non-OCTET STRING tag inside a constructed OCTET STRING" << endl
;
413 #if SNACC_EXCEPTION_ENABLE
414 SnaccExcep :: throwMe (- 16 );
421 bytesDecoded
+= totalElmtsLen1
;
423 } /* AsnOcts::FillOctetStringStk */
428 * decodes a seq of universally tagged octets until either EOC is
429 * encountered or the given len decoded. Return them in a
430 * single concatenated octet string
432 void AsnOcts :: BDecConsOcts ( BUF_TYPE b
, AsnLen elmtLen
, AsnLen
& bytesDecoded
, ENV_TYPE env
)
435 StrStk
strStkG ( 128 , 64 );
440 * decode each piece of the octet string, puting
441 * an entry in the octet/bit string stack for each
444 FillOctetStringStk ( b
, elmtLen
, bytesDecoded
, env
, strStkG
);
446 FillOctetStringStk ( b
, elmtLen
, bytesDecoded
, env
);
449 /* alloc single str long enough for combined octetstring */
450 octetLen
= strStkG
. totalByteLen
;
453 octs
= Asn1Alloc ( octetLen
);
455 octs
= ( char *) mem_mgr_ptr
-> Get ( octetLen
); /* Guido Grassel, 11.8.93 */
456 #endif /* _IBM_ENC_ */
458 strStkG
. CopyOut ( octs
);
462 int AsnOcts :: OctsEquiv ( const AsnOcts
& o
) const
464 return o
. octetLen
== octetLen
&& ! memcmpeq ( o
. octs
, octs
, octetLen
);
469 const AsnOctsTypeDesc
AsnOcts :: _desc ( NULL
, NULL
, false , AsnTypeDesc :: OCTET_STRING
, NULL
);
471 const AsnTypeDesc
* AsnOcts :: _getdesc () const
478 int AsnOcts :: TclGetVal ( Tcl_Interp
* interp
) const
480 return debinify ( interp
, octs
, octetLen
);
483 int AsnOcts :: TclSetVal ( Tcl_Interp
* interp
, const char * valstr
)
490 buf
= new char [ strlen ( valstr
)];
492 buf
= ( char *) mem_mgr_ptr
-> Get ( strlen ( valstr
)+ 1 );
493 #endif /* _IBM_ENC_ */
497 Tcl_AppendResult ( interp
, "Malloc failed for type " , _getdesc ()-> getmodule ()-> name
, "." , _getdesc ()-> getname (), NULL
);
500 if ( binify ( interp
, valstr
, buf
, & len
) != TCL_OK
)
502 Tcl_AppendResult ( interp
, " for type " , _getdesc ()-> getmodule ()-> name
, "." , _getdesc ()-> getname (), NULL
);
511 mem_mgr_ptr
-> Put (( void *) buf
);
512 #endif /* _IBM_ENC_ */