Search code examples
rubyencodingmime

Difference with Q encoding and Quoted Printable?


My apology if I am asking a beginners question.

May I ask what is the difference between RFC2047 4.2 The "Q" encoding and RFC 2045 6.7 Quoted-Printable Content-Transfer-Encoding?

From RFC2047:

The "Q" encoding is similar to the "Quoted-Printable" content-transfer-encoding defined in RFC 2045.

I am trying to implement decode logic using Ruby. I have read the answer below and am trying to understand why first.gsub('_',' ') is required for Q encoding.

Is there a way to decode q-encoded strings in Ruby?


Solution

  • By reading RFC2047 again, I have realized that the approach below does not decode underscore properly in cases where underscore is encoded as =5F.

    decoded = m[3].unpack('M').first.gsub('_',' ')
    

    Instead, as described in the last sentence of RFC2047 4.2 (2):

    Note that the "_" always represents hexadecimal 20, even if the SPACE character occupies a different code position in the character set in use.

    I have substituted literal underscore back to =20 first, then unpack it.

    I have coded as below:

    decoded = m[3].gsub('_', '=20').unpack('M').first()