/src/nss/lib/util/secasn1u.c
| Line | Count | Source (jump to first uncovered line) | 
| 1 |  | /* This Source Code Form is subject to the terms of the Mozilla Public | 
| 2 |  |  * License, v. 2.0. If a copy of the MPL was not distributed with this | 
| 3 |  |  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | 
| 4 |  |  | 
| 5 |  | /* | 
| 6 |  |  * Utility routines to complement the ASN.1 encoding and decoding functions. | 
| 7 |  |  */ | 
| 8 |  |  | 
| 9 |  | #include "secasn1.h" | 
| 10 |  |  | 
| 11 |  | /* | 
| 12 |  |  * We have a length that needs to be encoded; how many bytes will the | 
| 13 |  |  * encoding take? | 
| 14 |  |  * | 
| 15 |  |  * The rules are that 0 - 0x7f takes one byte (the length itself is the | 
| 16 |  |  * entire encoding); everything else takes one plus the number of bytes | 
| 17 |  |  * in the length. | 
| 18 |  |  */ | 
| 19 |  | int | 
| 20 |  | SEC_ASN1LengthLength(unsigned long len) | 
| 21 | 0 | { | 
| 22 | 0 |     int lenlen = 1; | 
| 23 |  | 
 | 
| 24 | 0 |     if (len > 0x7f) { | 
| 25 | 0 |         do { | 
| 26 | 0 |             lenlen++; | 
| 27 | 0 |             len >>= 8; | 
| 28 | 0 |         } while (len); | 
| 29 | 0 |     } | 
| 30 |  | 
 | 
| 31 | 0 |     return lenlen; | 
| 32 | 0 | } | 
| 33 |  |  | 
| 34 |  | /* | 
| 35 |  |  * XXX Move over (and rewrite as appropriate) the rest of the | 
| 36 |  |  * stuff in dersubr.c! | 
| 37 |  |  */ | 
| 38 |  |  | 
| 39 |  | /* | 
| 40 |  |  * Find the appropriate subtemplate for the given template. | 
| 41 |  |  * This may involve calling a "chooser" function, or it may just | 
| 42 |  |  * be right there.  In either case, it is expected to *have* a | 
| 43 |  |  * subtemplate; this is asserted in debug builds (in non-debug | 
| 44 |  |  * builds, NULL will be returned). | 
| 45 |  |  * | 
| 46 |  |  * "thing" is a pointer to the structure being encoded/decoded | 
| 47 |  |  * "encoding", when true, means that we are in the process of encoding | 
| 48 |  |  *  (as opposed to in the process of decoding) | 
| 49 |  |  */ | 
| 50 |  | const SEC_ASN1Template * | 
| 51 |  | SEC_ASN1GetSubtemplate(const SEC_ASN1Template *theTemplate, void *thing, | 
| 52 |  |                        PRBool encoding) | 
| 53 | 45.8k | { | 
| 54 | 45.8k |     const SEC_ASN1Template *subt = NULL; | 
| 55 |  |  | 
| 56 | 45.8k |     PORT_Assert(theTemplate->sub != NULL); | 
| 57 | 45.8k |     if (theTemplate->sub != NULL) { | 
| 58 | 45.8k |         if (theTemplate->kind & SEC_ASN1_DYNAMIC) { | 
| 59 | 0 |             SEC_ASN1TemplateChooserPtr chooserp; | 
| 60 |  | 
 | 
| 61 | 0 |             chooserp = *(SEC_ASN1TemplateChooserPtr *)theTemplate->sub; | 
| 62 | 0 |             if (chooserp) { | 
| 63 | 0 |                 if (thing != NULL) | 
| 64 | 0 |                     thing = (char *)thing - theTemplate->offset; | 
| 65 | 0 |                 subt = (*chooserp)(thing, encoding); | 
| 66 | 0 |             } | 
| 67 | 45.8k |         } else { | 
| 68 | 45.8k |             subt = (SEC_ASN1Template *)theTemplate->sub; | 
| 69 | 45.8k |         } | 
| 70 | 45.8k |     } | 
| 71 | 45.8k |     return subt; | 
| 72 | 45.8k | } | 
| 73 |  |  | 
| 74 |  | PRBool | 
| 75 |  | SEC_ASN1IsTemplateSimple(const SEC_ASN1Template *theTemplate) | 
| 76 | 0 | { | 
| 77 | 0 |     if (!theTemplate) { | 
| 78 | 0 |         return PR_TRUE; /* it doesn't get any simpler than NULL */ | 
| 79 | 0 |     } | 
| 80 |  |     /* only templates made of one primitive type or a choice of primitive | 
| 81 |  |        types are considered simple */ | 
| 82 | 0 |     if (!(theTemplate->kind & (~SEC_ASN1_TAGNUM_MASK))) { | 
| 83 | 0 |         return PR_TRUE; /* primitive type */ | 
| 84 | 0 |     } | 
| 85 | 0 |     if (!(theTemplate->kind & SEC_ASN1_CHOICE)) { | 
| 86 | 0 |         return PR_FALSE; /* no choice means not simple */ | 
| 87 | 0 |     } | 
| 88 | 0 |     while (++theTemplate && theTemplate->kind) { | 
| 89 | 0 |         if (theTemplate->kind & (~SEC_ASN1_TAGNUM_MASK)) { | 
| 90 | 0 |             return PR_FALSE; /* complex type */ | 
| 91 | 0 |         } | 
| 92 | 0 |     } | 
| 93 | 0 |     return PR_TRUE; /* choice of primitive types */ | 
| 94 | 0 | } |