File: asn1-func2.c

package info (click to toggle)
libksba 1.6.7-2
  • links: PTS, VCS
  • area: main
  • in suites: experimental, forky, sid, trixie
  • size: 3,996 kB
  • sloc: ansic: 23,298; sh: 12,428; yacc: 859; makefile: 292; sed: 37; awk: 32
file content (225 lines) | stat: -rw-r--r-- 5,003 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/* asn1-func2.c - More ASN.1 definitions
 *      Copyright (C) 2000, 2001 Fabio Fiorina
 *      Copyright (C) 2001 Free Software Foundation, Inc.
 *      Copyright (C) 2008, 2012 g10 Code GmbH
 *
 * This file is part of KSBA.
 *
 * KSBA is free software; you can redistribute it and/or modify
 * it under the terms of either
 *
 *   - the GNU Lesser General Public License as published by the Free
 *     Software Foundation; either version 3 of the License, or (at
 *     your option) any later version.
 *
 * or
 *
 *   - the GNU General Public License as published by the Free
 *     Software Foundation; either version 2 of the License, or (at
 *     your option) any later version.
 *
 * or both in parallel, as here.
 *
 * KSBA is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
 * License for more details.
 *
 * You should have received a copies of the GNU General Public License
 * and the GNU Lesser General Public License along with this program;
 * if not, see <http://www.gnu.org/licenses/>.
 */

/*
   This file has functions which rely on on the asn1-gentables created
   asn1-tables.c - we can't put this into asn1-func.c because this one
   is needed by asn1-gentables ;-)
*/

#include <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>

#include "util.h"
#include "ksba.h"
#include "asn1-func.h"


static AsnNode
set_right (AsnNode  node, AsnNode  right)
{
  if (node == NULL)
    return node;

  node->right = right;
  if (right)
    right->left = node;
  return node;
}


static AsnNode
set_down (AsnNode node, AsnNode down)
{
  if (node == NULL)
    return node;

  node->down = down;
  if (down)
    down->left = node;
  return node;
}


static AsnNode
find_up (AsnNode  node)
{
  AsnNode p;

  if (node == NULL)
    return NULL;

  p = node;
  while ((p->left != NULL) && (p->left->right == p))
    p = p->left;

  return p->left;
}




/**
 * Creates the structures needed to manage the ASN1 definitions. ROOT is
 * a vector created by the asn1-gentable tool.
 *
 * Input Parameter:
 *
 *   Name of the module
 *
 * Output Parameter:
 *
 *   KsbaAsntree *result : return the pointer to an object to be used
 *   with other functions.
 *
 * Return Value:
 *   0: structure created correctly.
 *   GPG_ERR_GENERAL: an error occured while structure creation.
 *   GPG_ERR_MODULE_NOT_FOUND: No such module NAME
 */
gpg_error_t
ksba_asn_create_tree (const char *mod_name, ksba_asn_tree_t *result)
{
  enum { DOWN, UP, RIGHT } move;
  const static_asn *root;
  const char *strgtbl;
  AsnNode pointer;
  AsnNode p = NULL;
  AsnNode p_last = NULL;
  unsigned long k;
  int rc;
  AsnNode link_next = NULL;

  if (!result)
    return gpg_error (GPG_ERR_INV_VALUE);
  *result = NULL;

  if (!mod_name)
    return gpg_error (GPG_ERR_INV_VALUE);
  root = _ksba_asn_lookup_table (mod_name, &strgtbl);
  if (!root)
    return gpg_error (GPG_ERR_MODULE_NOT_FOUND);

  pointer = NULL;
  move = UP;

  k = 0;
  while (root[k].stringvalue_off || root[k].type || root[k].name_off)
    {
      p = _ksba_asn_new_node (root[k].type);
      p->flags = root[k].flags;
      p->flags.help_down = 0;
      p->link_next = link_next;
      link_next = p;

      if (root[k].name_off)
	_ksba_asn_set_name (p, strgtbl + root[k].name_off);
      if (root[k].stringvalue_off)
        {
          if (root[k].type == TYPE_TAG)
            {
              unsigned long val;
              val = strtoul (strgtbl+root[k].stringvalue_off, NULL, 10);
              _ksba_asn_set_value (p, VALTYPE_ULONG, &val, sizeof(val));
            }
          else
            _ksba_asn_set_value (p, VALTYPE_CSTR,
                                 strgtbl+root[k].stringvalue_off, 0);
        }

      if (!pointer)
	pointer = p;

      if (move == DOWN)
	set_down (p_last, p);
      else if (move == RIGHT)
	set_right (p_last, p);

      p_last = p;

      if (root[k].flags.help_down)
	move = DOWN;
      else if (root[k].flags.help_right)
	move = RIGHT;
      else
	{
	  while (1)
	    {
	      if (p_last == pointer)
		break;

	      p_last = find_up (p_last);

	      if (p_last == NULL)
		break;

	      if (p_last->flags.help_right)
		{
		  p_last->flags.help_right = 0;
		  move = RIGHT;
		  break;
		}
	    }
	}
      k++;
    }

  if (p_last == pointer)
    {
      ksba_asn_tree_t tree;

      _ksba_asn_change_integer_value (pointer);
      _ksba_asn_expand_object_id (pointer);
      tree = xtrymalloc (sizeof *tree + strlen (mod_name));
      if (!tree)
        rc = gpg_error (GPG_ERR_ENOMEM);
      else
        {
          tree->parse_tree = pointer;
          tree->node_list = p;
          strcpy (tree->filename, mod_name);
          *result = tree;
          rc = 0;
        }
    }
  else
      rc = gpg_error (GPG_ERR_GENERAL);

  if (rc)
    _ksba_asn_delete_structure (pointer);

  return rc;
}