Browse Source

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
af/merge-core
Johannes Bildstein 9 years ago
parent
commit
fe9dcb7889
  1. 2
      src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs
  2. 21
      src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.Primitives.cs
  3. 14
      src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.TagDataEntry.cs
  4. 4
      src/ImageSharp/MetaData/Profiles/ICC/IccWriter.cs

2
src/ImageSharp/MetaData/Profiles/ICC/DataWriter/IccDataWriter.NonPrimitives.cs

@ -98,7 +98,7 @@ namespace ImageSharp
/// <returns>the number of bytes written</returns>
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);
}

21
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
/// </summary>
/// <param name="value">The string to write</param>
/// <param name="length">The desired length of the string including 1 padding character</param>
/// <param name="paddingChar">The character to pad to the given length</param>
/// <param name="length">The desired length of the string (including potential null terminator)</param>
/// <param name="ensureNullTerminator">If True, there will be a \0 added at the end</param>
/// <returns>the number of bytes written</returns>
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;

14
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
/// <returns>The number of bytes written</returns>
public int WriteSignatureTagDataEntry(IccSignatureTagDataEntry value)
{
return this.WriteAsciiString(value.SignatureData, 4, ' ');
return this.WriteAsciiString(value.SignatureData, 4, false);
}
/// <summary>
@ -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));

4
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());

Loading…
Cancel
Save