Mime quoted-printable Codec

Source: Internet
Author: User
Tags hex code

Quoted-printable is also one of the common encoding methods in mime mail. Like base64, it also encodes the input string or data into printable ASCII strings.

Quoted-printable: the input data is in the range of 33-60 and 62-126 and is directly output; others must be encoded as "=" plus two bytes of hex code (uppercase ). To ensure that the output line does not exceed the specified length, you can add the "=/R/N" sequence at the end of the line as the soft carriage return.

Int encodequoted (const unsigned char * psrc, char * pdst, int nsclen, int nmaxlinelen) {int ndstlen; // The output character count int nlinelen; // The output row length count ndstlen = 0; nlinelen = 0; For (INT I = 0; I <nsclen; I ++, psrc ++) {// ASCII 33-60, 62-126 is output, and the rest must be encoded if (* psrc> = '! ') & (* Psrc '~ ') & (* Psrc! = ') {* Pdst ++ = (char) * psrc; ndstlen ++; nlinelen ++;} else {sprintf (pdst, "= % 02x ", * psrc); pdst + = 3; ndstlen + = 3; nlinelen + = 3;} // output line feed? If (nlinelen> = nmaxlinelen-3) {sprintf (pdst, "=/R/N"); pdst + = 3; ndstlen + = 3; nlinelen = 0 ;}} // Add the end character * pdst = '/0' to the output; return ndstlen ;}

Quoted-printable decoding is simple, and the encoding process can be reversed.

Int decodequoted (const char * psrc, unsigned char * pdst, int nsclen) {int ndstlen; // count the output characters int I; I = 0; ndstlen = 0; while (I <nsclen) {If (strncmp (psrc, "=/R/N", 3) = 0) // press enter to skip {psrc + = 3; I + = 3;} else {If (* psrc = ') // The encoded byte {sscanf (psrc, "= % 02x", pdst ); pdst ++; psrc + = 3; I + = 3;} else // unencoded byte {* pdst ++ = (unsigned char) * psrc ++; I ++ ;}ndstlen ++ ;}// the output end character * pdst = '/0'; return ndstlen ;}

 

[Related resources]
  • RFC/STD documentation: Internet FAQ Archives
  • Bhw98 columns: http://www.csdn.net/develop/author/netauthor/bhw98/

    First Release: 2003-06-23
    Last revised:

     

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.