Here is a MIME64 encoder function written entirely in T-SQL© 2006 Peter Larsson, Developer Workshop, all rights reservedAs long as the copyright notice is visible within the function declarationand you include a note in the documentation of your system that thesefunctions are written by me, you may use these functions for free of charge.If you intend to use these functions in a commercial application,you are required to email me about your system and provide an email addressto the application manager.If you like the code and use it just for fun, send an e-mail to meand tell me about your experience with these functions.I hope you enjoy these functions. I spent about two hourswriting them because I could not find them on the Internet.CREATE FUNCTION dbo.fnMIME64Encode( @RFC3548 BIT = 1, @PlainText VARCHAR(6000))RETURNS VARCHAR(8000)ASBEGIN-- © 2006 Peter Larsson, Developer Workshop, all rights reserved DECLARE @Characters VARCHAR(64), @Index SMALLINT, @m1 TINYINT, @m2 TINYINT, @m3 TINYINT, @m4 TINYINT, @MimeText VARCHAR(8000), @FinalBlock TINYINT SELECT @Characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', @FinalBlock = DATALENGTH(@PlainText) % 3, @PlainText = @PlainText + REPLICATE(CHAR(0), (3 - DATALENGTH(@PlainText) % 3) % 3), @Index = DATALENGTH(@PlainText) - 2, @MimeText = '' WHILE @Index > 0 BEGIN SELECT @m1 = (ASCII(SUBSTRING(@PlainText, @Index, 1)) & 252) / 4, @m2 = (ASCII(SUBSTRING(@PlainText, @Index, 1)) & 3) * 16 + (ASCII(SUBSTRING(@PlainText, @Index + 1, 1)) & 240) / 16, @m3 = (ASCII(SUBSTRING(@PlainText, @Index + 1, 1)) & 15) * 4 + (ASCII(SUBSTRING(@PlainText, @Index + 2, 1)) & 192) / 64, @m4 = ASCII(SUBSTRING(@PlainText, @Index + 2, 1)) & 63 SELECT @MimeText = CASE WHEN @FinalBlock = 1 THEN SUBSTRING(@Characters, @m1 + 1, 1) + SUBSTRING(@Characters, @m2 + 1, 1) + '==' WHEN @FinalBlock = 2 THEN SUBSTRING(@Characters, @m1 + 1, 1) + SUBSTRING(@Characters, @m2 + 1, 1) + SUBSTRING(@Characters, @m3 + 1, 1) + '=' ELSE SUBSTRING(@Characters, @m1 + 1, 1) + SUBSTRING(@Characters, @m2 + 1, 1) + SUBSTRING(@Characters, @m3 + 1, 1) + SUBSTRING(@Characters, @m4 + 1, 1) END + @MimeText, @Index = @Index - 3, @FinalBlock = 0 END IF @RFC3548 = 1 BEGIN SELECT @Index = 1 + DATALENGTH(@MimeText) - DATALENGTH(@MimeText) % 76 IF @Index > DATALENGTH(@MimeText) SELECT @Index = @Index - 76 WHILE @Index > 1 SELECT @MimeText = STUFF(@MimeText, @Index, 0, CHAR(13) + CHAR(10)), @Index = @Index - 76 END RETURN @MimeTextEND
and of course the MIME64 decoder functionCREATE FUNCTION dbo.fnMIME64Decode( @MimeText VARCHAR(8000))RETURNS VARCHAR(6000)ASBEGIN-- © 2006 Peter Larsson, Developer Workshop, all rights reserved DECLARE @Characters VARCHAR(64), @Index SMALLINT, @m1 TINYINT, @m2 TINYINT, @m3 SMALLINT, @m4 SMALLINT, @p1 TINYINT, @p2 TINYINT, @p3 TINYINT, @PlainText VARCHAR(6000), @Paddings TINYINT SELECT @Characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/', @MimeText = REPLACE(REPLACE(@MimeText, CHAR(13), ''), CHAR(10), ''), @Index = DATALENGTH(@MimeText) - 3, @Paddings = DATALENGTH(@MimeText) - DATALENGTH(REPLACE(@MimeText, '=', '')), @PlainText = '' WHILE @Index > 0 SELECT @m1 = CHARINDEX(CAST(SUBSTRING(@MimeText, @Index, 1) AS BINARY(1)), CAST(@Characters AS BINARY(64))) - 1, @m2 = CHARINDEX(CAST(SUBSTRING(@MimeText, @Index + 1, 1) AS BINARY(1)), CAST(@Characters AS BINARY(64))) - 1, @m3 = CHARINDEX(CAST(SUBSTRING(@MimeText, @Index + 2, 1) AS BINARY(1)), CAST(@Characters AS BINARY(64))) - 1, @m4 = CHARINDEX(CAST(SUBSTRING(@MimeText, @Index + 3, 1) AS BINARY(1)), CAST(@Characters AS BINARY(64))) - 1, @p1 = (@m1 & 63) * 4 + (@m2 & 48) / 16, @p2 = (@m2 & 15) * 16 + (@m3 & 60) / 4, @p3 = (@m3 & 3) * 64 + (@m4 & 63), @PlainText = CHAR(@p1) + CHAR(@p2) + CHAR(@p3) + @PlainText, @Index = @Index - 4 RETURN LEFT(@PlainText, DATALENGTH(@PlainText) - @Paddings)END
Peter LarssonHelsingborg, Sweden