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