Major architectural refactoring to achieve 1:1 structural compatibility: 🏗️ **VC2022 Structure Replication** - Iso1EXIDocument: 1:1 replica of VC2022 iso1EXIDocument struct - DinEXIDocument: 1:1 replica of VC2022 dinEXIDocument struct - Iso2EXIDocument: 1:1 replica of VC2022 iso2EXIDocument struct - All _isUsed flags and Initialize() methods exactly matching VC2022 🔄 **VC2022 Function Porting** - ParseXmlToIso1(): Exact port of VC2022 parse_xml_to_iso1() - EncodeIso1ExiDocument(): Exact port of VC2022 encode_iso1ExiDocument() - Choice 76 (V2G_Message) encoding with identical logic - BulkChargingComplete ignore behavior preserved ⚡ **Call Sequence Alignment** - Old: EncodeV2GMessage() → direct EXI encoding - New: EncodeV2GMessage() → Iso1EXIDocument → EncodeIso1ExiDocument() - Exact VC2022 call chain: init → parse → encode → finish 🔍 **1:1 Debug Comparison Ready** - C# exiDoc.V2G_Message_isUsed ↔ VC2022 exiDoc->V2G_Message_isUsed - Identical structure enables line-by-line debugging comparison - Ready for precise 1-byte difference investigation (41 vs 42 bytes) 📁 **Project Reorganization** - Moved from csharp/ to Port/ for cleaner structure - Port/dotnet/ and Port/vc2022/ for parallel development - Complete build system and documentation updates 🎯 **Achievement**: 97.6% binary compatibility (41/42 bytes) Next: 1:1 debug session to identify exact byte difference location 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com>
215 lines
6.4 KiB
C#
215 lines
6.4 KiB
C#
/*
|
|
* 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);
|
|
}
|
|
}
|
|
} |