Skip to content

Commit

Permalink
Improved strings reading, aslo added support for inserting zero-termi…
Browse files Browse the repository at this point in the history
…nation char.
  • Loading branch information
TBXin committed Mar 15, 2016
1 parent 129299b commit 1c6adc0
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 10 deletions.
19 changes: 17 additions & 2 deletions src/NFirmware/FirmwareLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,24 @@ private IEnumerable<FirmwareStringMetadata> ReadStringTable([CanBeNull] StringTa
{
var offset = reader.BaseStream.Position;
var dataLength = 0;
while (reader.ReadByte() != 0 && reader.BaseStream.Position <= stringTableDefinition.OffsetTo)

while (true)
{
dataLength++;
if (reader.BaseStream.Position <= stringTableDefinition.OffsetTo)
{
dataLength++;
var byte1 = reader.ReadByte();
byte? byte2;
if (reader.BaseStream.Position <= stringTableDefinition.OffsetTo)
{
byte2 = reader.ReadByte();
reader.BaseStream.Position--;
}
else break;

if (byte1 == 0x00 && byte2 != 0x00) break;
}
else break;
}
result.Add(new FirmwareStringMetadata(result.Count + 1, offset, dataLength));
}
Expand Down
2 changes: 1 addition & 1 deletion src/NFirmwareEditor/Core/Consts.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
internal static class Consts
{
internal const string ApplicationTitle = "NFirmwareEditor";
internal const string ApplicationVersion = "2.2";
internal const string ApplicationVersion = "2.3";
internal const string FirmwareFilter = "Firmware file|*.bin";
internal const string ExportImageFilter = "Exported images|*.images";
internal const string Encrypted = "encrypted";
Expand Down
19 changes: 12 additions & 7 deletions src/NFirmwareEditor/MainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,7 @@ private void RebuildImageCache([NotNull] Firmware firmware)
block1ImageCache[imageMetadata.Index] = new Bitmap(1, 1);
}
}
block1ImageCache.Add(0, new Bitmap(1, 16));

var block2ImageCache = new Dictionary<int, Image>();
foreach (var imageMetadata in firmware.Block2Images)
Expand All @@ -271,6 +272,7 @@ private void RebuildImageCache([NotNull] Firmware firmware)
block2ImageCache[imageMetadata.Index] = new Bitmap(1, 1);
}
}
block2ImageCache.Add(0, new Bitmap(1, 16));

var previousCache1 = m_block1ImageCache;
var previousCache2 = m_block2ImageCache;
Expand Down Expand Up @@ -512,7 +514,10 @@ private void CreateStringEditControls(byte[] firmwareString, IDictionary<int, Im
{
for (var i = 0; i < firmwareString.Length; i++)
{
if (i == firmwareString.Length - 1 && firmwareString[i] == 0x00) continue;

var stringChar = firmwareString[i];
var nullItem = new ImagedComboBox.ImagedComboBoxItem((byte)0, imageCache[0], "NULL");
var icb = new ImagedComboBox
{
Width = 200,
Expand All @@ -521,7 +526,9 @@ private void CreateStringEditControls(byte[] firmwareString, IDictionary<int, Im
ForeColor = Color.White,
Tag = new Tuple<FirmwareStringMetadata, int>(LastSelectedStringMetadata, i)
};
ImagedComboBox.ImagedComboBoxItem selectedItem = null;
icb.Items.Add(nullItem);

var selectedItem = nullItem;
foreach (var imageMetadata in CurrentImageBlockForStrings)
{
var item = new ImagedComboBox.ImagedComboBoxItem((byte)imageMetadata.Index, imageCache[imageMetadata.Index], string.Format("0x{0:X2}", imageMetadata.Index));
Expand Down Expand Up @@ -557,13 +564,13 @@ private void UpdateStringPreview()
if (LastSelectedStringMetadata == null) return;

var firmwareString = m_firmware.ReadString(LastSelectedStringMetadata);
var charMetadata = new List<FirmwareImageMetadata>();
var charMetadatas = new List<FirmwareImageMetadata>();
foreach (var charIndex in firmwareString)
{

charMetadata.Add(CurrentImageBlockForStrings.First(x => x.Index == charIndex));
var metadata = CurrentImageBlockForStrings.FirstOrDefault(x => x.Index == charIndex);
if (metadata != null) charMetadatas.Add(metadata);
}
var images = m_firmware.ReadImages(charMetadata).ToList();
var images = m_firmware.ReadImages(charMetadatas).ToList();
var data = FirmwareImageProcessor.MergeImages(images);

StringPrewviewPixelGrid.Data = data;
Expand Down Expand Up @@ -894,7 +901,5 @@ private void ImportContextMenuItem_Click(object sender, EventArgs e)
ImageListBox.SelectedIndices.Clear();
ImageListBox.SelectedItem = lastSelectedItem;
}


}
}

0 comments on commit 1c6adc0

Please sign in to comment.