michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * Utility routines to complement the ASN.1 encoding and decoding functions. michael@0: */ michael@0: michael@0: #include "secasn1.h" michael@0: michael@0: michael@0: /* michael@0: * We have a length that needs to be encoded; how many bytes will the michael@0: * encoding take? michael@0: * michael@0: * The rules are that 0 - 0x7f takes one byte (the length itself is the michael@0: * entire encoding); everything else takes one plus the number of bytes michael@0: * in the length. michael@0: */ michael@0: int michael@0: SEC_ASN1LengthLength (unsigned long len) michael@0: { michael@0: int lenlen = 1; michael@0: michael@0: if (len > 0x7f) { michael@0: do { michael@0: lenlen++; michael@0: len >>= 8; michael@0: } while (len); michael@0: } michael@0: michael@0: return lenlen; michael@0: } michael@0: michael@0: michael@0: /* michael@0: * XXX Move over (and rewrite as appropriate) the rest of the michael@0: * stuff in dersubr.c! michael@0: */ michael@0: michael@0: michael@0: /* michael@0: * Find the appropriate subtemplate for the given template. michael@0: * This may involve calling a "chooser" function, or it may just michael@0: * be right there. In either case, it is expected to *have* a michael@0: * subtemplate; this is asserted in debug builds (in non-debug michael@0: * builds, NULL will be returned). michael@0: * michael@0: * "thing" is a pointer to the structure being encoded/decoded michael@0: * "encoding", when true, means that we are in the process of encoding michael@0: * (as opposed to in the process of decoding) michael@0: */ michael@0: const SEC_ASN1Template * michael@0: SEC_ASN1GetSubtemplate (const SEC_ASN1Template *theTemplate, void *thing, michael@0: PRBool encoding) michael@0: { michael@0: const SEC_ASN1Template *subt = NULL; michael@0: michael@0: PORT_Assert (theTemplate->sub != NULL); michael@0: if (theTemplate->sub != NULL) { michael@0: if (theTemplate->kind & SEC_ASN1_DYNAMIC) { michael@0: SEC_ASN1TemplateChooserPtr chooserp; michael@0: michael@0: chooserp = *(SEC_ASN1TemplateChooserPtr *) theTemplate->sub; michael@0: if (chooserp) { michael@0: if (thing != NULL) michael@0: thing = (char *)thing - theTemplate->offset; michael@0: subt = (* chooserp)(thing, encoding); michael@0: } michael@0: } else { michael@0: subt = (SEC_ASN1Template*)theTemplate->sub; michael@0: } michael@0: } michael@0: return subt; michael@0: } michael@0: michael@0: PRBool SEC_ASN1IsTemplateSimple(const SEC_ASN1Template *theTemplate) michael@0: { michael@0: if (!theTemplate) { michael@0: return PR_TRUE; /* it doesn't get any simpler than NULL */ michael@0: } michael@0: /* only templates made of one primitive type or a choice of primitive michael@0: types are considered simple */ michael@0: if (! (theTemplate->kind & (~SEC_ASN1_TAGNUM_MASK))) { michael@0: return PR_TRUE; /* primitive type */ michael@0: } michael@0: if (!(theTemplate->kind & SEC_ASN1_CHOICE)) { michael@0: return PR_FALSE; /* no choice means not simple */ michael@0: } michael@0: while (++theTemplate && theTemplate->kind) { michael@0: if (theTemplate->kind & (~SEC_ASN1_TAGNUM_MASK)) { michael@0: return PR_FALSE; /* complex type */ michael@0: } michael@0: } michael@0: return PR_TRUE; /* choice of primitive types */ michael@0: } michael@0: