[22] | 1 | /**
|
---|
| 2 | * @file Decoders.h
|
---|
| 3 | * Provides a set of TTCN-3 recoder decoder methods
|
---|
| 4 | * @author Yann garcia
|
---|
| 5 | * @version 1.0
|
---|
| 6 | * @date 20/07/2009
|
---|
| 7 | */
|
---|
| 8 |
|
---|
| 9 | #ifndef STF_370_Decoders
|
---|
| 10 | #define STF_370_Decoders
|
---|
| 11 |
|
---|
| 12 | #include <string>
|
---|
| 13 | #include <iostream>
|
---|
| 14 | #include <iomanip>
|
---|
| 15 |
|
---|
| 16 | #include "Singleton.h"
|
---|
| 17 |
|
---|
| 18 | /*! Class Decoders
|
---|
| 19 | * \brief This class provides a set of convesion methods. It shall be used with Singleton class.
|
---|
| 20 | */
|
---|
| 21 | class Decoders : public Singleton<Decoders>
|
---|
| 22 | {
|
---|
| 23 | private:
|
---|
| 24 | //! Singleton implementation
|
---|
| 25 | friend class Singleton<Decoders>;
|
---|
| 26 |
|
---|
| 27 | //! Convertion map from char to int.
|
---|
| 28 | std::map<char, int> m_char2digits;
|
---|
| 29 |
|
---|
| 30 | //! Decoders.
|
---|
| 31 | /*! Default ctor.
|
---|
| 32 | */
|
---|
| 33 | Decoders()
|
---|
| 34 | {
|
---|
| 35 | m_char2digits['0'] = 0x00;
|
---|
| 36 | m_char2digits['1'] = 0x01;
|
---|
| 37 | m_char2digits['2'] = 0x02;
|
---|
| 38 | m_char2digits['3'] = 0x03;
|
---|
| 39 | m_char2digits['4'] = 0x04;
|
---|
| 40 | m_char2digits['5'] = 0x05;
|
---|
| 41 | m_char2digits['6'] = 0x06;
|
---|
| 42 | m_char2digits['7'] = 0x07;
|
---|
| 43 | m_char2digits['8'] = 0x08;
|
---|
| 44 | m_char2digits['9'] = 0x09;
|
---|
| 45 | m_char2digits['a'] = 0x0a;
|
---|
| 46 | m_char2digits['b'] = 0x0b;
|
---|
| 47 | m_char2digits['c'] = 0x0c;
|
---|
| 48 | m_char2digits['d'] = 0x0d;
|
---|
| 49 | m_char2digits['e'] = 0x0e;
|
---|
| 50 | m_char2digits['f'] = 0x0f;
|
---|
| 51 | };
|
---|
| 52 |
|
---|
| 53 | public:
|
---|
| 54 | //! DecodeByte.
|
---|
| 55 | /*! Decode a byte, one byte without length.
|
---|
| 56 | * @param frame: The hexadecimal string
|
---|
| 57 | * @param [out/out] poffset: The current decoder index
|
---|
| 58 | * @return The message type.
|
---|
| 59 | */
|
---|
| 60 | unsigned char DecodeByte(std::string frame, int *poffset)
|
---|
| 61 | {
|
---|
| 62 | // Sanity checks.
|
---|
| 63 | if (frame.length() < (*poffset + 2))
|
---|
| 64 | {
|
---|
| 65 | return 0xff;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | // Extract the length.
|
---|
| 69 | try
|
---|
| 70 | {
|
---|
| 71 | return static_cast<unsigned char>(m_char2digits[tolower(frame[(*poffset)++])] << 4 | m_char2digits[tolower(frame[(*poffset)++])]);
|
---|
| 72 | }
|
---|
| 73 | catch (const std::exception& e)
|
---|
| 74 | {
|
---|
| 75 | return 0xff;
|
---|
| 76 | }
|
---|
| 77 | };
|
---|
| 78 |
|
---|
| 79 | //! DecodeLengthPlusString.
|
---|
| 80 | /*! Decode a bytes sequence <length> + <string>, with <length> on 2 bytes.
|
---|
| 81 | * @param frame: The hexadecimal string
|
---|
| 82 | * @param [out/out] poffset: The current decoder index
|
---|
| 83 | * @param [out] pLength: The length of the decoded string.
|
---|
| 84 | * @return The real string on success, an empty string otherwise. On error, *pLength is set to -1.
|
---|
| 85 | */
|
---|
| 86 | std::string DecodeLengthPlusString(std::string frame, int *pOffset, int *pLength)
|
---|
| 87 | {
|
---|
| 88 | *pLength = -1;
|
---|
| 89 |
|
---|
| 90 | // Sanity checks.
|
---|
| 91 | if (frame.length() < (*pOffset + 2 * sizeof(short)))
|
---|
| 92 | {
|
---|
| 93 | std::cerr << "DecodeLengthPlusString: bad offset value: " << *pOffset << std::endl;
|
---|
| 94 | return std::string("");
|
---|
| 95 | }
|
---|
| 96 |
|
---|
| 97 | try
|
---|
| 98 | {
|
---|
| 99 | // Extract the length.
|
---|
| 100 | int ended = *pOffset + 2 * sizeof(short);
|
---|
| 101 | //std::clog << "DecodeLengthPlusString: ended: " << ended << std::endl;
|
---|
| 102 | *pLength = 0;
|
---|
| 103 | for (std::string::const_iterator it = frame.begin() + *pOffset; it != frame.begin() + ended; ++it)
|
---|
| 104 | {
|
---|
| 105 | if ((*it == '0') && (*(it + 1) == '0'))
|
---|
| 106 | {
|
---|
| 107 | it += 1;
|
---|
| 108 | }
|
---|
| 109 | else
|
---|
| 110 | {
|
---|
| 111 | *pLength <<= 8;
|
---|
| 112 | *pLength |= static_cast<unsigned char>(m_char2digits[tolower(*it++)] << 4 | m_char2digits[tolower(*it)]);
|
---|
| 113 | }
|
---|
| 114 | *pOffset += 2;
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | //std::cout << "DecodeLengthPlusString: length value:" << *pLength << " - " << *pOffset << std::endl;
|
---|
| 118 | if (*pLength == 0)
|
---|
| 119 | {
|
---|
| 120 | std::cerr << "DecodeLengthPlusString: length is null" << std::endl;
|
---|
| 121 | return std::string("");
|
---|
| 122 | }
|
---|
| 123 |
|
---|
| 124 | std::ostringstream hexStream;
|
---|
| 125 | //hexStream << std:setw(2);
|
---|
| 126 | ended = *pOffset + 2 * *pLength;
|
---|
| 127 | for (std::string::const_iterator it = frame.begin() + *pOffset; it != frame.begin() + ended; ++it)
|
---|
| 128 | {
|
---|
| 129 | hexStream << static_cast<char>(m_char2digits[tolower(*it++)] << 4 | m_char2digits[tolower(*it)]);
|
---|
| 130 | *pOffset += 2;
|
---|
| 131 | }
|
---|
| 132 |
|
---|
| 133 | return hexStream.str();
|
---|
| 134 | }
|
---|
| 135 | catch (const std::exception& e)
|
---|
| 136 | {
|
---|
| 137 | *pLength = -1;
|
---|
| 138 | return std::string("");
|
---|
| 139 | }
|
---|
| 140 | };
|
---|
| 141 |
|
---|
| 142 | //! DecodeUInt16.
|
---|
| 143 | /*! Decode a bytes sequence <Integer> on 2 bytes.
|
---|
| 144 | * @param frame: The hexadecimal string
|
---|
| 145 | * @param [out/out] pOffset: The current decoder index
|
---|
| 146 | * @return The number of parameters value on success, -1 otherwise.
|
---|
| 147 | */
|
---|
| 148 | int DecodeUInt16(std::string frame, int *pOffset)
|
---|
| 149 | {
|
---|
| 150 | // Sanity checks.
|
---|
| 151 | if (frame.length() < (*pOffset + 2 * sizeof(short)))
|
---|
| 152 | {
|
---|
| 153 | std::cerr << "DecodeUInt16: bad offset value: " << *pOffset << std::endl;
|
---|
| 154 | return -1;
|
---|
| 155 | }
|
---|
| 156 |
|
---|
| 157 | try
|
---|
| 158 | {
|
---|
| 159 | // Extract the length.
|
---|
| 160 | int ended = *pOffset + 2 * sizeof(short);
|
---|
| 161 | //std::clog << "DecodeUInt16: ended: " << ended << std::endl;
|
---|
| 162 | int conv = 0;
|
---|
| 163 | for (std::string::const_iterator it = frame.begin() + *pOffset; it != frame.begin() + ended; ++it)
|
---|
| 164 | {
|
---|
| 165 | if ((*it == '0') && (*(it + 1) == '0'))
|
---|
| 166 | {
|
---|
| 167 | it += 1;
|
---|
| 168 | }
|
---|
| 169 | else
|
---|
| 170 | {
|
---|
| 171 | conv <<= 8;
|
---|
| 172 | conv |= static_cast<unsigned char>(m_char2digits[tolower(*it++)] << 4 | m_char2digits[tolower(*it)]);
|
---|
| 173 | }
|
---|
| 174 | *pOffset += 2;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
| 177 |
|
---|
| 178 | return conv;
|
---|
| 179 | }
|
---|
| 180 | catch (const std::exception& e)
|
---|
| 181 | {
|
---|
| 182 | return -1;
|
---|
| 183 | }
|
---|
| 184 | };
|
---|
| 185 |
|
---|
| 186 | //! DecodeParamsNum.
|
---|
| 187 | /*! Decode a bytes sequence <Integer> on 2 bytes.
|
---|
| 188 | * @param frame: The hexadecimal string
|
---|
| 189 | * @param [out/out] pOffset: The current decoder index
|
---|
| 190 | * @return The number of parameters value on success, -1 otherwise.
|
---|
| 191 | */
|
---|
| 192 | int DecodeParamsNum(std::string frame, int *pOffset)
|
---|
| 193 | {
|
---|
| 194 | return DecodeUInt16(frame, pOffset);
|
---|
| 195 | };
|
---|
| 196 |
|
---|
| 197 | //! DecodeInteger.
|
---|
| 198 | /*! Decode a bytes sequence <Integer> on 8 bytes.
|
---|
| 199 | * @param frame: The hexadecimal string
|
---|
| 200 | * @param [out/out] pOffset: The current decoder index
|
---|
| 201 | * @return The integer value on success, -1 otherwise.
|
---|
| 202 | */
|
---|
| 203 | int DecodeInteger(std::string frame, int *pOffset)
|
---|
| 204 | {
|
---|
| 205 | // Sanity checks.
|
---|
| 206 | if (frame.length() < (*pOffset + 2 * sizeof(long long)))
|
---|
| 207 | {
|
---|
| 208 | return -1;
|
---|
| 209 | }
|
---|
| 210 |
|
---|
| 211 | // Extract the length.
|
---|
| 212 | try
|
---|
| 213 | {
|
---|
| 214 | int ended = *pOffset + 2 * sizeof(long long);
|
---|
| 215 | int conv = 0;
|
---|
| 216 | for (std::string::const_iterator it = frame.begin() + *pOffset; it != frame.begin() + ended; ++it)
|
---|
| 217 | {
|
---|
| 218 | conv <<= 8;
|
---|
| 219 | conv |= static_cast<unsigned char>(m_char2digits[tolower(*it++)] << 4 | m_char2digits[tolower(*it)]);
|
---|
| 220 |
|
---|
| 221 | *pOffset += 2;
|
---|
| 222 | }
|
---|
| 223 |
|
---|
| 224 | return conv;
|
---|
| 225 | }
|
---|
| 226 | catch (const std::exception& e)
|
---|
| 227 | {
|
---|
| 228 | return -1;
|
---|
| 229 | }
|
---|
| 230 | };
|
---|
| 231 |
|
---|
| 232 | //! DecodeUInt32.
|
---|
| 233 | /*! Decode a bytes sequence <Integer> on 4 bytes.
|
---|
| 234 | * @param frame: The hexadecimal string
|
---|
| 235 | * @param [out/out] pOffset: The current decoder index
|
---|
| 236 | * @return The integer value on success, -1 otherwise.
|
---|
| 237 | */
|
---|
| 238 | int DecodeUInt32(std::string frame, int *pOffset)
|
---|
| 239 | {
|
---|
| 240 | // Sanity checks.
|
---|
| 241 | if (frame.length() < (*pOffset + 2 * sizeof(int)))
|
---|
| 242 | {
|
---|
| 243 | return -1;
|
---|
| 244 | }
|
---|
| 245 |
|
---|
| 246 | // Extract the length.
|
---|
| 247 | try
|
---|
| 248 | {
|
---|
| 249 | int ended = *pOffset + 2 * sizeof(int);
|
---|
| 250 | int conv = 0;
|
---|
| 251 | for (std::string::const_iterator it = frame.begin() + *pOffset; it != frame.begin() + ended; ++it)
|
---|
| 252 | {
|
---|
| 253 | conv <<= 8;
|
---|
| 254 | conv |= static_cast<unsigned char>(m_char2digits[tolower(*it++)] << 4 | m_char2digits[tolower(*it)]);
|
---|
| 255 |
|
---|
| 256 | *pOffset += 2;
|
---|
| 257 | }
|
---|
| 258 |
|
---|
| 259 | return conv;
|
---|
| 260 | }
|
---|
| 261 | catch (const std::exception& e)
|
---|
| 262 | {
|
---|
| 263 | return -1;
|
---|
| 264 | }
|
---|
| 265 | };
|
---|
| 266 |
|
---|
| 267 | //! DecodeEnumerated.
|
---|
| 268 | /*! Decode a bytes sequence <length> + <Integer>.
|
---|
| 269 | * @param frame: The hexadecimal string
|
---|
| 270 | * @param [out/out] pOffset: The current decoder index
|
---|
| 271 | * @return The byte value on success, 0xff otherwise.
|
---|
| 272 | */
|
---|
| 273 | unsigned char DecodeEnumerated(std::string frame, int *pOffset)
|
---|
| 274 | {
|
---|
| 275 | // Sanity checks.
|
---|
| 276 | if (frame.length() < (*pOffset + 2))
|
---|
| 277 | {
|
---|
| 278 | return 0xff;
|
---|
| 279 | }
|
---|
| 280 |
|
---|
| 281 | // Extract the length.
|
---|
| 282 | try
|
---|
| 283 | {
|
---|
| 284 | return static_cast<unsigned char>(m_char2digits[tolower(frame[(*pOffset)++])] << 4 | m_char2digits[tolower(frame[(*pOffset)++])]);
|
---|
| 285 | }
|
---|
| 286 | catch (const std::exception& e)
|
---|
| 287 | {
|
---|
| 288 | return 0xff;
|
---|
| 289 | }
|
---|
| 290 | };
|
---|
| 291 | };
|
---|
| 292 |
|
---|
| 293 | #endif // STF_370_Decoders
|
---|