Open-XML-SDK icon indicating copy to clipboard operation
Open-XML-SDK copied to clipboard

FlatOpc: line break at the end of binary part format

Open f1nzer opened this issue 3 years ago • 3 comments

Hi, I have a question about the next method: https://github.com/OfficeDev/Open-XML-SDK/blob/main/src/DocumentFormat.OpenXml/Packaging/OpenXmlPackage.FlatOpc.cs#L140-L159

According to the old blog post from Eric White:

the string must be broken into lines of 76 characters, and there must not be a line break at the beginning or end of the data

But the linked code adds a line break at the end of the data.

Also, the logic of this method is not obvious from my point of view. If it is a bug then it might be simplified a bit:

const int maxLineLength = 76;

var str = Convert.ToBase64String(byteArray);

var sb = new StringBuilder();
for (var i = 0; i < str.Length; i += maxLineLength)
{
    if (i > 0)
    {
        sb.AppendLine();
    }

    sb.Append(str.Substring(i, System.Math.Min(maxLineLength, str.Length - i)));
}

return sb.ToString();

f1nzer avatar Nov 20 '22 17:11 f1nzer

Hi @f1nzer

      It appeared the code snipped that was attached is working as expected.  

       byte[] byte1 = {2, 4, 6, 8, 10,
               12, 14, 16, 18, 20};

        const int maxLineLength = 76;
        var str = Convert.ToBase64String(byte1);

        var sb = new StringBuilder();
        for (var i = 0; i < str.Length; i += maxLineLength)
        {
            if (i > 0)
            {
                sb.AppendLine();
            }

            sb.Append(str.Substring(i, System.Math.Min(maxLineLength, str.Length - i)));
        }

        Console.WriteLine(sb.ToString());

I am getting the following output: AgQGCAoMDhASFA==

What would be your expectation that code should do?

hunyu avatar Nov 21 '22 14:11 hunyu

@hunyu the attached code snippet is what I want to suggest instead of how it is implemented right now.

Please check the link, that is already linked in the issue description: https://github.com/OfficeDev/Open-XML-SDK/blob/main/src/DocumentFormat.OpenXml/Packaging/OpenXmlPackage.FlatOpc.cs#L140-L159

f1nzer avatar Nov 21 '22 15:11 f1nzer

Hi @f1nzer

I am totally agreed. Code snipped you suggested and provided is much simpler. Once we released Open XML SDK update, I will bring this up to the team and see if we can consider what you suggested. Thank you very much for your time.

Hung-Chun Yu Microsoft Open Specifications Support.

hunyu avatar Nov 21 '22 17:11 hunyu