Files
V2GDecoderC/Port/dotnet/V2G/EXIDecoder.cs
chiDT d5263abab0 feat: Perfect C# structure alignment with VC2022 for exact debugging
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>
2025-09-10 22:01:08 +09:00

263 lines
9.0 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.
*/
using V2GDecoderNet.EXI;
using System.Text;
using System.Xml;
namespace V2GDecoderNet.V2G
{
/// <summary>
/// EXI Decoder for converting EXI binary data to XML
/// </summary>
public class EXIDecoder
{
private readonly EXIConfig _config;
public EXIDecoder(EXIConfig? config = null)
{
_config = config ?? new EXIConfig();
}
/// <summary>
/// Decode EXI binary data to XML string
/// </summary>
/// <param name="exiData">EXI binary data</param>
/// <returns>XML string representation</returns>
public string DecodeToXml(byte[] exiData)
{
if (exiData == null || exiData.Length == 0)
throw new ArgumentException("EXI data cannot be null or empty", nameof(exiData));
var inputStream = new BitInputStream(exiData);
var xmlBuilder = new StringBuilder();
try
{
DecodeDocument(inputStream, xmlBuilder);
return xmlBuilder.ToString();
}
catch (EXIException)
{
throw;
}
catch (Exception ex)
{
throw new EXIException(EXIErrorCodes.EXI_ERROR_UNKOWN_EVENT,
"Error during EXI decoding", ex);
}
}
/// <summary>
/// Decode EXI binary data to XmlDocument
/// </summary>
/// <param name="exiData">EXI binary data</param>
/// <returns>XmlDocument</returns>
public XmlDocument DecodeToXmlDocument(byte[] exiData)
{
string xmlString = DecodeToXml(exiData);
var xmlDoc = new XmlDocument();
xmlDoc.LoadXml(xmlString);
return xmlDoc;
}
/// <summary>
/// Validate EXI header and extract options
/// </summary>
/// <param name="inputStream">Input bit stream</param>
/// <returns>EXI header information</returns>
public EXIHeader DecodeHeader(BitInputStream inputStream)
{
var header = new EXIHeader();
// Check for EXI cookie ($EXI)
byte[] cookie = inputStream.ReadBytes(4);
if (cookie[0] != '$' || cookie[1] != 'E' || cookie[2] != 'X' || cookie[3] != 'I')
{
// No cookie found, assume default options
inputStream.SetPosition(0);
header.HasCookie = false;
return header;
}
header.HasCookie = true;
// Read format version
header.FormatVersion = inputStream.ReadBits(4);
// Read options presence flag
bool hasOptions = inputStream.ReadBit() == 1;
if (hasOptions)
{
// Read options (simplified implementation)
header.PreserveComments = inputStream.ReadBit() == 1;
header.PreservePIs = inputStream.ReadBit() == 1;
header.PreserveDTD = inputStream.ReadBit() == 1;
header.PreservePrefixes = inputStream.ReadBit() == 1;
// Skip remaining option bits for now
inputStream.AlignToByteBank();
}
return header;
}
private void DecodeDocument(BitInputStream inputStream, StringBuilder xmlBuilder)
{
// Decode EXI header
var header = DecodeHeader(inputStream);
// Start XML document
xmlBuilder.AppendLine("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
// Decode document content
DecodeDocumentContent(inputStream, xmlBuilder);
}
private void DecodeDocumentContent(BitInputStream inputStream, StringBuilder xmlBuilder)
{
var elementStack = new Stack<string>();
bool documentStarted = false;
while (!inputStream.IsEOF)
{
try
{
var eventCode = DecodeEventCode(inputStream);
switch (eventCode.Event)
{
case EXIEvent.START_DOCUMENT:
documentStarted = true;
break;
case EXIEvent.END_DOCUMENT:
return;
case EXIEvent.START_ELEMENT:
case EXIEvent.START_ELEMENT_GENERIC:
var elementName = DecodeElementName(inputStream, eventCode);
elementStack.Push(elementName);
xmlBuilder.Append($"<{elementName}");
// Handle attributes
DecodeAttributes(inputStream, xmlBuilder);
xmlBuilder.AppendLine(">");
break;
case EXIEvent.END_ELEMENT:
if (elementStack.Count > 0)
{
var endElementName = elementStack.Pop();
xmlBuilder.AppendLine($"</{endElementName}>");
}
break;
case EXIEvent.CHARACTERS:
var text = DecodeCharacters(inputStream);
xmlBuilder.Append(XmlEscape(text));
break;
default:
// Skip unsupported events
break;
}
}
catch (EXIException ex) when (ex.ErrorCode == EXIErrorCodes.EXI_ERROR_INPUT_STREAM_EOF)
{
break;
}
}
}
private EventCode DecodeEventCode(BitInputStream inputStream)
{
// Simplified event code decoding - in real implementation,
// this would be based on current grammar state
var code = inputStream.ReadBits(2);
return new EventCode
{
Event = code switch
{
0 => EXIEvent.START_ELEMENT,
1 => EXIEvent.END_ELEMENT,
2 => EXIEvent.CHARACTERS,
3 => EXIEvent.END_DOCUMENT,
_ => EXIEvent.START_ELEMENT
},
Code = code
};
}
private string DecodeElementName(BitInputStream inputStream, EventCode eventCode)
{
// Simplified element name decoding
var nameIndex = inputStream.ReadUnsignedInteger();
// In a real implementation, this would lookup from string tables
return $"Element{nameIndex}";
}
private void DecodeAttributes(BitInputStream inputStream, StringBuilder xmlBuilder)
{
// Simplified attribute handling
// In real implementation, would continue reading attributes until
// a non-attribute event code is encountered
}
private string DecodeCharacters(BitInputStream inputStream)
{
// Decode character data
var length = (int)inputStream.ReadUnsignedInteger();
var charData = inputStream.ReadBytes(length);
return _config.Strings switch
{
EXIConfig.StringRepresentation.ASCII => Encoding.ASCII.GetString(charData),
EXIConfig.StringRepresentation.UCS => Encoding.UTF8.GetString(charData),
_ => Encoding.UTF8.GetString(charData)
};
}
private static string XmlEscape(string text)
{
return text
.Replace("&", "&amp;")
.Replace("<", "&lt;")
.Replace(">", "&gt;")
.Replace("\"", "&quot;")
.Replace("'", "&apos;");
}
}
/// <summary>
/// EXI Header information
/// </summary>
public class EXIHeader
{
public bool HasCookie { get; set; }
public uint FormatVersion { get; set; }
public bool PreserveComments { get; set; }
public bool PreservePIs { get; set; }
public bool PreserveDTD { get; set; }
public bool PreservePrefixes { get; set; }
}
/// <summary>
/// EXI Event Code
/// </summary>
public class EventCode
{
public EXIEvent Event { get; set; }
public uint Code { get; set; }
}
}