Quoted-Printable

Обсуждение программ nnCron и nnCron LITE

Quoted-Printable

Postby Morituruz » Sat, 20 Sep 2014, 00:12

Я тут случайно нашёл какую-то реализацию работы с quoted-printable кодировкой.
Возможно тут найдутся добрые люди, которые смогут подправить её под местный диалект? :)
Code: Select all
0 [IF] *******************************************************
                                          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

******************************************************* [THEN]

0 [IF] =======================================================
 .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  <end>

    S" Foo = Bar " .LINE-TO-QUOTED-PRINTABLE   \  Foo =3D Bar=20
 ======================================================= [THEN]
 0 [IF] -------------------------------------------------------
     The following may already be defined.  Comment out what
     you don't need.
 ------------------------------------------------------- [THEN]
     : MACRO  ( "<name> <char> <ccc><char>" -- )
         :   CHAR PARSE POSTPONE SLITERAL  POSTPONE EVALUATE
         POSTPONE ; IMMEDIATE
         ;

     MACRO ]BACK     " 0= UNTIL 1+ THEN "
     MACRO IS-BLANK  " 33 - 0< "
     : TRIM  ( str1 len1 -- str1 len2 )  BACK[ IS-BLANK ]BACK ;

0 [IF] -------------------------------------------------------

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 )
 ------------------------------------------------------- [THEN]
 76 CONSTANT MAX#CHARS/LINE
 VARIABLE Char-Count

: .CHAR  ( char -- )  EMIT ( ) 1 Char-Count +! ;

: .OCTET  ( char -- )
     [CHAR] = EMIT

         0 <# # # #> TYPE  ( )
     R> BASE !
     3 Char-Count +! ;

: .Char-to-Quoted-Printable  ( str len -- same )

     OVER 1 >  OVER BL = AND  IF  .CHAR  EXIT THEN
     DUP [CHAR] = =           IF  .OCTET EXIT THEN
     DUP [CHAR] ! - 94 U<     IF  .CHAR  EXIT THEN
     .OCTET ;

0 [IF] -------------------------------------------------------
 .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.                              ( -- )
 ------------------------------------------------------- [THEN]
 : .Line-Break  ( -- )  13 EMIT  10 EMIT  0 Char-Count ! ;

: .Soft-Line-Break  ( -- )  [CHAR] = EMIT  .Line-Break ;

0 [IF] =======================================================

    .LINE-TO-QUOTED-PRINTABLE                   ( str len -- )

======================================================= [THEN]

: .LINE-TO-QUOTED-PRINTABLE  ( str len -- )
     0 Char-Count !
     BEGIN  DUP WHILE


             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

0 [IF] -------------------------------------------------------
 .Char-From-Octet  prints an Ascii character from an octet.
     Used in  .LINE-FROM-QUOTED-PRINTABLE.  ( str len -- same )
 ------------------------------------------------------- [THEN]
 : .Char-From-Octet            ( str len -- same )

         OVER CHAR+ 2                     ( str len addr 2)
         0 0 2SWAP >NUMBER 2DROP DROP EMIT    ( str len)
     R> BASE ! ;

0 [IF] =======================================================

    .LINE-FROM-QUOTED-PRINTABLE                 ( str len -- )

======================================================= [THEN]

: .LINE-FROM-QUOTED-PRINTABLE  ( str len -- )
     TRIM
     BEGIN  DUP WHILE


             DUP 3 < IF  2DROP  EXIT THEN  \  Can't be factor.
             .Char-From-Octet
             3 /STRING
         ELSE

             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  <end>

(
 --

 )
User avatar
Morituruz
 
Posts: 725
Joined: Sun, 14 Oct 2007, 01:51

Re: Quoted-Printable

Postby VoidVolker » Sat, 20 Sep 2014, 07:35

