Add .NET 8.0 C# port of OpenV2G EXI codec
- Port core EXI encoding/decoding functionality to C# - Implement V2G protocol parsing and analysis - Add simplified decoder/encoder for roundtrip testing - Create comprehensive error handling with EXI exceptions - Support both byte array and file stream operations - Include packet structure analysis for V2GTP data - Successfully builds and runs basic functionality tests 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
215
csharp/dotnet/EXI/BitInputStream.cs
Normal file
215
csharp/dotnet/EXI/BitInputStream.cs
Normal file
@@ -0,0 +1,215 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2024 C# Port
|
||||
* Original Copyright (C) 2007-2018 Siemens AG
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
namespace V2GDecoderNet.EXI
|
||||
{
|
||||
/// <summary>
|
||||
/// Bit input stream for reading EXI encoded data
|
||||
/// </summary>
|
||||
public class BitInputStream
|
||||
{
|
||||
private readonly byte[] _buffer;
|
||||
private int _position;
|
||||
private int _bitPosition;
|
||||
private readonly int _size;
|
||||
|
||||
public BitInputStream(byte[] buffer)
|
||||
{
|
||||
_buffer = buffer ?? throw new ArgumentNullException(nameof(buffer));
|
||||
_size = buffer.Length;
|
||||
_position = 0;
|
||||
_bitPosition = 0;
|
||||
}
|
||||
|
||||
public int Position => _position;
|
||||
public int BitPosition => _bitPosition;
|
||||
public int Size => _size;
|
||||
public bool IsEOF => _position >= _size;
|
||||
|
||||
/// <summary>
|
||||
/// Read a single bit
|
||||
/// </summary>
|
||||
/// <returns>Bit value (0 or 1), or -1 on EOF</returns>
|
||||
public int ReadBit()
|
||||
{
|
||||
if (_position >= _size)
|
||||
return -1;
|
||||
|
||||
int bit = (_buffer[_position] >> (7 - _bitPosition)) & 1;
|
||||
|
||||
_bitPosition++;
|
||||
if (_bitPosition == 8)
|
||||
{
|
||||
_bitPosition = 0;
|
||||
_position++;
|
||||
}
|
||||
|
||||
return bit;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read multiple bits as unsigned integer
|
||||
/// </summary>
|
||||
/// <param name="numBits">Number of bits to read (1-32)</param>
|
||||
/// <returns>Unsigned integer value</returns>
|
||||
public uint ReadBits(int numBits)
|
||||
{
|
||||
if (numBits < 1 || numBits > 32)
|
||||
throw new ArgumentException("Number of bits must be between 1 and 32", nameof(numBits));
|
||||
|
||||
uint result = 0;
|
||||
|
||||
for (int i = 0; i < numBits; i++)
|
||||
{
|
||||
int bit = ReadBit();
|
||||
if (bit == -1)
|
||||
throw new EXIException(EXIErrorCodes.EXI_ERROR_INPUT_STREAM_EOF);
|
||||
|
||||
result = (result << 1) | (uint)bit;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read unsigned integer using EXI encoding
|
||||
/// </summary>
|
||||
/// <returns>Unsigned integer value</returns>
|
||||
public uint ReadUnsignedInteger()
|
||||
{
|
||||
uint result = 0;
|
||||
bool continueBit;
|
||||
|
||||
do
|
||||
{
|
||||
if (_position >= _size)
|
||||
throw new EXIException(EXIErrorCodes.EXI_ERROR_INPUT_STREAM_EOF);
|
||||
|
||||
byte currentByte = _buffer[_position++];
|
||||
continueBit = (currentByte & 0x80) != 0;
|
||||
result = (result << 7) | (uint)(currentByte & 0x7F);
|
||||
|
||||
} while (continueBit);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read signed integer using EXI encoding
|
||||
/// </summary>
|
||||
/// <returns>Signed integer value</returns>
|
||||
public int ReadInteger()
|
||||
{
|
||||
uint unsignedValue = ReadUnsignedInteger();
|
||||
|
||||
// Check sign bit (LSB)
|
||||
bool isNegative = (unsignedValue & 1) != 0;
|
||||
int value = (int)(unsignedValue >> 1);
|
||||
|
||||
return isNegative ? -value : value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read a byte aligned to byte boundary
|
||||
/// </summary>
|
||||
/// <returns>Byte value</returns>
|
||||
public byte ReadByte()
|
||||
{
|
||||
// Align to byte boundary
|
||||
if (_bitPosition != 0)
|
||||
{
|
||||
_bitPosition = 0;
|
||||
_position++;
|
||||
}
|
||||
|
||||
if (_position >= _size)
|
||||
throw new EXIException(EXIErrorCodes.EXI_ERROR_INPUT_STREAM_EOF);
|
||||
|
||||
return _buffer[_position++];
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read multiple bytes
|
||||
/// </summary>
|
||||
/// <param name="count">Number of bytes to read</param>
|
||||
/// <returns>Byte array</returns>
|
||||
public byte[] ReadBytes(int count)
|
||||
{
|
||||
if (count < 0)
|
||||
throw new ArgumentException("Count cannot be negative", nameof(count));
|
||||
|
||||
// Align to byte boundary
|
||||
if (_bitPosition != 0)
|
||||
{
|
||||
_bitPosition = 0;
|
||||
_position++;
|
||||
}
|
||||
|
||||
if (_position + count > _size)
|
||||
throw new EXIException(EXIErrorCodes.EXI_ERROR_INPUT_STREAM_EOF);
|
||||
|
||||
var result = new byte[count];
|
||||
Array.Copy(_buffer, _position, result, 0, count);
|
||||
_position += count;
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Skip to next byte boundary
|
||||
/// </summary>
|
||||
public void AlignToByteBank()
|
||||
{
|
||||
if (_bitPosition != 0)
|
||||
{
|
||||
_bitPosition = 0;
|
||||
_position++;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset stream position to beginning
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
_position = 0;
|
||||
_bitPosition = 0;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Set stream position
|
||||
/// </summary>
|
||||
/// <param name="position">Byte position</param>
|
||||
/// <param name="bitPosition">Bit position within byte (0-7)</param>
|
||||
public void SetPosition(int position, int bitPosition = 0)
|
||||
{
|
||||
if (position < 0 || position > _size)
|
||||
throw new ArgumentException("Position out of range", nameof(position));
|
||||
|
||||
if (bitPosition < 0 || bitPosition > 7)
|
||||
throw new ArgumentException("Bit position must be 0-7", nameof(bitPosition));
|
||||
|
||||
_position = position;
|
||||
_bitPosition = bitPosition;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get remaining bytes in stream
|
||||
/// </summary>
|
||||
/// <returns>Number of remaining bytes</returns>
|
||||
public int GetRemainingBytes()
|
||||
{
|
||||
int remaining = _size - _position;
|
||||
if (_bitPosition > 0 && remaining > 0)
|
||||
remaining--;
|
||||
return Math.Max(0, remaining);
|
||||
}
|
||||
}
|
||||
}
|
||||
237
csharp/dotnet/EXI/BitOutputStream.cs
Normal file
237
csharp/dotnet/EXI/BitOutputStream.cs
Normal file
@@ -0,0 +1,237 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2024 C# Port
|
||||
* Original Copyright (C) 2007-2018 Siemens AG
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
namespace V2GDecoderNet.EXI
|
||||
{
|
||||
/// <summary>
|
||||
/// Bit output stream for writing EXI encoded data
|
||||
/// </summary>
|
||||
public class BitOutputStream
|
||||
{
|
||||
private byte[] _buffer;
|
||||
private int _position;
|
||||
private int _bitPosition;
|
||||
private int _capacity;
|
||||
|
||||
public BitOutputStream(int capacity = EXIConstants.BUFFER_SIZE)
|
||||
{
|
||||
_capacity = capacity;
|
||||
_buffer = new byte[capacity];
|
||||
_position = 0;
|
||||
_bitPosition = 0;
|
||||
}
|
||||
|
||||
public int Position => _position;
|
||||
public int BitPosition => _bitPosition;
|
||||
public int Capacity => _capacity;
|
||||
|
||||
/// <summary>
|
||||
/// Write a single bit
|
||||
/// </summary>
|
||||
/// <param name="bit">Bit value (0 or 1)</param>
|
||||
public void WriteBit(int bit)
|
||||
{
|
||||
if (bit != 0 && bit != 1)
|
||||
throw new ArgumentException("Bit value must be 0 or 1", nameof(bit));
|
||||
|
||||
EnsureCapacity(_position + 1);
|
||||
|
||||
if (bit == 1)
|
||||
{
|
||||
_buffer[_position] |= (byte)(1 << (7 - _bitPosition));
|
||||
}
|
||||
|
||||
_bitPosition++;
|
||||
if (_bitPosition == 8)
|
||||
{
|
||||
_bitPosition = 0;
|
||||
_position++;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write multiple bits from unsigned integer
|
||||
/// </summary>
|
||||
/// <param name="value">Value to write</param>
|
||||
/// <param name="numBits">Number of bits to write (1-32)</param>
|
||||
public void WriteBits(uint value, int numBits)
|
||||
{
|
||||
if (numBits < 1 || numBits > 32)
|
||||
throw new ArgumentException("Number of bits must be between 1 and 32", nameof(numBits));
|
||||
|
||||
for (int i = numBits - 1; i >= 0; i--)
|
||||
{
|
||||
int bit = (int)((value >> i) & 1);
|
||||
WriteBit(bit);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write unsigned integer using EXI encoding
|
||||
/// </summary>
|
||||
/// <param name="value">Unsigned integer value</param>
|
||||
public void WriteUnsignedInteger(uint value)
|
||||
{
|
||||
if (value == 0)
|
||||
{
|
||||
WriteByte(0);
|
||||
return;
|
||||
}
|
||||
|
||||
// Calculate number of bytes needed
|
||||
var bytes = new List<byte>();
|
||||
|
||||
while (value > 0)
|
||||
{
|
||||
byte currentByte = (byte)(value & 0x7F);
|
||||
value >>= 7;
|
||||
|
||||
if (value > 0)
|
||||
currentByte |= 0x80; // Set continuation bit
|
||||
|
||||
bytes.Add(currentByte);
|
||||
}
|
||||
|
||||
// Write bytes in reverse order (big-endian)
|
||||
for (int i = bytes.Count - 1; i >= 0; i--)
|
||||
{
|
||||
WriteByte(bytes[i]);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write signed integer using EXI encoding
|
||||
/// </summary>
|
||||
/// <param name="value">Signed integer value</param>
|
||||
public void WriteInteger(int value)
|
||||
{
|
||||
// Encode sign in LSB, shift value
|
||||
uint unsignedValue;
|
||||
if (value < 0)
|
||||
{
|
||||
unsignedValue = ((uint)(-value) << 1) | 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
unsignedValue = (uint)value << 1;
|
||||
}
|
||||
|
||||
WriteUnsignedInteger(unsignedValue);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write a byte aligned to byte boundary
|
||||
/// </summary>
|
||||
/// <param name="value">Byte value</param>
|
||||
public void WriteByte(byte value)
|
||||
{
|
||||
// Align to byte boundary
|
||||
if (_bitPosition != 0)
|
||||
{
|
||||
_bitPosition = 0;
|
||||
_position++;
|
||||
}
|
||||
|
||||
EnsureCapacity(_position + 1);
|
||||
_buffer[_position++] = value;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Write multiple bytes
|
||||
/// </summary>
|
||||
/// <param name="data">Byte array to write</param>
|
||||
public void WriteBytes(byte[] data)
|
||||
{
|
||||
if (data == null || data.Length == 0)
|
||||
return;
|
||||
|
||||
// Align to byte boundary
|
||||
if (_bitPosition != 0)
|
||||
{
|
||||
_bitPosition = 0;
|
||||
_position++;
|
||||
}
|
||||
|
||||
EnsureCapacity(_position + data.Length);
|
||||
Array.Copy(data, 0, _buffer, _position, data.Length);
|
||||
_position += data.Length;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Align to next byte boundary
|
||||
/// </summary>
|
||||
public void AlignToByteBank()
|
||||
{
|
||||
if (_bitPosition != 0)
|
||||
{
|
||||
_bitPosition = 0;
|
||||
_position++;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the written data as byte array
|
||||
/// </summary>
|
||||
/// <returns>Byte array containing written data</returns>
|
||||
public byte[] ToArray()
|
||||
{
|
||||
int length = _position + (_bitPosition > 0 ? 1 : 0);
|
||||
var result = new byte[length];
|
||||
Array.Copy(_buffer, result, length);
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get the current buffer length in bytes
|
||||
/// </summary>
|
||||
/// <returns>Length in bytes</returns>
|
||||
public int GetLength()
|
||||
{
|
||||
return _position + (_bitPosition > 0 ? 1 : 0);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reset the stream position to beginning
|
||||
/// </summary>
|
||||
public void Reset()
|
||||
{
|
||||
_position = 0;
|
||||
_bitPosition = 0;
|
||||
Array.Clear(_buffer, 0, _buffer.Length);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ensure buffer has enough capacity
|
||||
/// </summary>
|
||||
/// <param name="requiredSize">Required size in bytes</param>
|
||||
private void EnsureCapacity(int requiredSize)
|
||||
{
|
||||
if (requiredSize > _capacity)
|
||||
{
|
||||
int newCapacity = Math.Max(_capacity * 2, requiredSize);
|
||||
var newBuffer = new byte[newCapacity];
|
||||
Array.Copy(_buffer, newBuffer, _position);
|
||||
_buffer = newBuffer;
|
||||
_capacity = newCapacity;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get current buffer usage statistics
|
||||
/// </summary>
|
||||
/// <returns>Usage information</returns>
|
||||
public (int UsedBytes, int TotalCapacity, double UsagePercentage) GetUsageStats()
|
||||
{
|
||||
int usedBytes = GetLength();
|
||||
double usage = (double)usedBytes / _capacity * 100.0;
|
||||
return (usedBytes, _capacity, usage);
|
||||
}
|
||||
}
|
||||
}
|
||||
198
csharp/dotnet/EXI/ByteStream.cs
Normal file
198
csharp/dotnet/EXI/ByteStream.cs
Normal file
@@ -0,0 +1,198 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2024 C# Port
|
||||
* Original Copyright (C) 2007-2018 Siemens AG
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
using System.IO;
|
||||
|
||||
namespace V2GDecoderNet.EXI
|
||||
{
|
||||
/// <summary>
|
||||
/// Byte Stream utilities for file operations
|
||||
/// </summary>
|
||||
public static class ByteStream
|
||||
{
|
||||
/// <summary>
|
||||
/// Write bytes to file
|
||||
/// </summary>
|
||||
/// <param name="data">byte array</param>
|
||||
/// <param name="filename">File name</param>
|
||||
/// <returns>Error-Code != 0 on failure</returns>
|
||||
public static int WriteBytesToFile(byte[] data, string filename)
|
||||
{
|
||||
try
|
||||
{
|
||||
if (data == null)
|
||||
return EXIErrorCodes.EXI_ERROR_OUT_OF_BYTE_BUFFER;
|
||||
|
||||
if (string.IsNullOrEmpty(filename))
|
||||
return EXIErrorCodes.EXI_ERROR_OUTPUT_FILE;
|
||||
|
||||
File.WriteAllBytes(filename, data);
|
||||
return 0; // Success
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_OUTPUT_FILE;
|
||||
}
|
||||
catch (DirectoryNotFoundException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_OUTPUT_FILE;
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_OUTPUT_FILE;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_OUTPUT_FILE;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read bytes from file
|
||||
/// </summary>
|
||||
/// <param name="filename">File name</param>
|
||||
/// <param name="data">Output byte array</param>
|
||||
/// <param name="bytesRead">Number of bytes actually read</param>
|
||||
/// <returns>Error-Code != 0 on failure</returns>
|
||||
public static int ReadBytesFromFile(string filename, out byte[] data, out int bytesRead)
|
||||
{
|
||||
data = Array.Empty<byte>();
|
||||
bytesRead = 0;
|
||||
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(filename))
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
|
||||
if (!File.Exists(filename))
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
|
||||
data = File.ReadAllBytes(filename);
|
||||
bytesRead = data.Length;
|
||||
return 0; // Success
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
}
|
||||
catch (DirectoryNotFoundException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read bytes from file with buffer size limit
|
||||
/// </summary>
|
||||
/// <param name="filename">File name</param>
|
||||
/// <param name="maxSize">Maximum buffer size</param>
|
||||
/// <param name="data">Output byte array</param>
|
||||
/// <param name="bytesRead">Number of bytes actually read</param>
|
||||
/// <returns>Error-Code != 0 on failure</returns>
|
||||
public static int ReadBytesFromFile(string filename, int maxSize, out byte[] data, out int bytesRead)
|
||||
{
|
||||
data = Array.Empty<byte>();
|
||||
bytesRead = 0;
|
||||
|
||||
try
|
||||
{
|
||||
if (string.IsNullOrEmpty(filename))
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
|
||||
if (!File.Exists(filename))
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
|
||||
using var fileStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
|
||||
var fileSize = (int)fileStream.Length;
|
||||
|
||||
if (fileSize > maxSize)
|
||||
return EXIErrorCodes.EXI_ERROR_OUT_OF_BYTE_BUFFER;
|
||||
|
||||
data = new byte[fileSize];
|
||||
bytesRead = fileStream.Read(data, 0, fileSize);
|
||||
|
||||
return 0; // Success
|
||||
}
|
||||
catch (UnauthorizedAccessException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
}
|
||||
catch (DirectoryNotFoundException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
}
|
||||
catch (FileNotFoundException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
}
|
||||
catch (IOException)
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
}
|
||||
catch
|
||||
{
|
||||
return EXIErrorCodes.EXI_ERROR_INPUT_FILE_HANDLE;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert hex string to byte array
|
||||
/// </summary>
|
||||
/// <param name="hex">Hex string</param>
|
||||
/// <returns>Byte array</returns>
|
||||
public static byte[] HexStringToByteArray(string hex)
|
||||
{
|
||||
if (string.IsNullOrEmpty(hex))
|
||||
return Array.Empty<byte>();
|
||||
|
||||
// Remove any whitespace or separators
|
||||
hex = hex.Replace(" ", "").Replace("-", "").Replace(":", "");
|
||||
|
||||
if (hex.Length % 2 != 0)
|
||||
throw new ArgumentException("Hex string must have even number of characters");
|
||||
|
||||
var result = new byte[hex.Length / 2];
|
||||
for (int i = 0; i < result.Length; i++)
|
||||
{
|
||||
if (!byte.TryParse(hex.Substring(i * 2, 2), System.Globalization.NumberStyles.HexNumber, null, out result[i]))
|
||||
throw new ArgumentException($"Invalid hex characters at position {i * 2}");
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert byte array to hex string
|
||||
/// </summary>
|
||||
/// <param name="data">Byte array</param>
|
||||
/// <param name="uppercase">Use uppercase hex digits</param>
|
||||
/// <returns>Hex string</returns>
|
||||
public static string ByteArrayToHexString(byte[] data, bool uppercase = true)
|
||||
{
|
||||
if (data == null || data.Length == 0)
|
||||
return string.Empty;
|
||||
|
||||
var format = uppercase ? "X2" : "x2";
|
||||
return string.Concat(data.Select(b => b.ToString(format)));
|
||||
}
|
||||
}
|
||||
}
|
||||
259
csharp/dotnet/EXI/EXITypes.cs
Normal file
259
csharp/dotnet/EXI/EXITypes.cs
Normal file
@@ -0,0 +1,259 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2024 C# Port
|
||||
* Original Copyright (C) 2007-2018 Siemens AG
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
* GNU Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public License
|
||||
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
namespace V2GDecoderNet.EXI
|
||||
{
|
||||
/// <summary>
|
||||
/// Basic type definitions and constants for EXI codec
|
||||
/// </summary>
|
||||
public static class EXIConstants
|
||||
{
|
||||
/// <summary>Number of bits for each byte</summary>
|
||||
public const int BITS_IN_BYTE = 8;
|
||||
|
||||
/// <summary>EXI Date-Time offset for year</summary>
|
||||
public const int DATETIME_YEAR_OFFSET = 2000;
|
||||
|
||||
/// <summary>EXI Date-Time number of bits for monthDay</summary>
|
||||
public const int DATETIME_NUMBER_BITS_MONTHDAY = 9;
|
||||
|
||||
/// <summary>EXI Date-Time number of bits for time</summary>
|
||||
public const int DATETIME_NUMBER_BITS_TIME = 17;
|
||||
|
||||
/// <summary>EXI Date-Time number of bits for timezone</summary>
|
||||
public const int DATETIME_NUMBER_BITS_TIMEZONE = 11;
|
||||
|
||||
/// <summary>EXI Date-Time month multiplicator</summary>
|
||||
public const int DATETIME_MONTH_MULTIPLICATOR = 32;
|
||||
|
||||
/// <summary>EXI Date-Time offset for timezone minutes</summary>
|
||||
public const int DATETIME_TIMEZONE_OFFSET_IN_MINUTES = 896;
|
||||
|
||||
/// <summary>Maximum integer value for uint</summary>
|
||||
public const int UINT_MAX_VALUE = 65535;
|
||||
|
||||
/// <summary>EXI Float exponent special values</summary>
|
||||
public const int FLOAT_EXPONENT_SPECIAL_VALUES = -16384;
|
||||
|
||||
/// <summary>EXI Float mantissa infinity</summary>
|
||||
public const long FLOAT_MANTISSA_INFINITY = 1;
|
||||
|
||||
/// <summary>EXI Float minus mantissa infinity</summary>
|
||||
public const long FLOAT_MANTISSA_MINUS_INFINITY = -1;
|
||||
|
||||
/// <summary>EXI Float not a number</summary>
|
||||
public const long FLOAT_MANTISSA_NOT_A_NUMBER = 0;
|
||||
|
||||
/// <summary>Maximum number of cascading elements, XML tree depth</summary>
|
||||
public const int EXI_ELEMENT_STACK_SIZE = 24;
|
||||
|
||||
/// <summary>Default buffer size</summary>
|
||||
public const int BUFFER_SIZE = 4096;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EXI Events enumeration
|
||||
/// </summary>
|
||||
public enum EXIEvent
|
||||
{
|
||||
/// <summary>Start Document SD</summary>
|
||||
START_DOCUMENT,
|
||||
/// <summary>End Document ED</summary>
|
||||
END_DOCUMENT,
|
||||
/// <summary>Start Element SE(qname)</summary>
|
||||
START_ELEMENT,
|
||||
/// <summary>Start Element SE(uri:*)</summary>
|
||||
START_ELEMENT_NS,
|
||||
/// <summary>Start Element SE(*) generic</summary>
|
||||
START_ELEMENT_GENERIC,
|
||||
/// <summary>Start Element SE(*) generic undeclared</summary>
|
||||
START_ELEMENT_GENERIC_UNDECLARED,
|
||||
/// <summary>End Element EE</summary>
|
||||
END_ELEMENT,
|
||||
/// <summary>End Element EE undeclared</summary>
|
||||
END_ELEMENT_UNDECLARED,
|
||||
/// <summary>Characters CH</summary>
|
||||
CHARACTERS,
|
||||
/// <summary>Characters CH generic</summary>
|
||||
CHARACTERS_GENERIC,
|
||||
/// <summary>Attribute AT(qname)</summary>
|
||||
ATTRIBUTE,
|
||||
/// <summary>Attribute AT(uri:*)</summary>
|
||||
ATTRIBUTE_NS,
|
||||
/// <summary>Attribute AT(*) generic</summary>
|
||||
ATTRIBUTE_GENERIC,
|
||||
/// <summary>Attribute AT(*) generic undeclared</summary>
|
||||
ATTRIBUTE_GENERIC_UNDECLARED,
|
||||
/// <summary>Attribute AT(xsi:type)</summary>
|
||||
ATTRIBUTE_XSI_TYPE,
|
||||
/// <summary>Attribute AT(xsi:nil)</summary>
|
||||
ATTRIBUTE_XSI_NIL,
|
||||
/// <summary>Self Contained SC</summary>
|
||||
SELF_CONTAINED,
|
||||
/// <summary>Entity Reference ER</summary>
|
||||
ENTITY_REFERENCE,
|
||||
/// <summary>Comment CM</summary>
|
||||
COMMENT,
|
||||
/// <summary>Processing Instruction PI</summary>
|
||||
PROCESSING_INSTRUCTION,
|
||||
/// <summary>Document Type Definition DTD</summary>
|
||||
DOCTYPE_DECLARATION,
|
||||
/// <summary>Namespace Declaration NS</summary>
|
||||
NAMESPACE_DECLARATION
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EXI Integer types
|
||||
/// </summary>
|
||||
public enum EXIIntegerType
|
||||
{
|
||||
UNSIGNED_INTEGER_8,
|
||||
UNSIGNED_INTEGER_16,
|
||||
UNSIGNED_INTEGER_32,
|
||||
UNSIGNED_INTEGER_64,
|
||||
INTEGER_8,
|
||||
INTEGER_16,
|
||||
INTEGER_32,
|
||||
INTEGER_64,
|
||||
UNSIGNED_INTEGER_BIG
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EXI String types
|
||||
/// </summary>
|
||||
public enum EXIStringType
|
||||
{
|
||||
ASCII,
|
||||
UTF8,
|
||||
UTF16
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Configuration settings for EXI processing
|
||||
/// </summary>
|
||||
public class EXIConfig
|
||||
{
|
||||
/// <summary>Stream type configuration</summary>
|
||||
public enum StreamType
|
||||
{
|
||||
BYTE_ARRAY = 1,
|
||||
FILE_STREAM = 2
|
||||
}
|
||||
|
||||
/// <summary>Memory allocation mode</summary>
|
||||
public enum MemoryAllocation
|
||||
{
|
||||
STATIC_ALLOCATION = 1,
|
||||
DYNAMIC_ALLOCATION = 2
|
||||
}
|
||||
|
||||
/// <summary>String representation mode</summary>
|
||||
public enum StringRepresentation
|
||||
{
|
||||
ASCII = 1,
|
||||
UCS = 2
|
||||
}
|
||||
|
||||
public StreamType Stream { get; set; } = StreamType.BYTE_ARRAY;
|
||||
public MemoryAllocation Memory { get; set; } = MemoryAllocation.DYNAMIC_ALLOCATION;
|
||||
public StringRepresentation Strings { get; set; } = StringRepresentation.UCS;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EXI Integer value holder
|
||||
/// </summary>
|
||||
public class EXIInteger
|
||||
{
|
||||
public EXIIntegerType Type { get; set; }
|
||||
public ulong Value { get; set; }
|
||||
|
||||
public EXIInteger(EXIIntegerType type, ulong value)
|
||||
{
|
||||
Type = type;
|
||||
Value = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EXI String value holder
|
||||
/// </summary>
|
||||
public class EXIString
|
||||
{
|
||||
public EXIStringType Type { get; set; }
|
||||
public byte[] Data { get; set; }
|
||||
public int Length { get; set; }
|
||||
|
||||
public EXIString(byte[] data, EXIStringType type = EXIStringType.UTF8)
|
||||
{
|
||||
Data = data ?? throw new ArgumentNullException(nameof(data));
|
||||
Length = data.Length;
|
||||
Type = type;
|
||||
}
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return Type switch
|
||||
{
|
||||
EXIStringType.ASCII => System.Text.Encoding.ASCII.GetString(Data, 0, Length),
|
||||
EXIStringType.UTF8 => System.Text.Encoding.UTF8.GetString(Data, 0, Length),
|
||||
EXIStringType.UTF16 => System.Text.Encoding.Unicode.GetString(Data, 0, Length),
|
||||
_ => System.Text.Encoding.UTF8.GetString(Data, 0, Length)
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Bitstream for EXI encoding/decoding operations
|
||||
/// </summary>
|
||||
public class Bitstream
|
||||
{
|
||||
public byte[] Buffer { get; set; }
|
||||
public int Position { get; set; }
|
||||
public int BitPosition { get; set; }
|
||||
public int Size { get; set; }
|
||||
|
||||
public Bitstream(int size = EXIConstants.BUFFER_SIZE)
|
||||
{
|
||||
Buffer = new byte[size];
|
||||
Size = size;
|
||||
Position = 0;
|
||||
BitPosition = 0;
|
||||
}
|
||||
|
||||
public Bitstream(byte[] data)
|
||||
{
|
||||
Buffer = data ?? throw new ArgumentNullException(nameof(data));
|
||||
Size = data.Length;
|
||||
Position = 0;
|
||||
BitPosition = 0;
|
||||
}
|
||||
|
||||
public void Reset()
|
||||
{
|
||||
Position = 0;
|
||||
BitPosition = 0;
|
||||
}
|
||||
|
||||
public byte[] ToArray()
|
||||
{
|
||||
var result = new byte[Position + (BitPosition > 0 ? 1 : 0)];
|
||||
Array.Copy(Buffer, result, result.Length);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
132
csharp/dotnet/EXI/ErrorCodes.cs
Normal file
132
csharp/dotnet/EXI/ErrorCodes.cs
Normal file
@@ -0,0 +1,132 @@
|
||||
/*
|
||||
* Copyright (C) 2007-2024 C# Port
|
||||
* Original Copyright (C) 2007-2018 Siemens AG
|
||||
*
|
||||
* This program is free software: you can redistribute it and/or modify
|
||||
* it under the terms of the GNU Lesser General Public License as published
|
||||
* by the Free Software Foundation, either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*/
|
||||
|
||||
namespace V2GDecoderNet.EXI
|
||||
{
|
||||
/// <summary>
|
||||
/// EXI Error Codes definitions
|
||||
/// </summary>
|
||||
public static class EXIErrorCodes
|
||||
{
|
||||
// Stream errors
|
||||
public const int EXI_ERROR_INPUT_STREAM_EOF = -10;
|
||||
public const int EXI_ERROR_OUTPUT_STREAM_EOF = -11;
|
||||
public const int EXI_ERROR_INPUT_FILE_HANDLE = -12;
|
||||
public const int EXI_ERROR_OUTPUT_FILE = -13;
|
||||
|
||||
// Buffer errors
|
||||
public const int EXI_ERROR_OUT_OF_BOUNDS = -100;
|
||||
public const int EXI_ERROR_OUT_OF_STRING_BUFFER = -101;
|
||||
public const int EXI_ERROR_OUT_OF_BYTE_BUFFER = -103;
|
||||
public const int EXI_ERROR_OUT_OF_GRAMMAR_STACK = -104;
|
||||
public const int EXI_ERROR_OUT_OF_RUNTIME_GRAMMAR_STACK = -105;
|
||||
public const int EXI_ERROR_OUT_OF_QNAMES = -106;
|
||||
|
||||
// Grammar errors
|
||||
public const int EXI_ERROR_UNKOWN_GRAMMAR_ID = -108;
|
||||
public const int EXI_ERROR_UNKOWN_EVENT = -109;
|
||||
public const int EXI_ERROR_UNKOWN_EVENT_CODE = -110;
|
||||
public const int EXI_ERROR_UNEXPECTED_EVENT_LEVEL1 = -111;
|
||||
public const int EXI_ERROR_UNEXPECTED_EVENT_LEVEL2 = -112;
|
||||
|
||||
// Document structure errors
|
||||
public const int EXI_ERROR_UNEXPECTED_START_DOCUMENT = -113;
|
||||
public const int EXI_ERROR_UNEXPECTED_END_DOCUMENT = -114;
|
||||
public const int EXI_ERROR_UNEXPECTED_START_ELEMENT = -115;
|
||||
public const int EXI_ERROR_UNEXPECTED_START_ELEMENT_NS = -116;
|
||||
public const int EXI_ERROR_UNEXPECTED_START_ELEMENT_GENERIC = -117;
|
||||
public const int EXI_ERROR_UNEXPECTED_START_ELEMENT_GENERIC_UNDECLARED = -118;
|
||||
public const int EXI_ERROR_UNEXPECTED_END_ELEMENT = -119;
|
||||
public const int EXI_ERROR_UNEXPECTED_CHARACTERS = -120;
|
||||
public const int EXI_ERROR_UNEXPECTED_ATTRIBUTE = -121;
|
||||
public const int EXI_ERROR_UNEXPECTED_ATTRIBUTE_NS = -122;
|
||||
public const int EXI_ERROR_UNEXPECTED_ATTRIBUTE_GENERIC = -123;
|
||||
public const int EXI_ERROR_UNEXPECTED_ATTRIBUTE_GENERIC_UNDECLARED = -124;
|
||||
public const int EXI_ERROR_UNEXPECTED_ATTRIBUTE_XSI_TYPE = -125;
|
||||
public const int EXI_ERROR_UNEXPECTED_ATTRIBUTE_XSI_NIL = -126;
|
||||
public const int EXI_ERROR_UNEXPECTED_GRAMMAR_ID = -127;
|
||||
public const int EXI_ERROR_UNEXPECTED_ATTRIBUTE_MOVE_TO_CONTENT_RULE = -128;
|
||||
|
||||
// Unsupported features
|
||||
public const int EXI_UNSUPPORTED_NBIT_INTEGER_LENGTH = -132;
|
||||
public const int EXI_UNSUPPORTED_EVENT_CODE_CHARACTERISTICS = -133;
|
||||
public const int EXI_UNSUPPORTED_INTEGER_VALUE = -134;
|
||||
public const int EXI_NEGATIVE_UNSIGNED_INTEGER_VALUE = -135;
|
||||
public const int EXI_UNSUPPORTED_LIST_VALUE_TYPE = -136;
|
||||
public const int EXI_UNSUPPORTED_HEADER_COOKIE = -137;
|
||||
public const int EXI_UNSUPPORTED_HEADER_OPTIONS = -138;
|
||||
public const int EXI_UNSUPPORTED_GLOBAL_ATTRIBUTE_VALUE_TYPE = -139;
|
||||
public const int EXI_UNSUPPORTED_DATATYPE = -140;
|
||||
public const int EXI_UNSUPPORTED_STRING_VALUE_TYPE = -141;
|
||||
public const int EXI_UNSUPPORTED_INTEGER_VALUE_TYPE = -142;
|
||||
public const int EXI_UNSUPPORTED_DATETIME_TYPE = -143;
|
||||
public const int EXI_UNSUPPORTED_FRAGMENT_ELEMENT = -144;
|
||||
public const int EXI_UNSUPPORTED_GRAMMAR_LEARNING_CH = -150;
|
||||
|
||||
// String values errors
|
||||
public const int EXI_ERROR_STRINGVALUES_NOT_SUPPORTED = -160;
|
||||
public const int EXI_ERROR_STRINGVALUES_OUT_OF_ENTRIES = -161;
|
||||
public const int EXI_ERROR_STRINGVALUES_OUT_OF_MEMORY = -162;
|
||||
public const int EXI_ERROR_STRINGVALUES_OUT_OF_BOUND = -163;
|
||||
public const int EXI_ERROR_STRINGVALUES_CHARACTER = -164;
|
||||
|
||||
// Value errors
|
||||
public const int EXI_ERROR_UNEXPECTED_BYTE_VALUE = -200;
|
||||
|
||||
// Conversion errors
|
||||
public const int EXI_ERROR_CONVERSION_NO_ASCII_CHARACTERS = -300;
|
||||
public const int EXI_ERROR_CONVERSION_TYPE_TO_STRING = -301;
|
||||
|
||||
// Support errors
|
||||
public const int EXI_DEVIANT_SUPPORT_NOT_DEPLOYED = -500;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// EXI Exception for error handling
|
||||
/// </summary>
|
||||
public class EXIException : Exception
|
||||
{
|
||||
public int ErrorCode { get; }
|
||||
|
||||
public EXIException(int errorCode) : base(GetErrorMessage(errorCode))
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
}
|
||||
|
||||
public EXIException(int errorCode, string message) : base(message)
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
}
|
||||
|
||||
public EXIException(int errorCode, string message, Exception innerException)
|
||||
: base(message, innerException)
|
||||
{
|
||||
ErrorCode = errorCode;
|
||||
}
|
||||
|
||||
private static string GetErrorMessage(int errorCode)
|
||||
{
|
||||
return errorCode switch
|
||||
{
|
||||
EXIErrorCodes.EXI_ERROR_INPUT_STREAM_EOF => "Input stream EOF",
|
||||
EXIErrorCodes.EXI_ERROR_OUTPUT_STREAM_EOF => "Output stream EOF",
|
||||
EXIErrorCodes.EXI_ERROR_OUT_OF_BOUNDS => "Out of bounds",
|
||||
EXIErrorCodes.EXI_ERROR_OUT_OF_STRING_BUFFER => "Out of string buffer",
|
||||
EXIErrorCodes.EXI_ERROR_OUT_OF_BYTE_BUFFER => "Out of byte buffer",
|
||||
EXIErrorCodes.EXI_ERROR_UNKOWN_GRAMMAR_ID => "Unknown grammar ID",
|
||||
EXIErrorCodes.EXI_ERROR_UNKOWN_EVENT => "Unknown event",
|
||||
EXIErrorCodes.EXI_ERROR_UNEXPECTED_START_DOCUMENT => "Unexpected start document",
|
||||
EXIErrorCodes.EXI_ERROR_UNEXPECTED_END_DOCUMENT => "Unexpected end document",
|
||||
EXIErrorCodes.EXI_UNSUPPORTED_DATATYPE => "Unsupported datatype",
|
||||
_ => $"EXI error code: {errorCode}"
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user