| 1 |
/* asn1-func2.c - More ASN.1 definitions |
| 2 |
* Copyright (C) 2000, 2001 Fabio Fiorina |
| 3 |
* Copyright (C) 2001 Free Software Foundation, Inc. |
| 4 |
* Copyright (C) 2008, 2012 g10 Code GmbH |
| 5 |
* |
| 6 |
* This file is part of KSBA. |
| 7 |
* |
| 8 |
* KSBA is free software; you can redistribute it and/or modify |
| 9 |
* it under the terms of either |
| 10 |
* |
| 11 |
* - the GNU Lesser General Public License as published by the Free |
| 12 |
* Software Foundation; either version 3 of the License, or (at |
| 13 |
* your option) any later version. |
| 14 |
* |
| 15 |
* or |
| 16 |
* |
| 17 |
* - the GNU General Public License as published by the Free |
| 18 |
* Software Foundation; either version 2 of the License, or (at |
| 19 |
* your option) any later version. |
| 20 |
* |
| 21 |
* or both in parallel, as here. |
| 22 |
* |
| 23 |
* KSBA is distributed in the hope that it will be useful, but WITHOUT |
| 24 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY |
| 25 |
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public |
| 26 |
* License for more details. |
| 27 |
* |
| 28 |
* You should have received a copies of the GNU General Public License |
| 29 |
* and the GNU Lesser General Public License along with this program; |
| 30 |
* if not, see <http://www.gnu.org/licenses/>. |
| 31 |
*/ |
| 32 |
|
| 33 |
/* |
| 34 |
This file has functions which rely on on the asn1-gentables created |
| 35 |
asn1-tables.c - we can't put this into asn1-func.c because this one |
| 36 |
is needed by asn1-gentables ;-) |
| 37 |
*/ |
| 38 |
|
| 39 |
#include <config.h> |
| 40 |
#include <stdio.h> |
| 41 |
#include <stdlib.h> |
| 42 |
#include <string.h> |
| 43 |
#include <ctype.h> |
| 44 |
#include <assert.h> |
| 45 |
|
| 46 |
#include "util.h" |
| 47 |
#include "ksba.h" |
| 48 |
#include "asn1-func.h" |
| 49 |
|
| 50 |
|
| 51 |
static AsnNode |
| 52 |
set_right (AsnNode node, AsnNode right) |
| 53 |
{ |
| 54 |
if (node == NULL) |
| 55 |
return node; |
| 56 |
|
| 57 |
node->right = right; |
| 58 |
if (right) |
| 59 |
right->left = node; |
| 60 |
return node; |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
static AsnNode |
| 65 |
set_down (AsnNode node, AsnNode down) |
| 66 |
{ |
| 67 |
if (node == NULL) |
| 68 |
return node; |
| 69 |
|
| 70 |
node->down = down; |
| 71 |
if (down) |
| 72 |
down->left = node; |
| 73 |
return node; |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
static AsnNode |
| 78 |
find_up (AsnNode node) |
| 79 |
{ |
| 80 |
AsnNode p; |
| 81 |
|
| 82 |
if (node == NULL) |
| 83 |
return NULL; |
| 84 |
|
| 85 |
p = node; |
| 86 |
while ((p->left != NULL) && (p->left->right == p)) |
| 87 |
p = p->left; |
| 88 |
|
| 89 |
return p->left; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
/** |
| 96 |
* Creates the structures needed to manage the ASN1 definitions. ROOT is |
| 97 |
* a vector created by the asn1-gentable tool. |
| 98 |
* |
| 99 |
* Input Parameter: |
| 100 |
* |
| 101 |
* Name of the module |
| 102 |
* |
| 103 |
* Output Parameter: |
| 104 |
* |
| 105 |
* KsbaAsntree *result : return the pointer to an object to be used |
| 106 |
* with other functions. |
| 107 |
* |
| 108 |
* Return Value: |
| 109 |
* 0: structure created correctly. |
| 110 |
* GPG_ERR_GENERAL: an error occured while structure creation. |
| 111 |
* GPG_ERR_MODULE_NOT_FOUND: No such module NAME |
| 112 |
*/ |
| 113 |
gpg_error_t |
| 114 |
ksba_asn_create_tree (const char *mod_name, ksba_asn_tree_t *result) |
| 115 |
{ |
| 116 |
enum { DOWN, UP, RIGHT } move; |
| 117 |
const static_asn *root; |
| 118 |
const char *strgtbl; |
| 119 |
AsnNode pointer; |
| 120 |
AsnNode p = NULL; |
| 121 |
AsnNode p_last = NULL; |
| 122 |
unsigned long k; |
| 123 |
int rc; |
| 124 |
AsnNode link_next = NULL; |
| 125 |
|
| 126 |
if (!result) |
| 127 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 128 |
*result = NULL; |
| 129 |
|
| 130 |
if (!mod_name) |
| 131 |
return gpg_error (GPG_ERR_INV_VALUE); |
| 132 |
root = _ksba_asn_lookup_table (mod_name, &strgtbl); |
| 133 |
if (!root) |
| 134 |
return gpg_error (GPG_ERR_MODULE_NOT_FOUND); |
| 135 |
|
| 136 |
pointer = NULL; |
| 137 |
move = UP; |
| 138 |
|
| 139 |
k = 0; |
| 140 |
while (root[k].stringvalue_off || root[k].type || root[k].name_off) |
| 141 |
{ |
| 142 |
p = _ksba_asn_new_node (root[k].type); |
| 143 |
p->flags = root[k].flags; |
| 144 |
p->flags.help_down = 0; |
| 145 |
p->link_next = link_next; |
| 146 |
link_next = p; |
| 147 |
|
| 148 |
if (root[k].name_off) |
| 149 |
_ksba_asn_set_name (p, strgtbl + root[k].name_off); |
| 150 |
if (root[k].stringvalue_off) |
| 151 |
{ |
| 152 |
if (root[k].type == TYPE_TAG) |
| 153 |
{ |
| 154 |
unsigned long val; |
| 155 |
val = strtoul (strgtbl+root[k].stringvalue_off, NULL, 10); |
| 156 |
_ksba_asn_set_value (p, VALTYPE_ULONG, &val, sizeof(val)); |
| 157 |
} |
| 158 |
else |
| 159 |
_ksba_asn_set_value (p, VALTYPE_CSTR, |
| 160 |
strgtbl+root[k].stringvalue_off, 0); |
| 161 |
} |
| 162 |
|
| 163 |
if (!pointer) |
| 164 |
pointer = p; |
| 165 |
|
| 166 |
if (move == DOWN) |
| 167 |
set_down (p_last, p); |
| 168 |
else if (move == RIGHT) |
| 169 |
set_right (p_last, p); |
| 170 |
|
| 171 |
p_last = p; |
| 172 |
|
| 173 |
if (root[k].flags.help_down) |
| 174 |
move = DOWN; |
| 175 |
else if (root[k].flags.help_right) |
| 176 |
move = RIGHT; |
| 177 |
else |
| 178 |
{ |
| 179 |
while (1) |
| 180 |
{ |
| 181 |
if (p_last == pointer) |
| 182 |
break; |
| 183 |
|
| 184 |
p_last = find_up (p_last); |
| 185 |
|
| 186 |
if (p_last == NULL) |
| 187 |
break; |
| 188 |
|
| 189 |
if (p_last->flags.help_right) |
| 190 |
{ |
| 191 |
p_last->flags.help_right = 0; |
| 192 |
move = RIGHT; |
| 193 |
break; |
| 194 |
} |
| 195 |
} |
| 196 |
} |
| 197 |
k++; |
| 198 |
} |
| 199 |
|
| 200 |
if (p_last == pointer) |
| 201 |
{ |
| 202 |
ksba_asn_tree_t tree; |
| 203 |
|
| 204 |
_ksba_asn_change_integer_value (pointer); |
| 205 |
_ksba_asn_expand_object_id (pointer); |
| 206 |
tree = xtrymalloc (sizeof *tree + strlen (mod_name)); |
| 207 |
if (!tree) |
| 208 |
rc = gpg_error (GPG_ERR_ENOMEM); |
| 209 |
else |
| 210 |
{ |
| 211 |
tree->parse_tree = pointer; |
| 212 |
tree->node_list = p; |
| 213 |
strcpy (tree->filename, mod_name); |
| 214 |
*result = tree; |
| 215 |
rc = 0; |
| 216 |
} |
| 217 |
} |
| 218 |
else |
| 219 |
rc = gpg_error (GPG_ERR_GENERAL); |
| 220 |
|
| 221 |
if (rc) |
| 222 |
_ksba_asn_delete_structure (pointer); |
| 223 |
|
| 224 |
return rc; |
| 225 |
} |