1298178Sdelphij/*-
2298178Sdelphij * Copyright (c) 2016 Christos Zoulas
3298178Sdelphij * All rights reserved.
4298178Sdelphij *
5298178Sdelphij * Redistribution and use in source and binary forms, with or without
6298178Sdelphij * modification, are permitted provided that the following conditions
7298178Sdelphij * are met:
8298178Sdelphij * 1. Redistributions of source code must retain the above copyright
9298178Sdelphij *    notice, this list of conditions and the following disclaimer.
10298178Sdelphij * 2. Redistributions in binary form must reproduce the above copyright
11298178Sdelphij *    notice, this list of conditions and the following disclaimer in the
12298178Sdelphij *    documentation and/or other materials provided with the distribution.
13298178Sdelphij *
14298178Sdelphij * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
15298178Sdelphij * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
16298178Sdelphij * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17298178Sdelphij * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
18298178Sdelphij * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19298178Sdelphij * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20298178Sdelphij * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21298178Sdelphij * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22298178Sdelphij * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23298178Sdelphij * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24298178Sdelphij * POSSIBILITY OF SUCH DAMAGE.
25298178Sdelphij */
26298178Sdelphij/*
27298178Sdelphij * DER (Distinguished Encoding Rules) Parser
28298178Sdelphij *
29298178Sdelphij * Sources:
30298178Sdelphij * https://en.wikipedia.org/wiki/X.690
31298178Sdelphij * http://fm4dd.com/openssl/certexamples.htm
32298178Sdelphij * http://blog.engelke.com/2014/10/17/parsing-ber-and-der-encoded-asn-1-objects/
33298178Sdelphij */
34298178Sdelphij#ifndef TEST_DER
35298178Sdelphij#include "file.h"
36298178Sdelphij
37298178Sdelphij#ifndef lint
38362844SdelphijFILE_RCSID("@(#)$File: der.c,v 1.20 2020/06/07 19:10:37 christos Exp $")
39298178Sdelphij#endif
40362844Sdelphij#else
41362844Sdelphij#define SIZE_T_FORMAT "z"
42362844Sdelphij#define CAST(a, b) ((a)(b))
43298178Sdelphij#endif
44298178Sdelphij
45298178Sdelphij#include <sys/types.h>
46298178Sdelphij
47298178Sdelphij#include <stdio.h>
48298178Sdelphij#include <fcntl.h>
49298178Sdelphij#include <stdlib.h>
50298178Sdelphij#include <string.h>
51298178Sdelphij#include <ctype.h>
52298178Sdelphij
53298178Sdelphij#ifndef TEST_DER
54298178Sdelphij#include "magic.h"
55298178Sdelphij#include "der.h"
56299736Sdelphij#else
57309847Sdelphij#include <sys/mman.h>
58309847Sdelphij#include <sys/stat.h>
59299736Sdelphij#include <err.h>
60298178Sdelphij#endif
61298178Sdelphij
62354939Sdelphij#define DER_BAD	CAST(uint32_t, -1)
63298178Sdelphij
64298178Sdelphij#define DER_CLASS_UNIVERSAL	0
65298178Sdelphij#define	DER_CLASS_APPLICATION	1
66298178Sdelphij#define	DER_CLASS_CONTEXT	2
67298178Sdelphij#define	DER_CLASS_PRIVATE	3
68362844Sdelphij#if defined(DEBUG_DER) || defined(TEST_DER)
69298178Sdelphijstatic const char der_class[] = "UACP";
70299736Sdelphij#endif
71298178Sdelphij
72298178Sdelphij#define DER_TYPE_PRIMITIVE	0
73298178Sdelphij#define DER_TYPE_CONSTRUCTED	1
74362844Sdelphij#if defined(DEBUG_DER) || defined(TEST_DER)
75298178Sdelphijstatic const char der_type[] = "PC";
76299736Sdelphij#endif
77298178Sdelphij
78298178Sdelphij#define	DER_TAG_EOC			0x00
79298178Sdelphij#define	DER_TAG_BOOLEAN			0x01
80298178Sdelphij#define	DER_TAG_INTEGER			0x02
81298178Sdelphij#define	DER_TAG_BIT STRING		0x03
82298178Sdelphij#define	DER_TAG_OCTET_STRING		0x04
83298178Sdelphij#define	DER_TAG_NULL			0x05
84298178Sdelphij#define	DER_TAG_OBJECT_IDENTIFIER	0x06
85298178Sdelphij#define	DER_TAG_OBJECT_DESCRIPTOR	0x07
86298178Sdelphij#define	DER_TAG_EXTERNAL		0x08
87298178Sdelphij#define	DER_TAG_REAL			0x09
88298178Sdelphij#define	DER_TAG_ENUMERATED		0x0a
89298178Sdelphij#define	DER_TAG_EMBEDDED_PDV		0x0b
90298178Sdelphij#define	DER_TAG_UTF8_STRING		0x0c
91298178Sdelphij#define	DER_TAG_RELATIVE_OID		0x0d
92362844Sdelphij#define DER_TAG_TIME			0x0e
93298178Sdelphij#define DER_TAG_RESERVED_2		0x0f
94298178Sdelphij#define	DER_TAG_SEQUENCE		0x10
95298178Sdelphij#define	DER_TAG_SET			0x11
96298178Sdelphij#define	DER_TAG_NUMERIC_STRING		0x12
97298178Sdelphij#define	DER_TAG_PRINTABLE_STRING	0x13
98298178Sdelphij#define	DER_TAG_T61_STRING		0x14
99298178Sdelphij#define	DER_TAG_VIDEOTEX_STRING		0x15
100298178Sdelphij#define	DER_TAG_IA5_STRING		0x16
101298178Sdelphij#define	DER_TAG_UTCTIME			0x17
102298178Sdelphij#define	DER_TAG_GENERALIZED_TIME	0x18
103298178Sdelphij#define	DER_TAG_GRAPHIC_STRING		0x19
104298178Sdelphij#define	DER_TAG_VISIBLE_STRING		0x1a
105298178Sdelphij#define	DER_TAG_GENERAL_STRING		0x1b
106298178Sdelphij#define	DER_TAG_UNIVERSAL_STRING	0x1c
107298178Sdelphij#define	DER_TAG_CHARACTER_STRING	0x1d
108298178Sdelphij#define	DER_TAG_BMP_STRING		0x1e
109362844Sdelphij#define	DER_TAG_DATE			0x1f
110362844Sdelphij#define	DER_TAG_TIME_OF_DAY		0x20
111362844Sdelphij#define	DER_TAG_DATE_TIME		0x21
112362844Sdelphij#define	DER_TAG_DURATION		0x22
113362844Sdelphij#define	DER_TAG_OID_IRI			0x23
114362844Sdelphij#define	DER_TAG_RELATIVE_OID_IRI	0x24
115362844Sdelphij#define	DER_TAG_LAST			0x25
116298178Sdelphij
117298178Sdelphijstatic const char *der__tag[] = {
118298178Sdelphij	"eoc", "bool", "int", "bit_str", "octet_str",
119298178Sdelphij	"null", "obj_id", "obj_desc", "ext", "real",
120362844Sdelphij	"enum", "embed", "utf8_str", "rel_oid", "time",
121298178Sdelphij	"res2", "seq", "set", "num_str", "prt_str",
122362844Sdelphij	"t61_str", "vid_str", "ia5_str", "utc_time", "gen_time",
123362844Sdelphij	"gr_str", "vis_str", "gen_str", "univ_str", "char_str",
124362844Sdelphij	"bmp_str", "date", "tod", "datetime", "duration",
125362844Sdelphij	"oid-iri", "rel-oid-iri",
126298178Sdelphij};
127298178Sdelphij
128298178Sdelphij#ifdef DEBUG_DER
129298178Sdelphij#define DPRINTF(a) printf a
130298178Sdelphij#else
131298178Sdelphij#define DPRINTF(a)
132298178Sdelphij#endif
133298178Sdelphij
134298178Sdelphij#ifdef TEST_DER
135298178Sdelphijstatic uint8_t
136298178Sdelphijgetclass(uint8_t c)
137298178Sdelphij{
138298178Sdelphij	return c >> 6;
139298178Sdelphij}
140298178Sdelphij
141298178Sdelphijstatic uint8_t
142298178Sdelphijgettype(uint8_t c)
143298178Sdelphij{
144298178Sdelphij	return (c >> 5) & 1;
145298178Sdelphij}
146298178Sdelphij#endif
147298178Sdelphij
148298178Sdelphijstatic uint32_t
149298178Sdelphijgettag(const uint8_t *c, size_t *p, size_t l)
150298178Sdelphij{
151298178Sdelphij	uint32_t tag;
152298178Sdelphij
153298178Sdelphij	if (*p >= l)
154298178Sdelphij		return DER_BAD;
155298178Sdelphij
156298178Sdelphij	tag = c[(*p)++] & 0x1f;
157298178Sdelphij
158298178Sdelphij	if (tag != 0x1f)
159298178Sdelphij		return tag;
160298178Sdelphij
161298178Sdelphij	if (*p >= l)
162298178Sdelphij		return DER_BAD;
163298178Sdelphij
164298178Sdelphij	while (c[*p] >= 0x80) {
165298178Sdelphij		tag = tag * 128 + c[(*p)++] - 0x80;
166298178Sdelphij		if (*p >= l)
167298178Sdelphij			return DER_BAD;
168298178Sdelphij	}
169298178Sdelphij	return tag;
170298178Sdelphij}
171298178Sdelphij
172328874Seadler/*
173328874Seadler * Read the length of a DER tag from the input.
174328874Seadler *
175328874Seadler * `c` is the input, `p` is an output parameter that specifies how much of the
176328874Seadler * input we consumed, and `l` is the maximum input length.
177328874Seadler *
178328874Seadler * Returns the length, or DER_BAD if the end of the input is reached or the
179328874Seadler * length exceeds the remaining input.
180328874Seadler */
181298178Sdelphijstatic uint32_t
182298178Sdelphijgetlength(const uint8_t *c, size_t *p, size_t l)
183298178Sdelphij{
184298178Sdelphij	uint8_t digits, i;
185298178Sdelphij	size_t len;
186328874Seadler	int is_onebyte_result;
187298178Sdelphij
188362844Sdelphij	if (*p >= l) {
189362844Sdelphij		DPRINTF(("%s:[1] %zu >= %zu\n", __func__, *p, l));
190298178Sdelphij		return DER_BAD;
191362844Sdelphij	}
192298178Sdelphij
193328874Seadler	/*
194328874Seadler	 * Digits can either be 0b0 followed by the result, or 0b1
195328874Seadler	 * followed by the number of digits of the result. In either case,
196328874Seadler	 * we verify that we can read so many bytes from the input.
197328874Seadler	 */
198328874Seadler	is_onebyte_result = (c[*p] & 0x80) == 0;
199328874Seadler	digits = c[(*p)++] & 0x7f;
200362844Sdelphij	if (*p + digits >= l) {
201362844Sdelphij		DPRINTF(("%s:[2] %zu + %u >= %zu\n", __func__, *p, digits, l));
202328874Seadler		return DER_BAD;
203362844Sdelphij	}
204298178Sdelphij
205328874Seadler	if (is_onebyte_result)
206298178Sdelphij		return digits;
207298178Sdelphij
208328874Seadler	/*
209328874Seadler	 * Decode len. We've already verified that we're allowed to read
210328874Seadler	 * `digits` bytes.
211328874Seadler	 */
212298178Sdelphij	len = 0;
213298178Sdelphij	for (i = 0; i < digits; i++)
214298178Sdelphij		len = (len << 8) | c[(*p)++];
215328874Seadler
216362844Sdelphij	if (len > UINT32_MAX - *p || *p + len > l) {
217362844Sdelphij		DPRINTF(("%s:[3] bad len %zu + %zu >= %zu\n",
218362844Sdelphij		    __func__, *p, len, l));
219309847Sdelphij		return DER_BAD;
220362844Sdelphij	}
221328874Seadler	return CAST(uint32_t, len);
222298178Sdelphij}
223298178Sdelphij
224298178Sdelphijstatic const char *
225298178Sdelphijder_tag(char *buf, size_t len, uint32_t tag)
226298178Sdelphij{
227362844Sdelphij	if (tag < DER_TAG_LAST)
228298178Sdelphij		strlcpy(buf, der__tag[tag], len);
229298178Sdelphij	else
230298178Sdelphij		snprintf(buf, len, "%#x", tag);
231298178Sdelphij	return buf;
232298178Sdelphij}
233298178Sdelphij
234298178Sdelphij#ifndef TEST_DER
235298178Sdelphijstatic int
236298178Sdelphijder_data(char *buf, size_t blen, uint32_t tag, const void *q, uint32_t len)
237298178Sdelphij{
238309847Sdelphij	const uint8_t *d = CAST(const uint8_t *, q);
239298178Sdelphij	switch (tag) {
240298178Sdelphij	case DER_TAG_PRINTABLE_STRING:
241298178Sdelphij	case DER_TAG_UTF8_STRING:
242298178Sdelphij	case DER_TAG_IA5_STRING:
243362844Sdelphij		return snprintf(buf, blen, "%.*s", len, RCAST(const char *, q));
244298178Sdelphij	case DER_TAG_UTCTIME:
245362844Sdelphij		if (len < 12)
246362844Sdelphij			break;
247362844Sdelphij		return snprintf(buf, blen,
248362844Sdelphij		    "20%c%c-%c%c-%c%c %c%c:%c%c:%c%c GMT", d[0], d[1], d[2],
249362844Sdelphij		    d[3], d[4], d[5], d[6], d[7], d[8], d[9], d[10], d[11]);
250362844Sdelphij		break;
251298178Sdelphij	default:
252298178Sdelphij		break;
253298178Sdelphij	}
254354939Sdelphij
255298178Sdelphij	for (uint32_t i = 0; i < len; i++) {
256298178Sdelphij		uint32_t z = i << 1;
257298178Sdelphij		if (z < blen - 2)
258298178Sdelphij			snprintf(buf + z, blen - z, "%.2x", d[i]);
259298178Sdelphij	}
260298178Sdelphij	return len * 2;
261298178Sdelphij}
262298178Sdelphij
263298178Sdelphijint32_t
264298178Sdelphijder_offs(struct magic_set *ms, struct magic *m, size_t nbytes)
265298178Sdelphij{
266309847Sdelphij	const uint8_t *b = RCAST(const uint8_t *, ms->search.s);
267302221Sdelphij	size_t offs = 0, len = ms->search.s_len ? ms->search.s_len : nbytes;
268298178Sdelphij
269362844Sdelphij	if (gettag(b, &offs, len) == DER_BAD) {
270362844Sdelphij		DPRINTF(("%s: bad tag 1\n", __func__));
271298178Sdelphij		return -1;
272362844Sdelphij	}
273354939Sdelphij	DPRINTF(("%s1: %d %" SIZE_T_FORMAT "u %u\n", __func__, ms->offset,
274354939Sdelphij	    offs, m->offset));
275298178Sdelphij
276298178Sdelphij	uint32_t tlen = getlength(b, &offs, len);
277362844Sdelphij	if (tlen == DER_BAD) {
278362844Sdelphij		DPRINTF(("%s: bad tag 2\n", __func__));
279298178Sdelphij		return -1;
280362844Sdelphij	}
281354939Sdelphij	DPRINTF(("%s2: %d %" SIZE_T_FORMAT "u %u\n", __func__, ms->offset,
282354939Sdelphij	    offs, tlen));
283298178Sdelphij
284298178Sdelphij	offs += ms->offset + m->offset;
285298178Sdelphij	DPRINTF(("cont_level = %d\n", m->cont_level));
286298178Sdelphij#ifdef DEBUG_DER
287298178Sdelphij	for (size_t i = 0; i < m->cont_level; i++)
288354939Sdelphij		printf("cont_level[%" SIZE_T_FORMAT "u] = %u\n", i,
289354939Sdelphij		    ms->c.li[i].off);
290298178Sdelphij#endif
291298178Sdelphij	if (m->cont_level != 0) {
292298178Sdelphij		if (offs + tlen > nbytes)
293328874Seadler			return -1;
294328874Seadler		ms->c.li[m->cont_level - 1].off = CAST(int, offs + tlen);
295298178Sdelphij		DPRINTF(("cont_level[%u] = %u\n", m->cont_level - 1,
296298178Sdelphij		    ms->c.li[m->cont_level - 1].off));
297298178Sdelphij	}
298328874Seadler	return CAST(int32_t, offs);
299298178Sdelphij}
300298178Sdelphij
301298178Sdelphijint
302298178Sdelphijder_cmp(struct magic_set *ms, struct magic *m)
303298178Sdelphij{
304309847Sdelphij	const uint8_t *b = RCAST(const uint8_t *, ms->search.s);
305298178Sdelphij	const char *s = m->value.s;
306298178Sdelphij	size_t offs = 0, len = ms->search.s_len;
307298178Sdelphij	uint32_t tag, tlen;
308298178Sdelphij	char buf[128];
309298178Sdelphij
310362844Sdelphij	DPRINTF(("%s: compare %zu bytes\n", __func__, len));
311362844Sdelphij
312298178Sdelphij	tag = gettag(b, &offs, len);
313362844Sdelphij	if (tag == DER_BAD) {
314362844Sdelphij		DPRINTF(("%s: bad tag 1\n", __func__));
315298178Sdelphij		return -1;
316362844Sdelphij	}
317298178Sdelphij
318362844Sdelphij	DPRINTF(("%s1: %d %" SIZE_T_FORMAT "u %u\n", __func__, ms->offset,
319362844Sdelphij	    offs, m->offset));
320362844Sdelphij
321298178Sdelphij	tlen = getlength(b, &offs, len);
322362844Sdelphij	if (tlen == DER_BAD) {
323362844Sdelphij		DPRINTF(("%s: bad tag 2\n", __func__));
324298178Sdelphij		return -1;
325362844Sdelphij	}
326298178Sdelphij
327298178Sdelphij	der_tag(buf, sizeof(buf), tag);
328298178Sdelphij	if ((ms->flags & MAGIC_DEBUG) != 0)
329298178Sdelphij		fprintf(stderr, "%s: tag %p got=%s exp=%s\n", __func__, b,
330298178Sdelphij		    buf, s);
331298178Sdelphij	size_t slen = strlen(buf);
332298178Sdelphij
333298178Sdelphij	if (strncmp(buf, s, slen) != 0)
334298178Sdelphij		return 0;
335298178Sdelphij
336298178Sdelphij	s += slen;
337298178Sdelphij
338298178Sdelphijagain:
339298178Sdelphij	switch (*s) {
340298178Sdelphij	case '\0':
341298178Sdelphij		return 1;
342298178Sdelphij	case '=':
343298178Sdelphij		s++;
344298178Sdelphij		goto val;
345298178Sdelphij	default:
346354939Sdelphij		if (!isdigit(CAST(unsigned char, *s)))
347298178Sdelphij			return 0;
348298178Sdelphij
349298178Sdelphij		slen = 0;
350298178Sdelphij		do
351298178Sdelphij			slen = slen * 10 + *s - '0';
352354939Sdelphij		while (isdigit(CAST(unsigned char, *++s)));
353298178Sdelphij		if ((ms->flags & MAGIC_DEBUG) != 0)
354354939Sdelphij			fprintf(stderr, "%s: len %" SIZE_T_FORMAT "u %u\n",
355354939Sdelphij			    __func__, slen, tlen);
356298178Sdelphij		if (tlen != slen)
357298178Sdelphij			return 0;
358298178Sdelphij		goto again;
359298178Sdelphij	}
360298178Sdelphijval:
361354939Sdelphij	DPRINTF(("%s: before data %" SIZE_T_FORMAT "u %u\n", __func__, offs,
362354939Sdelphij	    tlen));
363298178Sdelphij	der_data(buf, sizeof(buf), tag, b + offs, tlen);
364298178Sdelphij	if ((ms->flags & MAGIC_DEBUG) != 0)
365298178Sdelphij		fprintf(stderr, "%s: data %s %s\n", __func__, buf, s);
366298178Sdelphij	if (strcmp(buf, s) != 0 && strcmp("x", s) != 0)
367298178Sdelphij		return 0;
368298178Sdelphij	strlcpy(ms->ms_value.s, buf, sizeof(ms->ms_value.s));
369298178Sdelphij	return 1;
370298178Sdelphij}
371298178Sdelphij#endif
372298178Sdelphij
373298178Sdelphij#ifdef TEST_DER
374298178Sdelphijstatic void
375298178Sdelphijprinttag(uint32_t tag, const void *q, uint32_t len)
376298178Sdelphij{
377298178Sdelphij	const uint8_t *d = q;
378298178Sdelphij	switch (tag) {
379298178Sdelphij	case DER_TAG_PRINTABLE_STRING:
380298178Sdelphij	case DER_TAG_UTF8_STRING:
381362844Sdelphij	case DER_TAG_IA5_STRING:
382362844Sdelphij	case DER_TAG_UTCTIME:
383298178Sdelphij		printf("%.*s\n", len, (const char *)q);
384298178Sdelphij		return;
385298178Sdelphij	default:
386298178Sdelphij		break;
387298178Sdelphij	}
388354939Sdelphij
389298178Sdelphij	for (uint32_t i = 0; i < len; i++)
390298178Sdelphij		printf("%.2x", d[i]);
391298178Sdelphij	printf("\n");
392298178Sdelphij}
393298178Sdelphij
394298178Sdelphijstatic void
395298178Sdelphijprintdata(size_t level, const void *v, size_t x, size_t l)
396298178Sdelphij{
397298178Sdelphij	const uint8_t *p = v, *ep = p + l;
398298178Sdelphij	size_t ox;
399298178Sdelphij	char buf[128];
400298178Sdelphij
401298178Sdelphij	while (p + x < ep) {
402298178Sdelphij		const uint8_t *q;
403298178Sdelphij		uint8_t c = getclass(p[x]);
404298178Sdelphij		uint8_t t = gettype(p[x]);
405298178Sdelphij		ox = x;
406362844Sdelphij//		if (x != 0)
407362844Sdelphij//		printf("%.2x %.2x %.2x\n", p[x - 1], p[x], p[x + 1]);
408298178Sdelphij		uint32_t tag = gettag(p, &x, ep - p + x);
409298178Sdelphij		if (p + x >= ep)
410298178Sdelphij			break;
411298178Sdelphij		uint32_t len = getlength(p, &x, ep - p + x);
412354939Sdelphij
413354939Sdelphij		printf("%" SIZE_T_FORMAT "u %" SIZE_T_FORMAT "u-%"
414354939Sdelphij		    SIZE_T_FORMAT "u %c,%c,%s,%u:", level, ox, x,
415298178Sdelphij		    der_class[c], der_type[t],
416298178Sdelphij		    der_tag(buf, sizeof(buf), tag), len);
417298178Sdelphij		q = p + x;
418298178Sdelphij		if (p + len > ep)
419298178Sdelphij			errx(EXIT_FAILURE, "corrupt der");
420298178Sdelphij		printtag(tag, q, len);
421298178Sdelphij		if (t != DER_TYPE_PRIMITIVE)
422298178Sdelphij			printdata(level + 1, p, x, len + x);
423298178Sdelphij		x += len;
424298178Sdelphij	}
425298178Sdelphij}
426298178Sdelphij
427298178Sdelphijint
428298178Sdelphijmain(int argc, char *argv[])
429298178Sdelphij{
430298178Sdelphij	int fd;
431298178Sdelphij	struct stat st;
432298178Sdelphij	size_t l;
433298178Sdelphij	void *p;
434298178Sdelphij
435298178Sdelphij	if ((fd = open(argv[1], O_RDONLY)) == -1)
436298178Sdelphij		err(EXIT_FAILURE, "open `%s'", argv[1]);
437298178Sdelphij	if (fstat(fd, &st) == -1)
438298178Sdelphij		err(EXIT_FAILURE, "stat `%s'", argv[1]);
439298178Sdelphij	l = (size_t)st.st_size;
440298178Sdelphij	if ((p = mmap(NULL, l, PROT_READ, MAP_FILE, fd, 0)) == MAP_FAILED)
441298178Sdelphij		err(EXIT_FAILURE, "mmap `%s'", argv[1]);
442298178Sdelphij
443298178Sdelphij	printdata(0, p, 0, l);
444298178Sdelphij	munmap(p, l);
445298178Sdelphij	return 0;
446298178Sdelphij}
447298178Sdelphij#endif
448