summaryrefslogtreecommitdiff
path: root/fuzz/corpus2array.c
blob: 5d2c832f07c7fd60a1bb5cf5387837a4c7798ec5 (plain)
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
/*
 * Copyright (C) 2019-2022 Free Software Foundation, Inc.
 *
 * This file is part of LIBTASN1.
 *
 * The LIBTASN1 library is free software; you can redistribute it
 * and/or modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA
 */

/*
 Helper tool to convert a corpus from libtasn1_array2tree_fuzzer into a asn1_static_node C code.

 Example usage:
   $ cat libtasn1_array2tree_fuzzer.in/e04b8bc97fcf0d6386dfa44e32b1700e42e0eb64 | ./corpus2array
   const asn1_static_node tab[] = {
     { "999=999", CONST_UNIVERSAL|CONST_IMPLICIT|CONST_SET|CONST_ASSIGN|CONST_DOWN, "993\xff~\xff$y\xd9\x80" },
     { NULL, 0, NULL }
   };   cat leak-d69d42ff01f6c3acca35dee27538a99e6821fced | ./corpus2array
 */

#include <stdio.h>
#include <string.h>
#include <ctype.h>

struct fuzz_elem
{
  unsigned int type;
  char name[20];
  char value[20];
};

static char *
escape (unsigned char *s)
{
  static char out[19 * 6 + 1];
  char *p = out;

  while (*s)
    {
      if (*s > 127)
	{
	  if (isxdigit (s[1]))
	    {
	      sprintf (p, "\\u%04x", *s++);
	      p += 6;
	    }
	  else
	    {
	      sprintf (p, "\\x%02x", *s++);
	      p += 4;
	    }
	}
      else if (*s == '\n')
	{
	  sprintf (p, "\\n");
	  p += 2;
	  s++;
	}
      else if (*s == '\t')
	{
	  sprintf (p, "\\t");
	  p += 2;
	  s++;
	}
      else
	*p++ = *s++;
    }
  *p = 0;

  return out;
}

static const char *typename[24] = {
  "CONST_UNIVERSAL",
  "CONST_PRIVATE",
  "CONST_APPLICATION",
  "CONST_EXPLICIT",
  "CONST_IMPLICIT",
  "CONST_TAG",
  "CONST_OPTION",
  "CONST_DEFAULT",
  "CONST_TRUE",
  "CONST_FALSE",
  "CONST_LIST",
  "CONST_MIN_MAX",
  "CONST_1_PARAM",
  "CONST_SIZE",
  "CONST_DEFINED_BY",
  "CONST_GENERALIZED",
  "CONST_UTC",
  NULL,				/* #define CONST_IMPORTS     (1<<25) */
  "CONST_NOT_USED",
  "CONST_SET",
  "CONST_ASSIGN",
  "CONST_DOWN",
  "CONST_RIGHT",
  NULL
};

int
main (void)
{
  struct fuzz_elem e;

  printf ("const asn1_static_node tab[] = {\n");

  while (fread (&e, sizeof (e), 1, stdin) == 1)
    {
      e.name[sizeof (e.name) - 1] = 0;
      e.value[sizeof (e.value) - 1] = 0;
      if (strcmp (e.name, "NULL"))
	printf ("  { \"%s\"", escape ((unsigned char *) e.name));
      else
	printf ("  { NULL");

      if (e.type)
	{
	  int add = 0;
	  int i;

	  /* we leave the lowest 8 bit out */
	  if ((e.type & 0xFF) == 17)
	    {
	      printf (", ASN1_ETYPE_TIME");
	      add = 1;
	    }

	  for (i = 8; i < 32; i++)
	    {
	      if ((e.type & (1U << i)) && typename[i - 8])
		{
		  printf (add ? "|%s" : ", %s", typename[i - 8]);
		  add = 1;
		}
	    }

	  if (!add)
	    printf (", %u", e.type);
	}
      else
	printf (", 0");

      if (strcmp (e.value, "NULL"))
	printf (", \"%s\" },\n", escape ((unsigned char *) e.value));
      else
	printf (", NULL },");
    }

  printf ("  { NULL, 0, NULL }\n");
  printf ("};\n");

  return 0;
}