MIME quoted-printable codec

zhaozj2021-02-16  44

Quoted-Printable is also one of the commonly used coding methods in MIME messages. Like BASE64, it also intenss the input string or data encoding a printable string of the ASCII code.

The basic method of the quoted-printable encoding is: input data within 33-60, 62-126, direct output; other need to be encoded as "=" plus two bytes of HEX code (uppercase). To ensure that the output line does not exceed the specified length, the "= / r / n" sequence can be added as a soft back bus at the end of the line.

Int Encodequoted (const unsigned char * psrc, char * pdst, int nsrclen, int nmaxlinelen)

{

INT ndstlen; // output character count

INT NLINELEN; / / Output Director count

Ndstlen = 0;

NLINELEN = 0;

For (int i = 0; i

{

// ASCII 33-60, 62-126 is originally output, the rest needs to be encoded

IF ((* psrc> = '!') && (* psrc <= '~') && (* psrc! = '=')))

{

* PDST = (char) * psrc;

NDSTLEN ;

NLINELEN ;

}

Else

{

Sprintf (PDST, "=% 02x", * psrc);

PDST = 3;

NDSTLEN = 3;

NLINELEN = 3;

}

/ / Output wrap?

IF (nLinelen> = nmaxlinelen - 3)

{

Sprintf (PDST, "= / R / N");

PDST = 3;

NDSTLEN = 3;

NLINELEN = 0;

}

}

// Output add a end value

* PDST = '/ 0';

Return ndstlen;

}

Quoted-printable decoding is very simple, it will be in turn of the encoding process.

INT decodequoted (const char * psrc, unsigned char * pdst, int nsrclen)

{

INT ndstlen; // output character count

INT I;

i = 0;

Ndstlen = 0;

While (i

{

IF (strncmp (psrc, "= / r / n", 3) == 0) // soft back to the bus, skip

{

PSRC = 3;

I = 3;

}

Else

{

IF (* psrc == '=') // is the encoded byte

{

SSCANF (PSRC, "=% 02x", PDST);

PDST ;

PSRC = 3;

I = 3;

}

ELSE // Non-coded bytes

{

* PDST = (unsigned char) * psrc ;

i ;

}

NDSTLEN ;

}

}

// Output add a end value

* PDST = '/ 0';

Return ndstlen;

}

[related resources]

RFC / STD document: Internet FAQ Archives BHW98 column: http://www.9cbs.net/develop/author/netauthor/bhw98/

First release: 2003-06-23 Last revision: 2003-06-23

转载请注明原文地址:https://www.9cbs.com/read-23667.html

New Post(0)