blob: a9b134303a29687117f93cb001f477a9a01a1193 [file] [log] [blame]
Pau Espin Pedrolc90e6f82021-10-19 14:45:17 +02001/* csn1_enc.c
Ivan Kluchnikov487a1412011-12-21 13:17:53 +03002 * Routines for CSN1 dissection in wireshark.
3 *
4 * Copyright (C) 2011 Ivan Klyuchnikov
5 *
6 * By Vincent Helfre, based on original code by Jari Sassi
7 * with the gracious authorization of STE
8 * Copyright (c) 2011 ST-Ericsson
9 *
10 * $Id: packet-csn1.c 39140 2011-09-25 22:01:50Z wmeier $
11 *
12 * Wireshark - Network traffic analyzer
13 * By Gerald Combs <gerald@wireshark.org>
14 * Copyright 1998 Gerald Combs
15 *
16 * This program is free software; you can redistribute it and/or
17 * modify it under the terms of the GNU General Public License
18 * as published by the Free Software Foundation; either version 2
19 * of the License, or (at your option) any later version.
20 *
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030025 */
26
Ivan Kluchnikov835f91e2012-04-30 18:00:36 +040027#include <assert.h>
28#include <string.h>
Holger Hans Peter Freythercf0265a2013-07-28 15:57:20 +020029#define __STDC_FORMAT_MACROS
30#include <inttypes.h>
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030031#include "csn1.h"
Ivan Kluchnikova9f1ff22012-05-24 22:25:06 +040032#include <gprs_debug.h>
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030033
Pau Espin Pedrold636f742020-02-03 15:37:08 +010034#include <osmocom/core/logging.h>
35#include <osmocom/core/utils.h>
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030036
Pau Espin Pedrolc90e6f82021-10-19 14:45:17 +020037const unsigned char ixBitsTab[] = {0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5};
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030038
39/* Returns no_of_bits (up to 8) masked with 0x2B */
Pau Espin Pedrolc90e6f82021-10-19 14:45:17 +020040guint8
Vadim Yanitskiy39a65052020-01-25 01:24:59 +070041get_masked_bits8(struct bitvec *vector, unsigned *readIndex, gint bit_offset, const gint no_of_bits)
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030042{
43 static const guint8 maskBits[] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF};
44 //gint byte_offset = bit_offset >> 3; /* divide by 8 */
45 gint relative_bit_offset = bit_offset & 0x07; /* modulo 8 */
46 guint8 result;
47 gint bit_shift = 8 - relative_bit_offset - (gint) no_of_bits;
Vadim Yanitskiy39a65052020-01-25 01:24:59 +070048 *readIndex -= relative_bit_offset;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030049 if (bit_shift >= 0)
50 {
Vadim Yanitskiy39a65052020-01-25 01:24:59 +070051 result = (0x2B ^ ((guint8)bitvec_read_field(vector, readIndex, 8))) >> bit_shift;
52 *readIndex-= bit_shift;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030053 result &= maskBits[no_of_bits];
54 }
55 else
Pau Espin Pedrolf960d5b2020-01-23 19:05:00 +010056 {
Vadim Yanitskiy39a65052020-01-25 01:24:59 +070057 guint8 hight_part = (0x2B ^ ((guint8)bitvec_read_field(vector, readIndex, 8))) & maskBits[8 - relative_bit_offset];
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030058 hight_part = (guint8) (hight_part << (-bit_shift));
Vadim Yanitskiy39a65052020-01-25 01:24:59 +070059 result = (0x2B ^ ((guint8)bitvec_read_field(vector, readIndex, 8))) >> (8 + bit_shift);
60 *readIndex = *readIndex - (8 - (-bit_shift));
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030061 result |= hight_part;
62 }
63 return result;
64}
65
66/**
67 * ================================================================================================
68 * set initial/start values in help data structure used for packing/unpacking operation
69 * ================================================================================================
70 */
71void
72csnStreamInit(csnStream_t* ar, gint bit_offset, gint remaining_bits_len)
73{
74 ar->remaining_bits_len = remaining_bits_len;
75 ar->bit_offset = bit_offset;
Ivan Kluchnikov1b517342013-12-30 14:26:06 +040076 ar->direction = 0;
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030077}
78
Pau Espin Pedrold636f742020-02-03 15:37:08 +010079static const struct value_string csn1_error_names[] = {
80 { CSN_OK, "General 0" },
81 { CSN_ERROR_GENERAL, "General -1" },
82 { CSN_ERROR_DATA_NOT_VALID, "DATA_NOT VALID" },
83 { CSN_ERROR_IN_SCRIPT, "IN SCRIPT" },
84 { CSN_ERROR_INVALID_UNION_INDEX, "INVALID UNION INDEX" },
85 { CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, "NEED_MORE BITS TO UNPACK" },
Vadim Yanitskiyfee767f2020-02-11 05:28:02 +070086 { CSN_ERROR_ILLEGAL_BIT_VALUE, "ILLEGAL BIT VALUE" },
87 { CSN_ERROR_INTERNAL, "INTERNAL" },
Pau Espin Pedrold636f742020-02-03 15:37:08 +010088 { CSN_ERROR_STREAM_NOT_SUPPORTED, "STREAM_NOT_SUPPORTED" },
89 { CSN_ERROR_MESSAGE_TOO_LONG, "MESSAGE_TOO_LONG" },
90 { 0, NULL }
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030091};
92
Pau Espin Pedrold636f742020-02-03 15:37:08 +010093
Pau Espin Pedrolc90e6f82021-10-19 14:45:17 +020094gint16 ProcessError_impl(const char *file, int line, unsigned *readIndex,
Pau Espin Pedrold636f742020-02-03 15:37:08 +010095 const char* sz, gint16 err, const CSN_DESCR* pDescr)
Ivan Kluchnikov487a1412011-12-21 13:17:53 +030096{
Pau Espin Pedrolac2b8662020-02-03 18:58:24 +010097 /* Don't add trailing newline, top caller is responsible for appending it */
Pau Espin Pedrold636f742020-02-03 15:37:08 +010098 if (err != CSN_OK)
Vadim Yanitskiy15530492020-02-17 19:38:22 +070099 LOGPSRC(DCSN1, LOGL_ERROR, file, line, "%s: error %s (%d) at %s (idx %u)",
Pau Espin Pedrold636f742020-02-03 15:37:08 +0100100 sz, get_value_string(csn1_error_names, err), err,
Vadim Yanitskiy39a65052020-01-25 01:24:59 +0700101 pDescr ? pDescr->sz : "-", *readIndex);
Ivan Kluchnikov487a1412011-12-21 13:17:53 +0300102 return err;
103}