aboutsummaryrefslogtreecommitdiffstats
path: root/unpack.c
blob: 7d2b08a60a89d55e626231ac08d935abb51744c4 (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
/*
 * Copyright (C) 2020-2021  Tomasz Kramkowski <tk@the-tk.com>
 * SPDX-License-Identifier: MIT
 */
#include <ctype.h>
#include <errno.h>
#include <inttypes.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#include "common.h"
#include "ieee754b.h"
#include "pack.h"
#include "trace.h"

static uintmax_t read_val(const unsigned char *buf, size_t size, enum endian e)
{
	uintmax_t val = 0;

	for (size_t i = 0; i < size; i++) {
		val |= (uintmax_t)(buf[i] & 0xff) << (e == LITTLE ? i : size - i - 1) * 8;
	}

	return val;
}

static intmax_t minval(size_t s)
{
	switch (s) {
	case 1: return INTMAX_C(-128);
	case 2: return INTMAX_C(-32768);
	case 4: return INTMAX_C(-2147483648);
	default: return -INTMAX_C(9223372036854775807) - 1;
	}
}

enum pack_status unpack(const void *buf_, size_t size, const char *fmt, ...)
{
	enum endian endianness = BIG;
	const unsigned char *buf = buf_;
	size_t offset = 0;
	va_list ap;

	tr_call("unpack(%p, %" PRIuSIZE ", %s, ...)", (const void *)buf, size, fmt);

	va_start(ap, fmt);

	for (int i = 0; fmt[i] != '\0'; i++) {
		bool sign;
		size_t count = 1, s;
		union {
			signed   char      *b;
			unsigned char      *B;
			         short     *h;
			unsigned short     *H;
			         int       *i;
			unsigned int       *I;
			         long      *l;
			unsigned long      *L;
			         long long *q;
			unsigned long long *Q;
			         float     *f;
			         double    *d;
		} arg;
		union { uintmax_t u; intmax_t s; float f; double d; } val;
		tr_debug("i: %d, fmt[i]: %c", i, fmt[i]);
		if (isdigit(fmt[i])) {
			unsigned long long c;
			char *end;

			errno = 0;
			c = strtoull(&fmt[i], &end, 10);
			if ((c == ULLONG_MAX && errno == ERANGE) || c > SIZE_MAX)
				return PACK_FMTINVAL;
			count = c;
			i += end - &fmt[i];
		} else if (fmt[i] == '*') {
			count = va_arg(ap, size_t);
			i++;
		}
		sign = islower(fmt[i]);
		tr_debug("count: %" PRIuSIZE ", i: %d, fmt[i]: %c, sign: %ssigned",
			 count, i, fmt[i], sign ? "" : "un");
		switch (fmt[i]) {
		case '>': endianness = BIG; continue;
		case '<': endianness = LITTLE; continue;
		case 'b': arg.b = va_arg(ap, signed   char      *); break;
		case 'B': arg.B = va_arg(ap, unsigned char      *); break;
		case 'h': arg.h = va_arg(ap,          short     *); break;
		case 'H': arg.H = va_arg(ap, unsigned short     *); break;
		case 'i': arg.i = va_arg(ap,          int       *); break;
		case 'I': arg.I = va_arg(ap, unsigned           *); break;
		case 'l': arg.l = va_arg(ap,          long      *); break;
		case 'L': arg.L = va_arg(ap, unsigned long      *); break;
		case 'q': arg.q = va_arg(ap,          long long *); break;
		case 'Q': arg.Q = va_arg(ap, unsigned long long *); break;
		case 'f': arg.f = va_arg(ap,          float     *); break;
		case 'd': arg.d = va_arg(ap,          double    *); break;
		case 'x': break;
		default: return PACK_FMTINVAL;
		}

		s = getsize(fmt[i]);
		tr_debug("s: %" PRIuSIZE, s);
		if (s == (size_t)-1) return PACK_FMTINVAL;

		if (size - offset < s * count) return PACK_TOOSMALL;

		if (fmt[i] == 'x') goto skip;

		for (size_t j = 0; j < count; j++) {
			val.u = read_val(buf + offset + s * j, s, endianness);
			tr_debug("val.u: %" PRIuMAX ", at: %" PRIuSIZE, val.u, offset + s * j);

			if (fmt[i] == 'f') {
				float f = ieee754b32_deserialise(val.u);
				val.f = f;
				tr_debug("val.f: %f", val.f);
			} else if (fmt[i] == 'd') {
				double d = ieee754b64_deserialise(val.u);
				val.d = d;
				tr_debug("val.d: %f", val.d);
			} else if (sign) {
				intmax_t vals;
				if (!(val.u & (UINTMAX_C(1) << (s * 8 - 1)))) {
					vals = val.u;
				} else {
					vals = minval(s);
					vals += val.u ^ (UINTMAX_C(1) << (s * 8 - 1));
				}
				val.s = vals;
				tr_debug("val.s: %" PRIdMAX, val.s);
			}

			switch (fmt[i]) {
			case 'b': arg.b[j] = val.s; break;
			case 'B': arg.B[j] = val.u; break;
			case 'h': arg.h[j] = val.s; break;
			case 'H': arg.H[j] = val.u; break;
			case 'i': arg.i[j] = val.s; break;
			case 'I': arg.I[j] = val.u; break;
			case 'l': arg.l[j] = val.s; break;
			case 'L': arg.L[j] = val.u; break;
			case 'q': arg.q[j] = val.s; break;
			case 'Q': arg.Q[j] = val.u; break;
			case 'f': arg.f[j] = val.f; break;
			case 'd': arg.d[j] = val.d; break;
			}
		}
skip:
		offset += s * count;
	}

	va_end(ap);

	return PACK_OK;
}