Results 1 to 3 of 3

Thread: Breaking QByteArray::toBase64() results into 64 character lines

  1. #1
    Join Date
    Jun 2008
    Location
    Boulder, Colorado, USA
    Posts
    70
    Thanks
    16
    Thanked 1 Time in 1 Post
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Default Breaking QByteArray::toBase64() results into 64 character lines

    It would be nice if QByteArray::toBase64() had an option to split the results into multiple fixed-length lines.

    Currently its results are just in a single line (i.e. with no line breaks). (We actually have a requirement for this, my group's model serialization can't have arbitrarily long lines). The documentation states that this function conforms to RFC 2045 which seems to require lines of no more that 76 characters, but there may be a technical reason why this isn't always required. But still, it would be nice. (Apparently some other Base64 encodings require line lengths of 64 characters, so that's a good general length).

    Could QByteArray::toBase64() be enhanced with an optional integer parameter of the maximum line length, with 0 as the default for the current single-line behavior?

    Here is a function we are now using to do this line breaking:

    Qt Code:
    1. QByteArray UserImageData::wrapAt64Chars (const QByteArray& inArray)
    2. {
    3. // This function takes a QByteArray lacking line breaks and returns
    4. // that QByteArray value with line breaks inserted after every
    5. // 64 bytes. It is intended for use with Base64-encoded QByteArrays
    6. // created with QByteArray::toBase64() which generates a Base64 string
    7. // without line breaks [as of Qt 4.8.5].
    8. //
    9. // Note that the QByteArray::fromBase64() static method ignores all
    10. // characters which are not part of the encoding, including CR and LF.
    11. // This is a provision of the "RFC 2045" Base64 standard supported by
    12. // these methods. RFC 2045 specficies a maximum line length of 76
    13. // characters. See: http://en.wikipedia.org/wiki/Base64
    14. //
    15. // The code below was adapted from this method in Qt 4.8:
    16. // ByteArray QSslCertificatePrivate::QByteArray_from_X509();
    17.  
    18. const int inSize = inArray.size();
    19. const int outEst = inSize + (inSize/32) + 4;
    20. QByteArray outArray;
    21. outArray.reserve (outEst);
    22.  
    23. const char* inPtr = inArray.data();
    24. for (int inCrs = 0; inCrs <= inSize - 64; inCrs += 64)
    25. {
    26. outArray += QByteArray::fromRawData (inPtr + inCrs, 64);
    27. outArray += "\n";
    28. }
    29.  
    30. const int rem = inSize % 64;
    31. if (rem > 0)
    32. {
    33. outArray += QByteArray::fromRawData (inPtr + inSize - rem, rem);
    34. outArray += "\n";
    35. }
    36.  
    37. return (outArray);
    38. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by philw; 28th October 2013 at 03:31.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,734
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Breaking QByteArray::toBase64() results into 64 character lines

    You would need to take this to the Qt developers' mailing list, discuss it, and submit a relevant patch for approval if there was interest. Since you are asking to change the API and binary compatibility it is unlikely to be accepted during the Qt5 cycle... but stranger things have happened.

    I can also see an issue with exactly what you use to separate the lines of base64 characters: the native line ending, or the CR LF pair that would be expected in RFC822 mail. Base64 encoding can be used for purposes other than email and the native line may be more appropriate at times.

    If 4n character lines are desired out you can encode your input 3n bytes at a time rather than post-process the full result.
    Qt Code:
    1. const int bytesPerLine = 54; // 72 chars per line out
    2. QByteArray output;
    3. for(int i = 0; i < input.size(); i += bytesPerLine) {
    4. output += input.mid(i, bytesPerLine).toBase64();
    5. output += "\r\n";
    6. }
    To copy to clipboard, switch view to plain text mode 
    May not be the most efficient for large input.

  3. #3
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,418
    Thanks
    37
    Thanked 1,545 Times in 1,495 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Breaking QByteArray::toBase64() results into 64 character lines

    Quote Originally Posted by ChrisW67 View Post
    You would need to take this to the Qt developers' mailing list, discuss it, and submit a relevant patch for approval if there was interest. Since you are asking to change the API and binary compatibility it is unlikely to be accepted during the Qt5 cycle... but stranger things have happened.
    An overload should be OK to add.

    I am wondering though if it would not be most effecient to handle it at serialization time, i.e. serialize the data in lines of the length required by the serialization format.

    Cheers,
    _

Similar Threads

  1. Replies: 2
    Last Post: 11th October 2011, 14:55
  2. Weird results when passing a QByteArray* to a method
    By agerlach in forum Qt Programming
    Replies: 1
    Last Post: 1st December 2010, 22:05
  3. Qt Designer Problem moving widget after breaking layout
    By nickolais in forum Qt Tools
    Replies: 0
    Last Post: 10th June 2010, 22:21
  4. Character by Character (Unicode?) File Reading
    By mclark in forum Qt Programming
    Replies: 4
    Last Post: 22nd April 2009, 15:28
  5. NEED HELP: How to download file without breaking function
    By codeslicer in forum Qt Programming
    Replies: 10
    Last Post: 22nd April 2008, 04:10

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.