From 1064bfb90f59976f4d8062875f9503325c4c369d Mon Sep 17 00:00:00 2001 From: Johannes Bildstein Date: Fri, 24 Mar 2017 20:41:55 +0100 Subject: [PATCH] conflict when writing spaces or ensuring null terminator when writing spaces, the given length should be written and, if necessary padded to the final length. when ensuring null terminator, length needs to be one less to add the null terminator within length --- .../DataWriter/IccDataWriter.NonPrimitives.cs | 2 +- .../DataWriter/IccDataWriter.Primitives.cs | 21 +++++++++++++------ .../DataWriter/IccDataWriter.TagDataEntry.cs | 14 ++++++------- .../MetaData/Profiles/ICC/IccWriter.cs | 4 ++-- 4 files changed, 25 insertions(+), 16 deletions(-) diff --git a/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs b/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs index 5c57436d81..f9c4eceea4 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs @@ -98,7 +98,7 @@ namespace ImageSharp /// the number of bytes written public int WriteNamedColor(IccNamedColor value) { - return this.WriteAsciiString(value.Name, 32, '\0') + return this.WriteAsciiString(value.Name, 32, true) + this.WriteArray(value.PcsCoordinates) + this.WriteArray(value.DeviceCoordinates); } diff --git a/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.Primitives.cs b/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.Primitives.cs index 92120d0ac1..041cb4c234 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.Primitives.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.Primitives.cs @@ -189,10 +189,10 @@ namespace ImageSharp /// Writes an ASCII encoded string resizes it to the given length /// /// The string to write - /// The desired length of the string including 1 padding character - /// The character to pad to the given length + /// The desired length of the string (including potential null terminator) + /// If True, there will be a \0 added at the end /// the number of bytes written - public int WriteAsciiString(string value, int length, char paddingChar) + public int WriteAsciiString(string value, int length, bool ensureNullTerminator) { if (length == 0) { @@ -206,14 +206,23 @@ namespace ImageSharp value = string.Empty; } - value = value.Substring(0, Math.Min(length - 1, value.Length)); + byte paddingChar = (byte)' '; + int lengthAdjust = 0; + + if (ensureNullTerminator) + { + paddingChar = 0; + lengthAdjust = 1; + } + + value = value.Substring(0, Math.Min(length - lengthAdjust, value.Length)); byte[] textData = AsciiEncoding.GetBytes(value); - int actualLength = Math.Min(length - 1, textData.Length); + int actualLength = Math.Min(length - lengthAdjust, textData.Length); this.dataStream.Write(textData, 0, actualLength); for (int i = 0; i < length - actualLength; i++) { - this.dataStream.WriteByte((byte)paddingChar); + this.dataStream.WriteByte(paddingChar); } return length; diff --git a/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.TagDataEntry.cs b/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.TagDataEntry.cs index 893c71dde0..06be71276c 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.TagDataEntry.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.TagDataEntry.cs @@ -196,7 +196,7 @@ namespace ImageSharp int count = this.WriteUInt32((uint)value.ColorantData.Length); foreach (IccColorantTableEntry colorant in value.ColorantData) { - count += this.WriteAsciiString(colorant.Name, 32, '\0'); + count += this.WriteAsciiString(colorant.Name, 32, true); count += this.WriteUInt16(colorant.Pcs1); count += this.WriteUInt16(colorant.Pcs2); count += this.WriteUInt16(colorant.Pcs3); @@ -564,8 +564,8 @@ namespace ImageSharp { string[] code = value.Texts[i].Culture.Name.Split('-'); - count += this.WriteAsciiString(code[0].ToLower(), 2, ' '); - count += this.WriteAsciiString(code[1].ToUpper(), 2, ' '); + count += this.WriteAsciiString(code[0].ToLower(), 2, false); + count += this.WriteAsciiString(code[1].ToUpper(), 2, false); count += this.WriteUInt32((uint)lengths[i]); count += this.WriteUInt32(offset[i]); @@ -624,8 +624,8 @@ namespace ImageSharp int count = this.WriteInt32(value.VendorFlags) + this.WriteUInt32((uint)value.Colors.Length) + this.WriteUInt32((uint)value.CoordinateCount) - + this.WriteAsciiString(value.Prefix, 32, '\0') - + this.WriteAsciiString(value.Suffix, 32, '\0'); + + this.WriteAsciiString(value.Prefix, 32, true) + + this.WriteAsciiString(value.Suffix, 32, true); foreach (IccNamedColor color in value.Colors) { @@ -757,7 +757,7 @@ namespace ImageSharp /// The number of bytes written public int WriteSignatureTagDataEntry(IccSignatureTagDataEntry value) { - return this.WriteAsciiString(value.SignatureData, 4, ' '); + return this.WriteAsciiString(value.SignatureData, 4, false); } /// @@ -900,7 +900,7 @@ namespace ImageSharp else { this.dataStream.Position += 3; - count += size = this.WriteAsciiString(value.ScriptCode, 67, '\0'); + count += size = this.WriteAsciiString(value.ScriptCode, 67, true); this.dataStream.Position -= size + 3; count += this.WriteUInt16(value.ScriptCodeCode); count += this.WriteByte((byte)(value.ScriptCode.Length > 66 ? 67 : value.ScriptCode.Length)); diff --git a/src/ImageSharp/MetaData/Profiles/ICC/IccWriter.cs b/src/ImageSharp/MetaData/Profiles/ICC/IccWriter.cs index 5ff1f7af15..895eb05542 100644 --- a/src/ImageSharp/MetaData/Profiles/ICC/IccWriter.cs +++ b/src/ImageSharp/MetaData/Profiles/ICC/IccWriter.cs @@ -34,7 +34,7 @@ namespace ImageSharp writer.SetIndex(0); writer.WriteUInt32(writer.Length + 128); - writer.WriteAsciiString(header.CmmType, 4, ' '); + writer.WriteAsciiString(header.CmmType, 4, false); writer.WriteVersionNumber(header.Version); writer.WriteUInt32((uint)header.Class); writer.WriteUInt32((uint)header.DataColorSpace); @@ -48,7 +48,7 @@ namespace ImageSharp writer.WriteInt64((long)header.DeviceAttributes); writer.WriteUInt32((uint)header.RenderingIntent); writer.WriteXyzNumber(header.PcsIlluminant); - writer.WriteAsciiString(header.CreatorSignature, 4, ' '); + writer.WriteAsciiString(header.CreatorSignature, 4, false); #if !NETSTANDARD1_1 IccProfileId id = IccProfile.CalculateHash(writer.GetData());