aboutsummaryrefslogtreecommitdiffstats
path: root/library/IuUP_EncDec.cc
blob: 81742e8a9ebea7af33ad969c4e84d9ca2d9d053c (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
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
#include "Octetstring.hh"
#include "Error.hh"
#include "Logger.hh"

#include "IuUP_Types.hh"

#include <stdint.h>

extern "C" {

typedef uint8_t ubit_t;
typedef uint8_t pbit_t;

static int osmo_pbit2ubit(ubit_t *out, const pbit_t *in, unsigned int num_bits)
{
	unsigned int i;
	ubit_t *cur = out;
	ubit_t *limit = out + num_bits;

	for (i = 0; i < (num_bits/8)+1; i++) {
		pbit_t byte = in[i];
		*cur++ = (byte >> 7) & 1;
		if (cur >= limit)
			break;
		*cur++ = (byte >> 6) & 1;
		if (cur >= limit)
			break;
		*cur++ = (byte >> 5) & 1;
		if (cur >= limit)
			break;
		*cur++ = (byte >> 4) & 1;
		if (cur >= limit)
			break;
		*cur++ = (byte >> 3) & 1;
		if (cur >= limit)
			break;
		*cur++ = (byte >> 2) & 1;
		if (cur >= limit)
			break;
		*cur++ = (byte >> 1) & 1;
		if (cur >= limit)
			break;
		*cur++ = (byte >> 0) & 1;
		if (cur >= limit)
			break;
	}
	return cur - out;
}


struct osmo_crc8gen_code {
	int bits;           /*!< Actual number of bits of the CRC */
	uint8_t poly;      /*!< Polynom (normal representation, MSB omitted */
	uint8_t init;      /*!< Initialization value of the CRC state */
	uint8_t remainder; /*!< Remainder of the CRC (final XOR) */
};

static uint8_t
osmo_crc8gen_compute_bits(const struct osmo_crc8gen_code *code,
                           const ubit_t *in, int len)
{
	const uint8_t poly = code->poly;
	uint8_t crc = code->init;
	int i, n = code->bits-1;

	for (i=0; i<len; i++) {
		uint8_t bit = in[i] & 1;
		crc ^= (bit << n);
		if (crc & ((uint8_t)1 << n)) {
			crc <<= 1;
			crc ^= poly;
		} else {
			crc <<= 1;
		}
		crc &= ((uint8_t)1 << code->bits) - 1;
	}

	crc ^= code->remainder;

	return crc;
}

struct osmo_crc16gen_code {
	int bits;           /*!< Actual number of bits of the CRC */
	uint16_t poly;      /*!< Polynom (normal representation, MSB omitted */
	uint16_t init;      /*!< Initialization value of the CRC state */
	uint16_t remainder; /*!< Remainder of the CRC (final XOR) */
};

static uint16_t
osmo_crc16gen_compute_bits(const struct osmo_crc16gen_code *code,
                           const ubit_t *in, int len)
{
	const uint16_t poly = code->poly;
	uint16_t crc = code->init;
	int i, n = code->bits-1;

	for (i=0; i<len; i++) {
		uint16_t bit = in[i] & 1;
		crc ^= (bit << n);
		if (crc & ((uint16_t)1 << n)) {
			crc <<= 1;
			crc ^= poly;
		} else {
			crc <<= 1;
		}
		crc &= ((uint16_t)1 << code->bits) - 1;
	}

	crc ^= code->remainder;

	return crc;
}

}


static const struct osmo_crc8gen_code iuup_hdr_crc_code = {
	.bits = 6,
	.poly = 47,
	.init = 0,
	.remainder = 0,
};

static const struct osmo_crc16gen_code iuup_data_crc_code = {
	.bits = 10,
	.poly = 563,
	.init = 0,
	.remainder = 0,
};

static int iuup_get_payload_offset(const uint8_t *iuup_pdu)
{
	uint8_t pdu_type = iuup_pdu[0] >> 4;
	switch (pdu_type) {
	case 0:
	case 14:
		return 4;
	case 1:
		return 3;
	default:
		return -1;
	}
}

int osmo_iuup_compute_payload_crc(const uint8_t *iuup_pdu, unsigned int pdu_len)
{
	ubit_t buf[1024*8];
	uint8_t pdu_type;
	int offset, payload_len_bytes;

	if (pdu_len < 1)
		return -1;

	pdu_type = iuup_pdu[0] >> 4;

	/* Type 1 has no CRC */
	if (pdu_type == 1)
		return 0;

	offset = iuup_get_payload_offset(iuup_pdu);
	if (offset < 0)
		return offset;

	if (pdu_len < (unsigned int)offset)
		return -1;

	payload_len_bytes = pdu_len - offset;
	osmo_pbit2ubit(buf, iuup_pdu+offset, payload_len_bytes*8);
	return osmo_crc16gen_compute_bits(&iuup_data_crc_code, buf, payload_len_bytes*8);
}

int osmo_iuup_compute_header_crc(const uint8_t *iuup_pdu, unsigned int pdu_len)
{
	ubit_t buf[2*8];

	if (pdu_len < 2)
		return -1;

	osmo_pbit2ubit(buf, iuup_pdu, 2*8);
	return osmo_crc8gen_compute_bits(&iuup_hdr_crc_code, buf, 2*8);
}
/* IuUP CRC Implementation */

/* (C) 2017 by Harald Welte <laforge@gnumonks.org>
 *
 * All Rights Reserved
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of 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.
 *
 * This program 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 Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace IuUP__Types {


INTEGER f__IuUP__compute__crc__payload(OCTETSTRING const &in)
{
	int crc_calc;
	const unsigned char *data = (const unsigned char *)in;
	int len = in.lengthof();

	crc_calc = osmo_iuup_compute_payload_crc(data, len);

	return INTEGER(crc_calc);
}

INTEGER f__IuUP__compute__crc__header(OCTETSTRING const &in)
{
	int crc_calc;
	const unsigned char *data = (const unsigned char *)in;
	int len = in.lengthof();

	crc_calc = osmo_iuup_compute_header_crc(data, len);

	return INTEGER(crc_calc);
}

OCTETSTRING f__enc__IuUP__PDU(const IuUP__PDU& pdu)
{
	TTCN_Buffer buf;
	int crc_hdr, crc_payload;
	uint8_t in[2];
	pdu.encode(IuUP__PDU_descr_, buf, TTCN_EncDec::CT_RAW);
	OCTETSTRING ret_val(buf.get_len(), buf.get_data());

	crc_hdr = osmo_iuup_compute_header_crc(buf.get_data(), buf.get_len());
	crc_payload = osmo_iuup_compute_payload_crc(buf.get_data(), buf.get_len());
	in[0] = (crc_hdr & 0x3f) << 2;
	in[0] |= (crc_payload & 0x3ff) >> 8;
	in[1] = crc_payload & 0xff;
	OCTETSTRING CHECKSUM(2, in);

	ret_val = substr(ret_val, 0, 2) + CHECKSUM + substr(ret_val, 4, ret_val.lengthof()-4);

	return ret_val;
}

}