1178825Sdfr/*
2233294Sstas * Copyright (c) 2005 Kungliga Tekniska H��gskolan
3233294Sstas * (Royal Institute of Technology, Stockholm, Sweden).
4233294Sstas * All rights reserved.
5178825Sdfr *
6233294Sstas * Redistribution and use in source and binary forms, with or without
7233294Sstas * modification, are permitted provided that the following conditions
8233294Sstas * are met:
9178825Sdfr *
10233294Sstas * 1. Redistributions of source code must retain the above copyright
11233294Sstas *    notice, this list of conditions and the following disclaimer.
12178825Sdfr *
13233294Sstas * 2. Redistributions in binary form must reproduce the above copyright
14233294Sstas *    notice, this list of conditions and the following disclaimer in the
15233294Sstas *    documentation and/or other materials provided with the distribution.
16178825Sdfr *
17233294Sstas * 3. Neither the name of the Institute nor the names of its contributors
18233294Sstas *    may be used to endorse or promote products derived from this software
19233294Sstas *    without specific prior written permission.
20178825Sdfr *
21233294Sstas * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
22233294Sstas * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23233294Sstas * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24233294Sstas * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
25233294Sstas * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26233294Sstas * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27233294Sstas * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28233294Sstas * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29233294Sstas * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30233294Sstas * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31233294Sstas * SUCH DAMAGE.
32178825Sdfr */
33178825Sdfr
34178825Sdfr#include "der_locl.h"
35178825Sdfr#include <com_err.h>
36178825Sdfr#include <sys/types.h>
37178825Sdfr#include <sys/stat.h>
38178825Sdfr#include <ctype.h>
39178825Sdfr#include <getarg.h>
40178825Sdfr#include <hex.h>
41178825Sdfr#include <err.h>
42178825Sdfr
43233294SstasRCSID("$Id$");
44178825Sdfr
45178825Sdfrstatic int
46178825Sdfrdoit(const char *fn)
47178825Sdfr{
48178825Sdfr    char buf[2048];
49233294Sstas    char *fnout = NULL;
50178825Sdfr    const char *bname;
51178825Sdfr    unsigned long line = 0;
52178825Sdfr    FILE *f, *fout;
53178825Sdfr    size_t offset = 0;
54178825Sdfr
55178825Sdfr    f = fopen(fn, "r");
56178825Sdfr    if (f == NULL)
57178825Sdfr	err(1, "fopen");
58178825Sdfr
59178825Sdfr    bname = strrchr(fn, '/');
60178825Sdfr    if (bname)
61178825Sdfr	bname++;
62178825Sdfr    else
63178825Sdfr	bname = fn;
64178825Sdfr
65233294Sstas    if (asprintf(&fnout, "%s.out", bname) < 0 || fnout == NULL)
66178825Sdfr	errx(1, "malloc");
67178825Sdfr
68178825Sdfr    fout = fopen(fnout, "w");
69178825Sdfr    if (fout == NULL)
70178825Sdfr	err(1, "fopen: output file");
71178825Sdfr
72178825Sdfr    while (fgets(buf, sizeof(buf), f) != NULL) {
73178825Sdfr	char *ptr, *class, *type, *tag, *length, *data, *foo;
74178825Sdfr	int ret, l, c, ty, ta;
75178825Sdfr	unsigned char p[6], *pdata;
76178825Sdfr	size_t sz;
77178825Sdfr
78178825Sdfr	line++;
79178825Sdfr
80178825Sdfr	buf[strcspn(buf, "\r\n")] = '\0';
81178825Sdfr	if (buf[0] == '#' || buf[0] == '\0')
82178825Sdfr	    continue;
83178825Sdfr
84178825Sdfr	ptr = buf;
85178825Sdfr	while (isspace((unsigned char)*ptr))
86178825Sdfr	       ptr++;
87178825Sdfr
88178825Sdfr	class = strtok_r(ptr, " \t\n", &foo);
89178825Sdfr	if (class == NULL) errx(1, "class missing on line %lu", line);
90178825Sdfr	type = strtok_r(NULL, " \t\n", &foo);
91178825Sdfr	if (type == NULL) errx(1, "type missing on line %lu", line);
92178825Sdfr	tag = strtok_r(NULL, " \t\n", &foo);
93178825Sdfr	if (tag == NULL) errx(1, "tag missing on line %lu", line);
94178825Sdfr	length = strtok_r(NULL, " \t\n", &foo);
95178825Sdfr	if (length == NULL) errx(1, "length missing on line %lu", line);
96178825Sdfr	data = strtok_r(NULL, " \t\n", &foo);
97178825Sdfr
98178825Sdfr	c = der_get_class_num(class);
99178825Sdfr	if (c == -1) errx(1, "no valid class on line %lu", line);
100178825Sdfr	ty = der_get_type_num(type);
101178825Sdfr	if (ty == -1) errx(1, "no valid type on line %lu", line);
102178825Sdfr	ta = der_get_tag_num(tag);
103178825Sdfr	if (ta == -1)
104178825Sdfr	    ta = atoi(tag);
105178825Sdfr
106178825Sdfr	l = atoi(length);
107178825Sdfr
108178825Sdfr	printf("line: %3lu offset: %3lu class: %d type: %d "
109233294Sstas	       "tag: %3d length: %3d %s\n",
110233294Sstas	       line, (unsigned long)offset, c, ty, ta, l,
111178825Sdfr	       data ? "<have data>" : "<no data>");
112178825Sdfr
113178825Sdfr	ret = der_put_length_and_tag(p + sizeof(p) - 1, sizeof(p),
114178825Sdfr				     l,
115178825Sdfr				     c,
116178825Sdfr				     ty,
117178825Sdfr				     ta,
118178825Sdfr				     &sz);
119178825Sdfr	if (ret)
120178825Sdfr	    errx(1, "der_put_length_and_tag: %d", ret);
121233294Sstas
122178825Sdfr	if (fwrite(p + sizeof(p) - sz , sz, 1, fout) != 1)
123178825Sdfr	    err(1, "fwrite length/tag failed");
124178825Sdfr	offset += sz;
125233294Sstas
126178825Sdfr	if (data) {
127178825Sdfr	    size_t datalen;
128233294Sstas
129178825Sdfr	    datalen = strlen(data) / 2;
130178825Sdfr	    pdata = emalloc(sz);
131233294Sstas
132178825Sdfr	    if (hex_decode(data, pdata, datalen) != datalen)
133178825Sdfr		errx(1, "failed to decode data");
134233294Sstas
135178825Sdfr	    if (fwrite(pdata, datalen, 1, fout) != 1)
136178825Sdfr		err(1, "fwrite data failed");
137178825Sdfr	    offset += datalen;
138233294Sstas
139178825Sdfr	    free(pdata);
140178825Sdfr	}
141178825Sdfr    }
142178825Sdfr    printf("line: eof offset: %lu\n", (unsigned long)offset);
143233294Sstas
144178825Sdfr    fclose(fout);
145178825Sdfr    fclose(f);
146178825Sdfr    return 0;
147178825Sdfr}
148178825Sdfr
149178825Sdfr
150178825Sdfrstatic int version_flag;
151178825Sdfrstatic int help_flag;
152178825Sdfrstruct getargs args[] = {
153178825Sdfr    { "version", 0, arg_flag, &version_flag },
154178825Sdfr    { "help", 0, arg_flag, &help_flag }
155178825Sdfr};
156178825Sdfrint num_args = sizeof(args) / sizeof(args[0]);
157178825Sdfr
158178825Sdfrstatic void
159178825Sdfrusage(int code)
160178825Sdfr{
161178825Sdfr    arg_printusage(args, num_args, NULL, "parse-file");
162178825Sdfr    exit(code);
163178825Sdfr}
164178825Sdfr
165178825Sdfrint
166178825Sdfrmain(int argc, char **argv)
167178825Sdfr{
168178825Sdfr    int optidx = 0;
169178825Sdfr
170178825Sdfr    setprogname (argv[0]);
171178825Sdfr
172178825Sdfr    if(getarg(args, num_args, argc, argv, &optidx))
173178825Sdfr	usage(1);
174178825Sdfr    if(help_flag)
175178825Sdfr	usage(0);
176178825Sdfr    if(version_flag) {
177178825Sdfr	print_version(NULL);
178178825Sdfr	exit(0);
179178825Sdfr    }
180178825Sdfr    argv += optidx;
181178825Sdfr    argc -= optidx;
182178825Sdfr    if (argc != 1)
183178825Sdfr	usage (1);
184178825Sdfr
185178825Sdfr    return doit (argv[0]);
186178825Sdfr}
187