MIME 格式是一种压缩与解压缩的算法,SMTP 都用它,如果需要我可以给你两个源文件,很简单的

解决方案 »

  1.   

    #ifndef _MIME_H
    #define _MIME_H#define MIMELINELEN 72/* bit values for MimeMode */
    #define MM_CVTMIME 0x0001 /* convert 8 to 7 bit MIME */
    #define MM_PASS8BIT 0x0002 /* just send 8 bit data blind */
    #define MM_MIME8BIT 0x0004 /* convert 8-bit data to MIME *//*
    **  Flags passed to mime8to7.
    */#define M87F_OUTER 0 /* outer context */
    #define M87F_NO8BIT 0x0001 /* can't have 8-bit in this section */
    #define M87F_DIGEST 0x0002 /* processing multipart/digest */
    #define M87F_NO8TO7 0x0004 /* don't do 8->7 bit conversions */
    int  MimeInit();
    int  MimeEnd();size_t mime8to7(char *buf , char *data , long st);
    size_t mime7to8(char *buf , char *data , long st);#endif#include "stdafx.h"
    #include <string.h>
    #include <stdio.h>
    #include <stdlib.h>
    #include "mime.h"static char Base64Code[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";int isbase64code(int c)
    {
    //return ((strchr(Base64Code,c) == NULL) || ( c == 0) ? 0 : 1);
    if(c!='='){
    if(strchr(Base64Code,c)==NULL)
    return 0;
    if(c==0)
    return 0;
    }
    return 1;
    }/*
    *Encode 
    */
    size_t mime8to7(char *buf , char *data , long st)
    {
    unsigned char c1,c2;
    register char *p;
    register char *bp;
    char          bbuf[128]; //keep every line
    long        linelen; //only for splite line
    long        len;

    *bbuf=0;
    p=data;
    bp=buf;
    linelen = 0; while( p - data < st)
    {
    c1 = *p++;
    /*
    if (linelen >= MIMELINELEN)  //every line = 72
    {
    *bp = '\0';
    buf = putline(buf, bbuf);
    linelen = 0;
    bp = bbuf;
    }
    linelen += 4;
    */

    *bp++ = Base64Code[(c1 >> 2)];
    c1 = (c1 & 0x03) << 4; if (p - data >= st)
    {
    *bp++ = Base64Code[c1];
    *bp++ = '=';
    *bp++ = '=';
    break;
    }
    else
    c2 = *p++; c1 |= (c2 >> 4) & 0x0f;
    *bp++ = Base64Code[c1];
    c1 = (c2 & 0x0f) << 2; if (p - data >= st)
    {
    *bp++ = Base64Code[c1];
    *bp++ = '=';
    break;
    }
    else
    c2 = *p++; c1 |= (c2 >> 6) & 0x03;
    *bp++ = Base64Code[c1];
    *bp++ = Base64Code[c2 & 0x3f];
    }
    *bp = '\0';
    len = bp - buf;
    //printf("%s\n",buf);
    //putline(buf, bbuf);
    return len;
    }
    /*
    *decode
    */
    static char index_64[128] =
    {
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,-1,
    -1,-1,-1,-1, -1,-1,-1,-1, -1,-1,-1,62, -1,-1,-1,63,
    52,53,54,55, 56,57,58,59, 60,61,-1,-1, -1,-1,-1,-1,
    -1, 0, 1, 2,  3, 4, 5, 6,  7, 8, 9,10, 11,12,13,14,
    15,16,17,18, 19,20,21,22, 23,24,25,-1, -1,-1,-1,-1,
    -1,26,27,28, 29,30,31,32, 33,34,35,36, 37,38,39,40,
    41,42,43,44, 45,46,47,48, 49,50,51,-1, -1,-1,-1,-1
    };#define CHAR64(c)  (((c) < 0 || (c) > 127) ? -1 : index_64[(c)])size_t mime7to8(char *buf , char *data , long st)
    {
    unsigned char c1,c2,c3,c4;
    register char *p;
    register char *fbufp;
    long        len;

    len = 0;
    fbufp = buf;
    p = data; //printf("st: %ld\n",st); while ( p - data <= st)
    {
    //printf("%d ",p-data);
    c1 = *p++;
    if (!isbase64code(c1))
    continue; do
    {
    if (p - data > st)
    break;
    else
    c2 = *p++;
    } while (!isbase64code(c2));
    if (p - data > st)
    break; do
    {
    if (p - data > st)
    break;
    else
    c3 = *p++;
    } while (!isbase64code(c3));
    if (p - data > st)
    break; do
    {
    if (p - data > st)
    break;
    else
    c4 = *p++;
    } while (!isbase64code(c4));
    if (p - data > st)
    break; if (c1 == '=' || c2 == '=')
    continue; c1 = CHAR64(c1);
    c2 = CHAR64(c2); *fbufp = (c1 << 2) | ((c2 & 0x30) >> 4);
    fbufp++; if (c3 == '=')
    continue; c3 = CHAR64(c3);
    *fbufp = ((c2 & 0x0f) << 4) | ((c3 & 0x3c) >> 2);
    fbufp++;
    if (c4 == '=')
    continue; c4 = CHAR64(c4);
    *fbufp = ((c3 & 0x03) << 6) | c4;
    fbufp++;
    }
    *fbufp = '\0'; len = fbufp - buf;
    return len;
    }
      

  2.   

    谢谢patrickgamp2,可否简单说一下程序处理的内容?
    MIME是不是有多种编码方法:Base64,QP,8-bit。请问您的程序是对那种编码有效。
      

  3.   

    Base64,其实你细看一下程序就知道了