Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1 | /* csn1.cpp |
| 2 | * 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. |
| 25 | * |
| 26 | * You should have received a copy of the GNU General Public License |
| 27 | * along with this program; if not, write to the Free Software |
| 28 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 29 | */ |
| 30 | |
Ivan Kluchnikov | 835f91e | 2012-04-30 18:00:36 +0400 | [diff] [blame] | 31 | #include <assert.h> |
| 32 | #include <string.h> |
Holger Hans Peter Freyther | cf0265a | 2013-07-28 15:57:20 +0200 | [diff] [blame] | 33 | #define __STDC_FORMAT_MACROS |
| 34 | #include <inttypes.h> |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 35 | #include "csn1.h" |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 36 | #include <gprs_debug.h> |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 37 | |
Pau Espin Pedrol | d636f74 | 2020-02-03 15:37:08 +0100 | [diff] [blame] | 38 | #include <osmocom/core/logging.h> |
| 39 | #include <osmocom/core/utils.h> |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 40 | |
| 41 | #define pvDATA(_pv, _offset) ((void*) ((unsigned char*)_pv + _offset)) |
| 42 | #define pui8DATA(_pv, _offset) ((guint8*) pvDATA(_pv, _offset)) |
| 43 | #define pui16DATA(_pv, _offset) ((guint16*) pvDATA(_pv, _offset)) |
| 44 | #define pui32DATA(_pv, _offset) ((guint32*) pvDATA(_pv, _offset)) |
| 45 | #define pui64DATA(_pv, _offset) ((guint64*) pvDATA(_pv, _offset)) |
| 46 | /* used to tag existence of next element in variable length lists */ |
| 47 | #define STANDARD_TAG 1 |
| 48 | #define REVERSED_TAG 0 |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 49 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 50 | static const unsigned char ixBitsTab[] = {0, 1, 1, 2, 2, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4, 5}; |
| 51 | |
| 52 | |
| 53 | /* Returns no_of_bits (up to 8) masked with 0x2B */ |
| 54 | |
| 55 | static guint8 |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 56 | get_masked_bits8(struct bitvec *vector, unsigned *readIndex, gint bit_offset, const gint no_of_bits) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 57 | { |
| 58 | static const guint8 maskBits[] = {0x00, 0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF}; |
| 59 | //gint byte_offset = bit_offset >> 3; /* divide by 8 */ |
| 60 | gint relative_bit_offset = bit_offset & 0x07; /* modulo 8 */ |
| 61 | guint8 result; |
| 62 | gint bit_shift = 8 - relative_bit_offset - (gint) no_of_bits; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 63 | *readIndex -= relative_bit_offset; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 64 | if (bit_shift >= 0) |
| 65 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 66 | result = (0x2B ^ ((guint8)bitvec_read_field(vector, readIndex, 8))) >> bit_shift; |
| 67 | *readIndex-= bit_shift; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 68 | result &= maskBits[no_of_bits]; |
| 69 | } |
| 70 | else |
Pau Espin Pedrol | f960d5b | 2020-01-23 19:05:00 +0100 | [diff] [blame] | 71 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 72 | guint8 hight_part = (0x2B ^ ((guint8)bitvec_read_field(vector, readIndex, 8))) & maskBits[8 - relative_bit_offset]; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 73 | hight_part = (guint8) (hight_part << (-bit_shift)); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 74 | result = (0x2B ^ ((guint8)bitvec_read_field(vector, readIndex, 8))) >> (8 + bit_shift); |
| 75 | *readIndex = *readIndex - (8 - (-bit_shift)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 76 | result |= hight_part; |
| 77 | } |
| 78 | return result; |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * ================================================================================================ |
| 83 | * set initial/start values in help data structure used for packing/unpacking operation |
| 84 | * ================================================================================================ |
| 85 | */ |
| 86 | void |
| 87 | csnStreamInit(csnStream_t* ar, gint bit_offset, gint remaining_bits_len) |
| 88 | { |
| 89 | ar->remaining_bits_len = remaining_bits_len; |
| 90 | ar->bit_offset = bit_offset; |
Ivan Kluchnikov | 1b51734 | 2013-12-30 14:26:06 +0400 | [diff] [blame] | 91 | ar->direction = 0; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 92 | } |
| 93 | |
Pau Espin Pedrol | d636f74 | 2020-02-03 15:37:08 +0100 | [diff] [blame] | 94 | static const struct value_string csn1_error_names[] = { |
| 95 | { CSN_OK, "General 0" }, |
| 96 | { CSN_ERROR_GENERAL, "General -1" }, |
| 97 | { CSN_ERROR_DATA_NOT_VALID, "DATA_NOT VALID" }, |
| 98 | { CSN_ERROR_IN_SCRIPT, "IN SCRIPT" }, |
| 99 | { CSN_ERROR_INVALID_UNION_INDEX, "INVALID UNION INDEX" }, |
| 100 | { CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, "NEED_MORE BITS TO UNPACK" }, |
Vadim Yanitskiy | fee767f | 2020-02-11 05:28:02 +0700 | [diff] [blame] | 101 | { CSN_ERROR_ILLEGAL_BIT_VALUE, "ILLEGAL BIT VALUE" }, |
| 102 | { CSN_ERROR_INTERNAL, "INTERNAL" }, |
Pau Espin Pedrol | d636f74 | 2020-02-03 15:37:08 +0100 | [diff] [blame] | 103 | { CSN_ERROR_STREAM_NOT_SUPPORTED, "STREAM_NOT_SUPPORTED" }, |
| 104 | { CSN_ERROR_MESSAGE_TOO_LONG, "MESSAGE_TOO_LONG" }, |
| 105 | { 0, NULL } |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 106 | }; |
| 107 | |
Pau Espin Pedrol | d636f74 | 2020-02-03 15:37:08 +0100 | [diff] [blame] | 108 | |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 109 | static gint16 ProcessError_impl(const char *file, int line, unsigned *readIndex, |
Pau Espin Pedrol | d636f74 | 2020-02-03 15:37:08 +0100 | [diff] [blame] | 110 | const char* sz, gint16 err, const CSN_DESCR* pDescr) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 111 | { |
Pau Espin Pedrol | ac2b866 | 2020-02-03 18:58:24 +0100 | [diff] [blame] | 112 | /* Don't add trailing newline, top caller is responsible for appending it */ |
Pau Espin Pedrol | d636f74 | 2020-02-03 15:37:08 +0100 | [diff] [blame] | 113 | if (err != CSN_OK) |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 114 | LOGPSRC(DCSN1, LOGL_ERROR, file, line, "%s: error %s (%d) at %s (idx %u)", |
Pau Espin Pedrol | d636f74 | 2020-02-03 15:37:08 +0100 | [diff] [blame] | 115 | sz, get_value_string(csn1_error_names, err), err, |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 116 | pDescr ? pDescr->sz : "-", *readIndex); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 117 | return err; |
| 118 | } |
| 119 | |
Pau Espin Pedrol | d636f74 | 2020-02-03 15:37:08 +0100 | [diff] [blame] | 120 | #define ProcessError(readIndex, sz, err, pDescr) \ |
| 121 | ProcessError_impl(__FILE__, __LINE__, readIndex, sz, err, pDescr) |
| 122 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 123 | |
| 124 | /** |
| 125 | * ================================================================================================ |
| 126 | * Return TRUE if tag in bit stream indicates existence of next list element, |
| 127 | * otherwise return FALSE. |
| 128 | * Will work for tag values equal to both 0 and 1. |
| 129 | * ================================================================================================ |
| 130 | */ |
| 131 | |
| 132 | static gboolean |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 133 | existNextElement(struct bitvec *vector, unsigned *readIndex, guint8 Tag) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 134 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 135 | int res = bitvec_get_bit_pos(vector, (*readIndex)++); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 136 | if (Tag == STANDARD_TAG) |
| 137 | { |
| 138 | return (res > 0); |
| 139 | } |
| 140 | return (res == 0); |
| 141 | } |
| 142 | |
| 143 | |
| 144 | gint16 |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 145 | csnStreamDecoder(csnStream_t* ar, const CSN_DESCR* pDescr, struct bitvec *vector, unsigned *readIndex, void* data) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 146 | { |
| 147 | gint remaining_bits_len = ar->remaining_bits_len; |
| 148 | gint bit_offset = ar->bit_offset; |
Anders Broman | c0190c8 | 2020-01-24 14:34:14 +0100 | [diff] [blame] | 149 | guint8* pui8 = NULL; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 150 | guint16* pui16; |
| 151 | guint32* pui32; |
| 152 | guint64* pui64; |
| 153 | guint8 Tag = STANDARD_TAG; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 154 | unsigned ib; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 155 | |
| 156 | if (remaining_bits_len <= 0) |
| 157 | { |
| 158 | return 0; |
| 159 | } |
| 160 | |
| 161 | do |
| 162 | { |
| 163 | switch (pDescr->type) |
| 164 | { |
| 165 | case CSN_BIT: |
| 166 | { |
| 167 | if (remaining_bits_len > 0) |
| 168 | { |
| 169 | pui8 = pui8DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 170 | *pui8 = bitvec_read_field(vector, readIndex, 1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 171 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 172 | /* end add the bit value to protocol tree */ |
| 173 | } |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 174 | else if(pDescr->may_be_null) |
| 175 | { |
| 176 | pui8 = pui8DATA(data, pDescr->offset); |
| 177 | *pui8 = 0; |
| 178 | LOGPC(DCSN1, LOGL_NOTICE, "%s = NULL | ", pDescr->sz); |
| 179 | } |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 180 | else |
| 181 | { |
| 182 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 183 | } |
| 184 | |
| 185 | pDescr++; |
| 186 | remaining_bits_len--; |
| 187 | bit_offset++; |
| 188 | break; |
| 189 | } |
| 190 | |
| 191 | case CSN_NULL: |
| 192 | { /* Empty member! */ |
Anders Broman | 72c102a | 2020-01-24 14:31:15 +0100 | [diff] [blame] | 193 | bit_offset += pDescr->i; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 194 | pDescr++; |
| 195 | break; |
| 196 | } |
| 197 | |
| 198 | case CSN_UINT: |
| 199 | { |
| 200 | guint8 no_of_bits = (guint8) pDescr->i; |
| 201 | |
| 202 | if (remaining_bits_len >= no_of_bits) |
| 203 | { |
| 204 | if (no_of_bits <= 8) |
| 205 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 206 | guint8 ui8 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 207 | pui8 = pui8DATA(data, pDescr->offset); |
| 208 | *pui8 = ui8; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 209 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 210 | } |
| 211 | else if (no_of_bits <= 16) |
| 212 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 213 | guint16 ui16 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 214 | pui16 = pui16DATA(data, pDescr->offset); |
| 215 | *pui16 = ui16; |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 216 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui16); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 217 | } |
| 218 | else if (no_of_bits <= 32) |
| 219 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 220 | guint32 ui32 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 221 | pui32 = pui32DATA(data, pDescr->offset); |
| 222 | *pui32 = ui32; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 223 | LOGPC(DCSN1, LOGL_NOTICE, "%s = 0x%08x | ", pDescr->sz , *pui32); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 224 | } |
| 225 | else |
| 226 | { |
| 227 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_GENERAL, pDescr); |
| 228 | } |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 229 | remaining_bits_len -= no_of_bits; |
| 230 | bit_offset += no_of_bits; |
| 231 | } |
| 232 | else if(pDescr->may_be_null) |
| 233 | { |
| 234 | if (no_of_bits <= 8) |
| 235 | { |
| 236 | pui8 = pui8DATA(data, pDescr->offset); |
| 237 | *pui8 = 0; |
| 238 | } |
| 239 | else if (no_of_bits <= 16) |
| 240 | { |
| 241 | pui16 = pui16DATA(data, pDescr->offset); |
| 242 | *pui16 = 0; |
| 243 | } |
| 244 | else if (no_of_bits <= 32) |
| 245 | { |
| 246 | pui32 = pui32DATA(data, pDescr->offset); |
| 247 | *pui32 = 0; |
| 248 | } |
| 249 | LOGPC(DCSN1, LOGL_NOTICE, "%s = NULL | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 250 | } |
| 251 | else |
| 252 | { |
| 253 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 254 | } |
| 255 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 256 | pDescr++; |
| 257 | break; |
| 258 | } |
| 259 | |
| 260 | case CSN_UINT_OFFSET: |
| 261 | { |
| 262 | guint8 no_of_bits = (guint8) pDescr->i; |
| 263 | |
| 264 | if (remaining_bits_len >= no_of_bits) |
| 265 | { |
| 266 | if (no_of_bits <= 8) |
| 267 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 268 | guint8 ui8 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 269 | pui8 = pui8DATA(data, pDescr->offset); |
| 270 | *pui8 = ui8 + (guint8)pDescr->descr.value; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 271 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 272 | } |
| 273 | else if (no_of_bits <= 16) |
| 274 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 275 | guint16 ui16 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 276 | pui16 = pui16DATA(data, pDescr->offset); |
| 277 | *pui16 = ui16 + (guint16)pDescr->descr.value; |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 278 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui16); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 279 | } |
| 280 | else if (no_of_bits <= 32) |
| 281 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 282 | guint32 ui32 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 283 | pui32 = pui32DATA(data, pDescr->offset); |
| 284 | *pui32 = ui32 + (guint16)pDescr->descr.value; |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 285 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui32); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 286 | } |
| 287 | else |
| 288 | { |
| 289 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_GENERAL, pDescr); |
| 290 | } |
| 291 | } |
| 292 | else |
| 293 | { |
| 294 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 295 | } |
| 296 | |
| 297 | remaining_bits_len -= no_of_bits; |
| 298 | bit_offset += no_of_bits; |
| 299 | pDescr++; |
| 300 | break; |
| 301 | } |
| 302 | |
| 303 | case CSN_UINT_LH: |
| 304 | { |
| 305 | guint8 no_of_bits = (guint8) pDescr->i; |
| 306 | |
| 307 | if (remaining_bits_len >= no_of_bits) |
| 308 | { |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 309 | if (no_of_bits <= 8) |
| 310 | { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 311 | guint8 ui8 = get_masked_bits8(vector, readIndex, bit_offset, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 312 | pui8 = pui8DATA(data, pDescr->offset); |
| 313 | *pui8 = ui8; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 314 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 315 | } |
| 316 | else |
| 317 | {/* Maybe we should support more than 8 bits ? */ |
| 318 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_GENERAL, pDescr); |
| 319 | } |
| 320 | } |
| 321 | else |
| 322 | { |
| 323 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 324 | } |
| 325 | |
| 326 | remaining_bits_len -= no_of_bits; |
| 327 | bit_offset += no_of_bits; |
| 328 | pDescr++; |
| 329 | break; |
| 330 | } |
| 331 | |
| 332 | case CSN_UINT_ARRAY: |
| 333 | { |
| 334 | guint8 no_of_bits = (guint8) pDescr->i; |
| 335 | guint16 nCount = (guint16)pDescr->descr.value; /* nCount supplied by value i.e. M_UINT_ARRAY(...) */ |
| 336 | |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 337 | if (pDescr->value != 0) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 338 | { /* nCount specified by a reference to field holding value i.e. M_VAR_UINT_ARRAY(...) */ |
| 339 | nCount = *pui16DATA(data, nCount); |
| 340 | } |
| 341 | |
| 342 | if (remaining_bits_len >= no_of_bits) |
| 343 | { |
| 344 | remaining_bits_len -= (no_of_bits*nCount); |
| 345 | if (no_of_bits <= 8) |
| 346 | { |
| 347 | pui8 = pui8DATA(data, pDescr->offset); |
| 348 | do |
| 349 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 350 | *pui8 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 351 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 352 | pui8++; |
| 353 | bit_offset += no_of_bits; |
| 354 | } while (--nCount > 0); |
| 355 | } |
| 356 | else if (no_of_bits <= 16) |
| 357 | { |
| 358 | return ProcessError(readIndex,"csnStreamDecoder NOTIMPLEMENTED", 999, pDescr); |
| 359 | } |
| 360 | else if (no_of_bits <= 32) |
| 361 | { |
| 362 | return ProcessError(readIndex,"csnStreamDecoder NOTIMPLEMENTED", 999, pDescr); |
| 363 | } |
| 364 | else |
| 365 | { |
| 366 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_GENERAL, pDescr); |
| 367 | } |
| 368 | } |
| 369 | else |
| 370 | { |
| 371 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 372 | } |
| 373 | pDescr++; |
| 374 | break; |
| 375 | } |
| 376 | |
| 377 | case CSN_VARIABLE_TARRAY_OFFSET: |
| 378 | case CSN_VARIABLE_TARRAY: |
| 379 | case CSN_TYPE_ARRAY: |
| 380 | { |
| 381 | gint16 Status; |
| 382 | csnStream_t arT = *ar; |
| 383 | gint16 nCount = pDescr->i; |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 384 | guint16 nSize = (guint16)(gint32)pDescr->value; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 385 | |
| 386 | pui8 = pui8DATA(data, pDescr->offset); |
| 387 | if (pDescr->type == CSN_VARIABLE_TARRAY) |
| 388 | { /* Count specified in field */ |
| 389 | nCount = *pui8DATA(data, pDescr->i); |
| 390 | } |
| 391 | else if (pDescr->type == CSN_VARIABLE_TARRAY_OFFSET) |
| 392 | { /* Count specified in field */ |
| 393 | nCount = *pui8DATA(data, pDescr->i); |
| 394 | /* nCount--; the 1 offset is already taken into account in CSN_UINT_OFFSET */ |
| 395 | } |
| 396 | |
| 397 | while (nCount > 0) |
| 398 | { /* resulting array of length 0 is possible |
| 399 | * but no bits shall be read from bitstream |
| 400 | */ |
| 401 | |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 402 | LOGPC(DCSN1, LOGL_NOTICE, "%s | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 403 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 404 | Status = csnStreamDecoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, readIndex, pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 405 | if (Status >= 0) |
| 406 | { |
| 407 | pui8 += nSize; |
| 408 | remaining_bits_len = arT.remaining_bits_len; |
| 409 | bit_offset = arT.bit_offset; |
| 410 | } |
| 411 | else |
| 412 | { |
| 413 | return Status; |
| 414 | } |
| 415 | nCount--; |
| 416 | } |
| 417 | |
| 418 | pDescr++; |
| 419 | break; |
| 420 | } |
| 421 | |
| 422 | case CSN_BITMAP: |
| 423 | { /* bitmap with given length. The result is left aligned! */ |
| 424 | guint8 no_of_bits = (guint8) pDescr->i; /* length of bitmap */ |
| 425 | |
| 426 | if (no_of_bits > 0) |
| 427 | { |
| 428 | |
| 429 | if (no_of_bits <= 32) |
| 430 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 431 | for(ib = 0; ib < 4; ib++) |
Ivan Kluchnikov | 74b459f | 2012-04-11 22:02:15 +0400 | [diff] [blame] | 432 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 433 | guint8 ui8 = bitvec_read_field(vector, readIndex, 8); |
Ivan Kluchnikov | 74b459f | 2012-04-11 22:02:15 +0400 | [diff] [blame] | 434 | pui8 = pui8DATA(data, pDescr->offset+ib); |
| 435 | *pui8 = ui8; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 436 | LOGPC(DCSN1, LOGL_NOTICE, "%s[%u] = %u | ", pDescr->sz , ib, (unsigned)*pui8); |
Ivan Kluchnikov | 74b459f | 2012-04-11 22:02:15 +0400 | [diff] [blame] | 437 | } |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 438 | } |
| 439 | else if (no_of_bits <= 64) |
| 440 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 441 | for(ib = 0; ib < 8; ib++) |
Ivan Kluchnikov | 74b459f | 2012-04-11 22:02:15 +0400 | [diff] [blame] | 442 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 443 | guint8 ui8 = bitvec_read_field(vector, readIndex, 8); |
Ivan Kluchnikov | 74b459f | 2012-04-11 22:02:15 +0400 | [diff] [blame] | 444 | pui8 = pui8DATA(data, pDescr->offset+ib); |
| 445 | *pui8 = ui8; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 446 | LOGPC(DCSN1, LOGL_NOTICE, "%s[%u] = %u | ", pDescr->sz , ib, (unsigned)*pui8); |
Ivan Kluchnikov | 74b459f | 2012-04-11 22:02:15 +0400 | [diff] [blame] | 447 | } |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 448 | } |
| 449 | else |
| 450 | { |
| 451 | return ProcessError(readIndex,"csnStreamDecoder NOT IMPLEMENTED", 999, pDescr); |
| 452 | } |
| 453 | |
| 454 | remaining_bits_len -= no_of_bits; |
| 455 | assert(remaining_bits_len >= 0); |
| 456 | bit_offset += no_of_bits; |
| 457 | } |
| 458 | /* bitmap was successfully extracted or it was empty */ |
| 459 | |
| 460 | pDescr++; |
| 461 | break; |
| 462 | } |
| 463 | |
| 464 | case CSN_TYPE: |
| 465 | { |
| 466 | gint16 Status; |
| 467 | csnStream_t arT = *ar; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 468 | LOGPC(DCSN1, LOGL_NOTICE, " : %s | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 469 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 470 | Status = csnStreamDecoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, readIndex, pvDATA(data, pDescr->offset)); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 471 | LOGPC(DCSN1, LOGL_NOTICE, ": End %s | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 472 | if (Status >= 0) |
| 473 | { |
| 474 | remaining_bits_len = arT.remaining_bits_len; |
| 475 | bit_offset = arT.bit_offset; |
| 476 | pDescr++; |
| 477 | } |
| 478 | else |
| 479 | { |
| 480 | /* Has already been processed: ProcessError("csnStreamDecoder", Status, pDescr); */ |
| 481 | return Status; |
| 482 | } |
| 483 | |
| 484 | break; |
| 485 | } |
| 486 | |
| 487 | case CSN_CHOICE: |
| 488 | { |
| 489 | gint16 count = pDescr->i; |
| 490 | guint8 i = 0; |
| 491 | CSN_ChoiceElement_t* pChoice = (CSN_ChoiceElement_t*) pDescr->descr.ptr; |
| 492 | |
Vadim Yanitskiy | 5574a58 | 2020-02-11 05:39:06 +0700 | [diff] [blame] | 493 | /* Make sure that the list of choice items is not empty */ |
| 494 | if (!count) |
| 495 | return ProcessError(readIndex, "csnStreamDecoder", CSN_ERROR_IN_SCRIPT, pDescr); |
| 496 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 497 | while (count > 0) |
| 498 | { |
| 499 | guint8 no_of_bits = pChoice->bits; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 500 | guint8 value = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 501 | if (value == pChoice->value) |
| 502 | { |
| 503 | CSN_DESCR descr[2]; |
| 504 | gint16 Status; |
| 505 | csnStream_t arT = *ar; |
| 506 | |
| 507 | descr[0] = pChoice->descr; |
| 508 | memset(&descr[1], 0x00, sizeof(CSN_DESCR)); |
| 509 | descr[1].type = CSN_END; |
| 510 | pui8 = pui8DATA(data, pDescr->offset); |
| 511 | *pui8 = i; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 512 | LOGPC(DCSN1, LOGL_NOTICE, "Choice %s = %u | ", pDescr->sz , (unsigned)value); |
Pau Espin Pedrol | 7cce825 | 2020-01-24 16:41:14 +0100 | [diff] [blame] | 513 | if (!pChoice->keep_bits) { |
| 514 | bit_offset += no_of_bits; |
| 515 | remaining_bits_len -= no_of_bits; |
| 516 | } |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 517 | |
| 518 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 519 | Status = csnStreamDecoder(&arT, descr, vector, readIndex, data); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 520 | |
| 521 | if (Status >= 0) |
| 522 | { |
| 523 | remaining_bits_len = arT.remaining_bits_len; |
| 524 | bit_offset = arT.bit_offset; |
| 525 | } |
| 526 | else |
| 527 | { |
| 528 | return Status; |
| 529 | } |
| 530 | break; |
| 531 | } |
| 532 | |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 533 | *readIndex -= no_of_bits; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 534 | count--; |
| 535 | pChoice++; |
| 536 | i++; |
| 537 | } |
| 538 | |
Vadim Yanitskiy | 5574a58 | 2020-02-11 05:39:06 +0700 | [diff] [blame] | 539 | /* Neither of the choice items matched => unknown value */ |
| 540 | if (!count) |
| 541 | return ProcessError(readIndex, "csnStreamDecoder", CSN_ERROR_STREAM_NOT_SUPPORTED, pDescr); |
| 542 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 543 | pDescr++; |
| 544 | break; |
| 545 | } |
| 546 | |
| 547 | case CSN_SERIALIZE: |
| 548 | { |
Pau Espin Pedrol | 5b71697 | 2020-01-24 17:24:01 +0100 | [diff] [blame] | 549 | StreamSerializeFcn_t serialize = (StreamSerializeFcn_t)pDescr->aux_fn; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 550 | csnStream_t arT = *ar; |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 551 | guint8 length_len = pDescr->i; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 552 | gint16 Status = -1; |
| 553 | |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 554 | guint8 length = bitvec_read_field(vector, readIndex, length_len); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 555 | |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 556 | LOGPC(DCSN1, LOGL_NOTICE, "%s length = %u | ", pDescr->sz , length); |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 557 | bit_offset += length_len; |
| 558 | remaining_bits_len -= length_len; |
| 559 | |
Pau Espin Pedrol | 98e4c53 | 2020-01-24 16:26:37 +0100 | [diff] [blame] | 560 | csnStreamInit(&arT, bit_offset, length > 0 ? length : remaining_bits_len); |
Daniel Willmann | 6f0796a | 2014-01-15 09:57:07 +0100 | [diff] [blame] | 561 | arT.direction = 1; |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 562 | LOGPC(DCSN1, LOGL_NOTICE, "offset = %u | ", pDescr->offset); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 563 | Status = serialize(&arT, vector, readIndex, pvDATA(data, pDescr->offset)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 564 | |
| 565 | if (Status >= 0) |
| 566 | { |
Pau Espin Pedrol | 98e4c53 | 2020-01-24 16:26:37 +0100 | [diff] [blame] | 567 | if (length > 0) { |
| 568 | remaining_bits_len -= length; |
| 569 | bit_offset += length; |
| 570 | } else { |
| 571 | remaining_bits_len = arT.remaining_bits_len; |
| 572 | bit_offset = arT.bit_offset; |
| 573 | } |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 574 | pDescr++; |
| 575 | } |
| 576 | else |
| 577 | { |
| 578 | /* Has already been processed: */ |
| 579 | return Status; |
| 580 | } |
| 581 | |
| 582 | break; |
| 583 | } |
| 584 | |
| 585 | case CSN_UNION_LH: |
| 586 | case CSN_UNION: |
| 587 | { |
| 588 | gint16 Bits; |
| 589 | guint8 index; |
| 590 | gint16 count = pDescr->i; |
| 591 | const CSN_DESCR* pDescrNext = pDescr; |
| 592 | |
| 593 | pDescrNext += count + 1; /* now this is next after the union */ |
| 594 | if ((count <= 0) || (count > 16)) |
| 595 | { |
| 596 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_INVALID_UNION_INDEX, pDescr); |
| 597 | } |
| 598 | |
| 599 | /* Now get the bits to extract the index */ |
| 600 | Bits = ixBitsTab[count]; |
| 601 | index = 0; |
| 602 | |
| 603 | while (Bits > 0) |
| 604 | { |
| 605 | index <<= 1; |
| 606 | |
| 607 | if (CSN_UNION_LH == pDescr->type) |
| 608 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 609 | index |= get_masked_bits8(vector, readIndex, bit_offset, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 610 | } |
| 611 | else |
| 612 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 613 | index |= bitvec_read_field(vector, readIndex, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 614 | } |
| 615 | remaining_bits_len--; |
| 616 | bit_offset++; |
| 617 | Bits--; |
| 618 | } |
| 619 | |
| 620 | /* Assign UnionType */ |
| 621 | pui8 = pui8DATA(data, pDescr->offset); |
| 622 | *pui8 = index; |
Pau Espin Pedrol | f960d5b | 2020-01-23 19:05:00 +0100 | [diff] [blame] | 623 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 624 | |
| 625 | /* script index to continue on, limited in case we do not have a power of 2 */ |
| 626 | pDescr += (MIN(index + 1, count)); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 627 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 628 | |
| 629 | switch (pDescr->type) |
| 630 | { /* get the right element of the union based on computed index */ |
| 631 | |
| 632 | case CSN_BIT: |
| 633 | { |
| 634 | pui8 = pui8DATA(data, pDescr->offset); |
| 635 | *pui8 = 0x00; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 636 | if (bitvec_read_field(vector, readIndex, 1) > 0) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 637 | { |
| 638 | *pui8 = 0x01; |
| 639 | } |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 640 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 641 | remaining_bits_len -= 1; |
| 642 | bit_offset++; |
| 643 | pDescr++; |
| 644 | break; |
| 645 | } |
| 646 | |
| 647 | case CSN_NULL: |
| 648 | { /* Empty member! */ |
Anders Broman | 72c102a | 2020-01-24 14:31:15 +0100 | [diff] [blame] | 649 | bit_offset += pDescr->i; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 650 | pDescr++; |
| 651 | break; |
| 652 | } |
| 653 | |
| 654 | case CSN_UINT: |
| 655 | { |
| 656 | guint8 no_of_bits = (guint8) pDescr->i; |
| 657 | if (remaining_bits_len >= no_of_bits) |
| 658 | { |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 659 | if (no_of_bits <= 8) |
| 660 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 661 | guint8 ui8 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 662 | pui8 = pui8DATA(data, pDescr->offset); |
| 663 | *pui8 = ui8; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 664 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 665 | } |
| 666 | else if (no_of_bits <= 16) |
| 667 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 668 | guint16 ui16 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 669 | pui16 = pui16DATA(data, pDescr->offset); |
| 670 | *pui16 = ui16; |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 671 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui16); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 672 | } |
| 673 | else if (no_of_bits <= 32) |
| 674 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 675 | guint32 ui32 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 676 | pui32 = pui32DATA(data, pDescr->offset); |
| 677 | *pui32 = ui32; |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 678 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui32); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 679 | } |
| 680 | else |
| 681 | { |
| 682 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_GENERAL, pDescr); |
| 683 | } |
| 684 | } |
| 685 | else |
| 686 | { |
| 687 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_GENERAL, pDescr); |
| 688 | } |
| 689 | |
Vadim Yanitskiy | e87066d | 2020-02-17 18:02:32 +0700 | [diff] [blame] | 690 | remaining_bits_len -= no_of_bits; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 691 | bit_offset += no_of_bits; |
| 692 | pDescr++; |
| 693 | break; |
| 694 | } |
| 695 | |
| 696 | case CSN_UINT_OFFSET: |
| 697 | { |
| 698 | guint8 no_of_bits = (guint8) pDescr->i; |
| 699 | |
| 700 | if (remaining_bits_len >= no_of_bits) |
| 701 | { |
| 702 | if (no_of_bits <= 8) |
| 703 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 704 | guint8 ui8 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 705 | pui8 = pui8DATA(data, pDescr->offset); |
| 706 | *pui8 = ui8 + (guint8)pDescr->descr.value; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 707 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 708 | } |
| 709 | else if (no_of_bits <= 16) |
| 710 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 711 | guint16 ui16 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 712 | pui16 = pui16DATA(data, pDescr->offset); |
| 713 | *pui16 = ui16 + (guint16)pDescr->descr.value; |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 714 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui16); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 715 | } |
| 716 | else if (no_of_bits <= 32) |
| 717 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 718 | guint32 ui32 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 719 | pui32 = pui32DATA(data, pDescr->offset); |
| 720 | *pui32 = ui32 + (guint16)pDescr->descr.value; |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 721 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui32); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 722 | } |
| 723 | else |
| 724 | { |
| 725 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_GENERAL, pDescr); |
| 726 | } |
| 727 | } |
| 728 | else |
| 729 | { |
| 730 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 731 | } |
| 732 | |
Vadim Yanitskiy | e87066d | 2020-02-17 18:02:32 +0700 | [diff] [blame] | 733 | remaining_bits_len -= no_of_bits; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 734 | bit_offset += no_of_bits; |
| 735 | pDescr++; |
| 736 | break; |
| 737 | } |
| 738 | |
| 739 | case CSN_UINT_LH: |
| 740 | { |
| 741 | guint8 no_of_bits = (guint8) pDescr->i; |
| 742 | |
| 743 | if (remaining_bits_len >= no_of_bits) |
| 744 | { |
| 745 | if (no_of_bits <= 8) |
| 746 | { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 747 | guint8 ui8 = get_masked_bits8(vector, readIndex, bit_offset, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 748 | pui8 = pui8DATA(data, pDescr->offset); |
| 749 | *pui8 = ui8; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 750 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 751 | } |
| 752 | else |
| 753 | { /* Maybe we should support more than 8 bits ? */ |
| 754 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_GENERAL, pDescr); |
| 755 | } |
| 756 | } |
| 757 | else |
| 758 | { |
| 759 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 760 | } |
| 761 | |
Vadim Yanitskiy | e87066d | 2020-02-17 18:02:32 +0700 | [diff] [blame] | 762 | remaining_bits_len -= no_of_bits; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 763 | bit_offset += no_of_bits; |
| 764 | pDescr++; |
| 765 | break; |
| 766 | } |
| 767 | |
| 768 | case CSN_UINT_ARRAY: |
| 769 | { |
| 770 | guint8 no_of_bits = (guint8) pDescr->i; |
| 771 | guint16 nCount = (guint16)pDescr->descr.value; /* nCount supplied by value i.e. M_UINT_ARRAY(...) */ |
| 772 | |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 773 | if (pDescr->value != 0) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 774 | { /* nCount specified by a reference to field holding value i.e. M_VAR_UINT_ARRAY(...) */ |
| 775 | nCount = *pui16DATA(data, nCount); |
| 776 | } |
| 777 | |
| 778 | if (remaining_bits_len >= no_of_bits) |
| 779 | { |
| 780 | remaining_bits_len -= (no_of_bits * nCount); |
| 781 | if (no_of_bits <= 8) |
| 782 | { |
| 783 | pui8 = pui8DATA(data, pDescr->offset); |
| 784 | |
| 785 | while (nCount > 0) |
| 786 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 787 | *pui8 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 788 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 789 | pui8++; |
| 790 | bit_offset += no_of_bits; |
| 791 | nCount--; |
| 792 | } |
| 793 | } |
| 794 | else if (no_of_bits <= 16) |
| 795 | { |
| 796 | pui16 = pui16DATA(data, pDescr->offset); |
| 797 | |
| 798 | while (nCount > 0) |
| 799 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 800 | *pui16 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 801 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui16); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 802 | pui16++; |
| 803 | bit_offset += no_of_bits; |
| 804 | nCount--; |
| 805 | } |
| 806 | } |
| 807 | else if (no_of_bits <= 32) |
| 808 | { /* not supported */ |
| 809 | return ProcessError(readIndex,"csnStreamDecoder NOT IMPLEMENTED", 999, pDescr); |
| 810 | } |
| 811 | else |
| 812 | { |
| 813 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_GENERAL, pDescr); |
| 814 | } |
| 815 | } |
| 816 | else |
| 817 | { |
| 818 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 819 | } |
| 820 | |
| 821 | pDescr++; |
| 822 | break; |
| 823 | } |
| 824 | |
| 825 | case CSN_VARIABLE_TARRAY_OFFSET: |
| 826 | case CSN_VARIABLE_TARRAY: |
| 827 | case CSN_TYPE_ARRAY: |
| 828 | { |
| 829 | gint16 Status; |
| 830 | csnStream_t arT = *ar; |
| 831 | guint16 nCount = (guint16) pDescr->i; |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 832 | guint16 nSize = (guint16)(guint32)pDescr->value; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 833 | |
| 834 | pui8 = pui8DATA(data, pDescr->offset); |
| 835 | |
| 836 | if (CSN_VARIABLE_TARRAY == pDescr->type) |
| 837 | { /* Count specified in field */ |
| 838 | nCount = *pui8DATA(data, pDescr->i); |
| 839 | } |
| 840 | else if (CSN_VARIABLE_TARRAY_OFFSET == pDescr->type) |
| 841 | { /* Count specified in field */ |
| 842 | nCount = *pui8DATA(data, pDescr->i); |
| 843 | nCount--; /* Offset 1 */ |
| 844 | } |
| 845 | |
| 846 | while (nCount--) /* Changed to handle length = 0. */ |
| 847 | { |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 848 | LOGPC(DCSN1, LOGL_NOTICE, "%s | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 849 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 850 | Status = csnStreamDecoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, readIndex, pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 851 | if (Status >= 0) |
| 852 | { |
| 853 | pui8 += nSize; |
| 854 | remaining_bits_len = arT.remaining_bits_len; |
| 855 | bit_offset = arT.bit_offset; |
| 856 | } |
| 857 | else |
| 858 | { |
| 859 | return Status; |
| 860 | } |
| 861 | } |
| 862 | |
| 863 | pDescr++; |
| 864 | break; |
| 865 | } |
| 866 | |
| 867 | case CSN_BITMAP: |
| 868 | { /* bitmap with given length. The result is left aligned! */ |
| 869 | guint8 no_of_bits = (guint8) pDescr->i; /* length of bitmap */ |
| 870 | |
| 871 | if (no_of_bits > 0) |
| 872 | { |
| 873 | |
| 874 | if (no_of_bits <= 32) |
| 875 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 876 | guint32 ui32 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 877 | pui32 = pui32DATA(data, pDescr->offset); |
| 878 | *pui32 = ui32; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 879 | } |
| 880 | else if (no_of_bits <= 64) |
| 881 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 882 | guint64 ui64 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 883 | pui64 = pui64DATA(data, pDescr->offset); |
| 884 | *pui64 = ui64; |
Holger Hans Peter Freyther | cf0265a | 2013-07-28 15:57:20 +0200 | [diff] [blame] | 885 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %lu | ", pDescr->sz , *pui64); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 886 | } |
| 887 | else |
| 888 | { |
| 889 | return ProcessError(readIndex,"csnStreamDecoder NOT IMPLEMENTED", 999, pDescr); |
| 890 | } |
| 891 | |
| 892 | remaining_bits_len -= no_of_bits; |
| 893 | assert(remaining_bits_len >= 0); |
| 894 | bit_offset += no_of_bits; |
| 895 | } |
| 896 | /* bitmap was successfully extracted or it was empty */ |
| 897 | |
| 898 | pDescr++; |
| 899 | break; |
| 900 | } |
| 901 | |
| 902 | case CSN_TYPE: |
| 903 | { |
| 904 | gint16 Status; |
| 905 | csnStream_t arT = *ar; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 906 | LOGPC(DCSN1, LOGL_NOTICE, " : %s | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 907 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 908 | Status = csnStreamDecoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, readIndex, pvDATA(data, pDescr->offset)); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 909 | LOGPC(DCSN1, LOGL_NOTICE, " : End %s | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 910 | if (Status >= 0) |
| 911 | { |
| 912 | remaining_bits_len = arT.remaining_bits_len; |
| 913 | bit_offset = arT.bit_offset; |
| 914 | pDescr++; |
| 915 | } |
| 916 | else |
| 917 | { /* return error code Has already been processed: */ |
| 918 | return Status; |
| 919 | } |
| 920 | |
| 921 | break; |
| 922 | } |
| 923 | |
| 924 | default: |
| 925 | { /* descriptions of union elements other than above are illegal */ |
| 926 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_IN_SCRIPT, pDescr); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | pDescr = pDescrNext; |
| 931 | break; |
| 932 | } |
| 933 | |
| 934 | case CSN_EXIST: |
| 935 | case CSN_EXIST_LH: |
| 936 | { |
| 937 | guint8 fExist; |
| 938 | |
| 939 | pui8 = pui8DATA(data, pDescr->offset); |
| 940 | |
| 941 | if (CSN_EXIST_LH == pDescr->type) |
| 942 | { |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 943 | fExist = get_masked_bits8(vector, readIndex, bit_offset, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 944 | } |
| 945 | else |
| 946 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 947 | fExist = bitvec_read_field(vector, readIndex, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 948 | } |
| 949 | |
| 950 | *pui8 = fExist; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 951 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 952 | pDescr++; |
Vadim Yanitskiy | d8e5e8b | 2020-02-17 18:16:20 +0700 | [diff] [blame] | 953 | bit_offset++; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 954 | remaining_bits_len -= 1; |
| 955 | |
| 956 | if (!fExist) |
| 957 | { |
| 958 | ar->remaining_bits_len = remaining_bits_len; |
| 959 | ar->bit_offset = bit_offset; |
| 960 | return remaining_bits_len; |
| 961 | } |
| 962 | |
| 963 | break; |
| 964 | } |
| 965 | |
| 966 | case CSN_NEXT_EXIST: |
| 967 | { |
| 968 | guint8 fExist; |
| 969 | |
| 970 | pui8 = pui8DATA(data, pDescr->offset); |
| 971 | |
| 972 | /* this if-statement represents the M_NEXT_EXIST_OR_NULL description element */ |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 973 | if ((pDescr->may_be_null) && (remaining_bits_len == 0)) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 974 | { /* no more bits to decode is fine here - end of message detected and allowed */ |
| 975 | |
| 976 | /* Skip i entries + this entry */ |
| 977 | pDescr += pDescr->i + 1; |
| 978 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 979 | /* Set the data member to "not exist" */ |
| 980 | *pui8 = 0; |
| 981 | break; |
| 982 | } |
| 983 | |
| 984 | /* the "regular" M_NEXT_EXIST description element */ |
| 985 | |
| 986 | fExist = 0x00; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 987 | if (bitvec_read_field(vector, readIndex, 1)) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 988 | { |
| 989 | fExist = 0x01; |
| 990 | } |
| 991 | |
| 992 | *pui8 = fExist; |
| 993 | remaining_bits_len -= 1; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 994 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 995 | ++bit_offset; |
| 996 | |
| 997 | if (fExist == 0) |
| 998 | { /* Skip 'i' entries */ |
| 999 | pDescr += pDescr->i; |
| 1000 | } |
| 1001 | |
| 1002 | pDescr++; |
| 1003 | break; |
| 1004 | } |
| 1005 | |
| 1006 | case CSN_NEXT_EXIST_LH: |
| 1007 | { |
| 1008 | guint8 fExist; |
| 1009 | pui8 = pui8DATA(data, pDescr->offset); |
| 1010 | |
| 1011 | /* this if-statement represents the M_NEXT_EXIST_OR_NULL_LH description element */ |
| 1012 | if ((pDescr->descr.ptr != NULL) && (remaining_bits_len == 0)) |
| 1013 | { /* no more bits to decode is fine here - end of message detected and allowed */ |
| 1014 | |
| 1015 | /* skip 'i' entries + this entry */ |
| 1016 | pDescr += pDescr->i + 1; |
| 1017 | |
| 1018 | /* pDescr now must be pointing to a CSN_END entry, if not this is an error */ |
| 1019 | if ( pDescr->type != CSN_END ) |
| 1020 | { /* substract one more bit from remaining_bits_len to make the "not enough bits" error to be triggered */ |
| 1021 | remaining_bits_len--; |
| 1022 | } |
| 1023 | |
| 1024 | /* set the data member to "not exist" */ |
| 1025 | *pui8 = 0; |
| 1026 | break; |
| 1027 | } |
| 1028 | |
| 1029 | /* the "regular" M_NEXT_EXIST_LH description element */ |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1030 | fExist = get_masked_bits8(vector, readIndex, bit_offset, 1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1031 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)fExist); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1032 | *pui8++ = fExist; |
| 1033 | remaining_bits_len -= 1; |
| 1034 | |
| 1035 | bit_offset++; |
| 1036 | |
| 1037 | if (fExist == 0) |
| 1038 | { /* Skip 'i' entries */ |
| 1039 | pDescr += pDescr->i; |
| 1040 | } |
| 1041 | pDescr++; |
| 1042 | |
| 1043 | break; |
| 1044 | } |
| 1045 | |
| 1046 | case CSN_VARIABLE_BITMAP_1: |
| 1047 | { /* Bitmap from here and to the end of message */ |
| 1048 | |
| 1049 | *pui8DATA(data, (gint16)pDescr->descr.value) = (guint8) remaining_bits_len; /* length of bitmap == remaining bits */ |
| 1050 | |
| 1051 | /*no break - |
| 1052 | * with a length set we have a regular variable length bitmap so we continue */ |
| 1053 | } |
Alexis La Goutte | e36fb5b | 2020-01-24 18:22:35 +0100 | [diff] [blame] | 1054 | /* FALL THROUGH */ |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1055 | case CSN_VARIABLE_BITMAP: |
| 1056 | { /* {CSN_VARIABLE_BITMAP, 0, offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER} |
| 1057 | * <N: bit (5)> <bitmap: bit(N + offset)> |
| 1058 | * Bit array with length (in bits) specified in parameter (pDescr->descr) |
| 1059 | * The result is right aligned! |
| 1060 | */ |
| 1061 | gint16 no_of_bits = *pui8DATA(data, (gint16)pDescr->descr.value); |
| 1062 | |
| 1063 | no_of_bits += pDescr->i; /* adjusted by offset */ |
| 1064 | |
| 1065 | if (no_of_bits > 0) |
| 1066 | { |
| 1067 | remaining_bits_len -= no_of_bits; |
| 1068 | |
| 1069 | if (remaining_bits_len < 0) |
| 1070 | { |
| 1071 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1072 | } |
| 1073 | |
| 1074 | { /* extract bits */ |
| 1075 | guint8* pui8 = pui8DATA(data, pDescr->offset); |
| 1076 | gint16 nB1 = no_of_bits & 0x07;/* no_of_bits Mod 8 */ |
| 1077 | |
| 1078 | if (nB1 > 0) |
| 1079 | { /* take care of the first byte - it will be right aligned */ |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1080 | *pui8 = bitvec_read_field(vector, readIndex, nB1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1081 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1082 | pui8++; |
| 1083 | no_of_bits -= nB1; |
| 1084 | bit_offset += nB1; /* (nB1 is no_of_bits Mod 8) */ |
| 1085 | } |
| 1086 | |
| 1087 | /* remaining no_of_bits is a multiple of 8 or 0 */ |
| 1088 | while (no_of_bits > 0) |
| 1089 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1090 | *pui8 = bitvec_read_field(vector, readIndex, 8); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1091 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1092 | pui8++; |
| 1093 | no_of_bits -= 8; |
| 1094 | } |
| 1095 | } |
| 1096 | } |
| 1097 | pDescr++; |
| 1098 | break; |
| 1099 | } |
| 1100 | |
| 1101 | case CSN_LEFT_ALIGNED_VAR_BMP_1: |
| 1102 | { /* Bitmap from here and to the end of message */ |
| 1103 | |
| 1104 | *pui8DATA(data, (gint16)pDescr->descr.value) = (guint8) remaining_bits_len; /* length of bitmap == remaining bits */ |
| 1105 | |
| 1106 | /* no break - |
| 1107 | * with a length set we have a regular left aligned variable length bitmap so we continue |
| 1108 | */ |
| 1109 | } |
Alexis La Goutte | e36fb5b | 2020-01-24 18:22:35 +0100 | [diff] [blame] | 1110 | /* FALL THROUGH */ |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1111 | case CSN_LEFT_ALIGNED_VAR_BMP: |
| 1112 | { /* {CSN_LEFT_ALIGNED_VAR_BMP, _OFFSET, (void*)offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER} |
| 1113 | * <N: bit (5)> <bitmap: bit(N + offset)> |
| 1114 | * bit array with length (in bits) specified in parameter (pDescr->descr) |
| 1115 | */ |
| 1116 | gint16 no_of_bits = *pui8DATA(data, (gint16)pDescr->descr.value);/* Size of bitmap */ |
| 1117 | |
| 1118 | no_of_bits += pDescr->i;/* size adjusted by offset */ |
| 1119 | |
| 1120 | if (no_of_bits > 0) |
| 1121 | { |
| 1122 | remaining_bits_len -= no_of_bits; |
| 1123 | |
| 1124 | if (remaining_bits_len < 0) |
| 1125 | { |
| 1126 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1127 | } |
| 1128 | |
| 1129 | { /* extract bits */ |
| 1130 | guint8* pui8 = pui8DATA(data, pDescr->offset); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1131 | |
Neels Hofmeyr | 0241526 | 2016-09-02 02:15:26 +0200 | [diff] [blame] | 1132 | while (no_of_bits >= 8) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1133 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1134 | *pui8 = bitvec_read_field(vector, readIndex, 8); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1135 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1136 | pui8++; |
| 1137 | no_of_bits -= 8; |
| 1138 | } |
Neels Hofmeyr | 0241526 | 2016-09-02 02:15:26 +0200 | [diff] [blame] | 1139 | if (no_of_bits > 0) |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1140 | { |
| 1141 | *pui8 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1142 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1143 | pui8++; |
Neels Hofmeyr | 0241526 | 2016-09-02 02:15:26 +0200 | [diff] [blame] | 1144 | bit_offset += no_of_bits; |
| 1145 | no_of_bits = 0; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | |
| 1150 | /* bitmap was successfully extracted or it was empty */ |
| 1151 | pDescr++; |
| 1152 | break; |
| 1153 | } |
| 1154 | |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 1155 | case CSN_PADDING_BITS: |
| 1156 | { /* Padding from here and to the end of message */ |
| 1157 | LOGPC(DCSN1, LOGL_NOTICE, "%s = ", pDescr->sz); |
| 1158 | if (remaining_bits_len > 0) |
| 1159 | { |
| 1160 | while (remaining_bits_len > 0) |
| 1161 | { |
Pascal Quantin | c515551 | 2020-01-24 17:33:06 +0100 | [diff] [blame] | 1162 | guint bits_to_handle = remaining_bits_len%8; |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 1163 | if (bits_to_handle > 0) |
| 1164 | { |
Vadim Yanitskiy | 28b4d27 | 2020-02-05 06:06:35 +0700 | [diff] [blame] | 1165 | LOGPC(DCSN1, LOGL_NOTICE, "%d|", bitvec_get_uint(vector, bits_to_handle)); |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 1166 | remaining_bits_len -= bits_to_handle; |
| 1167 | bit_offset += bits_to_handle; |
| 1168 | } |
| 1169 | else if (bits_to_handle == 0) |
| 1170 | { |
Vadim Yanitskiy | 28b4d27 | 2020-02-05 06:06:35 +0700 | [diff] [blame] | 1171 | LOGPC(DCSN1, LOGL_NOTICE, "%d|", bitvec_get_uint(vector, 8)); |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 1172 | remaining_bits_len -= 8; |
| 1173 | bit_offset += 8; |
| 1174 | } |
| 1175 | } |
| 1176 | } |
| 1177 | if (remaining_bits_len < 0) |
| 1178 | { |
| 1179 | return ProcessError(readIndex,"csnStreamDissector", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1180 | } |
| 1181 | |
| 1182 | /* Padding was successfully extracted or it was empty */ |
| 1183 | pDescr++; |
| 1184 | break; |
| 1185 | } |
| 1186 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1187 | case CSN_VARIABLE_ARRAY: |
| 1188 | { /* {int type; int i; void* descr; int offset; const char* sz; } CSN_DESCR; |
| 1189 | * {CSN_VARIABLE_ARRAY, _OFFSET, (void*)offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER} |
| 1190 | * Array with length specified in parameter: |
| 1191 | * <count: bit (x)> |
| 1192 | * <list: octet(count + offset)> |
| 1193 | */ |
| 1194 | gint16 count = *pui8DATA(data, (gint16)pDescr->descr.value); |
| 1195 | |
| 1196 | count += pDescr->i; /* Adjusted by offset */ |
| 1197 | |
| 1198 | if (count > 0) |
| 1199 | { |
| 1200 | remaining_bits_len -= count * 8; |
| 1201 | |
| 1202 | if (remaining_bits_len < 0) |
| 1203 | { |
| 1204 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1205 | } |
| 1206 | |
| 1207 | pui8 = pui8DATA(data, pDescr->offset); |
| 1208 | |
| 1209 | while (count > 0) |
| 1210 | { |
| 1211 | readIndex -= 8; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1212 | *pui8 = bitvec_read_field(vector, readIndex, 8); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1213 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1214 | pui8++; |
| 1215 | bit_offset += 8; |
| 1216 | count--; |
| 1217 | } |
| 1218 | } |
| 1219 | |
| 1220 | pDescr++; |
| 1221 | break; |
| 1222 | } |
| 1223 | |
| 1224 | case CSN_RECURSIVE_ARRAY: |
| 1225 | { /* Recursive way to specify an array: <list> ::= {1 <number: bit (4)> <list> | 0} |
| 1226 | * or more generally: <list> ::= { <tag> <element> <list> | <EndTag> } |
| 1227 | * where <element> ::= bit(value) |
| 1228 | * <tag> ::= 0 | 1 |
| 1229 | * <EndTag> ::= reversed tag i.e. tag == 1 -> EndTag == 0 and vice versa |
| 1230 | * {CSN_RECURSIVE_ARRAY, _BITS, (void*)offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER} |
| 1231 | * REMARK: recursive way to specify an array but an iterative implementation! |
| 1232 | */ |
| 1233 | gint16 no_of_bits = pDescr->i; |
| 1234 | guint8 ElementCount = 0; |
| 1235 | |
| 1236 | pui8 = pui8DATA(data, pDescr->offset); |
| 1237 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 1238 | while (existNextElement(vector, readIndex, Tag)) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1239 | { /* tag control shows existence of next list elements */ |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1240 | LOGPC(DCSN1, LOGL_NOTICE, "%s = Exist | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1241 | bit_offset++; |
| 1242 | remaining_bits_len--; |
| 1243 | |
| 1244 | /* extract and store no_of_bits long element from bitstream */ |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1245 | *pui8 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1246 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1247 | pui8++; |
| 1248 | remaining_bits_len -= no_of_bits; |
| 1249 | ElementCount++; |
| 1250 | |
| 1251 | if (remaining_bits_len < 0) |
| 1252 | { |
| 1253 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1254 | } |
| 1255 | |
| 1256 | bit_offset += no_of_bits; |
| 1257 | } |
| 1258 | |
Vadim Yanitskiy | 28b4d27 | 2020-02-05 06:06:35 +0700 | [diff] [blame] | 1259 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %d | ", pDescr->sz , bitvec_get_uint(vector, 1)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1260 | /* existNextElement() returned FALSE, 1 bit consumed */ |
| 1261 | bit_offset++; |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 1262 | remaining_bits_len--; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1263 | |
| 1264 | /* Store the counted number of elements of the array */ |
| 1265 | *pui8DATA(data, (gint16)pDescr->descr.value) = ElementCount; |
| 1266 | |
| 1267 | pDescr++; |
| 1268 | break; |
| 1269 | } |
| 1270 | |
| 1271 | case CSN_RECURSIVE_TARRAY: |
| 1272 | { /* Recursive way to specify an array of type: <lists> ::= { 1 <type> } ** 0 ; |
| 1273 | * M_REC_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField) |
| 1274 | * {t, offsetof(_STRUCT, _ElementCountField), (void*)CSNDESCR_##_MEMBER_TYPE, offsetof(_STRUCT, _MEMBER), #_MEMBER, (StreamSerializeFcn_t)sizeof(_MEMBER_TYPE)} |
| 1275 | */ |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 1276 | gint16 nSizeElement = (gint16)(gint32)pDescr->value; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1277 | guint8 ElementCount = 0; |
| 1278 | pui8 = pui8DATA(data, pDescr->offset); |
| 1279 | |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 1280 | while (existNextElement(vector, readIndex, Tag)) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1281 | { /* tag control shows existence of next list elements */ |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1282 | LOGPC(DCSN1, LOGL_NOTICE, "%s = Exist | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1283 | /* existNextElement() returned TRUE, 1 bit consumed */ |
| 1284 | bit_offset++; |
| 1285 | remaining_bits_len--; |
| 1286 | ElementCount++; |
| 1287 | |
| 1288 | { /* unpack the following data structure */ |
| 1289 | csnStream_t arT = *ar; |
| 1290 | gint16 Status; |
| 1291 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 1292 | Status = csnStreamDecoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, readIndex, pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1293 | |
| 1294 | if (Status >= 0) |
| 1295 | { /* successful completion */ |
| 1296 | pui8 += nSizeElement; /* -> to next data element */ |
| 1297 | remaining_bits_len = arT.remaining_bits_len; |
| 1298 | bit_offset = arT.bit_offset; |
| 1299 | } |
| 1300 | else |
| 1301 | { /* something went awry */ |
| 1302 | return Status; |
| 1303 | } |
| 1304 | } |
| 1305 | |
| 1306 | if (remaining_bits_len < 0) |
| 1307 | { |
| 1308 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1309 | } |
| 1310 | } |
| 1311 | |
Vadim Yanitskiy | 4b57b6d | 2020-02-17 19:39:52 +0700 | [diff] [blame] | 1312 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %d | ", pDescr->sz , bitvec_get_uint(vector, 1)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1313 | |
| 1314 | /* existNextElement() returned FALSE, 1 bit consumed */ |
Vadim Yanitskiy | e87066d | 2020-02-17 18:02:32 +0700 | [diff] [blame] | 1315 | remaining_bits_len--; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1316 | bit_offset++; |
| 1317 | |
| 1318 | /* Store the counted number of elements of the array */ |
| 1319 | *pui8DATA(data, (gint16)(gint32)pDescr->i) = ElementCount; |
| 1320 | |
| 1321 | pDescr++; |
| 1322 | break; |
| 1323 | } |
| 1324 | |
| 1325 | case CSN_RECURSIVE_TARRAY_2: |
| 1326 | { /* Recursive way to specify an array of type: <list> ::= <type> { 0 <type> } ** 1 ; */ |
| 1327 | |
| 1328 | Tag = REVERSED_TAG; |
| 1329 | |
| 1330 | /* NO break - |
| 1331 | * handling is exactly the same as for CSN_RECURSIVE_TARRAY_1 so we continue |
| 1332 | */ |
| 1333 | } |
Alexis La Goutte | e36fb5b | 2020-01-24 18:22:35 +0100 | [diff] [blame] | 1334 | /* FALL THROUGH */ |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1335 | case CSN_RECURSIVE_TARRAY_1: |
| 1336 | { /* Recursive way to specify an array of type: <lists> ::= <type> { 1 <type> } ** 0 ; |
| 1337 | * M_REC_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField) |
| 1338 | * {t, offsetof(_STRUCT, _ElementCountField), (void*)CSNDESCR_##_MEMBER_TYPE, offsetof(_STRUCT, _MEMBER), #_MEMBER, (StreamSerializeFcn_t)sizeof(_MEMBER_TYPE)} |
| 1339 | */ |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 1340 | gint16 nSizeElement = (gint16)(gint32)pDescr->value; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1341 | guint8 ElementCount = 0; |
| 1342 | csnStream_t arT = *ar; |
| 1343 | gboolean EndOfList = FALSE; |
| 1344 | gint16 Status; |
| 1345 | pui8 = pui8DATA(data, pDescr->offset); |
| 1346 | |
| 1347 | do |
| 1348 | { /* get data element */ |
| 1349 | ElementCount++; |
| 1350 | |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1351 | LOGPC(DCSN1, LOGL_NOTICE, "%s { | ", pDescr->sz); |
Pau Espin Pedrol | f960d5b | 2020-01-23 19:05:00 +0100 | [diff] [blame] | 1352 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1353 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 1354 | Status = csnStreamDecoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, readIndex, pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1355 | |
| 1356 | if (Status >= 0) |
| 1357 | { /* successful completion */ |
| 1358 | pui8 += nSizeElement; /* -> to next */ |
| 1359 | remaining_bits_len = arT.remaining_bits_len; |
| 1360 | bit_offset = arT.bit_offset; |
| 1361 | } |
| 1362 | else |
| 1363 | { /* something went awry */ |
| 1364 | return Status; |
| 1365 | } |
| 1366 | |
| 1367 | if (remaining_bits_len < 0) |
| 1368 | { |
| 1369 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1370 | } |
| 1371 | |
| 1372 | /* control of next element's tag */ |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1373 | LOGPC(DCSN1, LOGL_NOTICE, "%s } | ", pDescr->sz); |
Alexander Couzens | ccde5c9 | 2017-02-04 03:10:08 +0100 | [diff] [blame] | 1374 | EndOfList = !(existNextElement(vector, readIndex, Tag)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1375 | |
| 1376 | bit_offset++; |
| 1377 | remaining_bits_len--; /* 1 bit consumed (tag) */ |
| 1378 | } while (!EndOfList); |
| 1379 | |
| 1380 | |
| 1381 | /* Store the count of the array */ |
| 1382 | *pui8DATA(data, pDescr->i) = ElementCount; |
| 1383 | Tag = STANDARD_TAG; /* in case it was set to "reversed" */ |
| 1384 | pDescr++; |
| 1385 | break; |
| 1386 | } |
| 1387 | |
| 1388 | case CSN_FIXED: |
| 1389 | { /* Verify the fixed bits */ |
| 1390 | guint8 no_of_bits = (guint8) pDescr->i; |
| 1391 | guint32 ui32; |
| 1392 | |
| 1393 | if (no_of_bits <= 32) |
| 1394 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1395 | ui32 = bitvec_read_field(vector, readIndex, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1396 | } |
| 1397 | else |
| 1398 | { |
| 1399 | return ProcessError(readIndex,"no_of_bits > 32", -1, pDescr); |
| 1400 | } |
| 1401 | if (ui32 != (unsigned)(gint32)pDescr->offset) |
| 1402 | { |
| 1403 | return ProcessError(readIndex,"csnStreamDecoder FIXED value does not match", -1, pDescr); |
| 1404 | } |
| 1405 | |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 1406 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned int)ui32); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1407 | remaining_bits_len -= no_of_bits; |
| 1408 | bit_offset += no_of_bits; |
| 1409 | pDescr++; |
| 1410 | break; |
| 1411 | } |
| 1412 | |
| 1413 | case CSN_CALLBACK: |
| 1414 | { |
| 1415 | return ProcessError(readIndex,"csnStreamDecoder Callback not implemented", -1, pDescr); |
| 1416 | break; |
| 1417 | } |
| 1418 | |
| 1419 | case CSN_TRAP_ERROR: |
| 1420 | { |
| 1421 | return ProcessError(readIndex,"csnStreamDecoder", pDescr->i, pDescr); |
| 1422 | } |
| 1423 | |
| 1424 | case CSN_END: |
| 1425 | { |
| 1426 | ar->remaining_bits_len = remaining_bits_len; |
| 1427 | ar->bit_offset = bit_offset; |
| 1428 | return remaining_bits_len; |
| 1429 | } |
| 1430 | |
| 1431 | default: |
| 1432 | { |
| 1433 | assert(0); |
| 1434 | } |
| 1435 | |
| 1436 | |
| 1437 | } |
| 1438 | |
| 1439 | } while (remaining_bits_len >= 0); |
| 1440 | |
| 1441 | return ProcessError(readIndex,"csnStreamDecoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1442 | } |
| 1443 | |
| 1444 | |
| 1445 | |
| 1446 | |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1447 | gint16 csnStreamEncoder(csnStream_t* ar, const CSN_DESCR* pDescr, struct bitvec *vector, unsigned *writeIndex, void* data) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1448 | { |
| 1449 | gint remaining_bits_len = ar->remaining_bits_len; |
| 1450 | gint bit_offset = ar->bit_offset; |
| 1451 | guint8* pui8; |
| 1452 | guint16* pui16; |
| 1453 | guint32* pui32; |
| 1454 | guint64* pui64; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1455 | unsigned ib; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1456 | |
| 1457 | guint8 Tag = STANDARD_TAG; |
| 1458 | |
| 1459 | if (remaining_bits_len <= 0) |
| 1460 | { |
| 1461 | return 0; |
| 1462 | } |
| 1463 | |
| 1464 | do |
| 1465 | { |
| 1466 | switch (pDescr->type) |
| 1467 | { |
| 1468 | case CSN_BIT: |
| 1469 | { |
| 1470 | if (remaining_bits_len > 0) |
| 1471 | { |
| 1472 | pui8 = pui8DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1473 | bitvec_write_field(vector, writeIndex, *pui8, 1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1474 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1475 | /* end add the bit value to protocol tree */ |
| 1476 | } |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 1477 | else if(pDescr->may_be_null) |
| 1478 | { |
| 1479 | LOGPC(DCSN1, LOGL_NOTICE, "%s = NULL | ", pDescr->sz); |
| 1480 | } |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1481 | else |
| 1482 | { |
| 1483 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1484 | } |
| 1485 | |
| 1486 | pDescr++; |
| 1487 | remaining_bits_len--; |
| 1488 | bit_offset++; |
| 1489 | break; |
| 1490 | } |
| 1491 | |
| 1492 | case CSN_NULL: |
| 1493 | { /* Empty member! */ |
| 1494 | pDescr++; |
| 1495 | break; |
| 1496 | } |
| 1497 | |
| 1498 | case CSN_UINT: |
| 1499 | { |
| 1500 | guint8 no_of_bits = (guint8) pDescr->i; |
| 1501 | |
| 1502 | if (remaining_bits_len >= no_of_bits) |
| 1503 | { |
| 1504 | if (no_of_bits <= 8) |
| 1505 | { |
| 1506 | pui8 = pui8DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1507 | bitvec_write_field(vector, writeIndex, *pui8, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1508 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1509 | } |
| 1510 | else if (no_of_bits <= 16) |
| 1511 | { |
| 1512 | pui16 = pui16DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1513 | bitvec_write_field(vector, writeIndex, *pui16, no_of_bits); |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 1514 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui16); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1515 | } |
| 1516 | else if (no_of_bits <= 32) |
| 1517 | { |
| 1518 | pui32 = pui32DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1519 | bitvec_write_field(vector, writeIndex, *pui32, no_of_bits); |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 1520 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui32); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1521 | } |
| 1522 | else |
| 1523 | { |
| 1524 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_GENERAL, pDescr); |
| 1525 | } |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 1526 | |
| 1527 | remaining_bits_len -= no_of_bits; |
| 1528 | bit_offset += no_of_bits; |
| 1529 | } |
| 1530 | else if(pDescr->may_be_null) |
| 1531 | { |
| 1532 | LOGPC(DCSN1, LOGL_NOTICE, "%s = NULL | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1533 | } |
| 1534 | else |
| 1535 | { |
| 1536 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1537 | } |
| 1538 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1539 | pDescr++; |
| 1540 | break; |
| 1541 | } |
| 1542 | |
| 1543 | case CSN_UINT_OFFSET: |
| 1544 | { |
| 1545 | guint8 no_of_bits = (guint8) pDescr->i; |
| 1546 | |
| 1547 | if (remaining_bits_len >= no_of_bits) |
| 1548 | { |
| 1549 | if (no_of_bits <= 8) |
| 1550 | { |
| 1551 | pui8 = pui8DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1552 | bitvec_write_field(vector, writeIndex, *pui8 - (guint8)pDescr->descr.value, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1553 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)(*pui8 - (guint8)pDescr->descr.value)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1554 | } |
| 1555 | else if (no_of_bits <= 16) |
| 1556 | { |
| 1557 | pui16 = pui16DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1558 | bitvec_write_field(vector, writeIndex, *pui16 - (guint16)pDescr->descr.value, no_of_bits); |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 1559 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned short)(*pui16 - (guint16)pDescr->descr.value)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1560 | } |
| 1561 | else if (no_of_bits <= 32) |
| 1562 | { |
| 1563 | pui32 = pui32DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1564 | bitvec_write_field(vector, writeIndex, *pui32 - (guint16)pDescr->descr.value, no_of_bits); |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 1565 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned int)(*pui32 - (guint16)pDescr->descr.value)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1566 | } |
| 1567 | else |
| 1568 | { |
| 1569 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_GENERAL, pDescr); |
| 1570 | } |
| 1571 | } |
| 1572 | else |
| 1573 | { |
| 1574 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1575 | } |
| 1576 | |
| 1577 | remaining_bits_len -= no_of_bits; |
| 1578 | bit_offset += no_of_bits; |
| 1579 | pDescr++; |
| 1580 | break; |
| 1581 | } |
| 1582 | |
| 1583 | case CSN_UINT_LH: |
| 1584 | { |
| 1585 | guint8 no_of_bits = (guint8) pDescr->i; |
| 1586 | |
| 1587 | if (remaining_bits_len >= no_of_bits) |
| 1588 | { |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1589 | if (no_of_bits <= 8) |
| 1590 | { |
| 1591 | pui8 = pui8DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1592 | bitvec_write_field(vector, writeIndex, *pui8, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1593 | // TODO : Change get_masked_bits8() |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1594 | *writeIndex -= no_of_bits; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1595 | guint8 ui8 = get_masked_bits8(vector, writeIndex, bit_offset, no_of_bits); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1596 | *writeIndex -= no_of_bits; |
| 1597 | bitvec_write_field(vector, writeIndex, ui8, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1598 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1599 | |
| 1600 | } |
| 1601 | else |
| 1602 | {/* Maybe we should support more than 8 bits ? */ |
| 1603 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_GENERAL, pDescr); |
| 1604 | } |
| 1605 | } |
| 1606 | else |
| 1607 | { |
| 1608 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1609 | } |
| 1610 | |
| 1611 | remaining_bits_len -= no_of_bits; |
| 1612 | bit_offset += no_of_bits; |
| 1613 | pDescr++; |
| 1614 | break; |
| 1615 | } |
| 1616 | |
| 1617 | case CSN_UINT_ARRAY: |
| 1618 | { |
| 1619 | guint8 no_of_bits = (guint8) pDescr->i; |
| 1620 | guint16 nCount = (guint16)pDescr->descr.value; /* nCount supplied by value i.e. M_UINT_ARRAY(...) */ |
| 1621 | |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 1622 | if (pDescr->value != 0) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1623 | { /* nCount specified by a reference to field holding value i.e. M_VAR_UINT_ARRAY(...) */ |
| 1624 | nCount = *pui16DATA(data, nCount); |
| 1625 | } |
| 1626 | |
| 1627 | if (remaining_bits_len >= no_of_bits) |
| 1628 | { |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1629 | if (no_of_bits <= 8) |
| 1630 | { |
| 1631 | pui8 = pui8DATA(data, pDescr->offset); |
| 1632 | do |
| 1633 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1634 | bitvec_write_field(vector, writeIndex, *pui8, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1635 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1636 | pui8++; |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 1637 | remaining_bits_len -= no_of_bits; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1638 | bit_offset += no_of_bits; |
| 1639 | } while (--nCount > 0); |
| 1640 | } |
| 1641 | else if (no_of_bits <= 16) |
| 1642 | { |
| 1643 | return ProcessError(writeIndex,"csnStreamEncoder NOTIMPLEMENTED", 999, pDescr); |
| 1644 | } |
| 1645 | else if (no_of_bits <= 32) |
| 1646 | { |
| 1647 | return ProcessError(writeIndex,"csnStreamEncoder NOTIMPLEMENTED", 999, pDescr); |
| 1648 | } |
| 1649 | else |
| 1650 | { |
| 1651 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_GENERAL, pDescr); |
| 1652 | } |
| 1653 | } |
| 1654 | else |
| 1655 | { |
| 1656 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1657 | } |
| 1658 | pDescr++; |
| 1659 | break; |
| 1660 | } |
| 1661 | |
| 1662 | case CSN_VARIABLE_TARRAY_OFFSET: |
| 1663 | case CSN_VARIABLE_TARRAY: |
| 1664 | case CSN_TYPE_ARRAY: |
| 1665 | { |
| 1666 | gint16 Status; |
| 1667 | csnStream_t arT = *ar; |
| 1668 | gint16 nCount = pDescr->i; |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 1669 | guint16 nSize = (guint16)(gint32)pDescr->value; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1670 | |
| 1671 | pui8 = pui8DATA(data, pDescr->offset); |
| 1672 | if (pDescr->type == CSN_VARIABLE_TARRAY) |
| 1673 | { /* Count specified in field */ |
| 1674 | nCount = *pui8DATA(data, pDescr->i); |
| 1675 | } |
| 1676 | else if (pDescr->type == CSN_VARIABLE_TARRAY_OFFSET) |
| 1677 | { /* Count specified in field */ |
| 1678 | nCount = *pui8DATA(data, pDescr->i); |
| 1679 | /* nCount--; the 1 offset is already taken into account in CSN_UINT_OFFSET */ |
| 1680 | } |
| 1681 | |
| 1682 | while (nCount > 0) |
| 1683 | { /* resulting array of length 0 is possible |
| 1684 | * but no bits shall be read from bitstream |
| 1685 | */ |
| 1686 | |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1687 | LOGPC(DCSN1, LOGL_NOTICE, "%s : | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1688 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
| 1689 | Status = csnStreamEncoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, writeIndex, pui8); |
| 1690 | if (Status >= 0) |
| 1691 | { |
| 1692 | pui8 += nSize; |
| 1693 | remaining_bits_len = arT.remaining_bits_len; |
| 1694 | bit_offset = arT.bit_offset; |
| 1695 | |
| 1696 | } |
| 1697 | else |
| 1698 | { |
| 1699 | return Status; |
| 1700 | } |
| 1701 | nCount--; |
| 1702 | } |
| 1703 | |
| 1704 | pDescr++; |
| 1705 | break; |
| 1706 | } |
| 1707 | |
| 1708 | case CSN_BITMAP: |
| 1709 | { /* bitmap with given length. The result is left aligned! */ |
| 1710 | guint8 no_of_bits = (guint8) pDescr->i; /* length of bitmap */ |
| 1711 | |
| 1712 | if (no_of_bits > 0) |
| 1713 | { |
| 1714 | |
| 1715 | if (no_of_bits <= 32) |
| 1716 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1717 | for(ib = 0; ib < 4; ib++) |
Ivan Kluchnikov | 74b459f | 2012-04-11 22:02:15 +0400 | [diff] [blame] | 1718 | { |
| 1719 | pui8 = pui8DATA(data, pDescr->offset+ib); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1720 | bitvec_write_field(vector, writeIndex, *pui8, 8); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1721 | LOGPC(DCSN1, LOGL_NOTICE, "%s[%u] = %u | ", pDescr->sz , ib, (unsigned)*pui8); |
Ivan Kluchnikov | 74b459f | 2012-04-11 22:02:15 +0400 | [diff] [blame] | 1722 | } |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1723 | } |
| 1724 | else if (no_of_bits <= 64) |
| 1725 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1726 | for(ib = 0; ib < 8; ib++) |
Ivan Kluchnikov | 74b459f | 2012-04-11 22:02:15 +0400 | [diff] [blame] | 1727 | { |
| 1728 | pui8 = pui8DATA(data, pDescr->offset+ib); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1729 | bitvec_write_field(vector, writeIndex, *pui8, 8); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1730 | LOGPC(DCSN1, LOGL_NOTICE, "%s[%u] = %u | ", pDescr->sz , ib, (unsigned)*pui8); |
Ivan Kluchnikov | 74b459f | 2012-04-11 22:02:15 +0400 | [diff] [blame] | 1731 | } |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1732 | } |
| 1733 | else |
| 1734 | { |
| 1735 | return ProcessError(writeIndex,"csnStreamEncoder NOT IMPLEMENTED", 999, pDescr); |
| 1736 | } |
| 1737 | |
| 1738 | remaining_bits_len -= no_of_bits; |
| 1739 | assert(remaining_bits_len >= 0); |
| 1740 | bit_offset += no_of_bits; |
| 1741 | } |
| 1742 | /* bitmap was successfully extracted or it was empty */ |
| 1743 | |
| 1744 | pDescr++; |
| 1745 | break; |
| 1746 | } |
| 1747 | |
| 1748 | case CSN_TYPE: |
| 1749 | { |
| 1750 | gint16 Status; |
| 1751 | csnStream_t arT = *ar; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1752 | LOGPC(DCSN1, LOGL_NOTICE, " : %s | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1753 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
| 1754 | Status = csnStreamEncoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, writeIndex, pvDATA(data, pDescr->offset)); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1755 | LOGPC(DCSN1, LOGL_NOTICE, " : End %s | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1756 | if (Status >= 0) |
| 1757 | { |
| 1758 | |
| 1759 | remaining_bits_len = arT.remaining_bits_len; |
| 1760 | bit_offset = arT.bit_offset; |
| 1761 | pDescr++; |
| 1762 | } |
| 1763 | else |
| 1764 | { |
| 1765 | /* Has already been processed: ProcessError("csnStreamEncoder", Status, pDescr); */ |
| 1766 | return Status; |
| 1767 | } |
| 1768 | |
| 1769 | break; |
| 1770 | } |
| 1771 | |
| 1772 | case CSN_CHOICE: |
| 1773 | { |
| 1774 | //gint16 count = pDescr->i; |
| 1775 | guint8 i = 0; |
Anders Broman | 60bf845 | 2020-01-24 17:35:48 +0100 | [diff] [blame] | 1776 | const CSN_ChoiceElement_t* pChoice = (const CSN_ChoiceElement_t*) pDescr->descr.ptr; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1777 | |
| 1778 | pui8 = pui8DATA(data, pDescr->offset); |
| 1779 | i = *pui8; |
| 1780 | pChoice += i; |
| 1781 | guint8 no_of_bits = pChoice->bits; |
| 1782 | guint8 value = pChoice->value; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1783 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pChoice->descr.sz , (unsigned)value); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1784 | bitvec_write_field(vector, writeIndex, value, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1785 | |
| 1786 | CSN_DESCR descr[2]; |
| 1787 | gint16 Status; |
| 1788 | csnStream_t arT = *ar; |
| 1789 | |
| 1790 | descr[0] = pChoice->descr; |
| 1791 | memset(&descr[1], 0x00, sizeof(CSN_DESCR)); |
| 1792 | descr[1].type = CSN_END; |
| 1793 | bit_offset += no_of_bits; |
| 1794 | remaining_bits_len -= no_of_bits; |
| 1795 | |
| 1796 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
| 1797 | Status = csnStreamEncoder(&arT, descr, vector, writeIndex, data); |
| 1798 | |
| 1799 | if (Status >= 0) |
| 1800 | { |
| 1801 | remaining_bits_len = arT.remaining_bits_len; |
| 1802 | bit_offset = arT.bit_offset; |
| 1803 | } |
| 1804 | else |
| 1805 | { |
| 1806 | return Status; |
| 1807 | } |
| 1808 | |
| 1809 | pDescr++; |
| 1810 | break; |
| 1811 | } |
| 1812 | |
| 1813 | case CSN_SERIALIZE: |
| 1814 | { |
Pau Espin Pedrol | 5b71697 | 2020-01-24 17:24:01 +0100 | [diff] [blame] | 1815 | StreamSerializeFcn_t serialize = (StreamSerializeFcn_t)pDescr->aux_fn; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1816 | csnStream_t arT = *ar; |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 1817 | guint8 length_len = pDescr->i; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1818 | gint16 Status = -1; |
Ivan Kluchnikov | 9b06ff0 | 2012-06-15 10:13:30 +0400 | [diff] [blame] | 1819 | unsigned lengthIndex; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1820 | |
| 1821 | // store writeIndex for length value (7 bit) |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1822 | lengthIndex = *writeIndex; |
| 1823 | *writeIndex += length_len; |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 1824 | bit_offset += length_len; |
| 1825 | remaining_bits_len -= length_len; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1826 | arT.direction = 0; |
| 1827 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
| 1828 | Status = serialize(&arT, vector, writeIndex, pvDATA(data, pDescr->offset)); |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 1829 | |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1830 | bitvec_write_field(vector, &lengthIndex, *writeIndex - lengthIndex - length_len, length_len); |
| 1831 | LOGPC(DCSN1, LOGL_NOTICE, "%s length = %u | ", pDescr->sz , (unsigned)(*writeIndex - lengthIndex)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1832 | |
| 1833 | if (Status >= 0) |
| 1834 | { |
| 1835 | remaining_bits_len = arT.remaining_bits_len; |
| 1836 | bit_offset = arT.bit_offset; |
| 1837 | pDescr++; |
| 1838 | } |
| 1839 | else |
| 1840 | { |
Pau Espin Pedrol | f960d5b | 2020-01-23 19:05:00 +0100 | [diff] [blame] | 1841 | // Has already been processed: |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1842 | return Status; |
| 1843 | } |
| 1844 | |
| 1845 | break; |
| 1846 | } |
| 1847 | |
| 1848 | case CSN_UNION_LH: |
| 1849 | case CSN_UNION: |
| 1850 | { |
| 1851 | gint16 Bits; |
| 1852 | guint8 index; |
| 1853 | gint16 count = pDescr->i; |
| 1854 | const CSN_DESCR* pDescrNext = pDescr; |
| 1855 | |
| 1856 | pDescrNext += count + 1; /* now this is next after the union */ |
| 1857 | if ((count <= 0) || (count > 16)) |
| 1858 | { |
| 1859 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_INVALID_UNION_INDEX, pDescr); |
| 1860 | } |
| 1861 | |
| 1862 | /* Now get the bits to extract the index */ |
| 1863 | Bits = ixBitsTab[count]; |
| 1864 | index = 0; |
| 1865 | |
| 1866 | /* Assign UnionType */ |
| 1867 | pui8 = pui8DATA(data, pDescr->offset); |
| 1868 | //read index from data and write to vector |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1869 | bitvec_write_field(vector, writeIndex, *pui8, Bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1870 | |
Pau Espin Pedrol | f960d5b | 2020-01-23 19:05:00 +0100 | [diff] [blame] | 1871 | //decode index |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1872 | *writeIndex -= Bits; |
Pau Espin Pedrol | f960d5b | 2020-01-23 19:05:00 +0100 | [diff] [blame] | 1873 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1874 | while (Bits > 0) |
| 1875 | { |
| 1876 | index <<= 1; |
| 1877 | |
| 1878 | if (CSN_UNION_LH == pDescr->type) |
| 1879 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1880 | index |= get_masked_bits8(vector, writeIndex, bit_offset, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1881 | } |
| 1882 | else |
| 1883 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1884 | index |= bitvec_read_field(vector, writeIndex, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1885 | } |
| 1886 | |
| 1887 | remaining_bits_len--; |
| 1888 | bit_offset++; |
| 1889 | Bits--; |
| 1890 | } |
| 1891 | |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1892 | *writeIndex -= Bits; |
| 1893 | bitvec_write_field(vector, writeIndex, index, Bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1894 | |
| 1895 | |
| 1896 | /* script index to continue on, limited in case we do not have a power of 2 */ |
| 1897 | pDescr += (MIN(index + 1, count)); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1898 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)index); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1899 | |
| 1900 | switch (pDescr->type) |
| 1901 | { /* get the right element of the union based on computed index */ |
| 1902 | |
| 1903 | case CSN_BIT: |
| 1904 | { |
| 1905 | pui8 = pui8DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1906 | bitvec_write_field(vector, writeIndex, *pui8, 1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1907 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 1908 | remaining_bits_len--; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1909 | bit_offset++; |
| 1910 | pDescr++; |
| 1911 | break; |
| 1912 | } |
| 1913 | |
| 1914 | case CSN_NULL: |
| 1915 | { /* Empty member! */ |
| 1916 | pDescr++; |
| 1917 | break; |
| 1918 | } |
| 1919 | |
| 1920 | case CSN_UINT: |
| 1921 | { |
| 1922 | guint8 no_of_bits = (guint8) pDescr->i; |
| 1923 | if (remaining_bits_len >= no_of_bits) |
| 1924 | { |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1925 | if (no_of_bits <= 8) |
| 1926 | { |
| 1927 | pui8 = pui8DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1928 | bitvec_write_field(vector, writeIndex, *pui8, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1929 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1930 | } |
| 1931 | else if (no_of_bits <= 16) |
| 1932 | { |
| 1933 | pui16 = pui16DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1934 | bitvec_write_field(vector, writeIndex, *pui16, no_of_bits); |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 1935 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui16); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1936 | } |
| 1937 | else if (no_of_bits <= 32) |
| 1938 | { |
| 1939 | pui32 = pui32DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1940 | bitvec_write_field(vector, writeIndex, *pui32, no_of_bits); |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 1941 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui32); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1942 | } |
| 1943 | else |
| 1944 | { |
| 1945 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_GENERAL, pDescr); |
| 1946 | } |
| 1947 | } |
| 1948 | else |
| 1949 | { |
| 1950 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_GENERAL, pDescr); |
| 1951 | } |
| 1952 | |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 1953 | remaining_bits_len -= no_of_bits; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1954 | bit_offset += no_of_bits; |
| 1955 | pDescr++; |
| 1956 | break; |
| 1957 | } |
| 1958 | |
| 1959 | case CSN_UINT_OFFSET: |
| 1960 | { |
| 1961 | guint8 no_of_bits = (guint8) pDescr->i; |
| 1962 | |
| 1963 | if (remaining_bits_len >= no_of_bits) |
| 1964 | { |
| 1965 | if (no_of_bits <= 8) |
| 1966 | { |
| 1967 | pui8 = pui8DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1968 | bitvec_write_field(vector, writeIndex, *pui8 - (guint8)pDescr->descr.value, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 1969 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)(*pui8 - (guint8)pDescr->descr.value)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1970 | } |
| 1971 | else if (no_of_bits <= 16) |
| 1972 | { |
| 1973 | pui16 = pui16DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1974 | bitvec_write_field(vector, writeIndex, *pui16 - (guint16)pDescr->descr.value, no_of_bits); |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 1975 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned short)(*pui16 - (guint16)pDescr->descr.value)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1976 | } |
| 1977 | else if (no_of_bits <= 32) |
| 1978 | { |
| 1979 | pui32 = pui32DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 1980 | bitvec_write_field(vector, writeIndex, *pui32 - (guint16)pDescr->descr.value, no_of_bits); |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 1981 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned int)(*pui32 - (guint16)pDescr->descr.value)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 1982 | } |
| 1983 | else |
| 1984 | { |
| 1985 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_GENERAL, pDescr); |
| 1986 | } |
| 1987 | } |
| 1988 | else |
| 1989 | { |
| 1990 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 1991 | } |
| 1992 | |
| 1993 | remaining_bits_len -= no_of_bits; |
| 1994 | bit_offset += no_of_bits; |
| 1995 | pDescr++; |
| 1996 | break; |
| 1997 | } |
| 1998 | |
| 1999 | case CSN_UINT_LH: |
| 2000 | { |
| 2001 | guint8 no_of_bits = (guint8) pDescr->i; |
| 2002 | |
| 2003 | if (remaining_bits_len >= no_of_bits) |
| 2004 | { |
| 2005 | remaining_bits_len -= no_of_bits; |
| 2006 | if (no_of_bits <= 8) |
| 2007 | { |
| 2008 | pui8 = pui8DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2009 | bitvec_write_field(vector, writeIndex, *pui8, no_of_bits); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2010 | // TODO : Change get_masked_bits8() |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2011 | *writeIndex -= no_of_bits; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2012 | guint8 ui8 = get_masked_bits8(vector, writeIndex, bit_offset, no_of_bits); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2013 | *writeIndex -= no_of_bits; |
| 2014 | bitvec_write_field(vector, writeIndex, ui8, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2015 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2016 | |
| 2017 | } |
| 2018 | else |
| 2019 | {/* Maybe we should support more than 8 bits ? */ |
| 2020 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_GENERAL, pDescr); |
| 2021 | } |
| 2022 | } |
| 2023 | else |
| 2024 | { |
| 2025 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 2026 | } |
| 2027 | |
| 2028 | remaining_bits_len -= no_of_bits; |
| 2029 | bit_offset += no_of_bits; |
| 2030 | pDescr++; |
| 2031 | break; |
| 2032 | } |
| 2033 | |
| 2034 | case CSN_UINT_ARRAY: |
| 2035 | { |
| 2036 | guint8 no_of_bits = (guint8) pDescr->i; |
| 2037 | guint16 nCount = (guint16)pDescr->descr.value; /* nCount supplied by value i.e. M_UINT_ARRAY(...) */ |
| 2038 | |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 2039 | if (pDescr->value != 0) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2040 | { /* nCount specified by a reference to field holding value i.e. M_VAR_UINT_ARRAY(...) */ |
| 2041 | nCount = *pui16DATA(data, nCount); |
| 2042 | } |
| 2043 | |
| 2044 | if (remaining_bits_len >= no_of_bits) |
| 2045 | { |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2046 | if (no_of_bits <= 8) |
| 2047 | { |
| 2048 | pui8 = pui8DATA(data, pDescr->offset); |
| 2049 | do |
| 2050 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2051 | bitvec_write_field(vector, writeIndex, *pui8, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2052 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2053 | pui8++; |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 2054 | remaining_bits_len -= no_of_bits; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2055 | bit_offset += no_of_bits; |
| 2056 | } while (--nCount > 0); |
| 2057 | } |
| 2058 | else if (no_of_bits <= 16) |
| 2059 | { |
| 2060 | return ProcessError(writeIndex,"csnStreamEncoder NOTIMPLEMENTED", 999, pDescr); |
| 2061 | } |
| 2062 | else if (no_of_bits <= 32) |
| 2063 | { |
| 2064 | return ProcessError(writeIndex,"csnStreamEncoder NOTIMPLEMENTED", 999, pDescr); |
| 2065 | } |
| 2066 | else |
| 2067 | { |
| 2068 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_GENERAL, pDescr); |
| 2069 | } |
| 2070 | } |
| 2071 | else |
| 2072 | { |
| 2073 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 2074 | } |
| 2075 | pDescr++; |
| 2076 | break; |
| 2077 | } |
| 2078 | |
| 2079 | case CSN_VARIABLE_TARRAY_OFFSET: |
| 2080 | case CSN_VARIABLE_TARRAY: |
| 2081 | case CSN_TYPE_ARRAY: |
| 2082 | { |
| 2083 | gint16 Status; |
| 2084 | csnStream_t arT = *ar; |
| 2085 | gint16 nCount = pDescr->i; |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 2086 | guint16 nSize = (guint16)(gint32)pDescr->value; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2087 | |
| 2088 | pui8 = pui8DATA(data, pDescr->offset); |
| 2089 | if (pDescr->type == CSN_VARIABLE_TARRAY) |
| 2090 | { /* Count specified in field */ |
| 2091 | nCount = *pui8DATA(data, pDescr->i); |
| 2092 | } |
| 2093 | else if (pDescr->type == CSN_VARIABLE_TARRAY_OFFSET) |
| 2094 | { /* Count specified in field */ |
| 2095 | nCount = *pui8DATA(data, pDescr->i); |
| 2096 | /* nCount--; the 1 offset is already taken into account in CSN_UINT_OFFSET */ |
| 2097 | } |
| 2098 | |
| 2099 | while (nCount > 0) |
| 2100 | { /* resulting array of length 0 is possible |
| 2101 | * but no bits shall be read from bitstream |
| 2102 | */ |
| 2103 | |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2104 | LOGPC(DCSN1, LOGL_NOTICE, "%s : | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2105 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
| 2106 | Status = csnStreamEncoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, writeIndex, pui8); |
| 2107 | if (Status >= 0) |
| 2108 | { |
| 2109 | pui8 += nSize; |
| 2110 | remaining_bits_len = arT.remaining_bits_len; |
| 2111 | bit_offset = arT.bit_offset; |
| 2112 | } |
| 2113 | else |
| 2114 | { |
| 2115 | return Status; |
| 2116 | } |
| 2117 | nCount--; |
| 2118 | } |
| 2119 | |
| 2120 | pDescr++; |
| 2121 | break; |
| 2122 | } |
| 2123 | |
| 2124 | case CSN_BITMAP: |
| 2125 | { /* bitmap with given length. The result is left aligned! */ |
| 2126 | guint8 no_of_bits = (guint8) pDescr->i; /* length of bitmap */ |
| 2127 | |
| 2128 | if (no_of_bits > 0) |
| 2129 | { |
| 2130 | |
| 2131 | if (no_of_bits <= 32) |
| 2132 | { |
| 2133 | pui32 = pui32DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2134 | bitvec_write_field(vector, writeIndex, *pui32, no_of_bits); |
Vadim Yanitskiy | 1553049 | 2020-02-17 19:38:22 +0700 | [diff] [blame] | 2135 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , *pui32); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2136 | } |
| 2137 | else if (no_of_bits <= 64) |
| 2138 | { |
| 2139 | pui64 = pui64DATA(data, pDescr->offset); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2140 | bitvec_write_field(vector, writeIndex, *pui64, no_of_bits); |
Holger Hans Peter Freyther | cf0265a | 2013-07-28 15:57:20 +0200 | [diff] [blame] | 2141 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %lu | ", pDescr->sz , *pui64); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2142 | } |
| 2143 | else |
| 2144 | { |
| 2145 | return ProcessError(writeIndex,"csnStreamEncoder NOT IMPLEMENTED", 999, pDescr); |
| 2146 | } |
| 2147 | |
| 2148 | remaining_bits_len -= no_of_bits; |
| 2149 | assert(remaining_bits_len >= 0); |
| 2150 | bit_offset += no_of_bits; |
| 2151 | } |
| 2152 | /* bitmap was successfully extracted or it was empty */ |
| 2153 | |
| 2154 | pDescr++; |
| 2155 | break; |
| 2156 | } |
| 2157 | |
| 2158 | case CSN_TYPE: |
| 2159 | { |
| 2160 | gint16 Status; |
| 2161 | csnStream_t arT = *ar; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2162 | LOGPC(DCSN1, LOGL_NOTICE, " : %s | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2163 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
| 2164 | Status = csnStreamEncoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, writeIndex, pvDATA(data, pDescr->offset)); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2165 | LOGPC(DCSN1, LOGL_NOTICE, " : End %s | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2166 | if (Status >= 0) |
| 2167 | { |
| 2168 | remaining_bits_len = arT.remaining_bits_len; |
| 2169 | bit_offset = arT.bit_offset; |
| 2170 | pDescr++; |
| 2171 | } |
| 2172 | else |
| 2173 | { |
| 2174 | /* Has already been processed: ProcessError("csnStreamEncoder", Status, pDescr); */ |
| 2175 | return Status; |
| 2176 | } |
| 2177 | |
| 2178 | break; |
| 2179 | } |
| 2180 | |
| 2181 | default: |
| 2182 | { /* descriptions of union elements other than above are illegal */ |
| 2183 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_IN_SCRIPT, pDescr); |
| 2184 | } |
| 2185 | } |
| 2186 | |
| 2187 | pDescr = pDescrNext; |
| 2188 | break; |
| 2189 | } |
| 2190 | |
| 2191 | case CSN_EXIST: |
| 2192 | case CSN_EXIST_LH: |
| 2193 | { |
| 2194 | guint8 fExist; |
| 2195 | unsigned exist = 0; |
| 2196 | pui8 = pui8DATA(data, pDescr->offset); |
| 2197 | exist = *pui8; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2198 | bitvec_write_field(vector, writeIndex, *pui8, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2199 | writeIndex--; |
| 2200 | if (CSN_EXIST_LH == pDescr->type) |
| 2201 | { |
| 2202 | fExist = get_masked_bits8(vector, writeIndex, bit_offset, 1); |
| 2203 | } |
| 2204 | else |
| 2205 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2206 | fExist = bitvec_read_field(vector, writeIndex, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2207 | } |
| 2208 | writeIndex--; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2209 | bitvec_write_field(vector, writeIndex, fExist, 1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2210 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz, (unsigned)fExist); |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 2211 | remaining_bits_len--; |
| 2212 | bit_offset++; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2213 | pDescr++; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2214 | |
| 2215 | if (!exist) |
| 2216 | { |
| 2217 | ar->remaining_bits_len = remaining_bits_len; |
| 2218 | ar->bit_offset = bit_offset; |
| 2219 | return remaining_bits_len; |
| 2220 | } |
| 2221 | break; |
| 2222 | } |
| 2223 | |
| 2224 | case CSN_NEXT_EXIST: |
| 2225 | { |
| 2226 | guint8 fExist; |
| 2227 | |
| 2228 | pui8 = pui8DATA(data, pDescr->offset); |
| 2229 | |
| 2230 | /* this if-statement represents the M_NEXT_EXIST_OR_NULL description element */ |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 2231 | if ((pDescr->may_be_null) && (remaining_bits_len == 0)) |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2232 | { /* no more bits to decode is fine here - end of message detected and allowed */ |
| 2233 | |
| 2234 | /* Skip i entries + this entry */ |
| 2235 | pDescr += pDescr->i + 1; |
| 2236 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2237 | break; |
| 2238 | } |
| 2239 | |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2240 | bitvec_write_field(vector, writeIndex, *pui8, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2241 | fExist = *pui8; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2242 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2243 | |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 2244 | remaining_bits_len--; |
| 2245 | bit_offset++; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2246 | |
| 2247 | if (fExist == 0) |
| 2248 | { /* Skip 'i' entries */ |
| 2249 | pDescr += pDescr->i; |
| 2250 | } |
| 2251 | |
| 2252 | pDescr++; |
| 2253 | break; |
| 2254 | } |
| 2255 | |
| 2256 | case CSN_NEXT_EXIST_LH: |
| 2257 | { |
| 2258 | guint8 fExist; |
| 2259 | pui8 = pui8DATA(data, pDescr->offset); |
| 2260 | |
| 2261 | /* this if-statement represents the M_NEXT_EXIST_OR_NULL_LH description element */ |
| 2262 | if ((pDescr->descr.ptr != NULL) && (remaining_bits_len == 0)) |
| 2263 | { /* no more bits to decode is fine here - end of message detected and allowed */ |
| 2264 | |
| 2265 | /* skip 'i' entries + this entry */ |
| 2266 | pDescr += pDescr->i + 1; |
| 2267 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2268 | /* set the data member to "not exist" */ |
| 2269 | //*pui8 = 0; |
| 2270 | break; |
| 2271 | } |
| 2272 | |
| 2273 | /* the "regular" M_NEXT_EXIST_LH description element */ |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2274 | bitvec_write_field(vector, writeIndex, *pui8, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2275 | writeIndex--; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2276 | fExist = get_masked_bits8(vector, writeIndex, bit_offset, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2277 | writeIndex--; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2278 | bitvec_write_field(vector, writeIndex, fExist, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2279 | pui8++; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2280 | |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 2281 | remaining_bits_len--; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2282 | bit_offset++; |
| 2283 | |
| 2284 | if (fExist == 0) |
| 2285 | { /* Skip 'i' entries */ |
| 2286 | pDescr += pDescr->i; |
| 2287 | } |
| 2288 | pDescr++; |
| 2289 | |
| 2290 | break; |
| 2291 | } |
| 2292 | |
| 2293 | case CSN_VARIABLE_BITMAP_1: |
| 2294 | { /* Bitmap from here and to the end of message */ |
| 2295 | |
| 2296 | //*pui8DATA(data, (gint16)pDescr->descr.value) = (guint8) remaining_bits_len; /* length of bitmap == remaining bits */ |
| 2297 | |
| 2298 | /*no break - |
| 2299 | * with a length set we have a regular variable length bitmap so we continue */ |
| 2300 | } |
Alexis La Goutte | e36fb5b | 2020-01-24 18:22:35 +0100 | [diff] [blame] | 2301 | /* FALL THROUGH */ |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2302 | case CSN_VARIABLE_BITMAP: |
| 2303 | { /* {CSN_VARIABLE_BITMAP, 0, offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER} |
| 2304 | * <N: bit (5)> <bitmap: bit(N + offset)> |
| 2305 | * Bit array with length (in bits) specified in parameter (pDescr->descr) |
| 2306 | * The result is right aligned! |
| 2307 | */ |
| 2308 | gint16 no_of_bits = *pui8DATA(data, (gint16)pDescr->descr.value); |
| 2309 | |
| 2310 | no_of_bits += pDescr->i; /* adjusted by offset */ |
| 2311 | |
| 2312 | if (no_of_bits > 0) |
| 2313 | { |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2314 | |
| 2315 | if (remaining_bits_len < 0) |
| 2316 | { |
| 2317 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 2318 | } |
| 2319 | |
| 2320 | { /* extract bits */ |
| 2321 | guint8* pui8 = pui8DATA(data, pDescr->offset); |
| 2322 | gint16 nB1 = no_of_bits & 0x07;/* no_of_bits Mod 8 */ |
| 2323 | |
| 2324 | if (nB1 > 0) |
| 2325 | { /* take care of the first byte - it will be right aligned */ |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2326 | bitvec_write_field(vector, writeIndex, *pui8, nB1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2327 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2328 | pui8++; |
| 2329 | no_of_bits -= nB1; |
| 2330 | bit_offset += nB1; /* (nB1 is no_of_bits Mod 8) */ |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 2331 | remaining_bits_len -= nB1; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2332 | } |
| 2333 | |
| 2334 | /* remaining no_of_bits is a multiple of 8 or 0 */ |
| 2335 | while (no_of_bits > 0) |
| 2336 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2337 | bitvec_write_field(vector, writeIndex, *pui8, 8); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2338 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2339 | pui8++; |
| 2340 | no_of_bits -= 8; |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 2341 | remaining_bits_len -= 8; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2342 | } |
| 2343 | } |
| 2344 | } |
| 2345 | pDescr++; |
| 2346 | break; |
| 2347 | } |
| 2348 | |
| 2349 | case CSN_LEFT_ALIGNED_VAR_BMP_1: |
| 2350 | { /* Bitmap from here and to the end of message */ |
| 2351 | |
| 2352 | //*pui8DATA(data, (gint16)pDescr->descr.value) = (guint8) remaining_bits_len; /* length of bitmap == remaining bits */ |
| 2353 | |
| 2354 | /* no break - |
| 2355 | * with a length set we have a regular left aligned variable length bitmap so we continue |
| 2356 | */ |
| 2357 | } |
Alexis La Goutte | e36fb5b | 2020-01-24 18:22:35 +0100 | [diff] [blame] | 2358 | /* FALL THROUGH */ |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2359 | case CSN_LEFT_ALIGNED_VAR_BMP: |
| 2360 | { /* {CSN_LEFT_ALIGNED_VAR_BMP, _OFFSET, (void*)offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER} |
| 2361 | * <N: bit (5)> <bitmap: bit(N + offset)> |
| 2362 | * bit array with length (in bits) specified in parameter (pDescr->descr) |
| 2363 | */ |
| 2364 | |
| 2365 | gint16 no_of_bits = *pui8DATA(data, (gint16)pDescr->descr.value);/* Size of bitmap */ |
| 2366 | |
| 2367 | no_of_bits += pDescr->i;/* size adjusted by offset */ |
| 2368 | |
| 2369 | if (no_of_bits > 0) |
| 2370 | { |
| 2371 | remaining_bits_len -= no_of_bits; |
| 2372 | |
| 2373 | if (remaining_bits_len < 0) |
| 2374 | { |
| 2375 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 2376 | } |
| 2377 | |
| 2378 | { /* extract bits */ |
| 2379 | guint8* pui8 = pui8DATA(data, pDescr->offset); |
| 2380 | gint16 nB1 = no_of_bits & 0x07;/* no_of_bits Mod 8 */ |
| 2381 | |
| 2382 | while (no_of_bits > 0) |
| 2383 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2384 | bitvec_write_field(vector, writeIndex, *pui8, 8); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2385 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2386 | pui8++; |
| 2387 | no_of_bits -= 8; |
| 2388 | } |
| 2389 | if (nB1 > 0) |
| 2390 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2391 | bitvec_write_field(vector, writeIndex, *pui8, nB1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2392 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2393 | pui8++; |
| 2394 | no_of_bits -= nB1; |
| 2395 | bit_offset += nB1; /* (nB1 is no_of_bits Mod 8) */ |
| 2396 | } |
| 2397 | } |
| 2398 | |
| 2399 | } |
| 2400 | |
| 2401 | /* bitmap was successfully extracted or it was empty */ |
| 2402 | pDescr++; |
| 2403 | break; |
| 2404 | } |
| 2405 | |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 2406 | case CSN_PADDING_BITS: |
| 2407 | { /* Padding from here and to the end of message */ |
| 2408 | LOGPC(DCSN1, LOGL_NOTICE, "%s = ", pDescr->sz); |
| 2409 | guint8 filler = 0x2b; |
| 2410 | if (remaining_bits_len > 0) |
| 2411 | { |
| 2412 | while (remaining_bits_len > 0) |
| 2413 | { |
| 2414 | guint8 bits_to_handle = remaining_bits_len%8; |
| 2415 | if (bits_to_handle > 0) |
| 2416 | { |
Saurabh Sharan | 656eed5 | 2016-03-10 14:15:29 +0530 | [diff] [blame] | 2417 | /* section 11 of 44.060 |
| 2418 | * The padding bits may be the 'null' string. Otherwise, the |
| 2419 | * padding bits starts with bit '0', followed by 'spare padding' |
| 2420 | * < padding bits > ::= { null | 0 < spare padding > ! < Ignore : 1 bit** = < no string > > } ; |
| 2421 | */ |
| 2422 | guint8 fl = filler&(0xff>>(8-bits_to_handle + 1)); |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2423 | bitvec_write_field(vector, writeIndex, fl, bits_to_handle); |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 2424 | LOGPC(DCSN1, LOGL_NOTICE, "%u|", fl); |
| 2425 | remaining_bits_len -= bits_to_handle; |
| 2426 | bit_offset += bits_to_handle; |
| 2427 | } |
| 2428 | else if (bits_to_handle == 0) |
| 2429 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2430 | bitvec_write_field(vector, writeIndex, filler, 8); |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 2431 | LOGPC(DCSN1, LOGL_NOTICE, "%u|", filler); |
| 2432 | remaining_bits_len -= 8; |
| 2433 | bit_offset += 8; |
| 2434 | } |
| 2435 | } |
| 2436 | } |
| 2437 | if (remaining_bits_len < 0) |
| 2438 | { |
| 2439 | return ProcessError(writeIndex,"csnStreamDissector", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 2440 | } |
| 2441 | |
| 2442 | /* Padding was successfully extracted or it was empty */ |
| 2443 | pDescr++; |
| 2444 | break; |
| 2445 | } |
| 2446 | |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2447 | case CSN_VARIABLE_ARRAY: |
| 2448 | { /* {int type; int i; void* descr; int offset; const char* sz; } CSN_DESCR; |
| 2449 | * {CSN_VARIABLE_ARRAY, _OFFSET, (void*)offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER} |
| 2450 | * Array with length specified in parameter: |
| 2451 | * <count: bit (x)> |
| 2452 | * <list: octet(count + offset)> |
| 2453 | */ |
| 2454 | gint16 count = *pui8DATA(data, (gint16)pDescr->descr.value); |
| 2455 | |
| 2456 | count += pDescr->i; /* Adjusted by offset */ |
| 2457 | |
| 2458 | if (count > 0) |
| 2459 | { |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2460 | if (remaining_bits_len < 0) |
| 2461 | { |
| 2462 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 2463 | } |
| 2464 | |
| 2465 | pui8 = pui8DATA(data, pDescr->offset); |
| 2466 | |
| 2467 | while (count > 0) |
| 2468 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2469 | bitvec_write_field(vector, writeIndex, *pui8, 8); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2470 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2471 | pui8++; |
| 2472 | bit_offset += 8; |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 2473 | remaining_bits_len -= 8; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2474 | count--; |
| 2475 | } |
| 2476 | } |
| 2477 | |
| 2478 | pDescr++; |
| 2479 | break; |
| 2480 | } |
| 2481 | |
| 2482 | case CSN_RECURSIVE_ARRAY: |
| 2483 | { /* Recursive way to specify an array: <list> ::= {1 <number: bit (4)> <list> | 0} |
| 2484 | * or more generally: <list> ::= { <tag> <element> <list> | <EndTag> } |
| 2485 | * where <element> ::= bit(value) |
| 2486 | * <tag> ::= 0 | 1 |
| 2487 | * <EndTag> ::= reversed tag i.e. tag == 1 -> EndTag == 0 and vice versa |
| 2488 | * {CSN_RECURSIVE_ARRAY, _BITS, (void*)offsetof(_STRUCT, _ElementCountField), offsetof(_STRUCT, _MEMBER), #_MEMBER} |
| 2489 | * REMARK: recursive way to specify an array but an iterative implementation! |
| 2490 | */ |
| 2491 | gint16 no_of_bits = pDescr->i; |
| 2492 | guint8 ElementCount = 0; |
| 2493 | pui8 = pui8DATA(data, pDescr->offset); |
| 2494 | ElementCount = *pui8DATA(data, (gint16)pDescr->descr.value); |
| 2495 | while (ElementCount > 0) |
| 2496 | { /* tag control shows existence of next list elements */ |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2497 | bitvec_write_field(vector, writeIndex, Tag, 1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2498 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)Tag); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2499 | bit_offset++; |
| 2500 | remaining_bits_len--; |
| 2501 | |
| 2502 | /* extract and store no_of_bits long element from bitstream */ |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2503 | bitvec_write_field(vector, writeIndex, *pui8, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2504 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)*pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2505 | pui8++; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2506 | ElementCount--; |
| 2507 | |
| 2508 | if (remaining_bits_len < 0) |
| 2509 | { |
| 2510 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 2511 | } |
| 2512 | |
| 2513 | bit_offset += no_of_bits; |
Jeff Morriss | c0b4f4a | 2020-01-24 15:00:58 +0100 | [diff] [blame] | 2514 | remaining_bits_len -= no_of_bits; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2515 | } |
| 2516 | |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2517 | bitvec_write_field(vector, writeIndex, !Tag, 1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2518 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)(!Tag)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2519 | bit_offset++; |
Saurabh Sharan | 2b09c39 | 2016-03-16 19:17:32 +0530 | [diff] [blame] | 2520 | remaining_bits_len--; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2521 | |
| 2522 | pDescr++; |
| 2523 | break; |
| 2524 | } |
| 2525 | |
| 2526 | case CSN_RECURSIVE_TARRAY: |
| 2527 | { /* Recursive way to specify an array of type: <lists> ::= { 1 <type> } ** 0 ; |
| 2528 | * M_REC_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField) |
| 2529 | * {t, offsetof(_STRUCT, _ElementCountField), (void*)CSNDESCR_##_MEMBER_TYPE, offsetof(_STRUCT, _MEMBER), #_MEMBER, (StreamSerializeFcn_t)sizeof(_MEMBER_TYPE)} |
| 2530 | */ |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 2531 | gint16 nSizeElement = (gint16)(gint32)pDescr->value; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2532 | guint8 ElementCount = 0; |
| 2533 | pui8 = pui8DATA(data, pDescr->offset); |
| 2534 | /* Store the counted number of elements of the array */ |
| 2535 | ElementCount = *pui8DATA(data, (gint16)(gint32)pDescr->i); |
| 2536 | |
| 2537 | while (ElementCount > 0) |
| 2538 | { /* tag control shows existence of next list elements */ |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2539 | bitvec_write_field(vector, writeIndex, Tag, 1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2540 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)Tag); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2541 | bit_offset++; |
| 2542 | |
| 2543 | remaining_bits_len--; |
| 2544 | ElementCount--; |
| 2545 | |
| 2546 | { /* unpack the following data structure */ |
| 2547 | csnStream_t arT = *ar; |
| 2548 | gint16 Status; |
| 2549 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
Ivan Kluchnikov | 4440845 | 2013-03-01 02:46:10 +0400 | [diff] [blame] | 2550 | Status = csnStreamEncoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, writeIndex, pui8); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2551 | |
| 2552 | if (Status >= 0) |
| 2553 | { /* successful completion */ |
| 2554 | pui8 += nSizeElement; /* -> to next data element */ |
| 2555 | remaining_bits_len = arT.remaining_bits_len; |
| 2556 | bit_offset = arT.bit_offset; |
| 2557 | } |
| 2558 | else |
| 2559 | { /* something went awry */ |
| 2560 | return Status; |
| 2561 | } |
| 2562 | } |
| 2563 | |
| 2564 | if (remaining_bits_len < 0) |
| 2565 | { |
| 2566 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 2567 | } |
| 2568 | } |
| 2569 | |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2570 | bitvec_write_field(vector, writeIndex, !Tag, 1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2571 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)(!Tag)); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2572 | bit_offset++; |
| 2573 | |
| 2574 | pDescr++; |
| 2575 | break; |
| 2576 | } |
| 2577 | |
| 2578 | case CSN_RECURSIVE_TARRAY_2: |
| 2579 | { /* Recursive way to specify an array of type: <list> ::= <type> { 0 <type> } ** 1 ; */ |
| 2580 | |
| 2581 | Tag = REVERSED_TAG; |
| 2582 | |
| 2583 | /* NO break - |
| 2584 | * handling is exactly the same as for CSN_RECURSIVE_TARRAY_1 so we continue |
| 2585 | */ |
| 2586 | } |
Alexis La Goutte | e36fb5b | 2020-01-24 18:22:35 +0100 | [diff] [blame] | 2587 | /* FALL THROUGH */ |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2588 | case CSN_RECURSIVE_TARRAY_1: |
| 2589 | { /* Recursive way to specify an array of type: <lists> ::= <type> { 1 <type> } ** 0 ; |
| 2590 | * M_REC_TARRAY(_STRUCT, _MEMBER, _MEMBER_TYPE, _ElementCountField) |
| 2591 | * {t, offsetof(_STRUCT, _ElementCountField), (void*)CSNDESCR_##_MEMBER_TYPE, offsetof(_STRUCT, _MEMBER), #_MEMBER, (StreamSerializeFcn_t)sizeof(_MEMBER_TYPE)} |
| 2592 | */ |
Pau Espin Pedrol | 900c2e2 | 2020-01-24 18:05:00 +0100 | [diff] [blame] | 2593 | gint16 nSizeElement = (gint16)(gint32)pDescr->value; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2594 | guint8 ElementCount = 0; |
| 2595 | guint8 ElementNum = 0; |
| 2596 | csnStream_t arT = *ar; |
| 2597 | gint16 Status; |
| 2598 | |
| 2599 | pui8 = pui8DATA(data, pDescr->offset); |
| 2600 | /* Store the count of the array */ |
| 2601 | ElementCount = *pui8DATA(data, pDescr->i); |
| 2602 | ElementNum = ElementCount; |
| 2603 | |
| 2604 | while (ElementCount > 0) |
| 2605 | { /* get data element */ |
| 2606 | if (ElementCount != ElementNum) |
| 2607 | { |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2608 | bitvec_write_field(vector, writeIndex, Tag, 1); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2609 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)Tag); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2610 | bit_offset++; |
| 2611 | remaining_bits_len--; |
| 2612 | } |
| 2613 | ElementCount--; |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2614 | LOGPC(DCSN1, LOGL_NOTICE, "%s { | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2615 | csnStreamInit(&arT, bit_offset, remaining_bits_len); |
Ivan Kluchnikov | 4440845 | 2013-03-01 02:46:10 +0400 | [diff] [blame] | 2616 | Status = csnStreamEncoder(&arT, (const CSN_DESCR*)pDescr->descr.ptr, vector, writeIndex, pui8); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2617 | LOGPC(DCSN1, LOGL_NOTICE, "%s } | ", pDescr->sz); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2618 | if (Status >= 0) |
| 2619 | { /* successful completion */ |
| 2620 | pui8 += nSizeElement; /* -> to next */ |
| 2621 | remaining_bits_len = arT.remaining_bits_len; |
| 2622 | bit_offset = arT.bit_offset; |
| 2623 | } |
| 2624 | else |
| 2625 | { /* something went awry */ |
| 2626 | return Status; |
| 2627 | } |
| 2628 | |
| 2629 | if (remaining_bits_len < 0) |
| 2630 | { |
| 2631 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 2632 | } |
| 2633 | |
| 2634 | } |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2635 | bitvec_write_field(vector, writeIndex, !Tag, 1); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2636 | bit_offset++; |
Ivan Kluchnikov | 701d9f8 | 2012-10-10 19:43:37 +0400 | [diff] [blame] | 2637 | remaining_bits_len--; |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2638 | Tag = STANDARD_TAG; /* in case it was set to "reversed" */ |
| 2639 | pDescr++; |
| 2640 | break; |
| 2641 | } |
| 2642 | |
| 2643 | case CSN_FIXED: |
| 2644 | { /* Verify the fixed bits */ |
| 2645 | guint8 no_of_bits = (guint8) pDescr->i; |
Vadim Yanitskiy | 39a6505 | 2020-01-25 01:24:59 +0700 | [diff] [blame] | 2646 | bitvec_write_field(vector, writeIndex, pDescr->offset, no_of_bits); |
Ivan Kluchnikov | a9f1ff2 | 2012-05-24 22:25:06 +0400 | [diff] [blame] | 2647 | LOGPC(DCSN1, LOGL_NOTICE, "%s = %u | ", pDescr->sz , (unsigned)pDescr->offset); |
Ivan Kluchnikov | 487a141 | 2011-12-21 13:17:53 +0300 | [diff] [blame] | 2648 | remaining_bits_len -= no_of_bits; |
| 2649 | bit_offset += no_of_bits; |
| 2650 | pDescr++; |
| 2651 | break; |
| 2652 | } |
| 2653 | |
| 2654 | case CSN_CALLBACK: |
| 2655 | { |
| 2656 | return ProcessError(writeIndex,"csnStreamEncoder Callback not implemented", -1, pDescr); |
| 2657 | break; |
| 2658 | } |
| 2659 | |
| 2660 | case CSN_TRAP_ERROR: |
| 2661 | { |
| 2662 | return ProcessError(writeIndex,"csnStreamEncoder", pDescr->i, pDescr); |
| 2663 | } |
| 2664 | |
| 2665 | case CSN_END: |
| 2666 | { |
| 2667 | ar->remaining_bits_len = remaining_bits_len; |
| 2668 | ar->bit_offset = bit_offset; |
| 2669 | return remaining_bits_len; |
| 2670 | } |
| 2671 | |
| 2672 | default: |
| 2673 | { |
| 2674 | assert(0); |
| 2675 | } |
| 2676 | |
| 2677 | } |
| 2678 | |
| 2679 | } while (remaining_bits_len >= 0); |
| 2680 | |
| 2681 | return ProcessError(writeIndex,"csnStreamEncoder", CSN_ERROR_NEED_MORE_BITS_TO_UNPACK, pDescr); |
| 2682 | } |