security/nss/lib/util/secasn1u.c

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/security/nss/lib/util/secasn1u.c	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,97 @@
     1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.7 +
     1.8 +/*
     1.9 + * Utility routines to complement the ASN.1 encoding and decoding functions.
    1.10 + */
    1.11 +
    1.12 +#include "secasn1.h"
    1.13 +
    1.14 +
    1.15 +/*
    1.16 + * We have a length that needs to be encoded; how many bytes will the
    1.17 + * encoding take?
    1.18 + *
    1.19 + * The rules are that 0 - 0x7f takes one byte (the length itself is the
    1.20 + * entire encoding); everything else takes one plus the number of bytes
    1.21 + * in the length.
    1.22 + */
    1.23 +int
    1.24 +SEC_ASN1LengthLength (unsigned long len)
    1.25 +{
    1.26 +    int lenlen = 1;
    1.27 +
    1.28 +    if (len > 0x7f) {
    1.29 +	do {
    1.30 +	    lenlen++;
    1.31 +	    len >>= 8;
    1.32 +	} while (len);
    1.33 +    }
    1.34 +
    1.35 +    return lenlen;
    1.36 +}
    1.37 +
    1.38 +
    1.39 +/*
    1.40 + * XXX Move over (and rewrite as appropriate) the rest of the
    1.41 + * stuff in dersubr.c!
    1.42 + */
    1.43 +
    1.44 +
    1.45 +/*
    1.46 + * Find the appropriate subtemplate for the given template.
    1.47 + * This may involve calling a "chooser" function, or it may just
    1.48 + * be right there.  In either case, it is expected to *have* a
    1.49 + * subtemplate; this is asserted in debug builds (in non-debug
    1.50 + * builds, NULL will be returned).
    1.51 + *
    1.52 + * "thing" is a pointer to the structure being encoded/decoded
    1.53 + * "encoding", when true, means that we are in the process of encoding
    1.54 + *	(as opposed to in the process of decoding)
    1.55 + */
    1.56 +const SEC_ASN1Template *
    1.57 +SEC_ASN1GetSubtemplate (const SEC_ASN1Template *theTemplate, void *thing,
    1.58 +			PRBool encoding)
    1.59 +{
    1.60 +    const SEC_ASN1Template *subt = NULL;
    1.61 +
    1.62 +    PORT_Assert (theTemplate->sub != NULL);
    1.63 +    if (theTemplate->sub != NULL) {
    1.64 +	if (theTemplate->kind & SEC_ASN1_DYNAMIC) {
    1.65 +	    SEC_ASN1TemplateChooserPtr chooserp;
    1.66 +
    1.67 +	    chooserp = *(SEC_ASN1TemplateChooserPtr *) theTemplate->sub;
    1.68 +	    if (chooserp) {
    1.69 +		if (thing != NULL)
    1.70 +		    thing = (char *)thing - theTemplate->offset;
    1.71 +		subt = (* chooserp)(thing, encoding);
    1.72 +	    }
    1.73 +	} else {
    1.74 +	    subt = (SEC_ASN1Template*)theTemplate->sub;
    1.75 +	}
    1.76 +    }
    1.77 +    return subt;
    1.78 +}
    1.79 +
    1.80 +PRBool SEC_ASN1IsTemplateSimple(const SEC_ASN1Template *theTemplate)
    1.81 +{
    1.82 +    if (!theTemplate) {
    1.83 +	return PR_TRUE; /* it doesn't get any simpler than NULL */
    1.84 +    }
    1.85 +    /* only templates made of one primitive type or a choice of primitive
    1.86 +       types are considered simple */
    1.87 +    if (! (theTemplate->kind & (~SEC_ASN1_TAGNUM_MASK))) {
    1.88 +	return PR_TRUE; /* primitive type */
    1.89 +    }
    1.90 +    if (!(theTemplate->kind & SEC_ASN1_CHOICE)) {
    1.91 +	return PR_FALSE; /* no choice means not simple */
    1.92 +    }
    1.93 +    while (++theTemplate && theTemplate->kind) {
    1.94 +	if (theTemplate->kind & (~SEC_ASN1_TAGNUM_MASK)) {
    1.95 +	    return PR_FALSE; /* complex type */
    1.96 +	}
    1.97 +    }
    1.98 +    return PR_TRUE; /* choice of primitive types */
    1.99 +}
   1.100 +

mercurial