LCOV - code coverage report
Current view: top level - third_party/heimdal/lib/asn1 - template.c (source / functions) Hit Total Coverage
Test: coverage report for vadcx-master-patch-75612 fe003de8 Lines: 909 1746 52.1 %
Date: 2024-02-29 22:57:05 Functions: 20 28 71.4 %

          Line data    Source code
       1             : /*
       2             :  * Copyright (c) 2009 Kungliga Tekniska Högskolan
       3             :  * (Royal Institute of Technology, Stockholm, Sweden).
       4             :  * All rights reserved.
       5             :  *
       6             :  * Portions Copyright (c) 2009 - 2010 Apple Inc. All rights reserved.
       7             :  *
       8             :  * Redistribution and use in source and binary forms, with or without
       9             :  * modification, are permitted provided that the following conditions
      10             :  * are met:
      11             :  *
      12             :  * 1. Redistributions of source code must retain the above copyright
      13             :  *    notice, this list of conditions and the following disclaimer.
      14             :  *
      15             :  * 2. Redistributions in binary form must reproduce the above copyright
      16             :  *    notice, this list of conditions and the following disclaimer in the
      17             :  *    documentation and/or other materials provided with the distribution.
      18             :  *
      19             :  * 3. Neither the name of the Institute nor the names of its contributors
      20             :  *    may be used to endorse or promote products derived from this software
      21             :  *    without specific prior written permission.
      22             :  *
      23             :  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
      24             :  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
      25             :  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
      26             :  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
      27             :  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
      28             :  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
      29             :  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
      30             :  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
      31             :  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
      32             :  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
      33             :  * SUCH DAMAGE.
      34             :  */
      35             : 
      36             : #include "der_locl.h"
      37             : #include <com_err.h>
      38             : #include <vis.h>
      39             : #include <vis-extras.h>
      40             : #include <heimbase.h>
      41             : 
      42             : #ifndef ENOTSUP
      43             : /* Very old MSVC CRTs don't have ENOTSUP */
      44             : #define ENOTSUP EINVAL
      45             : #endif
      46             : 
      47             : struct asn1_type_func asn1_template_prim[A1T_NUM_ENTRY] = {
      48             : #define el(name, type) {                                \
      49             :         (asn1_type_encode)der_put_##name,               \
      50             :         (asn1_type_decode)der_get_##name,               \
      51             :         (asn1_type_length)der_length_##name,            \
      52             :         (asn1_type_copy)der_copy_##name,                \
      53             :         (asn1_type_release)der_free_##name,             \
      54             :         (asn1_type_print)der_print_##name,              \
      55             :         sizeof(type)                                    \
      56             :     }
      57             : #define elber(name, type) {                             \
      58             :         (asn1_type_encode)der_put_##name,               \
      59             :         (asn1_type_decode)der_get_##name##_ber,         \
      60             :         (asn1_type_length)der_length_##name,            \
      61             :         (asn1_type_copy)der_copy_##name,                \
      62             :         (asn1_type_release)der_free_##name,             \
      63             :         (asn1_type_print)der_print_##name,              \
      64             :         sizeof(type)                                    \
      65             :     }
      66             :     el(integer, int),
      67             :     el(heim_integer, heim_integer),
      68             :     el(integer, int),
      69             :     el(integer64, int64_t),
      70             :     el(unsigned, unsigned),
      71             :     el(unsigned64, uint64_t),
      72             :     el(general_string, heim_general_string),
      73             :     el(octet_string, heim_octet_string),
      74             :     elber(octet_string, heim_octet_string),
      75             :     el(ia5_string, heim_ia5_string),
      76             :     el(bmp_string, heim_bmp_string),
      77             :     el(universal_string, heim_universal_string),
      78             :     el(printable_string, heim_printable_string),
      79             :     el(visible_string, heim_visible_string),
      80             :     el(utf8string, heim_utf8_string),
      81             :     el(generalized_time, time_t),
      82             :     el(utctime, time_t),
      83             :     el(bit_string, heim_bit_string),
      84             :     { (asn1_type_encode)der_put_boolean, (asn1_type_decode)der_get_boolean,
      85             :       (asn1_type_length)der_length_boolean, (asn1_type_copy)der_copy_integer,
      86             :       (asn1_type_release)der_free_integer, (asn1_type_print)der_print_boolean,
      87             :       sizeof(int)
      88             :     },
      89             :     el(oid, heim_oid),
      90             :     el(general_string, heim_general_string),
      91             : #undef el
      92             : #undef elber
      93             : };
      94             : 
      95             : size_t
      96   113344154 : _asn1_sizeofType(const struct asn1_template *t)
      97             : {
      98   113344154 :     return t->offset;
      99             : }
     100             : 
     101             : /*
     102             :  * Here is abstraction to not so well evil fact of bit fields in C,
     103             :  * they are endian dependent, so when getting and setting bits in the
     104             :  * host local structure we need to know the endianness of the host.
     105             :  *
     106             :  * Its not the first time in Heimdal this have bitten us, and some day
     107             :  * we'll grow up and use #defined constant, but bit fields are still
     108             :  * so pretty and shiny.
     109             :  */
     110             : 
     111             : static void
     112     5802248 : _asn1_bmember_get_bit(const unsigned char *p, void *data,
     113             :                       unsigned int bit, size_t size)
     114             : {
     115     5802248 :     unsigned int localbit = bit % 8;
     116     5802248 :     if ((*p >> (7 - localbit)) & 1) {
     117             : #ifdef WORDS_BIGENDIAN
     118             :         *(unsigned int *)data |= (1u << ((size * 8) - bit - 1));
     119             : #else
     120      968458 :         *(unsigned int *)data |= (1u << bit);
     121             : #endif
     122             :     }
     123     5594573 : }
     124             : 
     125             : int
     126     7008622 : _asn1_bmember_isset_bit(const void *data, unsigned int bit, size_t size)
     127             : {
     128             : #ifdef WORDS_BIGENDIAN
     129             :     if ((*(unsigned int *)data) & (1u << ((size * 8) - bit - 1)))
     130             :         return 1;
     131             :     return 0;
     132             : #else
     133     7008622 :     if ((*(unsigned int *)data) & (1u << bit))
     134     1176467 :         return 1;
     135     5626035 :     return 0;
     136             : #endif
     137             : }
     138             : 
     139             : void
     140     7008622 : _asn1_bmember_put_bit(unsigned char *p, const void *data, unsigned int bit,
     141             :                       size_t size, unsigned int *bitset)
     142             : {
     143     7008622 :     unsigned int localbit = bit % 8;
     144             : 
     145     7008622 :     if (_asn1_bmember_isset_bit(data, bit, size)) {
     146     1176467 :         *p |= (1u << (7 - localbit));
     147     1176467 :         if (*bitset == 0)
     148      430180 :             *bitset = (7 - localbit) + 1;
     149             :     }
     150     7008622 : }
     151             : 
     152             : /*
     153             :  * Utility function to tell us if the encoding of some type per its template
     154             :  * will have an outer tag.  This is needed when the caller wants to slap on an
     155             :  * IMPLICIT tag: if the inner type has a tag then we need to replace it.
     156             :  */
     157             : static int
     158        5397 : is_tagged(const struct asn1_template *t)
     159             : {
     160        5397 :     size_t elements = A1_HEADER_LEN(t);
     161             : 
     162        5397 :     t += A1_HEADER_LEN(t);
     163        5397 :     if (elements != 1)
     164           0 :         return 0;
     165        5397 :     switch (t->tt & A1_OP_MASK) {
     166           0 :     case A1_OP_SEQOF:       return 0;
     167           0 :     case A1_OP_SETOF:       return 0;
     168           0 :     case A1_OP_BMEMBER:     return 0;
     169           0 :     case A1_OP_PARSE:       return 0;
     170        3488 :     case A1_OP_TAG:         return 1;
     171           0 :     case A1_OP_CHOICE:      return 1;
     172        1909 :     case A1_OP_TYPE:        return 1;
     173           0 :     case A1_OP_TYPE_EXTERN: {
     174           0 :         const struct asn1_type_func *f = t->ptr;
     175             : 
     176             :         /*
     177             :          * XXX Add a boolean to struct asn1_type_func to tell us if the type is
     178             :          * tagged or not.  Basically, it's not tagged if it's primitive.
     179             :          */
     180           0 :         if (f->encode == (asn1_type_encode)encode_heim_any ||
     181           0 :             f->encode == (asn1_type_encode)encode_HEIM_ANY)
     182           0 :             return 0;
     183           0 :         abort(); /* XXX */
     184             :     }
     185           0 :     default: abort();
     186             :     }
     187             : }
     188             : 
     189             : static size_t
     190         861 : inner_type_taglen(const struct asn1_template *t)
     191             : {
     192         861 :     size_t elements = A1_HEADER_LEN(t);
     193             : 
     194         861 :     t += A1_HEADER_LEN(t);
     195         861 :     if (elements != 1)
     196           0 :         return 0;
     197         861 :     switch (t->tt & A1_OP_MASK) {
     198           0 :     case A1_OP_SEQOF:       return 0;
     199           0 :     case A1_OP_SETOF:       return 0;
     200           0 :     case A1_OP_BMEMBER:     return 0;
     201           0 :     case A1_OP_PARSE:       return 0;
     202           0 :     case A1_OP_CHOICE:      return 1;
     203         279 :     case A1_OP_TYPE:        return inner_type_taglen(t->ptr);
     204         582 :     case A1_OP_TAG:         return der_length_tag(A1_TAG_TAG(t->tt));
     205           0 :     case A1_OP_TYPE_EXTERN: {
     206           0 :         const struct asn1_type_func *f = t->ptr;
     207             : 
     208             :         /*
     209             :          * XXX Add a boolean to struct asn1_type_func to tell us if the type is
     210             :          * tagged or not.  Basically, it's not tagged if it's primitive.
     211             :          */
     212           0 :         if (f->encode == (asn1_type_encode)encode_heim_any ||
     213           0 :             f->encode == (asn1_type_encode)encode_HEIM_ANY)
     214           0 :             return 0;
     215           0 :         abort(); /* XXX */
     216             :     }
     217           0 :     default: abort();
     218             : #ifdef WIN32
     219             :              _exit(0); /* Quiet VC */
     220             : #endif
     221             :     }
     222             : }
     223             : 
     224             : /*
     225             :  * Compare some int of unknown size in a type ID field to the int value in
     226             :  * some IOS object's type ID template entry.
     227             :  *
     228             :  * This should be called with a `A1_TAG_T(ASN1_C_UNIV, PRIM, UT_Integer)'
     229             :  * template as the `ttypeid'.
     230             :  */
     231             : static int
     232           0 : typeid_int_cmp(const void *intp,
     233             :                int64_t i,
     234             :                const struct asn1_template *ttypeid)
     235             : {
     236           0 :     const struct asn1_template *tint = ttypeid->ptr;
     237             : 
     238           0 :     if ((tint[1].tt & A1_OP_MASK) != A1_OP_PARSE)
     239           0 :         return -1;
     240           0 :     if (A1_PARSE_TYPE(tint[1].tt) != A1T_INTEGER &&
     241           0 :         A1_PARSE_TYPE(tint[1].tt) != A1T_UNSIGNED &&
     242           0 :         A1_PARSE_TYPE(tint[1].tt) != A1T_INTEGER64 &&
     243           0 :         A1_PARSE_TYPE(tint[1].tt) != A1T_UNSIGNED64 &&
     244           0 :         A1_PARSE_TYPE(tint[1].tt) != A1T_IMEMBER)
     245           0 :         return -1;
     246           0 :     switch (tint[0].offset) {
     247           0 :     case 8:     return i - *(const int64_t *)intp;
     248           0 :     case 4:     return i - *(const int32_t *)intp;
     249           0 :     default:    return -1;
     250             :     }
     251             : }
     252             : 
     253             : /*
     254             :  * Map a logical SET/SEQUENCE member to a template entry.
     255             :  *
     256             :  * This should really have been done by the compiler, but clearly it wasn't.
     257             :  *
     258             :  * The point is that a struct type's template may be littered with entries that
     259             :  * don't directly correspond to a struct field (SET/SEQUENCE member), so we
     260             :  * have to count just the ones that do to get to the one we want.
     261             :  */
     262             : static const struct asn1_template *
     263        8444 : template4member(const struct asn1_template *t, size_t f)
     264             : {
     265        8444 :     size_t n = (uintptr_t)t->ptr;
     266         368 :     size_t i;
     267             : 
     268       20650 :     for (i = 0, t++; i < n; t++, i++) {
     269       20650 :         switch (t->tt & A1_OP_MASK) {
     270       16658 :         case A1_OP_TAG:
     271             :         case A1_OP_TYPE:
     272             :         case A1_OP_TYPE_EXTERN:
     273       16658 :             if (f-- == 0)
     274        8444 :                 return t;
     275        8214 :             continue;
     276           0 :         case A1_OP_OPENTYPE_OBJSET:
     277             :         case A1_OP_NAME:
     278           0 :             return NULL;
     279        3992 :         default:
     280        3992 :             continue;
     281             :         }
     282             :     }
     283           0 :     return NULL;
     284             : }
     285             : 
     286             : /*
     287             :  * Attempt to decode known open type alternatives into a CHOICE-like
     288             :  * discriminated union.
     289             :  *
     290             :  * Arguments:
     291             :  *
     292             :  *  - object set template
     293             :  *  - decoder flags
     294             :  *  - pointer to memory object (C struct) to decode into
     295             :  *  - template for type ID field of `data'
     296             :  *  - template for open type field of `data' (an octet string or HEIM_ANY)
     297             :  *
     298             :  * Returns:
     299             :  *
     300             :  *  - 0
     301             :  *  - ENOMEM
     302             :  *
     303             :  * Other errors in decoding open type values are ignored, but applications can
     304             :  * note that an error must have occurred.  (Perhaps we should generate a `ret'
     305             :  * field for the discriminated union we decode into that we could use to
     306             :  * indicate what went wrong with decoding an open type value?  The application
     307             :  * can always try to decode itself to find out what the error was, but the
     308             :  * whole point is to save the developer the bother of writing code to decode
     309             :  * open type values.  Then again, the specific cause of any one decode failure
     310             :  * is not usually very important to users, so it's not very important to
     311             :  * applications either.)
     312             :  *
     313             :  * Here `data' is something like this:
     314             :  *
     315             :  *      typedef struct SingleAttribute {
     316             :  *          heim_oid type;              // <--- decoded already
     317             :  *          HEIM_ANY value;             // <--- decoded already
     318             :  *       // We must set this:
     319             :  *       // vvvvvvvv
     320             :  *          struct {
     321             :  *              enum {
     322             :  *                  choice_SingleAttribute_iosnumunknown = 0,
     323             :  *                  choice_SingleAttribute_iosnum_id_at_name,
     324             :  *                  ..
     325             :  *                  choice_SingleAttribute_iosnum_id_at_emailAddress,
     326             :  *              } element;     // <--- map type ID to enum
     327             :  *              union {
     328             :  *                  X520name* at_name;
     329             :  *                  X520name* at_surname;
     330             :  *                  ..
     331             :  *                  AliasIA5String* at_emailAddress;
     332             :  *              } u;           // <--- alloc and decode val above into this
     333             :  *          } _ioschoice_value;
     334             :  *      } SingleAttribute;
     335             :  *
     336             :  * or
     337             :  *
     338             :  *      typedef struct AttributeSet {
     339             :  *          heim_oid type;              // <--- decoded already
     340             :  *          struct AttributeSet_values {
     341             :  *              unsigned int len;       // <--- decoded already
     342             :  *              HEIM_ANY *val;          // <--- decoded already
     343             :  *          } values;
     344             :  *       // We must set this:
     345             :  *       // vvvvvvvv
     346             :  *          struct {
     347             :  *              enum { choice_AttributeSet_iosnumunknown = 0,
     348             :  *                  choice_AttributeSet_iosnum_id_at_name,
     349             :  *                  choice_AttributeSet_iosnum_id_at_surname,
     350             :  *                  ..
     351             :  *                  choice_AttributeSet_iosnum_id_at_emailAddress,
     352             :  *              } element;         // <--- map type ID to enum
     353             :  *              unsigned int len;   // <--- set len to len as above
     354             :  *              union {
     355             :  *                  X520name *at_name;
     356             :  *                  X520name *at_surname;
     357             :  *                  ..
     358             :  *                  AliasIA5String *at_emailAddress;
     359             :  *              } *val;         // <--- alloc and decode vals above into this
     360             :  *          } _ioschoice_values;
     361             :  *      } AttributeSet;
     362             :  */
     363             : static int
     364        2880 : _asn1_decode_open_type(const struct asn1_template *t,
     365             :                        unsigned flags,
     366             :                        void *data,
     367             :                        const struct asn1_template *ttypeid,
     368             :                        const struct asn1_template *topentype)
     369             : {
     370        2880 :     const struct asn1_template *ttypeid_univ = ttypeid;
     371         184 :     const struct asn1_template *tactual_type;
     372        2880 :     const struct asn1_template *tos = t->ptr;
     373         184 :     size_t sz, n;
     374        2880 :     size_t i = 0;
     375        2880 :     unsigned int *lenp = NULL;  /* Pointer to array length field */
     376        2880 :     unsigned int len = 1;       /* Array length */
     377        2880 :     void **dp = NULL;           /* Decoded open type struct pointer */
     378         184 :     int *elementp;              /* Choice enum pointer */
     379        2880 :     int typeid_is_oid = 0;
     380        2880 :     int typeid_is_int = 0;
     381        2880 :     int ret = 0;
     382             : 
     383             :     /*
     384             :      * NOTE: Here expressions like `DPO(data, t->offset + ...)' refer to parts
     385             :      *       of a _ioschoice_<fieldName> struct field of `data'.
     386             :      *
     387             :      *       Expressions like `DPO(data, topentype->offset + ...)' refer to
     388             :      *       the open type field in `data', which is either a `heim_any', a
     389             :      *       `heim_octet_string', or an array of one of those.
     390             :      *
     391             :      *       Expressions like `DPO(data, ttypeid->offset)' refer to the open
     392             :      *       type's type ID field in `data'.
     393             :      */
     394             : 
     395             :     /*
     396             :      * Minimal setup:
     397             :      *
     398             :      *  - set type choice to choice_<type>_iosnumunknown (zero).
     399             :      *  - set union value to zero
     400             :      *
     401             :      * We need a pointer to the choice ID:
     402             :      *
     403             :      *      typedef struct AttributeSet {
     404             :      *          heim_oid type;              // <--- decoded already
     405             :      *          struct AttributeSet_values {
     406             :      *              unsigned int len;       // <--- decoded already
     407             :      *              HEIM_ANY *val;          // <--- decoded already
     408             :      *          } values;
     409             :      *          struct {
     410             :      *              enum { choice_AttributeSet_iosnumunknown = 0,
     411             :      * ----------->     ...
     412             :      *              } element; // HERE
     413             :      *              ...
     414             :      *          } ...
     415             :      *      }
     416             :      *
     417             :      * XXX NOTE: We're assuming that sizeof(enum) == sizeof(int)!
     418             :      */
     419        2880 :     elementp = DPO(data, t->offset);
     420        2880 :     *elementp = 0; /* Set the choice to choice_<type>_iosnumunknown */
     421        2880 :     if (t->tt & A1_OS_OT_IS_ARRAY) {
     422             :         /*
     423             :          * The open type is a SET OF / SEQUENCE OF -- an array.
     424             :          *
     425             :          * Get the number of elements to decode from:
     426             :          *
     427             :          *      typedef struct AttributeSet {
     428             :          *          heim_oid type;
     429             :          *          struct AttributeSet_values {
     430             :          * ------------>unsigned int len;       // HERE
     431             :          *              HEIM_ANY *val;
     432             :          *          } values;
     433             :          *          ...
     434             :          *      }
     435             :          */
     436           0 :         len = *((unsigned int *)DPO(data, topentype->offset));
     437             : 
     438             :         /*
     439             :          * Set the number of decoded elements to zero for now:
     440             :          *
     441             :          *      typedef struct AttributeSet {
     442             :          *          heim_oid type;
     443             :          *          struct AttributeSet_values {
     444             :          *              unsigned int len;
     445             :          *              HEIM_ANY *val;
     446             :          *          } values;
     447             :          *          struct {
     448             :          *              enum { ... } element;
     449             :          * ------------>unsigned int len;       // HERE
     450             :          *              ...
     451             :          *          } _ioschoice_values;
     452             :          *      }
     453             :          */
     454           0 :         lenp = DPO(data, t->offset + sizeof(*elementp));
     455           0 :         *lenp = 0;
     456             :         /*
     457             :          * Get a pointer to the place where we must put the decoded value:
     458             :          *
     459             :          *      typedef struct AttributeSet {
     460             :          *          heim_oid type;
     461             :          *          struct AttributeSet_values {
     462             :          *              unsigned int len;
     463             :          *              HEIM_ANY *val;
     464             :          *          } values;
     465             :          *          struct {
     466             :          *              enum { ... } element;
     467             :          *              unsigned int len;
     468             :          *              struct {
     469             :          *                  union { SomeType *some_choice; ... } u;
     470             :          * ------------>} *val;         // HERE
     471             :          *          } _ioschoice_values;
     472             :          *      } AttributeSet;
     473             :          */
     474           0 :         dp = DPO(data, t->offset + sizeof(*elementp) + sizeof(*lenp));
     475             :     } else {
     476             :         /*
     477             :          * Get a pointer to the place where we must put the decoded value:
     478             :          *
     479             :          *      typedef struct SingleAttribute {
     480             :          *          heim_oid type;
     481             :          *          HEIM_ANY value;
     482             :          *          struct {
     483             :          *              enum { ... } element;
     484             :          * ------------>union { SomeType *some_choice; ... } u; // HERE
     485             :          *          } _ioschoice_value;
     486             :          *      } SingleAttribute;
     487             :          */
     488        2880 :         dp = DPO(data, t->offset + sizeof(*elementp));
     489             :     }
     490             : 
     491             :     /* Align `dp' */
     492        3064 :     while (sizeof(void *) != sizeof(*elementp) &&
     493        5760 :         ((uintptr_t)dp) % sizeof(void *) != 0)
     494        2880 :         dp = (void *)(((char *)dp) + sizeof(*elementp));
     495        2880 :     *dp = NULL;
     496             : 
     497             :     /*
     498             :      * Find out the type of the type ID member.  We currently support only
     499             :      * integers and OIDs.
     500             :      *
     501             :      * Chase through any tags to get to the type.
     502             :      */
     503        2880 :     while (((ttypeid_univ->tt & A1_OP_MASK) == A1_OP_TAG &&
     504        2880 :             A1_TAG_CLASS(ttypeid_univ->tt) == ASN1_C_CONTEXT) ||
     505        2696 :            ((ttypeid_univ->tt & A1_OP_MASK) == A1_OP_TYPE)) {
     506           0 :         ttypeid_univ = ttypeid_univ->ptr;
     507           0 :         ttypeid_univ++;
     508             :     }
     509        2880 :     switch (ttypeid_univ->tt & A1_OP_MASK) {
     510        2880 :     case A1_OP_TAG:
     511        2880 :         if (A1_TAG_CLASS(ttypeid_univ->tt) != ASN1_C_UNIV)
     512           0 :             return 0;       /* Do nothing, silently */
     513        2880 :         switch (A1_TAG_TAG(ttypeid_univ->tt)) {
     514        2696 :         case UT_OID:
     515        2696 :             typeid_is_oid = 1;
     516        2696 :             break;
     517           0 :         case UT_Integer: {
     518           0 :             const struct asn1_template *tint = ttypeid_univ->ptr;
     519             : 
     520           0 :             tint++;
     521             :             
     522           0 :             if ((tint->tt & A1_OP_MASK) != A1_OP_PARSE)
     523           0 :                 return 0;   /* Do nothing, silently */
     524           0 :             if (A1_PARSE_TYPE(tint->tt) != A1T_INTEGER &&
     525           0 :                 A1_PARSE_TYPE(tint->tt) != A1T_UNSIGNED &&
     526           0 :                 A1_PARSE_TYPE(tint->tt) != A1T_INTEGER64 &&
     527           0 :                 A1_PARSE_TYPE(tint->tt) != A1T_UNSIGNED64 &&
     528           0 :                 A1_PARSE_TYPE(tint->tt) != A1T_IMEMBER)
     529           0 :                 return 0;   /* Do nothing, silently (maybe a large int) */
     530           0 :             typeid_is_int = 1;
     531           0 :             break;
     532             :         }
     533             :         /* It might be cool to support string types as type ID types */
     534           0 :         default: return 0;  /* Do nothing, silently */
     535             :         }
     536        2880 :         break;
     537           0 :     default: return 0;      /* Do nothing, silently */
     538             :     }
     539             : 
     540             :     /*
     541             :      * Find the type of the open type.
     542             :      *
     543             :      * An object set template looks like:
     544             :      *
     545             :      * const struct asn1_template asn1_ObjectSetName[] = {
     546             :      *     // Header entry (in this case it says there's 17 objects):
     547             :      *     { 0, 0, ((void*)17) },
     548             :      *
     549             :      *     // here's the name of the object set:
     550             :      *     { A1_OP_NAME, 0, "ObjectSetName" },
     551             :      *
     552             :      *     // then three entries per object: object name, object type ID,
     553             :      *     // object type:
     554             :      *     { A1_OP_NAME, 0, "ext-AuthorityInfoAccess" },
     555             :      *     { A1_OP_OPENTYPE_ID, 0, (const void*)&asn1_oid_oidName },
     556             :      *     { A1_OP_OPENTYPE, sizeof(SomeType), (const void*)&asn1_SomeType },
     557             :      *     ...
     558             :      * };
     559             :      *
     560             :      * `i' being a logical object offset, i*3+3 would be the index of the
     561             :      * A1_OP_OPENTYPE_ID entry for the current object, and i*3+4 the index of
     562             :      * the A1_OP_OPENTYPE entry for the current object.
     563             :      */
     564        2880 :     if (t->tt & A1_OS_IS_SORTED) {
     565        2880 :         size_t left = 0;
     566        2880 :         size_t right = A1_HEADER_LEN(tos);
     567        2880 :         const void *vp = DPO(data, ttypeid->offset);
     568        2880 :         int c = -1;
     569             : 
     570        9891 :         while (left < right) {
     571        9303 :             size_t mid = (left + right) >> 1;
     572             : 
     573        9303 :             if ((tos[3 + mid * 3].tt & A1_OP_MASK) != A1_OP_OPENTYPE_ID)
     574           0 :                 return 0;
     575        9303 :             if (typeid_is_int)
     576           0 :                 c = typeid_int_cmp(vp, (intptr_t)tos[3 + mid * 3].ptr,
     577             :                                    ttypeid_univ);
     578        9303 :             else if (typeid_is_oid)
     579        9303 :                 c = der_heim_oid_cmp(vp, tos[3 + mid * 3].ptr);
     580        9303 :             if (c < 0) {
     581        3055 :                 right = mid;
     582        6056 :             } else if (c > 0) {
     583        3484 :                 left = mid + 1;
     584             :             } else {
     585        2156 :                 i = mid;
     586        2156 :                 break;
     587             :             }
     588             :         }
     589        2880 :         if (c)
     590         540 :             return 0; /* No match */
     591             :     } else {
     592           0 :         for (i = 0, n = A1_HEADER_LEN(tos); i < n; i++) {
     593             :             /* We add 1 to `i' because we're skipping the header */
     594           0 :             if ((tos[3 + i*3].tt & A1_OP_MASK) != A1_OP_OPENTYPE_ID)
     595           0 :                 return 0;
     596           0 :             if (typeid_is_int &&
     597           0 :                 typeid_int_cmp(DPO(data, ttypeid->offset),
     598           0 :                                (intptr_t)tos[3 + i*3].ptr,
     599             :                                ttypeid_univ))
     600           0 :                 continue;
     601           0 :             if (typeid_is_oid &&
     602           0 :                 der_heim_oid_cmp(DPO(data, ttypeid->offset), tos[3 + i*3].ptr))
     603           0 :                 continue;
     604           0 :             break;
     605             :         }
     606           0 :         if (i == n)
     607           0 :             return 0; /* No match */
     608             :     }
     609             : 
     610             :     /* Match! */
     611        2292 :     *elementp = i+1; /* Zero is the "unknown" choice, so add 1 */
     612             : 
     613             :     /*
     614             :      * We want the A1_OP_OPENTYPE template entry.  Its `offset' is the sizeof
     615             :      * the object we'll be decoding into, and its `ptr' is the pointer to the
     616             :      * template for decoding that type.
     617             :      */
     618        2292 :     tactual_type = &tos[i*3 + 4];
     619             : 
     620             :     /* Decode the encoded open type value(s) */
     621        2292 :     if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
     622             :         /*
     623             :          * Not a SET OF/SEQUENCE OF open type, just singular.
     624             :          *
     625             :          * We need the address of the octet string / ANY field containing the
     626             :          * encoded open type value:
     627             :          *
     628             :          *      typedef struct SingleAttribute {
     629             :          *          heim_oid type;
     630             :          * -------->HEIM_ANY value; // HERE
     631             :          *          struct {
     632             :          *              ...
     633             :          *          } ...
     634             :          *      }
     635             :          */
     636        2292 :         const struct heim_base_data *d = DPOC(data, topentype->offset);
     637         136 :         void *o;
     638             : 
     639        2292 :         if (d->data && d->length) {
     640        2292 :             if ((o = calloc(1, tactual_type->offset)) == NULL)
     641           0 :                 return ENOMEM;
     642             : 
     643             :             /* Re-enter to decode the encoded open type value */
     644        2292 :             ret = _asn1_decode(tactual_type->ptr, flags, d->data, d->length, o, &sz);
     645             :             /*
     646             :              * Store the decoded object in the union:
     647             :              *
     648             :              *      typedef struct SingleAttribute {
     649             :              *          heim_oid type;
     650             :              *          HEIM_ANY value;
     651             :              *          struct {
     652             :              *              enum { ... } element;
     653             :              * ------------>union { SomeType *some_choice; ... } u; // HERE
     654             :              *          } _ioschoice_value;
     655             :              *      } SingleAttribute;
     656             :              *
     657             :              * All the union arms are pointers.
     658             :              */
     659        2292 :             if (ret) {
     660           0 :                 _asn1_free(tactual_type->ptr, o);
     661           0 :                 free(o);
     662             :                 /*
     663             :                  * So we failed to decode the open type -- that should not be fatal
     664             :                  * to decoding the rest of the input.  Only ENOMEM should be fatal.
     665             :                  */
     666           0 :                 ret = 0;
     667             :             } else {
     668        2292 :                 *dp = o;
     669             :             }
     670             :         }
     671        2292 :         return ret;
     672             :     } else {
     673           0 :         const struct heim_base_data * const *d;
     674           0 :         void **val; /* Array of pointers */
     675             : 
     676             :         /*
     677             :          * A SET OF/SEQUENCE OF open type, plural.
     678             :          *
     679             :          * We need the address of the octet string / ANY array pointer field
     680             :          * containing the encoded open type values:
     681             :          *
     682             :          *      typedef struct AttributeSet {
     683             :          *          heim_oid type;
     684             :          *          struct AttributeSet_values {
     685             :          *              unsigned int len;
     686             :          * ------------>HEIM_ANY *val;      // HERE
     687             :          *          } values;
     688             :          *      ...
     689             :          *      }
     690             :          *
     691             :          * We already know the value of the `len' field.
     692             :          */
     693           0 :         d = DPOC(data, topentype->offset + sizeof(unsigned int));
     694           0 :         while (sizeof(void *) != sizeof(len) &&
     695           0 :                ((uintptr_t)d) % sizeof(void *) != 0)
     696           0 :             d = (const void *)(((const char *)d) + sizeof(len));
     697             : 
     698           0 :         if ((val = calloc(len, sizeof(*val))) == NULL)
     699           0 :             ret = ENOMEM;
     700             : 
     701             :         /* Increment the count of decoded values as we decode */
     702           0 :         *lenp = len;
     703           0 :         for (i = 0; ret != ENOMEM && i < len; i++) {
     704           0 :             if ((val[i] = calloc(1, tactual_type->offset)) == NULL)
     705           0 :                 ret = ENOMEM;
     706           0 :             if (ret == 0)
     707             :                 /* Re-enter to decode the encoded open type value */
     708           0 :                 ret = _asn1_decode(tactual_type->ptr, flags, d[0][i].data,
     709           0 :                                    d[0][i].length, val[i], &sz);
     710           0 :             if (ret) {
     711           0 :                 _asn1_free(tactual_type->ptr, val[i]);
     712           0 :                 free(val[i]);
     713           0 :                 val[i] = NULL;
     714             :             }
     715             :         }
     716           0 :         if (ret != ENOMEM)
     717           0 :             ret = 0; /* See above */
     718           0 :         *dp = val;
     719           0 :         return ret;
     720             :     }
     721             : }
     722             : 
     723             : int
     724    49220117 : _asn1_decode(const struct asn1_template *t, unsigned flags,
     725             :              const unsigned char *p, size_t len, void *data, size_t *size)
     726             : {
     727    49220117 :     const struct asn1_template *tbase = t;
     728    49220117 :     const struct asn1_template *tdefval = NULL;
     729    49220117 :     size_t elements = A1_HEADER_LEN(t);
     730    49220117 :     size_t oldlen = len;
     731    49220117 :     int ret = 0;
     732    49220117 :     const unsigned char *startp = NULL;
     733    49220117 :     unsigned int template_flags = t->tt;
     734             : 
     735             :     /*
     736             :      * Important notes:
     737             :      *
     738             :      *  - by and large we don't call _asn1_free() on error, except when we're
     739             :      *    decoding optional things or choices, then we do call _asn1_free()
     740             :      *    here
     741             :      *
     742             :      *    instead we leave it to _asn1_decode_top() to call _asn1_free() on
     743             :      *    error
     744             :      *
     745             :      *  - on error all fields of whatever we didn't _asn1_free() must have been
     746             :      *    initialized to sane values because _asn1_decode_top() will call
     747             :      *    _asn1_free() on error, so we must have left everything initialized
     748             :      *    that _asn1_free() could possibly look at
     749             :      *
     750             :      *  - so we must initialize everything
     751             :      *
     752             :      *    FIXME? but we mostly rely on calloc() to do this...
     753             :      *
     754             :      *  - we don't use malloc() unless we're going to write over the whole
     755             :      *    thing with memcpy() or whatever
     756             :      */
     757             : 
     758             :     /* skip over header */
     759    49220117 :     t++;
     760             : 
     761    49220117 :     if (template_flags & A1_HF_PRESERVE)
     762      146339 :         startp = p;
     763             : 
     764   127931990 :     while (elements) {
     765    78918597 :         switch (t->tt & A1_OP_MASK) {
     766        2880 :         case A1_OP_OPENTYPE_OBJSET: {
     767        2880 :             size_t opentypeid = t->tt & ((1<<10)-1);
     768        2880 :             size_t opentype = (t->tt >> 10) & ((1<<10)-1);
     769             : 
     770             :             /* Note that the only error returned here would be ENOMEM */
     771        2880 :             ret = _asn1_decode_open_type(t, flags, data,
     772             :                                          template4member(tbase, opentypeid),
     773             :                                          template4member(tbase, opentype));
     774        2880 :             if (ret)
     775           0 :                 return ret;
     776        2696 :             break;
     777             :         }
     778           0 :         case A1_OP_TYPE_DECORATE_EXTERN: break;
     779           0 :         case A1_OP_TYPE_DECORATE: break;
     780    18463287 :         case A1_OP_NAME: break;
     781        3109 :         case A1_OP_DEFVAL:
     782        3109 :             tdefval = t;
     783        3109 :             break;
     784    12159826 :         case A1_OP_TYPE:
     785             :         case A1_OP_TYPE_EXTERN: {
     786      444995 :             size_t newsize, elsize;
     787    12159826 :             void *el = DPO(data, t->offset);
     788    12159826 :             void **pel = (void **)el;
     789             : 
     790    12159826 :             if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
     791    12156870 :                 elsize = _asn1_sizeofType(t->ptr);
     792             :             } else {
     793        2956 :                 const struct asn1_type_func *f = t->ptr;
     794        2956 :                 elsize = f->size;
     795             :             }
     796             : 
     797    12159826 :             if (t->tt & A1_FLAG_OPTIONAL) {
     798        1946 :                 *pel = calloc(1, elsize);
     799        1946 :                 if (*pel == NULL)
     800          70 :                     return ENOMEM;
     801        1946 :                 el = *pel;
     802        1946 :                 if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
     803         149 :                     ret = _asn1_decode(t->ptr, flags, p, len, el, &newsize);
     804             :                 } else {
     805        1797 :                     const struct asn1_type_func *f = t->ptr;
     806        1797 :                     ret = (f->decode)(p, len, el, &newsize);
     807             :                 }
     808        1946 :                 if (ret) {
     809             :                     /*
     810             :                      * Optional field not present in encoding, presumably,
     811             :                      * though we should really look more carefully at `ret'.
     812             :                      */
     813         253 :                     if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
     814          35 :                         _asn1_free(t->ptr, el);
     815             :                     } else {
     816         218 :                         const struct asn1_type_func *f = t->ptr;
     817         218 :                         f->release(el);
     818             :                     }
     819         253 :                     free(*pel);
     820         253 :                     *pel = NULL;
     821    12159756 :                     break;
     822             :                 }
     823             :             } else {
     824    12157880 :                 if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
     825    12156721 :                     ret = _asn1_decode(t->ptr, flags, p, len, el, &newsize);
     826             :                 } else {
     827        1159 :                     const struct asn1_type_func *f = t->ptr;
     828        1159 :                     ret = (f->decode)(p, len, el, &newsize);
     829             :                 }
     830             :             }
     831    12159573 :             if (ret) {
     832          70 :                 if (t->tt & A1_FLAG_OPTIONAL) {
     833          70 :                 } else if (t->tt & A1_FLAG_DEFAULT) {
     834           0 :                     if (!tdefval)
     835           0 :                         return ASN1_PARSE_ERROR; /* Can't happen */
     836             :                     /*
     837             :                      * Defaulted field not present in encoding, presumably,
     838             :                      * though we should really look more carefully at `ret'.
     839             :                      */
     840           0 :                     if (tdefval->tt & A1_DV_BOOLEAN) {
     841           0 :                         int *i = (void *)(char *)el;
     842             : 
     843           0 :                         *i = tdefval->ptr ? 1 : 0;
     844           0 :                     } else if (tdefval->tt & A1_DV_INTEGER64) {
     845           0 :                         int64_t *i = (void *)(char *)el;
     846             : 
     847           0 :                         *i = (int64_t)(intptr_t)tdefval->ptr;
     848           0 :                     } else if (tdefval->tt & A1_DV_INTEGER32) {
     849           0 :                         int32_t *i = (void *)(char *)el;
     850             : 
     851           0 :                         *i = (int32_t)(intptr_t)tdefval->ptr;
     852           0 :                     } else if (tdefval->tt & A1_DV_INTEGER) {
     853           0 :                         struct heim_integer *i = (void *)(char *)el;
     854             : 
     855           0 :                         if ((ret = der_copy_heim_integer(tdefval->ptr, i)))
     856           0 :                             return ret;
     857           0 :                     } else if (tdefval->tt & A1_DV_UTF8STRING) {
     858           0 :                         char **s = el;
     859             : 
     860           0 :                         if ((*s = strdup(tdefval->ptr)) == NULL)
     861           0 :                             return ENOMEM;
     862             :                     } else {
     863           0 :                         abort();
     864             :                     }
     865           0 :                     break;
     866             :                 }
     867          70 :                 return ret; /* Error decoding required field */
     868             :             }
     869    12159503 :             p += newsize; len -= newsize;
     870             : 
     871    12159503 :             break;
     872             :         }
     873    33801444 :         case A1_OP_TAG: {
     874     1234303 :             Der_type dertype;
     875    33801444 :             size_t newsize = 0;
     876    33801444 :             size_t datalen, l = 0;
     877    33801444 :             void *olddata = data;
     878    33801444 :             int is_indefinite = 0;
     879    33801444 :             int subflags = flags;
     880    33801444 :             int replace_tag = (t->tt & A1_FLAG_IMPLICIT) && is_tagged(t->ptr);
     881    33801444 :             void *el = data = DPO(data, t->offset);
     882    33801444 :             void **pel = (void **)el;
     883             : 
     884             :             /*
     885             :              * XXX If this type (chasing t->ptr through IMPLICIT tags, if this
     886             :              * one is too, till we find a non-TTag) is a [UNIVERSAL SET] type,
     887             :              * then we have to accept fields out of order.  For each field tag
     888             :              * we see we'd have to do a linear search of the SET's template
     889             :              * because it won't be sorted (or we could sort a copy and do a
     890             :              * binary search on that, but these SETs will always be small so it
     891             :              * won't be worthwhile).  We'll need a utility function to do all
     892             :              * of this.
     893             :              */
     894    33801444 :             ret = der_match_tag_and_length(p, len, A1_TAG_CLASS(t->tt),
     895    32567141 :                                            &dertype, A1_TAG_TAG(t->tt),
     896             :                                            &datalen, &l);
     897    33801444 :             if (ret) {
     898     1813704 :                 if (t->tt & A1_FLAG_OPTIONAL) {
     899     1562113 :                     data = olddata;
     900    33594790 :                     break;
     901      193877 :                 } else if (t->tt & A1_FLAG_DEFAULT) {
     902        2620 :                     if (!tdefval)
     903      206654 :                         return ASN1_PARSE_ERROR; /* Can't happen */
     904             :                     /*
     905             :                      * Defaulted field not present in encoding, presumably,
     906             :                      * though we should really look more carefully at `ret'.
     907             :                      */
     908        2620 :                     if (tdefval->tt & A1_DV_BOOLEAN) {
     909        2620 :                         int *i = (void *)(char *)data;
     910             : 
     911        2620 :                         *i = tdefval->ptr ? 1 : 0;
     912           0 :                     } else if (tdefval->tt & A1_DV_INTEGER64) {
     913           0 :                         int64_t *i = (void *)(char *)data;
     914             : 
     915           0 :                         *i = (int64_t)(intptr_t)tdefval->ptr;
     916           0 :                     } else if (tdefval->tt & A1_DV_INTEGER32) {
     917           0 :                         int32_t *i = (void *)(char *)data;
     918             : 
     919           0 :                         *i = (int32_t)(intptr_t)tdefval->ptr;
     920           0 :                     } else if (tdefval->tt & A1_DV_INTEGER) {
     921           0 :                         struct heim_integer *i = (void *)(char *)data;
     922             : 
     923           0 :                         if ((ret = der_copy_heim_integer(tdefval->ptr, i)))
     924           0 :                             return ret;
     925           0 :                     } else if (tdefval->tt & A1_DV_UTF8STRING) {
     926           0 :                         char **s = data;
     927             : 
     928           0 :                         if ((*s = strdup(tdefval->ptr)) == NULL)
     929           0 :                             return ENOMEM;
     930             :                     } else {
     931           0 :                         abort();
     932             :                     }
     933        2436 :                     data = olddata;
     934        2436 :                     break;
     935             :                 }
     936      191257 :                 return ret; /* Error decoding required field */
     937             :             }
     938             : 
     939    31987740 :             p += l; len -= l;
     940             : 
     941             :             /*
     942             :              * Only allow indefinite encoding for OCTET STRING and BER
     943             :              * for now. Should handle BIT STRING too.
     944             :              */
     945             : 
     946    31987740 :             if (dertype != A1_TAG_TYPE(t->tt) && (flags & A1_PF_ALLOW_BER)) {
     947           0 :                 const struct asn1_template *subtype = t->ptr;
     948           0 :                 subtype++; /* skip header */
     949             : 
     950           0 :                 if (((subtype->tt & A1_OP_MASK) == A1_OP_PARSE) &&
     951           0 :                     A1_PARSE_TYPE(subtype->tt) == A1T_OCTET_STRING)
     952           0 :                     subflags |= A1_PF_INDEFINTE;
     953             :             }
     954             : 
     955    31987740 :             if (datalen == ASN1_INDEFINITE) {
     956           0 :                 if ((flags & A1_PF_ALLOW_BER) == 0)
     957           0 :                     return ASN1_GOT_BER;
     958           0 :                 is_indefinite = 1;
     959           0 :                 datalen = len;
     960           0 :                 if (datalen < 2)
     961           0 :                     return ASN1_OVERRUN;
     962             :                 /* hide EndOfContent for sub-decoder, catching it below */
     963           0 :                 datalen -= 2;
     964    31987740 :             } else if (datalen > len)
     965           0 :                 return ASN1_OVERRUN;
     966             : 
     967    31987740 :             if (t->tt & A1_FLAG_OPTIONAL) {
     968     2054170 :                 size_t ellen = _asn1_sizeofType(t->ptr);
     969             : 
     970     2054170 :                 *pel = calloc(1, ellen);
     971     2054170 :                 if (*pel == NULL)
     972           0 :                     return ENOMEM;
     973     1978074 :                 data = *pel;
     974             :             }
     975             : 
     976    31987740 :             if (replace_tag) {
     977        1675 :                 const struct asn1_template *subtype = t->ptr;
     978        1675 :                 int have_tag = 0;
     979             : 
     980             :                 /*
     981             :                  * So, we have an IMPLICIT tag.  What we want to do is find the
     982             :                  * template for the body of the type so-tagged.  That's going
     983             :                  * to be a template that has a tag that isn't itself IMPLICIT.
     984             :                  *
     985             :                  * So we chase the pointer in the template until we find such a
     986             :                  * thing, then decode using that template.
     987             :                  */
     988        3678 :                 while (!have_tag) {
     989        2003 :                     subtype++;
     990        2003 :                     if ((subtype->tt & A1_OP_MASK) == A1_OP_TAG)
     991        1675 :                         replace_tag = (subtype->tt & A1_FLAG_IMPLICIT) && is_tagged(t->ptr);
     992        1899 :                     if (replace_tag) {
     993         328 :                         subtype = subtype->ptr;
     994         328 :                         continue;
     995             :                     }
     996        1675 :                     if ((subtype->tt & A1_OP_MASK) == A1_OP_TAG) {
     997        1675 :                         ret = _asn1_decode(subtype->ptr, subflags, p, datalen, data, &newsize);
     998        1675 :                         have_tag = 1;
     999             :                     } else {
    1000           0 :                         subtype = subtype->ptr;
    1001             :                     }
    1002             :                 }
    1003             :             } else {
    1004    31986065 :                 ret = _asn1_decode(t->ptr, subflags, p, datalen, data, &newsize);
    1005             :             }
    1006    31987740 :             if (ret == 0 && !is_indefinite && newsize != datalen)
    1007             :                 /* Hidden data */
    1008           0 :                 ret = ASN1_EXTRA_DATA;
    1009             : 
    1010    31987740 :             if (ret == 0) {
    1011    31972343 :                 if (is_indefinite) {
    1012             :                     /* If we use indefinite encoding, the newsize is the datasize. */
    1013           0 :                     datalen = newsize;
    1014             :                 }
    1015             : 
    1016    31972343 :                 len -= datalen;
    1017    31972343 :                 p += datalen;
    1018             : 
    1019             :                 /*
    1020             :                  * Indefinite encoding needs a trailing EndOfContent,
    1021             :                  * check for that.
    1022             :                  */
    1023    31972343 :                 if (is_indefinite) {
    1024           0 :                     ret = der_match_tag_and_length(p, len, ASN1_C_UNIV,
    1025             :                                                    &dertype, UT_EndOfContent,
    1026             :                                                    &datalen, &l);
    1027           0 :                     if (ret == 0 && dertype != PRIM)
    1028           0 :                         ret = ASN1_BAD_ID;
    1029           0 :                     else if (ret == 0 && datalen != 0)
    1030           0 :                         ret = ASN1_INDEF_EXTRA_DATA;
    1031           0 :                     if (ret == 0) {
    1032           0 :                         p += l; len -= l;
    1033             :                     }
    1034             :                 }
    1035             :             }
    1036    31987740 :             if (ret) {
    1037       15397 :                 if (!(t->tt & A1_FLAG_OPTIONAL))
    1038       14812 :                     return ret;
    1039             : 
    1040           0 :                 _asn1_free(t->ptr, data);
    1041           0 :                 free(data);
    1042           0 :                 *pel = NULL;
    1043           0 :                 return ret;
    1044             :             }
    1045    30804212 :             data = olddata;
    1046             : 
    1047    30804212 :             break;
    1048             :         }
    1049    11190849 :         case A1_OP_PARSE: {
    1050    11190849 :             unsigned int type = A1_PARSE_TYPE(t->tt);
    1051      410549 :             size_t newsize;
    1052    11190849 :             void *el = DPO(data, t->offset);
    1053             : 
    1054             :             /*
    1055             :              * INDEFINITE primitive types are one element after the
    1056             :              * same type but non-INDEFINITE version.
    1057             :             */
    1058    11190849 :             if (flags & A1_PF_INDEFINTE)
    1059           0 :                 type++;
    1060             : 
    1061    11190849 :             if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
    1062           0 :                 ABORT_ON_ERROR();
    1063           0 :                 return ASN1_PARSE_ERROR;
    1064             :             }
    1065             : 
    1066    11190849 :             ret = (asn1_template_prim[type].decode)(p, len, el, &newsize);
    1067    11190849 :             if (ret)
    1068           0 :                 return ret;
    1069    11190849 :             p += newsize; len -= newsize;
    1070             : 
    1071    11190849 :             break;
    1072             :         }
    1073     2083895 :         case A1_OP_SETOF:
    1074             :         case A1_OP_SEQOF: {
    1075     2083895 :             struct template_of *el = DPO(data, t->offset);
    1076       76730 :             size_t newsize;
    1077     2083895 :             size_t ellen = _asn1_sizeofType(t->ptr);
    1078     2083895 :             size_t vallength = 0;
    1079             : 
    1080     5494404 :             while (len > 0) {
    1081      126949 :                 void *tmp;
    1082     3410509 :                 size_t newlen = vallength + ellen;
    1083     3410509 :                 if (vallength > newlen)
    1084           0 :                     return ASN1_OVERFLOW;
    1085             : 
    1086             :                 /* XXX Slow */
    1087     3410509 :                 tmp = realloc(el->val, newlen);
    1088     3410509 :                 if (tmp == NULL)
    1089           0 :                     return ENOMEM;
    1090             : 
    1091     3410509 :                 memset(DPO(tmp, vallength), 0, ellen);
    1092     3410509 :                 el->val = tmp;
    1093             : 
    1094     3410509 :                 el->len++;
    1095     3410509 :                 ret = _asn1_decode(t->ptr, flags & (~A1_PF_INDEFINTE), p, len,
    1096     3283560 :                                    DPO(el->val, vallength), &newsize);
    1097     3410509 :                 if (ret)
    1098           0 :                     return ret;
    1099     3410509 :                 vallength = newlen;
    1100     3410509 :                 p += newsize; len -= newsize;
    1101             :             }
    1102             : 
    1103     2083895 :             break;
    1104             :         }
    1105      422620 :         case A1_OP_BMEMBER: {
    1106      422620 :             const struct asn1_template *bmember = t->ptr;
    1107      422620 :             size_t bsize = bmember->offset;
    1108      422620 :             size_t belements = A1_HEADER_LEN(bmember);
    1109      422620 :             size_t pos = 0;
    1110             : 
    1111      422620 :             bmember++;
    1112             : 
    1113      422620 :             memset(data, 0, bsize);
    1114             : 
    1115      422620 :             if (len < 1)
    1116           0 :                 return ASN1_OVERRUN;
    1117      422620 :             p++; len--;
    1118             : 
    1119     6225379 :             while (belements && len) {
    1120     6639051 :                 while (bmember->offset / 8 > pos / 8) {
    1121      836292 :                     if (len < 1)
    1122           0 :                         break;
    1123      836292 :                     p++; len--;
    1124      836292 :                     pos += 8;
    1125             :                 }
    1126     5802759 :                 if (len) {
    1127     5802248 :                     _asn1_bmember_get_bit(p, data, bmember->offset, bsize);
    1128     5802248 :                     belements--; bmember++;
    1129             :                 }
    1130             :             }
    1131      407673 :             len = 0;
    1132      407673 :             break;
    1133             :         }
    1134       92520 :         case A1_OP_CHOICE: {
    1135       92520 :             const struct asn1_template *choice = t->ptr;
    1136       92520 :             unsigned int *element = DPO(data, choice->offset);
    1137        3756 :             size_t datalen;
    1138        3756 :             unsigned int i;
    1139             : 
    1140             :             /*
    1141             :              * CHOICE element IDs are assigned in monotonically increasing
    1142             :              * fashion.  Therefore any unrealistic value is a suitable invalid
    1143             :              * CHOICE value.  The largest binary value (or -1 if treating the
    1144             :              * enum as signed on a twos-complement system, or...) will do.
    1145             :              */
    1146       92520 :             *element = ~0;
    1147             : 
    1148      107209 :             for (i = 1; i < A1_HEADER_LEN(choice) + 1 && choice[i].tt; i++) {
    1149             :                 /*
    1150             :                  * This is more permissive than is required.  CHOICE
    1151             :                  * alternatives must have different outer tags, so in principle
    1152             :                  * we should just match the tag at `p' and `len' in sequence to
    1153             :                  * the choice alternatives.
    1154             :                  *
    1155             :                  * Trying every alternative instead happens to do this anyways
    1156             :                  * because each one will first match the tag at `p' and `len',
    1157             :                  * but if there are CHOICE altnernatives with the same outer
    1158             :                  * tag, then we'll allow it, and they had better be unambiguous
    1159             :                  * in their internal details, otherwise there would be some
    1160             :                  * aliasing.
    1161             :                  *
    1162             :                  * Arguably the *compiler* should detect ambiguous CHOICE types
    1163             :                  * and raise an error, then we don't have to be concerned here
    1164             :                  * at all.
    1165             :                  */
    1166      111924 :                 ret = _asn1_decode(choice[i].ptr, 0, p, len,
    1167      107208 :                                    DPO(data, choice[i].offset), &datalen);
    1168      107208 :                 if (ret == 0) {
    1169       92519 :                     *element = i;
    1170       92519 :                     p += datalen; len -= datalen;
    1171       92519 :                     break;
    1172             :                 }
    1173       14689 :                 _asn1_free(choice[i].ptr, DPO(data, choice[i].offset));
    1174       14689 :                 if (ret != ASN1_BAD_ID && ret != ASN1_MISPLACED_FIELD &&
    1175             :                     ret != ASN1_MISSING_FIELD)
    1176           0 :                     return ret;
    1177             :             }
    1178       92520 :             if (i >= A1_HEADER_LEN(choice) + 1 || !choice[i].tt) {
    1179             :                 /*
    1180             :                  * If this is an extensible CHOICE, then choice->tt will be the
    1181             :                  * offset to u.ellipsis.  If it's not, then this "extension" is
    1182             :                  * an error and must stop parsing it.  (We could be permissive
    1183             :                  * and throw away the extension, though one might as well just
    1184             :                  * mark such a CHOICE as extensible.)
    1185             :                  */
    1186           1 :                 if (choice->tt == 0)
    1187           0 :                     return ASN1_BAD_ID;
    1188             : 
    1189             :                 /* This is the ellipsis case */
    1190           1 :                 *element = 0;
    1191           1 :                 ret = der_get_octet_string(p, len,
    1192           1 :                                            DPO(data, choice->tt), &datalen);
    1193           1 :                 if (ret)
    1194           0 :                     return ret;
    1195           1 :                 p += datalen; len -= datalen;
    1196             :             }
    1197             : 
    1198       92520 :             break;
    1199             :         }
    1200           0 :         default:
    1201           0 :             ABORT_ON_ERROR();
    1202             :             return ASN1_PARSE_ERROR;
    1203             :         }
    1204    78711873 :         t++;
    1205    78711873 :         elements--;
    1206             :     }
    1207             :     /* if we are using padding, eat up read of context */
    1208    49013393 :     if (template_flags & A1_HF_ELLIPSIS)
    1209      170349 :         len = 0;
    1210             : 
    1211    49013393 :     oldlen -= len;
    1212             : 
    1213    49013393 :     if (size)
    1214    48470672 :         *size = oldlen;
    1215             : 
    1216             :     /*
    1217             :      * saved the raw bits if asked for it, useful for signature
    1218             :      * verification.
    1219             :      */
    1220    49013393 :     if (startp) {
    1221      146339 :         heim_octet_string *save = data;
    1222             : 
    1223      146339 :         save->data = malloc(oldlen);
    1224      146339 :         if (save->data == NULL)
    1225           0 :             return ENOMEM;
    1226             :         else {
    1227      146339 :             save->length = oldlen;
    1228      146339 :             memcpy(save->data, startp, oldlen);
    1229             :         }
    1230             :     }
    1231    47219276 :     return 0;
    1232             : }
    1233             : 
    1234             : /*
    1235             :  * This should be called with a `A1_TAG_T(ASN1_C_UNIV, PRIM, UT_Integer)'
    1236             :  * template as the `ttypeid'.
    1237             :  */
    1238             : static int
    1239           0 : typeid_int_copy(void *intp,
    1240             :                 int64_t i,
    1241             :                 const struct asn1_template *ttypeid)
    1242             : {
    1243           0 :     const struct asn1_template *tint = ttypeid->ptr;
    1244             : 
    1245           0 :     if ((tint[1].tt & A1_OP_MASK) != A1_OP_PARSE)
    1246           0 :         return -1;
    1247           0 :     if (A1_PARSE_TYPE(tint[1].tt) != A1T_INTEGER)
    1248           0 :         return -1;
    1249           0 :     switch (tint[0].offset) {
    1250           0 :     case 8:     *((int64_t *)intp) = i; return 0;
    1251           0 :     case 4:     *((int32_t *)intp) = i; return 0;
    1252           0 :     default:    memset(intp, 0, tint[0].offset); return 0;
    1253             :     }
    1254             : }
    1255             : 
    1256             : /* See commentary in _asn1_decode_open_type() */
    1257             : static int
    1258         671 : _asn1_encode_open_type(const struct asn1_template *t,
    1259             :                        const void *data,    /* NOTE: Not really const */
    1260             :                        const struct asn1_template *ttypeid,
    1261             :                        const struct asn1_template *topentype)
    1262             : {
    1263         671 :     const struct asn1_template *ttypeid_univ = ttypeid;
    1264           0 :     const struct asn1_template *tactual_type;
    1265         671 :     const struct asn1_template *tos = t->ptr;
    1266           0 :     size_t sz, i;
    1267         671 :     unsigned int *lenp = NULL;
    1268         671 :     unsigned int len = 1;
    1269         671 :     int element = *(const int *)DPOC(data, t->offset);
    1270         671 :     int typeid_is_oid = 0;
    1271         671 :     int typeid_is_int = 0;
    1272         671 :     int enotsup = 0;
    1273         671 :     int ret = 0;
    1274             : 
    1275         671 :     if (element == 0 || element >= A1_HEADER_LEN(tos) + 1)
    1276         183 :         return 0;
    1277             : 
    1278         488 :     if (t->tt & A1_OS_OT_IS_ARRAY) {
    1279             :         /* The actual `len' is from the decoded open type field */
    1280           0 :         len = *(const unsigned int *)DPOC(data, t->offset + sizeof(element));
    1281             : 
    1282           0 :         if (!len)
    1283           0 :             return 0; /* The app may be encoding the open type by itself */
    1284             :     }
    1285             : 
    1286             :     /* Work out the type ID field's type */
    1287         488 :     while (((ttypeid_univ->tt & A1_OP_MASK) == A1_OP_TAG &&
    1288         488 :             A1_TAG_CLASS(ttypeid_univ->tt) == ASN1_C_CONTEXT) ||
    1289         488 :            ((ttypeid_univ->tt & A1_OP_MASK) == A1_OP_TYPE)) {
    1290           0 :         ttypeid_univ = ttypeid_univ->ptr;
    1291           0 :         ttypeid_univ++;
    1292             :     }
    1293         488 :     switch (ttypeid_univ->tt & A1_OP_MASK) {
    1294         488 :     case A1_OP_TAG:
    1295         488 :         if (A1_TAG_CLASS(ttypeid_univ->tt) != ASN1_C_UNIV) {
    1296           0 :             enotsup = 1;
    1297           0 :             break;
    1298             :         }
    1299         488 :         switch (A1_TAG_TAG(ttypeid_univ->tt)) {
    1300         488 :         case UT_OID:
    1301         488 :             typeid_is_oid = 1;
    1302         488 :             break;
    1303           0 :         case UT_Integer: {
    1304           0 :             const struct asn1_template *tint = ttypeid_univ->ptr;
    1305             : 
    1306           0 :             tint++;
    1307           0 :             if ((tint->tt & A1_OP_MASK) != A1_OP_PARSE ||
    1308           0 :                 A1_PARSE_TYPE(tint->tt) != A1T_INTEGER) {
    1309           0 :                 enotsup = 1;
    1310           0 :                 break;
    1311             :             }
    1312           0 :             typeid_is_int = 1;
    1313           0 :             break;
    1314             :         }
    1315           0 :         default: enotsup = 1; break;
    1316             :         }
    1317         488 :         break;
    1318           0 :     default: enotsup = 1; break;
    1319             :     }
    1320             : 
    1321             :     /*
    1322             :      * The app may not be aware of our automatic open type handling, so if the
    1323             :      * open type already appears to have been encoded, then ignore the decoded
    1324             :      * values.
    1325             :      */
    1326         488 :     if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
    1327         488 :         struct heim_base_data *os = DPO(data, topentype->offset);
    1328             : 
    1329         488 :         if (os->length && os->data)
    1330         488 :             return 0;
    1331             :     } else {
    1332           0 :         struct heim_base_data **os = DPO(data, topentype->offset + sizeof(len));
    1333             : 
    1334           0 :         while (sizeof(void *) != sizeof(unsigned int) &&
    1335           0 :                ((uintptr_t)os) % sizeof(void *) != 0)
    1336           0 :             os = (void *)(((char *)os) + sizeof(unsigned int));
    1337             : 
    1338           0 :         lenp = DPO(data, topentype->offset);
    1339           0 :         if (*lenp == len && os[0]->length && os[0]->data)
    1340           0 :             return 0;
    1341             :     }
    1342             : 
    1343           0 :     if (typeid_is_int) {
    1344             :         /*
    1345             :          * Copy the int from the type ID object field to the type ID struct
    1346             :          * field.
    1347             :          */
    1348           0 :         ret = typeid_int_copy(DPO(data, ttypeid->offset),
    1349           0 :                               (intptr_t)tos[3 + (element-1)*3].ptr, ttypeid_univ);
    1350           0 :     } else if (typeid_is_oid) {
    1351             :         /*
    1352             :          * Copy the OID from the type ID object field to the type ID struct
    1353             :          * field.
    1354             :          */
    1355           0 :         ret = der_copy_oid(tos[3 + (element-1)*3].ptr, DPO(data, ttypeid->offset));
    1356             :     } else
    1357           0 :         enotsup = 1;
    1358             : 
    1359             :     /*
    1360             :      * If the app did not already encode the open type, we can't help it if we
    1361             :      * don't know what it is.
    1362             :      */
    1363           0 :     if (enotsup)
    1364           0 :         return ENOTSUP;
    1365             : 
    1366           0 :     tactual_type = &tos[(element-1)*3 + 4];
    1367             : 
    1368           0 :     if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
    1369           0 :         struct heim_base_data *os = DPO(data, topentype->offset);
    1370           0 :         const void * const *d = DPOC(data, t->offset + sizeof(element));
    1371             : 
    1372           0 :         while (sizeof(void *) != sizeof(element) &&
    1373           0 :                ((uintptr_t)d) % sizeof(void *) != 0) {
    1374           0 :             d = (void *)(((char *)d) + sizeof(element));
    1375             :         }
    1376             : 
    1377           0 :         os->length = _asn1_length(tactual_type->ptr, *d);
    1378           0 :         if ((os->data = malloc(os->length)) == NULL)
    1379           0 :             return ENOMEM;
    1380           0 :         ret = _asn1_encode(tactual_type->ptr, (os->length - 1) + (unsigned char *)os->data, os->length, *d, &sz);
    1381             :     } else {
    1382           0 :         struct heim_base_data *os;
    1383           0 :         const void * const *val =
    1384           0 :             DPOC(data, t->offset + sizeof(element) + sizeof(*lenp));
    1385             : 
    1386           0 :         if ((os = calloc(len, sizeof(*os))) == NULL)
    1387           0 :             return ENOMEM;
    1388             : 
    1389           0 :         *lenp = len;
    1390           0 :         for (i = 0; ret == 0 && i < len; i++) {
    1391           0 :             os[i].length = _asn1_length(tactual_type->ptr, val[i]);
    1392           0 :             if ((os[i].data = malloc(os[i].length)) == NULL)
    1393           0 :                 ret = ENOMEM;
    1394           0 :             if (ret == 0)
    1395           0 :                 ret = _asn1_encode(tactual_type->ptr, (os[i].length - 1) + (unsigned char *)os[i].data, os[i].length,
    1396           0 :                                    val[i], &sz);
    1397             :         }
    1398           0 :         if (ret) {
    1399           0 :             for (i = 0; i < (*lenp); i++)
    1400           0 :                 free(os[i].data);
    1401           0 :             free(os);
    1402           0 :             *lenp = 0;
    1403           0 :             return ret;
    1404             :         }
    1405           0 :         *(struct heim_base_data **)DPO(data, topentype->offset + sizeof(len)) = os;
    1406             :     }
    1407           0 :     return ret;
    1408             : }
    1409             : 
    1410             : int
    1411    54463165 : _asn1_encode(const struct asn1_template *t, unsigned char *p, size_t len, const void *data, size_t *size)
    1412             : {
    1413    54463165 :     const struct asn1_template *tbase = t;
    1414    54463165 :     size_t elements = A1_HEADER_LEN(t);
    1415    54463165 :     int ret = 0;
    1416    54463165 :     size_t oldlen = len;
    1417             : 
    1418    54463165 :     t += A1_HEADER_LEN(t);
    1419             : 
    1420   142188721 :     while (elements) {
    1421    87725556 :         switch (t->tt & A1_OP_MASK) {
    1422         671 :         case A1_OP_OPENTYPE_OBJSET: {
    1423         671 :             size_t opentypeid = t->tt & ((1<<10)-1);
    1424         671 :             size_t opentype = (t->tt >> 10) & ((1<<10)-1);
    1425         671 :             ret = _asn1_encode_open_type(t, data,
    1426             :                                          template4member(tbase, opentypeid),
    1427             :                                          template4member(tbase, opentype));
    1428         671 :             if (ret)
    1429           0 :                 return ret;
    1430         671 :             break;
    1431             :         }
    1432    20546367 :         case A1_OP_NAME: break;
    1433         671 :         case A1_OP_DEFVAL: break;
    1434           0 :         case A1_OP_TYPE_DECORATE_EXTERN: break;
    1435           0 :         case A1_OP_TYPE_DECORATE: break;
    1436    13630623 :         case A1_OP_TYPE:
    1437             :         case A1_OP_TYPE_EXTERN: {
    1438      500316 :             size_t newsize;
    1439    13630623 :             const void *el = DPOC(data, t->offset);
    1440             : 
    1441    13630623 :             if (t->tt & A1_FLAG_OPTIONAL) {
    1442        2693 :                 void **pel = (void **)el;
    1443        2693 :                 if (*pel == NULL)
    1444    13130307 :                     break;
    1445        2056 :                 el = *pel;
    1446    13627930 :             } else if ((t->tt & A1_FLAG_DEFAULT) && elements > 1) {
    1447           0 :                 const struct asn1_template *tdefval = t - 1;
    1448             :                 /* Compare tdefval to whatever's at `el' */
    1449           0 :                 if (tdefval->tt & A1_DV_BOOLEAN) {
    1450           0 :                     const int *i = (void *)(char *)el;
    1451             : 
    1452           0 :                     if ((*i && tdefval->ptr) || (!*i && !tdefval->ptr))
    1453             :                         break;
    1454           0 :                 } else if (tdefval->tt & A1_DV_INTEGER64) {
    1455           0 :                     const int64_t *i = (void *)(char *)el;
    1456             : 
    1457           0 :                     if (*i == (int64_t)(intptr_t)tdefval->ptr)
    1458           0 :                         break;
    1459           0 :                 } else if (tdefval->tt & A1_DV_INTEGER32) {
    1460           0 :                     const int32_t *i = (void *)(char *)el;
    1461             : 
    1462           0 :                     if ((int64_t)(intptr_t)tdefval->ptr <= INT_MAX &&
    1463           0 :                         (int64_t)(intptr_t)tdefval->ptr >= INT_MIN &&
    1464           0 :                         *i == (int32_t)(intptr_t)tdefval->ptr)
    1465           0 :                         break;
    1466           0 :                 } else if (tdefval->tt & A1_DV_INTEGER) {
    1467           0 :                     const struct heim_integer *i = (void *)(char *)el;
    1468             : 
    1469           0 :                     if (der_heim_integer_cmp(i, tdefval->ptr) == 0)
    1470           0 :                         break;
    1471           0 :                 } else if (tdefval->tt & A1_DV_UTF8STRING) {
    1472           0 :                     const char * const *s = el;
    1473             : 
    1474           0 :                     if (*s && strcmp(*s, tdefval->ptr) == 0)
    1475           0 :                         break;
    1476             :                 } else {
    1477           0 :                     abort();
    1478             :                 }
    1479             :             }
    1480             : 
    1481    13629986 :             if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
    1482    13624329 :                 ret = _asn1_encode(t->ptr, p, len, el, &newsize);
    1483             :             } else {
    1484        5657 :                 const struct asn1_type_func *f = t->ptr;
    1485        5657 :                 ret = (f->encode)(p, len, el, &newsize);
    1486             :             }
    1487             : 
    1488    13629986 :             if (ret)
    1489           0 :                 return ret;
    1490    13629986 :             p -= newsize; len -= newsize;
    1491             : 
    1492    13629986 :             break;
    1493             :         }
    1494    37444606 :         case A1_OP_TAG: {
    1495    37444606 :             const void *olddata = data;
    1496    37444606 :             size_t l, datalen = 0;
    1497    37444606 :             int replace_tag = 0;
    1498             : 
    1499             :             /*
    1500             :              * XXX If this type (chasing t->ptr through IMPLICIT tags, if this
    1501             :              * one is too) till we find a non-TTag) is a [UNIVERSAL SET] type,
    1502             :              * then we have to sort [a copy of] its template by tag, then
    1503             :              * encode the SET using that sorted template.  These SETs will
    1504             :              * generally be small, so when they are we might want to allocate
    1505             :              * the copy on the stack and insertion sort it.  We'll need a
    1506             :              * utility function to do all of this.
    1507             :              */
    1508             : 
    1509    37444606 :             data = DPOC(data, t->offset);
    1510             : 
    1511    37444606 :             if (t->tt & A1_FLAG_OPTIONAL) {
    1512     4176183 :                 void **el = (void **)data;
    1513     4176183 :                 if (*el == NULL) {
    1514     1728661 :                     data = olddata;
    1515    37444606 :                     break;
    1516             :                 }
    1517     2295439 :                 data = *el;
    1518    33268423 :             } else if ((t->tt & A1_FLAG_DEFAULT) && elements > 1) {
    1519         671 :                 const struct asn1_template *tdefval = t - 1;
    1520         671 :                 int exclude = 0;
    1521             : 
    1522             :                 /* Compare tdefval to whatever's at `data' */
    1523         671 :                 if (tdefval->tt & A1_DV_BOOLEAN) {
    1524         671 :                     const int *i = (void *)(char *)data;
    1525             : 
    1526         671 :                     if ((*i && tdefval->ptr) || (!*i && !tdefval->ptr))
    1527         671 :                         exclude = 1;
    1528           0 :                 } else if (tdefval->tt & A1_DV_INTEGER64) {
    1529           0 :                     const int64_t *i = (void *)(char *)data;
    1530             : 
    1531           0 :                     if (*i == (int64_t)(intptr_t)tdefval->ptr)
    1532           0 :                         exclude = 1;
    1533           0 :                 } else if (tdefval->tt & A1_DV_INTEGER32) {
    1534           0 :                     const int32_t *i = (void *)(char *)data;
    1535             : 
    1536           0 :                     if ((int64_t)(intptr_t)tdefval->ptr <= INT_MAX &&
    1537           0 :                         (int64_t)(intptr_t)tdefval->ptr >= INT_MIN &&
    1538           0 :                         *i == (int32_t)(intptr_t)tdefval->ptr)
    1539           0 :                         exclude = 1;
    1540           0 :                 } else if (tdefval->tt & A1_DV_INTEGER) {
    1541           0 :                     const struct heim_integer *i = (void *)(char *)data;
    1542             : 
    1543           0 :                     if (der_heim_integer_cmp(i, tdefval->ptr) == 0)
    1544           0 :                         break;
    1545           0 :                 } else if (tdefval->tt & A1_DV_UTF8STRING) {
    1546           0 :                     const char * const *s = data;
    1547             : 
    1548           0 :                     if (*s && strcmp(*s, tdefval->ptr) == 0)
    1549           0 :                         exclude = 1;
    1550             :                 } else {
    1551           0 :                     abort();
    1552             :                 }
    1553         671 :                 if (exclude) {
    1554         671 :                     data = olddata;
    1555         671 :                     break;
    1556             :                 }
    1557             :             }
    1558             : 
    1559    35652206 :             replace_tag = (t->tt & A1_FLAG_IMPLICIT) && is_tagged(t->ptr);
    1560             : 
    1561             :             /* IMPLICIT tags need special handling (see gen_encode.c) */
    1562    35652206 :             if (replace_tag) {
    1563         525 :                 unsigned char *pfree, *psave = p;
    1564           0 :                 Der_class found_class;
    1565         525 :                 Der_type found_type = 0;
    1566           0 :                 unsigned int found_tag;
    1567         525 :                 size_t lensave = len;
    1568         525 :                 size_t oldtaglen = 0;
    1569         525 :                 size_t taglen = der_length_tag(A1_TAG_TAG(t->tt));;
    1570             : 
    1571             :                 /* Allocate a buffer at least as big as we need */
    1572         525 :                 len = _asn1_length(t->ptr, data) + taglen;
    1573         525 :                 if ((p = pfree = malloc(len)) == NULL) {
    1574           0 :                     ret = ENOMEM;
    1575             :                 } else {
    1576             :                     /*
    1577             :                      * Encode into it (with the wrong tag, which we'll replace
    1578             :                      * below).
    1579             :                      */
    1580         525 :                     p += len - 1;
    1581         525 :                     ret = _asn1_encode(t->ptr, p, len, data, &datalen);
    1582             :                 }
    1583         525 :                 if (ret == 0) {
    1584             :                     /* Get the old tag and, critically, its length */
    1585         525 :                     len -= datalen; p -= datalen;
    1586         525 :                     ret = der_get_tag(p + 1, datalen, &found_class, &found_type,
    1587             :                                       &found_tag, &oldtaglen);
    1588             :                 }
    1589         525 :                 if (ret == 0) {
    1590             :                     /* Drop the old tag */
    1591         525 :                     len += oldtaglen; p += oldtaglen;
    1592             :                     /* Put the new tag */
    1593         525 :                     ret = der_put_tag(p, len,
    1594         525 :                                       A1_TAG_CLASS(t->tt),
    1595             :                                       found_type,
    1596         525 :                                       A1_TAG_TAG(t->tt), &l);
    1597             :                 }
    1598         525 :                 if (ret == 0) {
    1599             :                     /* Copy the encoding where it belongs */
    1600         525 :                     psave -= (datalen + l - oldtaglen);
    1601         525 :                     lensave -= (datalen + l - oldtaglen);
    1602         525 :                     memcpy(psave + 1, p + 1 - l, datalen + l - oldtaglen);
    1603         525 :                     p = psave;
    1604         525 :                     len = lensave;
    1605             :                 }
    1606         525 :                 free(pfree);
    1607             :             } else {
    1608             :                 /* Easy case */
    1609    35651681 :                 ret = _asn1_encode(t->ptr, p, len, data, &datalen);
    1610    35651681 :                 if (ret)
    1611           0 :                     return ret;
    1612             : 
    1613    35651681 :                 len -= datalen; p -= datalen;
    1614             : 
    1615    36961858 :                 ret = der_put_length_and_tag(p, len, datalen,
    1616    35651681 :                                              A1_TAG_CLASS(t->tt),
    1617    35651681 :                                              A1_TAG_TYPE(t->tt),
    1618    35651681 :                                              A1_TAG_TAG(t->tt), &l);
    1619    35651681 :                 if (ret == 0) {
    1620    35651681 :                     p -= l; len -= l;
    1621             :                 }
    1622             :             }
    1623    35652206 :             if (ret)
    1624           0 :                 return ret;
    1625             : 
    1626    34342029 :             data = olddata;
    1627             : 
    1628    34342029 :             break;
    1629             :         }
    1630    12563756 :         case A1_OP_PARSE: {
    1631    12563756 :             unsigned int type = A1_PARSE_TYPE(t->tt);
    1632      462162 :             size_t newsize;
    1633    12563756 :             const void *el = DPOC(data, t->offset);
    1634             : 
    1635    12563756 :             if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
    1636           0 :                 ABORT_ON_ERROR();
    1637           0 :                 return ASN1_PARSE_ERROR;
    1638             :             }
    1639             : 
    1640    12563756 :             ret = (asn1_template_prim[type].encode)(p, len, el, &newsize);
    1641    12563756 :             if (ret)
    1642           0 :                 return ret;
    1643    12563756 :             p -= newsize; len -= newsize;
    1644             : 
    1645    12563756 :             break;
    1646             :         }
    1647        2342 :         case A1_OP_SETOF: {
    1648        2342 :             const struct template_of *el = DPOC(data, t->offset);
    1649        2342 :             size_t ellen = _asn1_sizeofType(t->ptr);
    1650           0 :             heim_octet_string *val;
    1651        2342 :             unsigned char *elptr = el->val;
    1652           0 :             size_t i, totallen;
    1653             : 
    1654        2342 :             if (el->len == 0)
    1655         312 :                 break;
    1656             : 
    1657        2030 :             if (el->len > UINT_MAX/sizeof(val[0]))
    1658           0 :                 return ERANGE;
    1659             : 
    1660        2030 :             val = calloc(el->len, sizeof(val[0]));
    1661        2030 :             if (val == NULL)
    1662           0 :                 return ENOMEM;
    1663             : 
    1664        4202 :             for(totallen = 0, i = 0; i < el->len; i++) {
    1665           0 :                 unsigned char *next;
    1666           0 :                 size_t l;
    1667             : 
    1668        2172 :                 val[i].length = _asn1_length(t->ptr, elptr);
    1669        2172 :                 if (val[i].length) {
    1670        2172 :                     val[i].data = malloc(val[i].length);
    1671        2172 :                     if (val[i].data == NULL) {
    1672           0 :                         ret = ENOMEM;
    1673           0 :                         break;
    1674             :                     }
    1675             :                 }
    1676             : 
    1677        2172 :                 ret = _asn1_encode(t->ptr, DPO(val[i].data, val[i].length - 1),
    1678        2172 :                                    val[i].length, elptr, &l);
    1679        2172 :                 if (ret)
    1680           0 :                     break;
    1681             : 
    1682        2172 :                 next = elptr + ellen;
    1683        2172 :                 if (next < elptr) {
    1684           0 :                     ret = ASN1_OVERFLOW;
    1685           0 :                     break;
    1686             :                 }
    1687        2172 :                 elptr = next;
    1688        2172 :                 totallen += val[i].length;
    1689             :             }
    1690        2030 :             if (ret == 0 && totallen > len)
    1691           0 :                 ret = ASN1_OVERFLOW;
    1692        2030 :             if (ret) {
    1693           0 :                 for (i = 0; i < el->len; i++)
    1694           0 :                     free(val[i].data);
    1695           0 :                 free(val);
    1696           0 :                 return ret;
    1697             :             }
    1698             : 
    1699        2030 :             len -= totallen;
    1700             : 
    1701        2030 :             qsort(val, el->len, sizeof(val[0]), _heim_der_set_sort);
    1702             : 
    1703        2030 :             i = el->len - 1;
    1704           0 :             do {
    1705        2172 :                 p -= val[i].length;
    1706        2172 :                 memcpy(p + 1, val[i].data, val[i].length);
    1707        2172 :                 free(val[i].data);
    1708        2172 :             } while(i-- > 0);
    1709        2030 :             free(val);
    1710             : 
    1711        2030 :             break;
    1712             : 
    1713             :         }
    1714     2168163 :         case A1_OP_SEQOF: {
    1715     2168163 :             struct template_of *el = DPO(data, t->offset);
    1716     2168163 :             size_t ellen = _asn1_sizeofType(t->ptr);
    1717       80717 :             size_t newsize;
    1718       80717 :             unsigned int i;
    1719     2168163 :             unsigned char *elptr = el->val;
    1720             : 
    1721     2168163 :             if (el->len == 0)
    1722     2087446 :                 break;
    1723             : 
    1724     1943687 :             elptr += ellen * (el->len - 1);
    1725             : 
    1726     5715115 :             for (i = 0; i < el->len; i++) {
    1727     3771428 :                 ret = _asn1_encode(t->ptr, p, len,
    1728             :                                    elptr,
    1729             :                                    &newsize);
    1730     3771428 :                 if (ret)
    1731           0 :                     return ret;
    1732     3771428 :                 p -= newsize; len -= newsize;
    1733     3771428 :                 elptr -= ellen;
    1734             :             }
    1735             : 
    1736     1872496 :             break;
    1737             :         }
    1738      497347 :         case A1_OP_BMEMBER: {
    1739      497347 :             const struct asn1_template *bmember = t->ptr;
    1740      497347 :             size_t bsize = bmember->offset;
    1741      497347 :             size_t belements = A1_HEADER_LEN(bmember);
    1742       17809 :             size_t pos;
    1743      497347 :             unsigned char c = 0;
    1744      497347 :             unsigned int bitset = 0;
    1745      497347 :             int rfc1510 = (bmember->tt & A1_HBF_RFC1510);
    1746             : 
    1747      497347 :             bmember += belements;
    1748             : 
    1749      497347 :             if (rfc1510)
    1750      479538 :                 pos = 31;
    1751             :             else
    1752           0 :                 pos = bmember->offset;
    1753             : 
    1754     7505969 :             while (belements && len) {
    1755     8500663 :                 while (bmember->offset / 8 < pos / 8) {
    1756     1492041 :                     if (rfc1510 || bitset || c) {
    1757     1492041 :                         if (len < 1)
    1758           0 :                             return ASN1_OVERFLOW;
    1759     1492041 :                         *p-- = c; len--;
    1760             :                     }
    1761     1492041 :                     c = 0;
    1762     1492041 :                     pos -= 8;
    1763             :                 }
    1764     7008622 :                 _asn1_bmember_put_bit(&c, data, bmember->offset, bsize, &bitset);
    1765     7008622 :                 belements--; bmember--;
    1766             :             }
    1767      497347 :             if (rfc1510 || bitset) {
    1768      497347 :                 if (len < 1)
    1769           0 :                     return ASN1_OVERFLOW;
    1770      497347 :                 *p-- = c; len--;
    1771             :             }
    1772             : 
    1773      497347 :             if (len < 1)
    1774           0 :                 return ASN1_OVERFLOW;
    1775      497347 :             if (rfc1510 || bitset == 0)
    1776      497347 :                 *p-- = 0;
    1777             :             else
    1778           0 :                 *p-- = bitset - 1;
    1779             : 
    1780      497347 :             len--;
    1781             : 
    1782      497347 :             break;
    1783             :         }
    1784       91233 :         case A1_OP_CHOICE: {
    1785       91233 :             const struct asn1_template *choice = t->ptr;
    1786       91233 :             const unsigned int *element = DPOC(data, choice->offset);
    1787        3316 :             size_t datalen;
    1788        3316 :             const void *el;
    1789             : 
    1790       91233 :             if (*element > A1_HEADER_LEN(choice)) {
    1791           0 :                 printf("element: %d\n", *element);
    1792           0 :                 return ASN1_PARSE_ERROR;
    1793             :             }
    1794             : 
    1795       91233 :             if (*element == 0) {
    1796           0 :                 if (choice->tt) {
    1797             :                     /* This is an extensible CHOICE */
    1798           0 :                     ret += der_put_octet_string(p, len,
    1799           0 :                                                 DPOC(data, choice->tt), &datalen);
    1800           0 :                     len -= datalen; p -= datalen;
    1801             :                 } /* else this is really an error -- XXX what to do? */
    1802             :             } else {
    1803       91233 :                 choice += *element;
    1804       91233 :                 el = DPOC(data, choice->offset);
    1805       91233 :                 ret = _asn1_encode(choice->ptr, p, len, el, &datalen);
    1806       91233 :                 if (ret)
    1807           0 :                     return ret;
    1808       91233 :                 len -= datalen; p -= datalen;
    1809             :             }
    1810             : 
    1811       91233 :             break;
    1812             :         }
    1813           0 :         default:
    1814           0 :             ABORT_ON_ERROR();
    1815             :         }
    1816    87725556 :         t--;
    1817    87725556 :         elements--;
    1818             :     }
    1819    54463165 :     if (size)
    1820    54463165 :         *size = oldlen - len;
    1821             : 
    1822    52460333 :     return 0;
    1823             : }
    1824             : 
    1825             : static size_t
    1826           0 : _asn1_length_open_type_helper(const struct asn1_template *t,
    1827             :                               size_t sz)
    1828             : {
    1829           0 :     const struct asn1_template *tinner = t->ptr;
    1830             : 
    1831           0 :     switch (t->tt & A1_OP_MASK) {
    1832           0 :     case A1_OP_TAG:
    1833             :         /* XXX Not tail-recursive :( */
    1834           0 :         sz = _asn1_length_open_type_helper(tinner, sz);
    1835           0 :         sz += der_length_len(sz);
    1836           0 :         sz += der_length_tag(A1_TAG_TAG(t->tt));
    1837           0 :         return sz;
    1838           0 :     default:
    1839           0 :         return sz;
    1840             :     }
    1841             : }
    1842             : 
    1843             : static size_t
    1844           0 : _asn1_length_open_type_id(const struct asn1_template *t,
    1845             :                           const void *data)
    1846             : {
    1847           0 :     struct asn1_template pretend[2] = {
    1848             :         { 0, 0, ((void*)(uintptr_t)1) },
    1849             :     };
    1850           0 :     pretend[1] = *t;
    1851           0 :     while ((t->tt & A1_OP_MASK) == A1_OP_TAG)
    1852           0 :         t = t->ptr;
    1853           0 :     pretend[0].offset = t->offset;
    1854           0 :     return _asn1_length(pretend, data);
    1855             : }
    1856             : 
    1857             : /* See commentary in _asn1_encode_open_type() */
    1858             : static size_t
    1859         671 : _asn1_length_open_type(const struct asn1_template *tbase,
    1860             :                        const struct asn1_template *t,
    1861             :                        const void *data,
    1862             :                        const struct asn1_template *ttypeid,
    1863             :                        const struct asn1_template *topentype)
    1864             : {
    1865         671 :     const struct asn1_template *ttypeid_univ = ttypeid;
    1866           0 :     const struct asn1_template *tactual_type;
    1867         671 :     const struct asn1_template *tos = t->ptr;
    1868         671 :     const unsigned int *lenp = NULL;
    1869         671 :     unsigned int len = 1;
    1870         671 :     size_t sz = 0;
    1871           0 :     size_t i;
    1872         671 :     int element = *(const int *)DPOC(data, t->offset);
    1873         671 :     int typeid_is_oid = 0;
    1874         671 :     int typeid_is_int = 0;
    1875             : 
    1876             :     /* If nothing to encode, we add nothing to the length */
    1877         671 :     if (element == 0 || element >= A1_HEADER_LEN(tos) + 1)
    1878         183 :         return 0;
    1879         488 :     if (t->tt & A1_OS_OT_IS_ARRAY) {
    1880           0 :         len = *(const unsigned int *)DPOC(data, t->offset + sizeof(element));
    1881           0 :         if (!len)
    1882           0 :             return 0;
    1883             :     }
    1884             : 
    1885             :     /* Work out the type ID field's type */
    1886         488 :     while (((ttypeid_univ->tt & A1_OP_MASK) == A1_OP_TAG &&
    1887         488 :             A1_TAG_CLASS(ttypeid_univ->tt) == ASN1_C_CONTEXT) ||
    1888         488 :            ((ttypeid_univ->tt & A1_OP_MASK) == A1_OP_TYPE)) {
    1889           0 :         ttypeid_univ = ttypeid_univ->ptr;
    1890           0 :         ttypeid_univ++;
    1891             :     }
    1892         488 :     switch (ttypeid_univ->tt & A1_OP_MASK) {
    1893         488 :     case A1_OP_TAG:
    1894         488 :         if (A1_TAG_CLASS(ttypeid_univ->tt) != ASN1_C_UNIV)
    1895           0 :             return 0;
    1896         488 :         switch (A1_TAG_TAG(ttypeid_univ->tt)) {
    1897         488 :         case UT_OID:
    1898         488 :             typeid_is_oid = 1;
    1899         488 :             break;
    1900           0 :         case UT_Integer: {
    1901           0 :             const struct asn1_template *tint = ttypeid_univ->ptr;
    1902             : 
    1903           0 :             tint++;
    1904           0 :             if ((tint->tt & A1_OP_MASK) != A1_OP_PARSE ||
    1905           0 :                 A1_PARSE_TYPE(tint->tt) != A1T_INTEGER)
    1906           0 :                 return 0;
    1907           0 :             typeid_is_int = 1;
    1908           0 :             break;
    1909             :         }
    1910           0 :         default: return 0;
    1911             :         }
    1912         488 :         break;
    1913           0 :     default: return 0;
    1914             :     }
    1915         488 :     if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
    1916         488 :         struct heim_base_data *os = DPO(data, topentype->offset);
    1917             : 
    1918         488 :         if (os->length && os->data)
    1919         488 :             return 0;
    1920             :     } else {
    1921           0 :         struct heim_base_data **os = DPO(data, topentype->offset + sizeof(len));
    1922             : 
    1923           0 :         while (sizeof(void *) != sizeof(unsigned int) &&
    1924           0 :                ((uintptr_t)os) % sizeof(void *) != 0)
    1925           0 :             os = (void *)(((char *)os) + sizeof(unsigned int));
    1926             : 
    1927           0 :         lenp = DPOC(data, topentype->offset);
    1928           0 :         if (*lenp == len && os[0]->length && os[0]->data)
    1929           0 :             return 0;
    1930             :     }
    1931             : 
    1932             :     /* Compute the size of the type ID field */
    1933           0 :     if (typeid_is_int) {
    1934           0 :         int64_t i8;
    1935           0 :         int32_t i4;
    1936             : 
    1937           0 :         switch (ttypeid_univ->offset) {
    1938           0 :         case 8:
    1939           0 :             i8 = (intptr_t)t->ptr;
    1940           0 :             sz = _asn1_length_open_type_id(ttypeid, &i8);
    1941           0 :             i8 = 0;
    1942           0 :             sz -= _asn1_length_open_type_id(ttypeid, &i8);
    1943           0 :             break;
    1944           0 :         case 4:
    1945           0 :             i4 = (intptr_t)t->ptr;
    1946           0 :             sz = _asn1_length_open_type_id(ttypeid, &i4);
    1947           0 :             i4 = 0;
    1948           0 :             sz -= _asn1_length_open_type_id(ttypeid, &i8);
    1949           0 :             break;
    1950           0 :         default:
    1951           0 :             return 0;
    1952             :         }
    1953           0 :     } else if (typeid_is_oid) {
    1954           0 :         heim_oid no_oid = { 0, 0 };
    1955             : 
    1956           0 :         sz = _asn1_length_open_type_id(ttypeid, tos[3 + (element - 1)*3].ptr);
    1957           0 :         sz -= _asn1_length_open_type_id(ttypeid, &no_oid);
    1958             :     }
    1959             : 
    1960           0 :     tactual_type = &tos[(element-1)*3 + 4];
    1961             : 
    1962             :     /* Compute the size of the encoded value(s) */
    1963           0 :     if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
    1964           0 :         const void * const *d = DPOC(data, t->offset + sizeof(element));
    1965             : 
    1966           0 :         while (sizeof(void *) != sizeof(element) &&
    1967           0 :                ((uintptr_t)d) % sizeof(void *) != 0)
    1968           0 :             d = (void *)(((char *)d) + sizeof(element));
    1969           0 :         if (*d)
    1970           0 :             sz += _asn1_length(tactual_type->ptr, *d);
    1971             :     } else {
    1972           0 :         size_t bodysz;
    1973           0 :         const void * const * val =
    1974           0 :             DPOC(data, t->offset + sizeof(element) + sizeof(*lenp));
    1975             : 
    1976             :         /* Compute the size of the encoded SET OF / SEQUENCE OF body */
    1977           0 :         for (i = 0, bodysz = 0; i < len; i++) {
    1978           0 :             if (val[i])
    1979           0 :                 bodysz += _asn1_length(tactual_type->ptr, val[i]);
    1980             :         }
    1981             : 
    1982             :         /*
    1983             :          * We now know the size of the body of the SET OF or SEQUENCE OF.  Now
    1984             :          * we just need to count the length of all the TLs on the outside.
    1985             :          */
    1986           0 :         sz += _asn1_length_open_type_helper(topentype, bodysz);
    1987             :     }
    1988           0 :     return sz;
    1989             : }
    1990             : 
    1991             : size_t
    1992    54483108 : _asn1_length(const struct asn1_template *t, const void *data)
    1993             : {
    1994    54483108 :     const struct asn1_template *tbase = t;
    1995    54483108 :     size_t elements = A1_HEADER_LEN(t);
    1996    54483108 :     size_t ret = 0;
    1997             : 
    1998    54483108 :     t += A1_HEADER_LEN(t);
    1999             : 
    2000   142238061 :     while (elements) {
    2001    87754953 :         switch (t->tt & A1_OP_MASK) {
    2002         671 :         case A1_OP_OPENTYPE_OBJSET: {
    2003         671 :             size_t opentypeid = t->tt & ((1<<10)-1);
    2004         671 :             size_t opentype = (t->tt >> 10) & ((1<<10)-1);
    2005         671 :             ret += _asn1_length_open_type(tbase, t, data,
    2006             :                                           template4member(tbase, opentypeid),
    2007             :                                           template4member(tbase, opentype));
    2008         671 :             break;
    2009             :         }
    2010    20553288 :         case A1_OP_NAME: break;
    2011         671 :         case A1_OP_DEFVAL: break;
    2012           0 :         case A1_OP_TYPE_DECORATE_EXTERN: break;
    2013           0 :         case A1_OP_TYPE_DECORATE: break;
    2014    13639527 :         case A1_OP_TYPE:
    2015             :         case A1_OP_TYPE_EXTERN: {
    2016    13639527 :             const void *el = DPOC(data, t->offset);
    2017             : 
    2018    13639527 :             if (t->tt & A1_FLAG_OPTIONAL) {
    2019        2893 :                 void **pel = (void **)el;
    2020        2893 :                 if (*pel == NULL)
    2021         654 :                     break;
    2022        2239 :                 el = *pel;
    2023    13636634 :             } else if ((t->tt & A1_FLAG_DEFAULT) && elements > 1) {
    2024           0 :                 const struct asn1_template *tdefval = t - 1;
    2025             : 
    2026             :                 /* Compare tdefval to whatever's at `el' */
    2027           0 :                 if (tdefval->tt & A1_DV_BOOLEAN) {
    2028           0 :                     const int *i = (void *)(char *)el;
    2029             : 
    2030           0 :                     if ((*i && tdefval->ptr) || (!*i && !tdefval->ptr))
    2031             :                         break;
    2032           0 :                 } else if (tdefval->tt & A1_DV_INTEGER64) {
    2033           0 :                     const int64_t *i = (void *)(char *)el;
    2034             : 
    2035           0 :                     if (*i == (int64_t)(intptr_t)tdefval->ptr)
    2036           0 :                         break;
    2037           0 :                 } else if (tdefval->tt & A1_DV_INTEGER32) {
    2038           0 :                     const int32_t *i = (void *)(char *)el;
    2039             : 
    2040           0 :                     if ((int64_t)(intptr_t)tdefval->ptr <= INT_MAX &&
    2041           0 :                         (int64_t)(intptr_t)tdefval->ptr >= INT_MIN &&
    2042           0 :                         *i == (int32_t)(intptr_t)tdefval->ptr)
    2043           0 :                         break;
    2044           0 :                 } else if (tdefval->tt & A1_DV_INTEGER) {
    2045           0 :                     const struct heim_integer *i = (void *)(char *)el;
    2046             : 
    2047           0 :                     if (der_heim_integer_cmp(i, tdefval->ptr) == 0)
    2048           0 :                         break;
    2049           0 :                 } else if (tdefval->tt & A1_DV_UTF8STRING) {
    2050           0 :                     const char * const *s = el;
    2051             : 
    2052           0 :                     if (*s && strcmp(*s, tdefval->ptr) == 0)
    2053           0 :                         break;
    2054             :                 } else {
    2055           0 :                     abort();
    2056             :                 }
    2057             :             }
    2058             : 
    2059    13638873 :             if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
    2060    13631325 :                 ret += _asn1_length(t->ptr, el);
    2061             :             } else {
    2062        7548 :                 const struct asn1_type_func *f = t->ptr;
    2063        7548 :                 ret += (f->length)(el);
    2064             :             }
    2065    13138557 :             break;
    2066             :         }
    2067    37451848 :         case A1_OP_TAG: {
    2068     1373245 :             size_t datalen;
    2069    37451848 :             const void *olddata = data;
    2070    37451848 :             size_t oldtaglen = 0;
    2071             : 
    2072    37451848 :             data = DPO(data, t->offset);
    2073             : 
    2074    37451848 :             if (t->tt & A1_FLAG_OPTIONAL) {
    2075     4176305 :                 void **el = (void **)data;
    2076     4176305 :                 if (*el == NULL) {
    2077     1728726 :                     data = olddata;
    2078     1728726 :                     break;
    2079             :                 }
    2080     2295496 :                 data = *el;
    2081    33275543 :             } else if ((t->tt & A1_FLAG_DEFAULT) && elements > 1) {
    2082         671 :                 const struct asn1_template *tdefval = t - 1;
    2083         671 :                 int exclude = 0;
    2084             : 
    2085             :                 /* Compare tdefval to whatever's at `data' */
    2086         671 :                 if (tdefval->tt & A1_DV_BOOLEAN) {
    2087         671 :                     const int *i = (void *)(char *)data;
    2088             : 
    2089         671 :                     if ((*i && tdefval->ptr) || (!*i && !tdefval->ptr))
    2090         671 :                         exclude = 1;
    2091           0 :                 } else if (tdefval->tt & A1_DV_INTEGER64) {
    2092           0 :                     const int64_t *i = (void *)(char *)data;
    2093             : 
    2094           0 :                     if (*i == (int64_t)(intptr_t)tdefval->ptr)
    2095           0 :                         exclude = 1;
    2096           0 :                 } else if (tdefval->tt & A1_DV_INTEGER32) {
    2097           0 :                     const int32_t *i = (void *)(char *)data;
    2098             : 
    2099           0 :                     if ((int64_t)(intptr_t)tdefval->ptr <= INT_MAX &&
    2100           0 :                         (int64_t)(intptr_t)tdefval->ptr >= INT_MIN &&
    2101           0 :                         *i == (int32_t)(intptr_t)tdefval->ptr)
    2102           0 :                         exclude = 1;
    2103           0 :                 } else if (tdefval->tt & A1_DV_INTEGER) {
    2104           0 :                     const struct heim_integer *i = (void *)(char *)data;
    2105             : 
    2106           0 :                     if (der_heim_integer_cmp(i, tdefval->ptr) == 0)
    2107           0 :                         exclude = 1;
    2108           0 :                 } else if (tdefval->tt & A1_DV_UTF8STRING) {
    2109           0 :                     const char * const *s = data;
    2110             : 
    2111           0 :                     if (*s && strcmp(*s, tdefval->ptr) == 0)
    2112           0 :                         exclude = 1;
    2113             :                 } else {
    2114           0 :                     abort();
    2115             :                 }
    2116         671 :                 if (exclude) {
    2117         671 :                     data = olddata;
    2118         671 :                     break;
    2119             :                 }
    2120             :             }
    2121             : 
    2122    35659383 :             if (t->tt & A1_FLAG_IMPLICIT)
    2123         582 :                 oldtaglen = inner_type_taglen(t->ptr);
    2124             : 
    2125    35659383 :             datalen = _asn1_length(t->ptr, data);
    2126    35659383 :             ret += datalen;
    2127    35659383 :             ret += der_length_tag(A1_TAG_TAG(t->tt));
    2128    35659383 :             ret += oldtaglen ? -oldtaglen : der_length_len(datalen);
    2129    35659383 :             data = olddata;
    2130    35659383 :             break;
    2131             :         }
    2132    12567813 :         case A1_OP_PARSE: {
    2133    12567813 :             unsigned int type = A1_PARSE_TYPE(t->tt);
    2134    12567813 :             const void *el = DPOC(data, t->offset);
    2135             : 
    2136    12567813 :             if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
    2137           0 :                 ABORT_ON_ERROR();
    2138      462162 :                 break;
    2139             :             }
    2140    12567813 :             ret += (asn1_template_prim[type].length)(el);
    2141    12567813 :             break;
    2142             :         }
    2143     2171296 :         case A1_OP_SETOF:
    2144             :         case A1_OP_SEQOF: {
    2145     2171296 :             const struct template_of *el = DPOC(data, t->offset);
    2146     2171296 :             size_t ellen = _asn1_sizeofType(t->ptr);
    2147     2171296 :             const unsigned char *element = el->val;
    2148       80717 :             unsigned int i;
    2149             : 
    2150     5945697 :             for (i = 0; i < el->len; i++) {
    2151     3774401 :                 ret += _asn1_length(t->ptr, element);
    2152     3774401 :                 element += ellen;
    2153             :             }
    2154             : 
    2155     2090579 :             break;
    2156             :         }
    2157      497347 :         case A1_OP_BMEMBER: {
    2158      497347 :             const struct asn1_template *bmember = t->ptr;
    2159      497347 :             size_t size = bmember->offset;
    2160      497347 :             size_t belements = A1_HEADER_LEN(bmember);
    2161      497347 :             int rfc1510 = (bmember->tt & A1_HBF_RFC1510);
    2162             : 
    2163      497347 :             if (rfc1510) {
    2164      497347 :                 ret += 5;
    2165             :             } else {
    2166             : 
    2167           0 :                 ret += 1;
    2168             : 
    2169           0 :                 bmember += belements;
    2170             : 
    2171           0 :                 while (belements) {
    2172           0 :                     if (_asn1_bmember_isset_bit(data, bmember->offset, size)) {
    2173           0 :                         ret += (bmember->offset / 8) + 1;
    2174           0 :                         break;
    2175             :                     }
    2176           0 :                     belements--; bmember--;
    2177             :                 }
    2178             :             }
    2179      479538 :             break;
    2180             :         }
    2181       92715 :         case A1_OP_CHOICE: {
    2182       92715 :             const struct asn1_template *choice = t->ptr;
    2183       92715 :             const unsigned int *element = DPOC(data, choice->offset);
    2184             : 
    2185       92715 :             if (*element > A1_HEADER_LEN(choice))
    2186           0 :                 break;
    2187             : 
    2188       92715 :             if (*element == 0) {
    2189           0 :                 if (choice->tt)
    2190           0 :                     ret += der_length_octet_string(DPOC(data, choice->tt));
    2191             :             } else {
    2192       92715 :                 choice += *element;
    2193       92715 :                 ret += _asn1_length(choice->ptr, DPOC(data, choice->offset));
    2194             :             }
    2195       89399 :             break;
    2196             :         }
    2197           0 :         default:
    2198           0 :             ABORT_ON_ERROR();
    2199     3217342 :             break;
    2200             :         }
    2201    87754953 :         elements--;
    2202    87754953 :         t--;
    2203             :     }
    2204    54483108 :     return ret;
    2205             : }
    2206             : 
    2207             : /* See commentary in _asn1_decode_open_type() */
    2208             : static void
    2209        5002 : _asn1_free_open_type(const struct asn1_template *t, /* object set template */
    2210             :                      void *data)
    2211             : {
    2212         216 :     const struct asn1_template *tactual_type;
    2213        5002 :     const struct asn1_template *tos = t->ptr;
    2214        5002 :     unsigned int *lenp = NULL;  /* Pointer to array length field */
    2215        5002 :     unsigned int len = 1;       /* Array length */
    2216         216 :     size_t i;
    2217         216 :     void **dp;
    2218         216 :     void **val;
    2219        5002 :     int *elementp = DPO(data, t->offset);   /* Choice enum pointer */
    2220             : 
    2221             :     /* XXX We assume sizeof(enum) == sizeof(int) */
    2222        5002 :     if (!*elementp || *elementp >= A1_HEADER_LEN(tos) + 1)
    2223        1440 :         return; /* Unknown choice -> it's not decoded, nothing to free here */
    2224        3466 :     tactual_type = tos[3*(*elementp - 1) + 4].ptr;
    2225             : 
    2226        3466 :     if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
    2227        3466 :         dp = DPO(data, t->offset + sizeof(*elementp));
    2228        3466 :         while (sizeof(void *) != sizeof(*elementp) &&
    2229        6932 :                ((uintptr_t)dp) % sizeof(void *) != 0)
    2230        3466 :             dp = (void *)(((char *)dp) + sizeof(*elementp));
    2231        3466 :         if (*dp) {
    2232        3466 :             _asn1_free(tactual_type, *dp);
    2233        3466 :             free(*dp);
    2234        3466 :             *dp = NULL;
    2235             :         }
    2236        3466 :         return;
    2237             :     }
    2238             : 
    2239           0 :     lenp = DPO(data, t->offset + sizeof(*elementp));
    2240           0 :     len = *lenp;
    2241           0 :     dp = DPO(data, t->offset + sizeof(*elementp) + sizeof(*lenp));
    2242           0 :     while (sizeof(void *) != sizeof(*elementp) &&
    2243           0 :            ((uintptr_t)dp) % sizeof(void *) != 0)
    2244           0 :         dp = (void *)(((char *)dp) + sizeof(*elementp));
    2245           0 :     val = *dp;
    2246             : 
    2247           0 :     for (i = 0; i < len; i++) {
    2248           0 :         if (val[i]) {
    2249           0 :             _asn1_free(tactual_type, val[i]);
    2250           0 :             free(val[i]);
    2251             :         }
    2252             :     }
    2253           0 :     free(val);
    2254           0 :     *lenp = 0;
    2255           0 :     *dp = NULL;
    2256             : }
    2257             : 
    2258             : void
    2259   342649566 : _asn1_free(const struct asn1_template *t, void *data)
    2260             : {
    2261   342649566 :     size_t elements = A1_HEADER_LEN(t);
    2262             : 
    2263   342649566 :     if (t->tt & A1_HF_PRESERVE)
    2264      393190 :         der_free_octet_string(data);
    2265             : 
    2266   342649566 :     t++;
    2267             : 
    2268   904574509 :     while (elements) {
    2269   561924943 :         switch (t->tt & A1_OP_MASK) {
    2270        5002 :         case A1_OP_OPENTYPE_OBJSET: {
    2271        5002 :             _asn1_free_open_type(t, data);
    2272        5002 :             break;
    2273             :         }
    2274   134415136 :         case A1_OP_NAME: break;
    2275        4443 :         case A1_OP_DEFVAL: break;
    2276    83268811 :         case A1_OP_TYPE_DECORATE_EXTERN:
    2277             :         case A1_OP_TYPE_DECORATE:
    2278             :         case A1_OP_TYPE:
    2279             :         case A1_OP_TYPE_EXTERN: {
    2280    83268811 :             void *el = DPO(data, t->offset);
    2281    83268811 :             void **pel = (void **)el;
    2282             : 
    2283    83268811 :             if (t->tt & A1_FLAG_OPTIONAL) {
    2284     6102701 :                 if (*pel == NULL)
    2285     4551407 :                     break;
    2286     1338546 :                 el = *pel;
    2287             :             }
    2288             : 
    2289    78552267 :             if ((t->tt & A1_OP_MASK) == A1_OP_TYPE || (t->tt & A1_OP_MASK) == A1_OP_TYPE_DECORATE) {
    2290    77160725 :                 _asn1_free(t->ptr, el);
    2291     1391542 :             } else if ((t->tt & A1_OP_MASK) == A1_OP_TYPE_EXTERN) {
    2292        9678 :                 const struct asn1_type_func *f = t->ptr;
    2293        9678 :                 (f->release)(el);
    2294             :             } else {
    2295             :                 /* A1_OP_TYPE_DECORATE_EXTERN */
    2296     1381864 :                 const struct asn1_type_func *f = t->ptr;
    2297             : 
    2298     1381864 :                 if (f && f->release)
    2299           0 :                     (f->release)(el);
    2300     1381864 :                 else if (f)
    2301     1381864 :                     memset(el, 0, f->size);
    2302             :             }
    2303    78552267 :             if (t->tt & A1_FLAG_OPTIONAL) {
    2304     1386157 :                 free(el);
    2305     1386157 :                 *pel = NULL;
    2306             :             }
    2307             : 
    2308    75807735 :             break;
    2309             :         }
    2310    76138058 :         case A1_OP_PARSE: {
    2311    76138058 :             unsigned int type = A1_PARSE_TYPE(t->tt);
    2312    76138058 :             void *el = DPO(data, t->offset);
    2313             : 
    2314    76138058 :             if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
    2315           0 :                 ABORT_ON_ERROR();
    2316     2630784 :                 break;
    2317             :             }
    2318    76138058 :             (asn1_template_prim[type].release)(el);
    2319    76138058 :             break;
    2320             :         }
    2321   240511210 :         case A1_OP_TAG: {
    2322   240511210 :             void *el = DPO(data, t->offset);
    2323             : 
    2324   240511210 :             if (t->tt & A1_FLAG_OPTIONAL) {
    2325    23221085 :                 void **pel = (void **)el;
    2326             : 
    2327    23221085 :                 if (*pel == NULL)
    2328    12665026 :                     break;
    2329    10104320 :                 _asn1_free(t->ptr, *pel);
    2330    10104320 :                 free(*pel);
    2331    10104320 :                 *pel = NULL;
    2332             :             } else {
    2333   217290125 :                 _asn1_free(t->ptr, el);
    2334             :             }
    2335             : 
    2336   219514526 :             break;
    2337             :         }
    2338    18654121 :         case A1_OP_SETOF:
    2339             :         case A1_OP_SEQOF: {
    2340    18654121 :             struct template_of *el = DPO(data, t->offset);
    2341    18654121 :             size_t ellen = _asn1_sizeofType(t->ptr);
    2342    18654121 :             unsigned char *element = el->val;
    2343      656711 :             unsigned int i;
    2344             : 
    2345    39311731 :             for (i = 0; i < el->len; i++) {
    2346    20657610 :                 _asn1_free(t->ptr, element);
    2347    20657610 :                 element += ellen;
    2348             :             }
    2349    18654121 :             free(el->val);
    2350    18654121 :             el->val = NULL;
    2351    18654121 :             el->len = 0;
    2352             : 
    2353    18654121 :             break;
    2354             :         }
    2355     2454202 :         case A1_OP_BMEMBER:
    2356     2454202 :             break;
    2357     1570350 :         case A1_OP_CHOICE: {
    2358     1570350 :             const struct asn1_template *choice = t->ptr;
    2359     1570350 :             const unsigned int *element = DPOC(data, choice->offset);
    2360             : 
    2361     1570350 :             if (*element > A1_HEADER_LEN(choice))
    2362           0 :                 break;
    2363             : 
    2364     1570350 :             if (*element == 0) {
    2365             :                 /*
    2366             :                  * If choice->tt != 0 then this is an extensible choice, and
    2367             :                  * the offset choice->tt is the offset to u.ellipsis.
    2368             :                  */
    2369         360 :                 if (choice->tt != 0)
    2370           2 :                     der_free_octet_string(DPO(data, choice->tt));
    2371             :                 /*
    2372             :                  * Else this was a not-fully initialized CHOICE.  We could
    2373             :                  * stand to memset clear the rest of it though...
    2374             :                  */
    2375             :             } else {
    2376     1569990 :                 choice += *element;
    2377     1569990 :                 _asn1_free(choice->ptr, DPO(data, choice->offset));
    2378             :             }
    2379     1515787 :             break;
    2380             :         }
    2381           0 :         default:
    2382           0 :             ABORT_ON_ERROR();
    2383    19487211 :             break;
    2384             :         }
    2385   561924943 :         t++;
    2386   561924943 :         elements--;
    2387             :     }
    2388   342649566 : }
    2389             : 
    2390             : static char *
    2391           0 : getindent(int flags, unsigned int i)
    2392             : {
    2393           0 :     char *s;
    2394             : 
    2395           0 :     if (!(flags & ASN1_PRINT_INDENT) ||  i == 0)
    2396           0 :         return NULL;
    2397           0 :     if (i > 128)
    2398           0 :         i = 128;
    2399           0 :     if ((s = malloc(i * 2 + 2)) == NULL)
    2400           0 :         return NULL;
    2401           0 :     s[0] = '\n';
    2402           0 :     s[i * 2 + 1] = '\0';
    2403           0 :     memset(s + 1, ' ', i * 2);
    2404           0 :     return s;
    2405             : }
    2406             : 
    2407             : static struct rk_strpool *_asn1_print(const struct asn1_template *,
    2408             :                                       struct rk_strpool *,
    2409             :                                       int,
    2410             :                                       unsigned int,
    2411             :                                       const void *,
    2412             :                                       const heim_octet_string *);
    2413             : 
    2414             : /* See commentary in _asn1_decode_open_type() */
    2415             : static struct rk_strpool *
    2416           0 : _asn1_print_open_type(const struct asn1_template *t, /* object set template */
    2417             :                       struct rk_strpool *r,
    2418             :                       int flags,
    2419             :                       unsigned int indent,
    2420             :                       const void *data,
    2421             :                       const char *opentype_name)
    2422             : {
    2423           0 :     const struct asn1_template *tactual_type;
    2424           0 :     const struct asn1_template *tos = t->ptr;
    2425           0 :     const unsigned int *lenp = NULL;  /* Pointer to array length field */
    2426           0 :     unsigned int len = 1;       /* Array length */
    2427           0 :     size_t i;
    2428           0 :     const void * const *dp;
    2429           0 :     const void * const *val;
    2430           0 :     const int *elementp = DPOC(data, t->offset);   /* Choice enum pointer */
    2431           0 :     char *indents = getindent(flags, indent);
    2432             : 
    2433             :     /* XXX We assume sizeof(enum) == sizeof(int) */
    2434           0 :     if (!*elementp || *elementp >= A1_HEADER_LEN(tos) + 1) {
    2435           0 :         r = rk_strpoolprintf(r, ",%s\"_%s_choice\":\"_ERROR_DECODING_\"",
    2436             :                              indents ? indents : "", opentype_name);
    2437           0 :         free(indents);
    2438           0 :         return r;
    2439             :     }
    2440           0 :     tactual_type = tos[3*(*elementp - 1) + 4].ptr;
    2441             : 
    2442           0 :     r = rk_strpoolprintf(r, ",%s\"_%s_choice\":\"%s\"",
    2443             :                          indents ? indents : "", opentype_name,
    2444           0 :                          (const char *)tos[3*(*elementp - 1) + 2].ptr);
    2445           0 :     if (!r) {
    2446           0 :         free(indents);
    2447           0 :         return r;
    2448             :     }
    2449             : 
    2450           0 :     if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
    2451           0 :         dp = DPOC(data, t->offset + sizeof(*elementp));
    2452           0 :         while (sizeof(void *) != sizeof(*elementp) &&
    2453           0 :                ((uintptr_t)dp) % sizeof(void *) != 0)
    2454           0 :             dp = (void *)(((char *)dp) + sizeof(*elementp));
    2455           0 :         if (*dp) {
    2456           0 :             struct rk_strpool *r2 = NULL;
    2457           0 :             char *s = NULL;
    2458             : 
    2459           0 :             r2 = _asn1_print(tactual_type, r2, flags, indent + 1, *dp, NULL);
    2460           0 :             if (r2 == NULL) {
    2461           0 :                 r = rk_strpoolprintf(r, ",%s\"_%s\":\"_ERROR_FORMATTING_\"",
    2462             :                                      indents ? indents : "", opentype_name);
    2463           0 :                 free(indents);
    2464           0 :                 return r;
    2465             :             }
    2466           0 :             s = rk_strpoolcollect(r2);
    2467           0 :             if (s)
    2468           0 :                 r = rk_strpoolprintf(r, ",%s\"_%s\":%s",
    2469             :                                      indents ? indents : "", opentype_name, s);
    2470           0 :             free(s);
    2471             :         }
    2472           0 :         free(indents);
    2473           0 :         return r;
    2474             :     }
    2475             : 
    2476           0 :     lenp = DPOC(data, t->offset + sizeof(*elementp));
    2477           0 :     len = *lenp;
    2478           0 :     dp = DPOC(data, t->offset + sizeof(*elementp) + sizeof(*lenp));
    2479           0 :     while (sizeof(void *) != sizeof(*elementp) &&
    2480           0 :            ((uintptr_t)dp) % sizeof(void *) != 0)
    2481           0 :         dp = (void *)(((char *)dp) + sizeof(*elementp));
    2482           0 :     val = *dp;
    2483             : 
    2484           0 :     r = rk_strpoolprintf(r, ",%s\"_%s\":[", indents ? indents : "",
    2485             :                          opentype_name);
    2486           0 :     free(indents);
    2487           0 :     indents = getindent(flags, indent + 1);
    2488           0 :     r = rk_strpoolprintf(r, "%s", indents ? indents : "");
    2489           0 :     for (i = 0; r && i < len; i++) {
    2490           0 :         struct rk_strpool *r2 = NULL;
    2491           0 :         char *s = NULL;;
    2492             : 
    2493           0 :         if (val[i]) {
    2494           0 :             r2 = _asn1_print(tactual_type, r2, flags, indent + 2, val[i], NULL);
    2495           0 :             if (r2 == NULL) {
    2496           0 :                 rk_strpoolfree(r);
    2497           0 :                 free(indents);
    2498           0 :                 return NULL;
    2499             :             }
    2500             :         }
    2501           0 :         if (i)
    2502           0 :             r = rk_strpoolprintf(r, ",%s", indents ? indents : "");
    2503           0 :         if (r)
    2504           0 :             r = rk_strpoolprintf(r, "%s", (s = rk_strpoolcollect(r2)));
    2505           0 :         free(s);
    2506             :     }
    2507           0 :     free(indents);
    2508           0 :     return rk_strpoolprintf(r, "]");
    2509             : }
    2510             : 
    2511             : static struct rk_strpool *
    2512           0 : _asn1_print(const struct asn1_template *t,
    2513             :             struct rk_strpool *r,
    2514             :             int flags,
    2515             :             unsigned int indent,
    2516             :             const void *data,
    2517             :             const heim_octet_string *saved)
    2518             : {
    2519           0 :     const struct asn1_template *tbase = t;
    2520           0 :     const struct asn1_template *tnames;
    2521           0 :     size_t nelements = A1_HEADER_LEN(t);
    2522           0 :     size_t elements = nelements;
    2523           0 :     size_t nnames = 0;
    2524           0 :     char *indents = getindent(flags, indent);
    2525             : 
    2526           0 :     for (t += nelements; t > tbase && (t->tt & A1_OP_MASK) == A1_OP_NAME; t--)
    2527           0 :         nnames++;
    2528             : 
    2529           0 :     tnames = tbase + nelements - nnames + 1;
    2530             : 
    2531           0 :     if (!r)
    2532           0 :         r = rk_strpoolprintf(r, "%s", "");
    2533             : 
    2534           0 :     if (nnames)
    2535           0 :         r = rk_strpoolprintf(r, "%s{\"_type\":\"%s\"",
    2536             :                              indents ? indents : "",
    2537           0 :                              (const char *)(tnames++)->ptr);
    2538           0 :     if (saved && r) {
    2539           0 :         char *s = der_print_octet_string(data, 0);
    2540             : 
    2541           0 :         if (!s) {
    2542           0 :             rk_strpoolfree(r);
    2543           0 :             free(indents);
    2544           0 :             return NULL;
    2545             :         }
    2546           0 :         r = rk_strpoolprintf(r, ",%s\"_save\":\"%s\"",
    2547             :                              indents ? indents : "", s);
    2548           0 :         free(s);
    2549             :     }
    2550           0 :     saved = NULL;
    2551           0 :     if (tbase->tt & A1_HF_PRESERVE)
    2552           0 :         saved = data;
    2553             : 
    2554           0 :     t = tbase + 1;
    2555           0 :     while (r && elements && (t->tt & A1_OP_MASK) != A1_OP_NAME) {
    2556           0 :         switch (t->tt & A1_OP_MASK) {
    2557           0 :         case A1_OP_NAME:
    2558           0 :             continue;
    2559           0 :         case A1_OP_DEFVAL:
    2560           0 :             t++;
    2561           0 :             elements--;
    2562           0 :             continue;
    2563           0 :         case A1_OP_OPENTYPE_OBJSET: {
    2564           0 :             size_t opentype = (t->tt >> 10) & ((1<<10)-1);
    2565           0 :             r = _asn1_print_open_type(t, r, flags, indent + 1, data,
    2566           0 :                                       tbase[(nelements - nnames) + 2 + opentype].ptr);
    2567           0 :             t++;
    2568           0 :             elements--;
    2569           0 :             continue;
    2570             :         }
    2571           0 :         default: break;
    2572             :         }
    2573           0 :         if (nnames &&
    2574           0 :             (t->tt & A1_OP_MASK) != A1_OP_TYPE_DECORATE_EXTERN &&
    2575           0 :             (t->tt & A1_OP_MASK) != A1_OP_TYPE_DECORATE)
    2576           0 :             r = rk_strpoolprintf(r, ",%s\"%s\":",
    2577             :                                  indents ? indents : "",
    2578           0 :                                  (const char *)(tnames++)->ptr);
    2579           0 :         switch (t->tt & A1_OP_MASK) {
    2580           0 :         case A1_OP_OPENTYPE_OBJSET:
    2581           0 :             break;
    2582           0 :         case A1_OP_NAME: break;
    2583           0 :         case A1_OP_DEFVAL: break;
    2584           0 :         case A1_OP_TYPE_DECORATE_EXTERN: break;
    2585           0 :         case A1_OP_TYPE_DECORATE: break; /* We could probably print this though */
    2586           0 :         case A1_OP_TYPE:
    2587             :         case A1_OP_TYPE_EXTERN: {
    2588           0 :             const void *el = DPOC(data, t->offset);
    2589             : 
    2590           0 :             if (t->tt & A1_FLAG_OPTIONAL) {
    2591           0 :                 const void * const *pel = (const void *const *)el;
    2592           0 :                 if (*pel == NULL) {
    2593           0 :                     r = rk_strpoolprintf(r, "null");
    2594           0 :                     break;
    2595             :                 }
    2596           0 :                 el = *pel;
    2597             :             }
    2598             : 
    2599           0 :             if ((t->tt & A1_OP_MASK) == A1_OP_TYPE) {
    2600           0 :                 r = _asn1_print(t->ptr, r, flags, indent + 1, el, saved);
    2601             :             } else {
    2602           0 :                 const struct asn1_type_func *f = t->ptr;
    2603           0 :                 char *s = NULL;
    2604             : 
    2605           0 :                 s = (f->print)(el, 0);
    2606           0 :                 if (s == NULL) {
    2607           0 :                     rk_strpoolfree(r);
    2608           0 :                     free(indents);
    2609           0 :                     return NULL;
    2610             :                 }
    2611           0 :                 r = rk_strpoolprintf(r, "%s", s);
    2612           0 :                 free(s);
    2613             :             }
    2614           0 :             break;
    2615             :         }
    2616           0 :         case A1_OP_PARSE: {
    2617           0 :             unsigned int type = A1_PARSE_TYPE(t->tt);
    2618           0 :             const void *el = DPOC(data, t->offset);
    2619           0 :             char *s = NULL;
    2620             : 
    2621           0 :             if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
    2622           0 :                 ABORT_ON_ERROR();
    2623           0 :                 break;
    2624             :             }
    2625             : 
    2626           0 :             if (type == A1T_IMEMBER && t->ptr) {
    2627             :                 /* Enumeration.  Use the symbolic name of this value */
    2628           0 :                 const struct asn1_template *tenum = t->ptr;
    2629           0 :                 size_t left = 0;
    2630           0 :                 size_t right = A1_HEADER_LEN(tenum);
    2631           0 :                 size_t mid;
    2632           0 :                 uint32_t v = *(unsigned int *)el;
    2633           0 :                 int c = -1;
    2634             : 
    2635           0 :                 while (left <= right) {
    2636           0 :                     mid = (left + right) >> 1;
    2637             : 
    2638           0 :                     if ((tenum[mid].tt & A1_OP_MASK) != A1_OP_NAME)
    2639           0 :                         break;
    2640           0 :                     c = v - tenum[mid].offset;
    2641           0 :                     if (c < 0) {
    2642           0 :                         if (mid)
    2643           0 :                             right = mid - 1;
    2644             :                         else
    2645           0 :                             break;
    2646           0 :                     } else if (c > 0) {
    2647           0 :                         left = mid + 1;
    2648             :                     } else {
    2649           0 :                         break;
    2650             :                     }
    2651             :                 }
    2652           0 :                 if (c == 0) {
    2653           0 :                     r = rk_strpoolprintf(r, "\"%s\"", (const char *)tenum[mid].ptr);
    2654           0 :                     break;
    2655             :                 }
    2656             :             }
    2657           0 :             s = (asn1_template_prim[type].print)(el, flags);
    2658           0 :             switch (type) {
    2659           0 :             case A1T_OID:
    2660             :             case A1T_IMEMBER:
    2661             :             case A1T_BOOLEAN:
    2662             :             case A1T_INTEGER:
    2663             :             case A1T_INTEGER64:
    2664             :             case A1T_UNSIGNED:
    2665             :             case A1T_UNSIGNED64:
    2666           0 :                 if (s)
    2667           0 :                     r = rk_strpoolprintf(r, "%s", s);
    2668           0 :                 break;
    2669           0 :             default: {
    2670           0 :                 char *s2 = NULL;
    2671             : 
    2672           0 :                 if (s)
    2673           0 :                     (void) rk_strasvis(&s2, s, VIS_CSTYLE|VIS_TAB|VIS_NL, "\"");
    2674           0 :                 free(s);
    2675           0 :                 s = s2;
    2676           0 :                 if (s)
    2677           0 :                     r = rk_strpoolprintf(r, "\"%s\"", s);
    2678             :             }
    2679             :             }
    2680           0 :             if (!s) {
    2681           0 :                 rk_strpoolfree(r);
    2682           0 :                 free(indents);
    2683           0 :                 return NULL;
    2684             :             }
    2685           0 :             free(s);
    2686           0 :             break;
    2687             :         }
    2688           0 :         case A1_OP_TAG: {
    2689           0 :             const void *el = DPOC(data, t->offset);
    2690             : 
    2691           0 :             if (t->tt & A1_FLAG_OPTIONAL) {
    2692           0 :                 const void * const *pel = (const void * const *)el;
    2693           0 :                 if (*pel == NULL) {
    2694           0 :                     r = rk_strpoolprintf(r, "null");
    2695           0 :                     break;
    2696             :                 }
    2697           0 :                 el = *pel;
    2698             :             }
    2699             : 
    2700           0 :             r = _asn1_print(t->ptr, r, flags, indent + 1, el, saved);
    2701           0 :             break;
    2702             :         }
    2703           0 :         case A1_OP_SETOF:
    2704             :         case A1_OP_SEQOF: {
    2705           0 :             const struct template_of *el = DPOC(data, t->offset);
    2706           0 :             size_t ellen = _asn1_sizeofType(t->ptr);
    2707           0 :             const unsigned char *element = el->val;
    2708           0 :             unsigned int i;
    2709             : 
    2710           0 :             r = rk_strpoolprintf(r, "%s[", indents ? indents : "");
    2711           0 :             for (i = 0; r && i < el->len; i++) {
    2712           0 :                 if (i)
    2713           0 :                     r = rk_strpoolprintf(r, ",%s", indents ? indents : "");
    2714           0 :                 r = _asn1_print(t->ptr, r, flags, indent + 1, element, saved);
    2715           0 :                 element += ellen;
    2716             :             }
    2717           0 :             if (r)
    2718           0 :                 r = rk_strpoolprintf(r, "]");
    2719           0 :             break;
    2720             :         }
    2721           0 :         case A1_OP_BMEMBER: {
    2722           0 :             const struct asn1_template *bmember = t->ptr;
    2723           0 :             size_t size = bmember->offset;
    2724           0 :             size_t belements = A1_HEADER_LEN(bmember);
    2725           0 :             int first = 1;
    2726             : 
    2727           0 :             bmember += belements;
    2728           0 :             r = rk_strpoolprintf(r, "%s[", indents ? indents : "");
    2729           0 :             while (r && belements) {
    2730           0 :                 if (r && _asn1_bmember_isset_bit(data, bmember->offset, size)) {
    2731           0 :                     if (!first)
    2732           0 :                         r = rk_strpoolprintf(r, ",");
    2733           0 :                     first = 0;
    2734           0 :                     r = rk_strpoolprintf(r, "%s\"%s\"", indents ? indents : "",
    2735           0 :                                          (const char *)bmember->ptr);
    2736             :                 }
    2737           0 :                 belements--; bmember--;
    2738             :             }
    2739           0 :             if (r)
    2740           0 :                 r = rk_strpoolprintf(r, "]");
    2741           0 :             break;
    2742             :         }
    2743           0 :         case A1_OP_CHOICE: {
    2744           0 :             const struct asn1_template *choice = t->ptr;
    2745           0 :             const unsigned int *element = DPOC(data, choice->offset);
    2746           0 :             unsigned int nchoices = ((uintptr_t)choice->ptr) >> 1;
    2747             : 
    2748           0 :             if (*element > A1_HEADER_LEN(choice)) {
    2749           0 :                 r = rk_strpoolprintf(r, "null");
    2750           0 :             } else if (*element == 0) {
    2751             :                 /* XXX If choice->tt then we should print the u.ellipsis */
    2752           0 :                 r = rk_strpoolprintf(r, "null");
    2753             :             } else {
    2754           0 :                 choice += *element;
    2755           0 :                 r = rk_strpoolprintf(r, "%s{\"_choice\":\"%s\",%s\"value\":",
    2756             :                                      indents ? indents : "",
    2757           0 :                                      (const char *)choice[nchoices].ptr,
    2758             :                                      indents ? indents : "");
    2759           0 :                 if (r)
    2760           0 :                     r = _asn1_print(choice->ptr, r, flags, indent + 1,
    2761           0 :                                     DPOC(data, choice->offset), NULL);
    2762           0 :                 if (r)
    2763           0 :                     r = rk_strpoolprintf(r, "}");
    2764             :             }
    2765           0 :             break;
    2766             :         }
    2767           0 :         default:
    2768           0 :             ABORT_ON_ERROR();
    2769           0 :             break;
    2770             :         }
    2771           0 :         t++;
    2772           0 :         elements--;
    2773             :     }
    2774           0 :     free(indents);
    2775           0 :     if (nnames && r)
    2776           0 :         return rk_strpoolprintf(r, "}");
    2777           0 :     return r;
    2778             : }
    2779             : 
    2780             : char *
    2781           0 : _asn1_print_top(const struct asn1_template *t,
    2782             :                 int flags,
    2783             :                 const void *data)
    2784             : {
    2785           0 :     struct rk_strpool *r = _asn1_print(t, NULL, flags, 0, data, NULL);
    2786             : 
    2787           0 :     if (r == NULL)
    2788           0 :         return NULL;
    2789           0 :     return rk_strpoolcollect(r);
    2790             : }
    2791             : 
    2792             : /* See commentary in _asn1_decode_open_type() */
    2793             : static int
    2794        2623 : _asn1_copy_open_type(const struct asn1_template *t, /* object set template */
    2795             :                      const void *from,
    2796             :                      void *to)
    2797             : {
    2798         160 :     const struct asn1_template *tactual_type;
    2799        2623 :     const struct asn1_template *tos = t->ptr;
    2800         160 :     size_t i;
    2801         160 :     const void * const *dfromp;
    2802         160 :     const void * const *valfrom;
    2803         160 :     const unsigned int *lenfromp;
    2804         160 :     void **dtop;
    2805         160 :     void **valto;
    2806         160 :     unsigned int *lentop;
    2807         160 :     unsigned int len;
    2808        2623 :     const int *efromp = DPO(from, t->offset);
    2809        2623 :     int *etop = DPO(to, t->offset);
    2810        2623 :     int ret = 0;
    2811             : 
    2812             :     /* XXX We assume sizeof(enum) == sizeof(int) */
    2813        2623 :     if (!*efromp || *efromp >= A1_HEADER_LEN(tos) + 1) {
    2814         531 :         if ((t->tt & A1_OS_OT_IS_ARRAY))
    2815           0 :             memset(etop, 0, sizeof(int) + sizeof(unsigned int) + sizeof(void *));
    2816             :         else
    2817         531 :             memset(etop, 0, sizeof(int) + sizeof(void *));
    2818         531 :         return 0; /* Unknown choice -> not copied */
    2819             :     }
    2820        2092 :     tactual_type = &tos[3*(*efromp - 1) + 4];
    2821             : 
    2822        2092 :     if (!(t->tt & A1_OS_OT_IS_ARRAY)) {
    2823        2092 :         dfromp = DPO(from, t->offset + sizeof(*efromp));
    2824        2092 :         while (sizeof(void *) != sizeof(*efromp) &&
    2825        4184 :                ((uintptr_t)dfromp) % sizeof(void *) != 0)
    2826        2092 :             dfromp = (void *)(((char *)dfromp) + sizeof(*efromp));
    2827        2092 :         if (!*dfromp)
    2828           0 :             return 0;
    2829             : 
    2830        2092 :         dtop = DPO(to, t->offset + sizeof(*etop));
    2831        2092 :         while (sizeof(void *) != sizeof(*etop) &&
    2832        4184 :                ((uintptr_t)dtop) % sizeof(void *) != 0)
    2833        2092 :             dtop = (void *)(((char *)dtop) + sizeof(*etop));
    2834             : 
    2835        2092 :         if ((*dtop = calloc(1, tactual_type->offset)) == NULL)
    2836           0 :             ret = ENOMEM;
    2837        2092 :         if (ret == 0)
    2838        2092 :             ret = _asn1_copy(tactual_type->ptr, *dfromp, *dtop);
    2839        2092 :         if (ret == 0)
    2840        2092 :             *etop = *efromp;
    2841        2092 :         return ret;
    2842             :     }
    2843             : 
    2844           0 :     lenfromp = DPO(from, t->offset + sizeof(*efromp));
    2845           0 :     dfromp   = DPO(from, t->offset + sizeof(*efromp) + sizeof(*lenfromp));
    2846           0 :     valfrom  = *dfromp;
    2847           0 :     lentop   = DPO(to,   t->offset + sizeof(*etop));
    2848           0 :     dtop     = DPO(to,   t->offset + sizeof(*etop)   + sizeof(*lentop));
    2849             : 
    2850           0 :     *etop = *efromp;
    2851             : 
    2852           0 :     len = *lenfromp;
    2853           0 :     *lentop = 0;
    2854           0 :     *dtop = NULL;
    2855           0 :     if ((valto = calloc(len, sizeof(valto[0]))) == NULL)
    2856           0 :         ret = ENOMEM;
    2857           0 :     for (i = 0, len = *lenfromp; ret == 0 && i < len; i++) {
    2858           0 :         if (valfrom[i] == NULL) {
    2859           0 :             valto[i] = NULL;
    2860           0 :             continue;
    2861             :         }
    2862           0 :         if ((valto[i] = calloc(1, tactual_type->offset)) == NULL)
    2863           0 :             ret = ENOMEM;
    2864             :         else
    2865           0 :             ret = _asn1_copy(tactual_type->ptr, valfrom[i], valto[i]);
    2866           0 :         (*lentop)++;
    2867             :     }
    2868             : 
    2869           0 :     for (i = 0; ret && i < (*lentop); i++) {
    2870           0 :         if (valto[i]) {
    2871           0 :             _asn1_free(tactual_type->ptr, valto[i]);
    2872           0 :             free(valto[i]);
    2873             :         }
    2874             :     }
    2875           0 :     if (ret) {
    2876           0 :         free(valto);
    2877           0 :         *lentop = 0;
    2878             :     } else
    2879           0 :         *dtop = valto;
    2880           0 :     return ret;
    2881             : }
    2882             : 
    2883             : int
    2884   260385433 : _asn1_copy(const struct asn1_template *t, const void *from, void *to)
    2885             : {
    2886   260385433 :     size_t elements = A1_HEADER_LEN(t);
    2887   260385433 :     int ret = 0;
    2888   260385433 :     int preserve = (t->tt & A1_HF_PRESERVE);
    2889             : 
    2890   260385433 :     t++;
    2891             : 
    2892   260385433 :     if (preserve) {
    2893      127496 :         ret = der_copy_octet_string(from, to);
    2894      127496 :         if (ret)
    2895           0 :             return ret;
    2896             :     }
    2897             : 
    2898   684548441 :     while (elements) {
    2899   424163008 :         switch (t->tt & A1_OP_MASK) {
    2900        2623 :         case A1_OP_OPENTYPE_OBJSET: {
    2901        2623 :             _asn1_copy_open_type(t, from, to);
    2902        2623 :             break;
    2903             :         }
    2904   103102313 :         case A1_OP_NAME: break;
    2905        2630 :         case A1_OP_DEFVAL: break;
    2906    57616041 :         case A1_OP_TYPE_DECORATE_EXTERN:
    2907             :         case A1_OP_TYPE_DECORATE:
    2908             :         case A1_OP_TYPE:
    2909             :         case A1_OP_TYPE_EXTERN: {
    2910    57616041 :             const void *fel = DPOC(from, t->offset);
    2911    57616041 :             void *tel = DPO(to, t->offset);
    2912    57616041 :             void **ptel = (void **)tel;
    2913     2006343 :             size_t size;
    2914             : 
    2915    57616041 :             if ((t->tt & A1_OP_MASK) == A1_OP_TYPE ||
    2916     5207281 :                 (t->tt & A1_OP_MASK) == A1_OP_TYPE_DECORATE) {
    2917    56357534 :                 size = _asn1_sizeofType(t->ptr);
    2918             :             } else {
    2919     1258507 :                 const struct asn1_type_func *f = t->ptr;
    2920     1258507 :                 size = f->size;
    2921             :             }
    2922             : 
    2923    57616041 :             if (t->tt & A1_FLAG_OPTIONAL) {
    2924     4137766 :                 void **pfel = (void **)fel;
    2925     4137766 :                 if (*pfel == NULL)
    2926     2780545 :                     break;
    2927     1257616 :                 fel = *pfel;
    2928             : 
    2929     1257616 :                 tel = *ptel = calloc(1, size);
    2930     1257616 :                 if (tel == NULL)
    2931           0 :                     return ENOMEM;
    2932             :             }
    2933             : 
    2934    54735891 :             if ((t->tt & A1_OP_MASK) == A1_OP_TYPE ||
    2935     2426736 :                 (t->tt & A1_OP_MASK) == A1_OP_TYPE_DECORATE) {
    2936    53478070 :                 ret = _asn1_copy(t->ptr, fel, tel);
    2937     1257821 :             } else if ((t->tt & A1_OP_MASK) == A1_OP_TYPE_EXTERN) {
    2938        3082 :                 const struct asn1_type_func *f = t->ptr;
    2939        3082 :                 ret = (f->copy)(fel, tel);
    2940             :             } else {
    2941     1254739 :                 const struct asn1_type_func *f = t->ptr;
    2942             : 
    2943             :                 /* A1_OP_TYPE_DECORATE_EXTERN */
    2944     1254739 :                 if (f && f->copy)
    2945           0 :                     ret = (f->copy)(fel, tel);
    2946     1254739 :                 else if (f)
    2947     1254739 :                     memset(tel, 0, f->size);
    2948             :             }
    2949             : 
    2950    54735891 :             if (ret) {
    2951           0 :                 if (t->tt & A1_FLAG_OPTIONAL) {
    2952           0 :                     free(*ptel);
    2953           0 :                     *ptel = NULL;
    2954             :                 }
    2955           0 :                 return ret;
    2956             :             }
    2957    52829153 :             break;
    2958             :         }
    2959    62005755 :         case A1_OP_PARSE: {
    2960    62005755 :             unsigned int type = A1_PARSE_TYPE(t->tt);
    2961    62005755 :             const void *fel = DPOC(from, t->offset);
    2962    62005755 :             void *tel = DPO(to, t->offset);
    2963             : 
    2964    62005755 :             if (type >= sizeof(asn1_template_prim)/sizeof(asn1_template_prim[0])) {
    2965           0 :                 ABORT_ON_ERROR();
    2966             :                 return ASN1_PARSE_ERROR;
    2967             :             }
    2968    62005755 :             ret = (asn1_template_prim[type].copy)(fel, tel);
    2969    62005755 :             if (ret)
    2970           0 :                 return ret;
    2971    59844275 :             break;
    2972             :         }
    2973   183704945 :         case A1_OP_TAG: {
    2974   183704945 :             const void *oldfrom = from;
    2975   183704945 :             void *oldto = to;
    2976   183704945 :             void **tel = NULL;
    2977             : 
    2978   183704945 :             from = DPOC(from, t->offset);
    2979   183704945 :             to = DPO(to, t->offset);
    2980             : 
    2981   183704945 :             if (t->tt & A1_FLAG_OPTIONAL) {
    2982    14202720 :                 void **fel = (void **)from;
    2983    14202720 :                 tel = (void **)to;
    2984    14202720 :                 if (*fel == NULL) {
    2985     7439141 :                     from = oldfrom;
    2986     7439141 :                     to = oldto;
    2987     7439141 :                     break;
    2988             :                 }
    2989     6503455 :                 from = *fel;
    2990             : 
    2991     6503455 :                 to = *tel = calloc(1, _asn1_sizeofType(t->ptr));
    2992     6503455 :                 if (to == NULL)
    2993           0 :                     return ENOMEM;
    2994             :             }
    2995             : 
    2996   176005680 :             ret = _asn1_copy(t->ptr, from, to);
    2997   176005680 :             if (ret) {
    2998           0 :                 if (tel) {
    2999           0 :                     free(*tel);
    3000           0 :                     *tel = NULL;
    3001             :                 }
    3002           0 :                 return ret;
    3003             :             }
    3004             : 
    3005   169866466 :             from = oldfrom;
    3006   169866466 :             to = oldto;
    3007             : 
    3008   169866466 :             break;
    3009             :         }
    3010    11192308 :         case A1_OP_SETOF:
    3011             :         case A1_OP_SEQOF: {
    3012    11192308 :             const struct template_of *fel = DPOC(from, t->offset);
    3013    11192308 :             struct template_of *tel = DPO(to, t->offset);
    3014    11192308 :             size_t ellen = _asn1_sizeofType(t->ptr);
    3015      390251 :             unsigned int i;
    3016             : 
    3017    11192308 :             tel->val = calloc(fel->len, ellen);
    3018    11192308 :             if (tel->val == NULL && fel->len > 0)
    3019           0 :                 return ENOMEM;
    3020             : 
    3021    11192308 :             tel->len = fel->len;
    3022             : 
    3023    25132287 :             for (i = 0; i < fel->len; i++) {
    3024    14428161 :                 ret = _asn1_copy(t->ptr,
    3025    13939979 :                                  DPOC(fel->val, (i * ellen)),
    3026    13939979 :                                  DPO(tel->val, (i *ellen)));
    3027    13939979 :                 if (ret)
    3028           0 :                     return ret;
    3029             :             }
    3030    10802057 :             break;
    3031             :         }
    3032     1553672 :         case A1_OP_BMEMBER: {
    3033     1553672 :             const struct asn1_template *bmember = t->ptr;
    3034     1553672 :             size_t size = bmember->offset;
    3035     1553672 :             memcpy(to, from, size);
    3036     1500038 :             break;
    3037             :         }
    3038     1263855 :         case A1_OP_CHOICE: {
    3039     1263855 :             const struct asn1_template *choice = t->ptr;
    3040     1263855 :             const unsigned int *felement = DPOC(from, choice->offset);
    3041     1263855 :             unsigned int *telement = DPO(to, choice->offset);
    3042             : 
    3043     1263855 :             if (*felement > A1_HEADER_LEN(choice))
    3044           0 :                 return ASN1_PARSE_ERROR;
    3045             : 
    3046     1263855 :             *telement = *felement;
    3047             : 
    3048     1263855 :             if (*felement == 0) {
    3049           0 :                 if (choice->tt)
    3050           0 :                     ret = der_copy_octet_string(DPOC(from, choice->tt), DPO(to, choice->tt));
    3051             :                 /*
    3052             :                  * Else we should really memset clear the rest of this choice,
    3053             :                  * but we don't really know its size.
    3054             :                  */
    3055             :             } else {
    3056     1263855 :                 choice += *felement;
    3057     1263855 :                 ret = _asn1_copy(choice->ptr,
    3058     1220619 :                                  DPOC(from, choice->offset),
    3059     1263855 :                                  DPO(to, choice->offset));
    3060             :             }
    3061     1263855 :             if (ret)
    3062           0 :                 return ret;
    3063     1220619 :             break;
    3064             :         }
    3065           0 :         default:
    3066           0 :             ABORT_ON_ERROR();
    3067    14773308 :             break;
    3068             :         }
    3069   424163008 :         t++;
    3070   424163008 :         elements--;
    3071             :     }
    3072   251301612 :     return 0;
    3073             : }
    3074             : 
    3075             : int
    3076     1555498 : _asn1_decode_top(const struct asn1_template *t, unsigned flags, const unsigned char *p, size_t len, void *data, size_t *size)
    3077             : {
    3078       56951 :     int ret;
    3079     1555498 :     memset(data, 0, t->offset);
    3080     1555498 :     ret = _asn1_decode(t, flags, p, len, data, size);
    3081     1555498 :     if (ret)
    3082      176533 :         _asn1_free_top(t, data);
    3083             : 
    3084     1555498 :     return ret;
    3085             : }
    3086             : 
    3087             : int
    3088    15695757 : _asn1_copy_top(const struct asn1_template *t, const void *from, void *to)
    3089             : {
    3090      549271 :     int ret;
    3091    15695757 :     memset(to, 0, t->offset);
    3092    15695757 :     ret = _asn1_copy(t, from, to);
    3093    15695757 :     if (ret)
    3094           0 :         _asn1_free_top(t, to);
    3095             : 
    3096    15695757 :     return ret;
    3097             : }
    3098             : 
    3099             : void
    3100    15848606 : _asn1_free_top(const struct asn1_template *t, void *data)
    3101             : {
    3102    15848606 :     _asn1_free(t, data);
    3103    15848606 :     memset(data, 0, t->offset);
    3104    15848606 : }

Generated by: LCOV version 1.14