Decoding of POP3 messages in C #

Source: Internet
Author: User
Tags base64
Base64 and the quoted-printable that are to be described below belong to MIME
(Multi-part (multi-part), multimedia email and WWW hypertext
A coding standard for transferring non-text numbers such as graphics, sounds, and Faxes
According to). MIME is defined in RFC1341.
Base64 is one of the most widely used codes on the internet today, almost
Some email software headers take it as the default binary encoding and it has become
A synonym for today's email coding.
The following is an example of Base64, which you can also see from the example
Base64 close contact with email:
content-type:text/plain;charset= "CN-GB"
Content-transfer-encoding:base64
Cqkjicagikg2wtlc68vjt6i088irobcncgnx99xfom1vz2fvo6yw19tgu8a619w+o6h0zwxuzxq6

Ly8ymdiumteyljiwljezmjoym6ops8nusagjdqojicagicagxkq438jtvp65pnf3ytkjumh0dha6

Ly9tb2dhby5izw50axvulm5lda0kcqkjrw1hawx0bzptb2dhb0aznzeubmv0dqojicagkioqkioq

Kioqkioqkioqkioqkioqkioqkioqkioqkioqkioqkioqkioqkioqicagicagicagicagicagdqoj

Icagkicz/chlvmfs5mqyw7s2vlk7tpjx36oss/3by9fjvkpksso0tryyu8h0z8iqdqojicagkioq

Kioqkioqkioqkioqkioqkioqkioqkioqkioqkioqkioqkioqkioqkioq
You can save it as a single file, which can be named: mogao.eml,
Double-click to open with Outlook (original information for the first two behavior messages, from line fourth
Start as encoded content).
The BASE64 algorithm is close to the uuencode algorithm and is simple: it will
The character stream order is placed in a 24-bit buffer, where the missing characters are filled with 0. and
The buffer is truncated to 4 parts, the high is first, each part is 6 bits,
Re-ABCDEFGHIJKLMNOPQRSTUVWXYZABCDEFGHIJKLMNOPQRSTUV with the following 64 characters: "
wxyz0123456789+/".
If the input is only one or two bytes, the output is replenished with the equal sign "=".
This can partition the additional information resulting in coding confusion. It is typically 76 characters per line.
Below I give the BASE64 encoding and decoding of the C language description:
/*BASE64 Code * *
void Base64 (unsigned char chasc[3],unsigned char chuue[4])
/*
CHASC: Encoded binary code
CHUUE: Encoded BASE64 code
*/
{
int i,k=2;
unsinged Char T=null;
for (i=0;i<3;i++)
{
* (chuue+i) =* (chasc+i) >>k;
* (chuue+i) |=t;
t=* (chasc+i) << (8-k);
t>>=2;
k+=2;
}
* (chuue+3) =* (chasc+2) &63;
for (i=0;i<4;i++)
if ((* (chuue+i) >=0) && (* (chuue+i) <=25)) * (chuue+i) +=65;
else if ((* (chuue+i) >=26) && (* (chuue+i) <=51)) * (chuue+i) +=71;
else if ((* (chuue+i) >=52) && (* (chuue+i) <=61)) * (chuue+i)-=4;
else if (* (chuue+i) ==62) * (chuue+i) = 43;
else if (* (chuue+i) ==63) * (chuue+i) = 47;
}
/*BASE64 Decoding * *
void unBase64 (unsigned char chuue[4],unsigned char chasc[3])
/*
Chuue: Base64 Code not decoded
CHASC: Decoded binary code
*/
{int i,k=2;
unsigned char t=null;
for (i=0;i<4;i++)
if ((* (chuue+i) >=65) && (* (chuue+i) <=90)) * (chuue+i)-=65;
else if ((* (chuue+i) >=97) && (* (chuue+i) <=122)) * (chuue+i)-=71;
else if ((* (chuue+i) >=48) && (* (chuue+i) <=57)) * (chuue+i) +=4;
else if (* (chuue+i) ==43) * (chuue+i) = 62;
else if (* (chuue+i) ==47) * (chuue+i) = 63;
else if (* (chuue+i) ==61) * (chuue+i) = 0;
for (i=0;i<3;i++)
{* (chhex+i) =* (chuue+i) <<k;
k+=2;
t=* (chuue+i+1) >>8-k;
* (chhex+i) |=t;
}
}
4. quoted-printable
Quoted-printable short QP, generally used in the email system. It
A 8-bit character encoding typically used in small amounts of text, such as Foxmail
It does the coding of the subject and the letter body. This type of coding should be well identified:
It has a large number of "=". Here is an example of it:
mime-version:1.0
Content-transfer-encoding:quoted-printable
=a1=b6=c2=d2=c2=eb=cb=e3=b7=a8=b4=f3=c8=ab=a1=b7
=d7=f7=d5=df:mogao=a3=ac=b0=d7=d4=c6=bb=c6=ba=d7=d5=be=a3=a8telnet://202.11
2.20.132:23=a3=a9=b3=c9=d4=b1=a1=a3
=c4=aa=b8=df=c8=ed=bc=fe=b9=a4=d7=f7=ca=d2=a3=bahttp://mogao.bentiun.
Net
Emailto:mogao@371.net
*********************************************
* =b3=fd=c1=cb=bc=c7=d2=e4=ca=b2=c3=b4=b6=bc=b2=bb=b4=f8=d7=df=a3=ac=b3=
fd=c1=cb=d7=e3=bc=a3=ca=b2=c3=b4=b6=bc=b2=bb=c1=f4=cf=c2*
*********************************************
You can save it as a single file named: Mogao.eml,
Double-click to open with Outlook (the original information for the first two behavior messages, from the
The four lines begin as encoded content).
QP's algorithm can be said to be the simplest or the lowest coding efficiency
(Its coding rate is 1:3), it is specifically designed to handle 8-bit characters.
Its algorithm is: read a character, if the ASCII code is greater than 127, that is, the character's
The 8th digit is 1, which is encoded, otherwise ignored (sometimes 7-bit character encoding).
The code is very simple, look at the following C language description can be:
/*QP Code * *
void Qp (unsigned char sour,unsigned char first,unsigned char second)
/*
Sour: Characters to encode
First: one character after encoding
Second: The second character after encoding
The second for the return value
*/
{
if (sour>127)
{first=sour>>4;
second=sour&15;
if (first>9) first+=55;
else first+=48;
if (second>9) second+=55;
else second+=48;
printf ("%c%c%c", ' = ', first,second);
}
}
/*QP Decoding * *
void Uqp (unsigned char sour,unsigned char first,unsigned char second)
/*
Sour: Characters after decoding
The first character of the FIRST:QP code
The second character of the SECOND:QP code
Sour is the return value
*/
{
if (first>=65) first-=55;
else first-=48;
if (second>=65) second-=55;
else second-=48;
Sour=null;
sour=first<<4;
Sour|=second;
}
Now you know why QP's coding rate is so low! About QP.
Detailed descriptions and exact definitions can be found in RFC2045.

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.