А откуда это? Там не хватает определения BACK[.

SP-Forth\devel\~ac\lib\string\mime-decode.f
95% вопросов уже обсуждались на форуме или ответы на них есть в мануале.        nnCron 1.93 b15.exe
Как правильно задавать вопросы.
User avatar
VoidVolker
Site Admin
 
Posts: 2898
Joined: Tue, 25 Apr 2006, 17:56

Re: Quoted-Printable

Postby Morituruz » Sat, 20 Sep 2014, 14:23

С одного старого форума по Forth. Определения BACK[ там нигде нет, печально.
User avatar
Morituruz
 
Posts: 725
Joined: Sun, 14 Oct 2007, 01:51

Re: Quoted-Printable

Postby Morituruz » Tue, 23 Sep 2014, 11:34

VoidVolker wrote:А откуда это? Там не хватает определения BACK[.

SP-Forth\devel\~ac\lib\string\mime-decode.f


В таком виде nncron не ругается на синтаксис:
Code: Select all
<%
: s@ ( s -- xs )
  @
;
: STR@ ( s -- addr u )
  s@ XCOUNT
\  DEBUG @ IF ." STR@:" 2DUP TYPE ." |" VTH CR THEN
;
: dequotep ( addr u -- addr2 u2 ) { \ s c }
  S" " -> s
  BASE @ >R HEX
  2DUP + >R DROP
  BEGIN
    DUP R@ <
  WHILE
    DUP C@ DUP [CHAR] = =
        IF DROP 1+ DUP 2+ SWAP 2 0 0 2SWAP 2DUP UPPER-CASE >NUMBER 2DROP D>S
           ?DUP IF -> c ^ c 1 s S+ THEN
        ELSE -> c
             c [CHAR] _ = IF BL -> c THEN
             ^ c 1 s S+ 1+
        THEN
  REPEAT DROP R> DROP
  R> BASE ! s STR@
;
%>

но и не работает:

Code: Select all
S" =D0=B6=D0=BE=D0=BF=D0=BA=D0=B0  dequotep
                                         ^ 0xC0000005L ACCESS_VIOLATION
User avatar
Morituruz
 
Posts: 725
Joined: Sun, 14 Oct 2007, 01:51

Re: Quoted-Printable

Postby Morituruz » Thu, 25 Sep 2014, 11:04

Code: Select all
<%
: SALLOT ( addr u -- xs )
  DUP 9 + ALLOCATE THROW >R
  DUP R@ ! R@ CELL+ SWAP CMOVE R>
  0 OVER XCOUNT + C!
;
: sALLOT
  SALLOT CELL ALLOCATE THROW DUP >R ! R>
;
: Str"" ( -- s )
  S" " sALLOT
;
: s@ ( s -- xs )
  @
;
\ : +!  ( приращение а --)  DUP @ ROT +  SWAP ! ;
: s! ( xs s -- )
  !
;
: STR@ ( s -- addr u )
  s@ XCOUNT
\  DEBUG @ IF ." STR@:" 2DUP TYPE ." |" VTH CR THEN
;
: STR+ { addr u s -- }
\ DEBUG @ IF ." STR+:" addr u TYPE CR THEN
  u 0 < IF 0xC000000D THROW THEN
  u 0= IF EXIT THEN \ оптимизация :)
  s s@ DUP @
  u + 9 + RESIZE THROW DUP DUP s s!
  XCOUNT + addr SWAP u CMOVE
  u SWAP +!
  0 s STR@ + C!
;

: dequotep ( addr u -- addr2 u2 ) { \ s c }
." LINE 1" CR
  Str"" -> s
." LINE 2" CR
  BASE @ >R HEX
." LINE 3" CR
  2DUP + >R DROP
." LINE 4" CR
  BEGIN
    DUP R@ <
  WHILE
." LINE 5" CR
    DUP C@ DUP [CHAR] = =
." LINE 6" CR
        IF DROP 1+ DUP 2+ SWAP 2 0 0 2SWAP 2DUP UPPER-CASE >NUMBER 2DROP D>S
." LINE 7" CR
           ?DUP IF -> c ^ c 1 s STR+ THEN
        ELSE -> c
." LINE 8" CR
             c [CHAR] _ = IF BL -> c THEN
             ^ c 1 s STR+ 1+
        THEN
  REPEAT DROP R> DROP
  R> BASE ! s STR@
." LINE 10" CR
;
%>


Code: Select all
S" =EF=EE_=EF=EE=E2=EE=E4=F3"  dequotep
LINE 1
LINE 2
LINE 3
LINE 4
LINE 5
LINE 6
LINE 7
LINE 5
EXCEPTION!  CODE:C0000005  ADDRESS:4B22CE  WORD:C@  REGISTERS:
45BED10   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00 ................
45BED20   00 00 00 00  00 00 00 00  00 00 00 00  00 00 00 00 ................
45BED30   02 00 00 00  2B 00 00 00  53 00 00 00  2B 00 00 00 ....+...S...+...
USER DATA: 44207D4 HANDLER: 45BEFB8 RETURN STACK:
0 :  4 :  8 :  C :  10 :  14 :  18 :  1C :  20 :  24 :  28 :  2C :  30 :  34 :  38 :  3C :  40 :  44
 :  48 :  4C :  50 :  54 :  58 :  5C :  60 :
S" =EF=EE_=EF=EE=E2=EE=E4=F3   dequotep
                                     ^ 0xC0000005L ACCESS_VIOLATION

:(
User avatar
Morituruz
 
Posts: 725
Joined: Sun, 14 Oct 2007, 01:51


Return to nnCron forum (Russian)

Who is online

Users browsing this forum: No registered users and 1 guest

cron