uses StartUp uses Ext.comment /* Subject: Quoted-Printable Date: 21 Feb 2000 01:50:28 GMT From: Wil Baden Organization: MindSpring Enterprises Newsgroups: comp.lang.forth ******************************************************* Wil Baden 2000-02-20 MIME Quoted-Printable Content-Transfer-Encoding ----------------------------------------------- MIME Quoted-Printable Content-Transfer-Encoding displays printable characters except "=" as Ascii, and others as octets. An octet is "=" followed by two hex digits. A space at the end of line must be displayed as an octet; otherwise it may be displayed as " ". "=" at the end of a line is used for continuing with the next line. Reference: RFC 1521 ******************************************************* ======================================================= .LINE-TO-QUOTED-PRINTABLE converts and prints an Ascii text line in MIME Quoted-Printable encoding. ( str len -- ) .LINE-FROM-QUOTED-PRINTABLE converts and prints a MIME Quoted-Printable line in Ascii text. ( str len -- ) These will be used in an editor or MIME agent -- not as in the fabricated examples. Fabricated Examples. S" Foo =3D =" .LINE-FROM-QUOTED-PRINTABLE \ S" Bar =20 " .LINE-FROM-QUOTED-PRINTABLE \ Foo = Bar S" Foo = Bar " .LINE-TO-QUOTED-PRINTABLE \ Foo =3D Bar=20 ======================================================= ------------------------------------------------------- The following may already be defined. Comment out what you don't need. ------------------------------------------------------- */ uses Ext.macro /* : MACRO ( " " -- ) : CHAR PARSE POSTPONE SLITERAL POSTPONE EVALUATE POSTPONE ; IMMEDIATE ; MACRO BACK[ " BEGIN DUP WHILE 1- 2DUP CHARS + C@ " MACRO ]BACK " 0= UNTIL 1+ THEN " MACRO IS-BLANK " 33 - 0< " : TRIM ( str1 len1 -- str1 len2 ) BACK[ IS-BLANK ]BACK ; */ /* ------------------------------------------------------- MAX#CHARS/LINE is the maximum number of characters per line in MIME Quoted-Printable format. Prescribed as 76. Char-Count has the count of characters used so far in a MIME Quoted-Printable output line. .CHAR prints a character and increments Char-Count. Used in .Char-to-Quoted-Printable . ( char -- ) .OCTET prints a character as an octet, i.e. "=" followed by two hex digits. Bumps Char-Count. Used in .Char-to-Quoted-Printable . ( char -- ) .Char-to-Quoted-Printable prints next character as Ascii or octet. Used in .LINE-TO-QUOTED-PRINTABLE . ( str len -- same ) ------------------------------------------------------- */ 76 CONSTANT MAX#CHARS/LINE VARIABLE Char-Count : .CHAR ( char -- ) EMIT ( ) 1 Char-Count +! ; : .OCTET ( char -- ) [CHAR] = EMIT 16 BASE DUP @ >R ! 0 <# # # #> TYPE ( ) R> BASE ! 3 Char-Count +! ; : .Char-to-Quoted-Printable ( str len -- same ) OVER C@ ( str len char) OVER 1 > OVER BL = AND IF .CHAR EXIT THEN DUP [CHAR] = = IF .OCTET EXIT THEN DUP [CHAR] ! - 94 U< IF .CHAR EXIT THEN .OCTET ; /* ------------------------------------------------------- .Line-Break prints a line break and resets Char-Count. Used in .Soft-Line-Break and .LINE-TO-QUOTED-PRINTABLE . ( -- ) .Soft-Line-Break prints a soft line break and resets Char-Count. Used in .LINE-TO-QUOTED-PRINTABLE . The next line will be appended. ( -- ) ------------------------------------------------------- */ : .Line-Break ( -- ) 13 EMIT 10 EMIT 0 Char-Count ! ; : .Soft-Line-Break ( -- ) [CHAR] = EMIT .Line-Break ; /* ======================================================= .LINE-TO-QUOTED-PRINTABLE ( str len -- ) ======================================================= */ : .LINE-TO-QUOTED-PRINTABLE ( str len -- ) 0 Char-Count ! BEGIN DUP WHILE Char-Count @ 4 + MAX#CHARS/LINE > IF .Soft-Line-Break THEN .Char-to-Quoted-Printable 1 /STRING REPEAT 2DROP ( ) .Line-Break ; \ Example \ S" Foo = Bar " .LINE-TO-QUOTED-PRINTABLE \ Foo =3D Bar =20 /* ------------------------------------------------------- .Char-From-Octet prints an Ascii character from an octet. Used in .LINE-FROM-QUOTED-PRINTABLE. ( str len -- same ) ------------------------------------------------------- */ : .Char-From-Octet ( str len -- same ) 16 BASE DUP @ >R ! OVER CHAR+ 2 ( str len addr 2) 0 0 2SWAP >NUMBER 2DROP DROP EMIT ( str len) R> BASE ! ; /* ======================================================= .LINE-FROM-QUOTED-PRINTABLE ( str len -- ) ======================================================= */ : .LINE-FROM-QUOTED-PRINTABLE ( str len -- ) TRIM BEGIN DUP WHILE OVER C@ [CHAR] = = IF DUP 3 < IF 2DROP EXIT THEN \ Can't be factor. .Char-From-Octet 3 /STRING ELSE OVER C@ EMIT 1 /STRING THEN REPEAT 2DROP ( ) CR ; \ Example \ S" \ = " .LINE-FROM-QUOTED-PRINTABLE \ \ S" Foo =3D =" .LINE-FROM-QUOTED-PRINTABLE \ \ S" Bar =20 " .LINE-FROM-QUOTED-PRINTABLE \ Foo = Bar /* -- Wil Baden Costa Mesa, California WilBaden@Netcom.com */