RAD Studio and Internet Message Format

Posted by on in Blogs

Introduction

The RFC-2822 standard specifies a syntax for text messages that are sent between computer users, within the framework of "electronic mail" messages.  The RFC-2822 specifies lines should be no longer than 78 characters, and the MIME spec RFC-2045, says:

The encoded output stream must be represented in lines of no more than 76 characters each.  All line breaks or other characters not found in Table 1- The Base64 Alphabet must be ignored by decoding software.

 

RAD Studio and TNetEncoding.Base64.

RAD Studio (Delphi and C++ Builder) follows the RFC-2822 and RFC-2045 specs!

For example, given this Delphi code:

 procedure TForm55.Button1Click(Sender: TObject);
var
  S: string;
begin
  S := 'jfdlksjflkasjdlfow7r89fpsduf87s8fy4387rhfofldsajfhasddlsihjsldhjlsdhlfhjsdafhjsadh';
  Memo1.Text :=  TNetEncoding.Base64.EncodeBytesToString(TEncoding.GetEncoding('ISO-8859-1').GetBytes(S));
end;

TEncodingBase64

You will notice that this produces a #13#10 in the middle.

Delphi and/or C++ Builder is automatically wrapping at 76 characters per line (TBase64Encoding.kCharsPerLine.)  You can override this in one of the constructors and pass 0: TBase64Encoding.Create(0) where 0 means don't wrap lines.

Delphi and C++ Builder does this because it's a net encoding and base64 is used for representing binary in, eg, email; Recalling that the RFC-2822 specifies lines should be no longer than 78 characters and the MIME spec RFC-2045, and it says: “The encoded output stream must be represented in lines of no more than 76 characters each.  All line breaks or other characters not found in Table 1- The Base64 Alphabet must be ignored by decoding software.” 

We see that Delphi and C++ Builder matches the spec accurately!

This is completely valid.  RAD Studio (Delphi and/or C++ Builder) creates 100% valid MIME base64 encoded text! 

Compared to this corresponding .NET code, which (unlike Delphi or C++ Builder) does not put anything in the middle:

Convert.ToBase64String(Encoding.GetEncoding("ISO-8859-1").GetBytes(Key1+ ":" + Secret1))

 



About
Gold User, Rank: 90, Points: 4
Al Mannarino has 25+ years of software development experience, including object-oriented analysis and design (OOAD) and developing and deploying production applications. He is currently a Principal Software Consultant and Evangelist for Embarcadero Technologies. Prior to joining Embarcadero, Al spent three years working with CodeGear, a division of Borland that was acquired by Embarcadero in 2008. He also worked for five years as a lead systems engineer for Borland supporting application lifecycle management, software delivery optimization and developer tools solutions. Prior to Borland, Al served as a systems engineer for companies including Objectivity, Versant, Red Brick Systems, Information Builders, and was an electrical engineer for Grumman Aerospace performing application implementations on complex electrical-mechanical systems. Al has a bachelor's of science degree in electrical engineering from Manhattan College.

Comments

Check out more tips and tricks in this development video: