線上訂房服務-台灣趴趴狗聯合訂房中心
發文 回覆 瀏覽次數:1052
推到 Plurk!
推到 Facebook!

請問關於郵件解碼的函式.謝謝

尚未結案
kagaya
中階會員


發表:74
回覆:175
積分:59
註冊:2002-12-28

發送簡訊給我
#1 引用回覆 回覆 發表時間:2004-01-05 00:15:21 IP:211.76.xxx.xxx 未訂閱
函式內容如下.是改自delphi的函式 在delphi裡可以解出所有郵件的主旨 可是在bcb裡有少部份主旨解出來是空白的 我不太了解是那裡改錯了.能否向各位大大請教.謝謝    
int Ord(char x){
        return (int)x;
}    String decode(String s){
          int s1,s2,s3,hex,step,j,byte_ptr,real_bytes;
          String t,v;
          char Encoding;
          byte a1[5],b1[4];            s1=s.Pos("=?");
        s2=1;
        hex=0;
        if(s1>0){
                for(s2=s.Length();s2>=1;s2--){
                        if(s.SubString(s2,2)=="?=")break;
                }
        }
        if(s1==0||s2==1){
                return "1:" s;
        }
        t=s.SubString(s1 2,s2-2-s1);
        s3=t.Pos("?");
        t=t.Delete(1,s3);
        if(t==""){
                return "2:" s;
        }
        Encoding=t[1];
        t=t.Delete(1,2);
        v="";
        step=0;
        switch(Encoding){
                case 'Q':
                      while(t!=""){
                                switch(step){
                                        case 0:
                                              if(t[1]=='_'){v =" ";}else
                                              if(t[1]=='='){step=1;}else
                                              {v =t[1];}
                                        break;
                                        case 1:
                                               if(t[1]<='9'){hex=(Ord(t[1])-Ord('0'))*16;}else
                                               {hex=(Ord(t[1])-55)*16;}
                                               step=2;
                                        break;
                                        case 2:
                                              if(t[1]<='9'){hex =(Ord(t[1])-Ord('0'));}else
                                              {hex =Ord(t[1])-55;}
                                              v =(char)hex;
                                              step=0;
                                        break;
                                }
                      t.Delete(1,1);
                      }
                break;
                case 'B':
                        byte_ptr=0;
                        for(j=1;j<=t.Length();j  ){
                                byte_ptr  ;   
                                if(t[j]>='A'&&t[j]<='Z'){a1[byte_ptr]=Ord(t[j])-65;}
                                if(t[j]>='a'&&t[j]<='z'){a1[byte_ptr]=Ord(t[j])-71;}
                                if(t[j]>='0'&&t[j]<='9'){a1[byte_ptr]=Ord(t[j]) 4;}
                                if(t[j]=' '){a1[byte_ptr]=62;}
                                if(t[j]='/'){a1[byte_ptr]=63;}
                                if(t[j]='='){a1[byte_ptr]=64;}
                                if(byte_ptr=4){
                                        byte_ptr=0;
                                        real_bytes=3;
                                        if(a1[1]==64){real_bytes=0;}
                                        if(a1[3]==64){a1[3]=0;a1[4]=0;real_bytes=1;}
                                        if(a1[4]==64){a1[4]=0;real_bytes=2;}
                                        b1[1]=a1[1]*4 floor(a1[2]/16);
                                        b1[2]=(a1[2])*16 floor(a1[3]/4);
                                        b1[3]=(a1[3]%4)*64 a1[4];
                                        if(real_bytes>0){v =(char)b1[1];}
                                        if(real_bytes>1){v =(char)b1[2];}
                                        if(real_bytes>2){v =(char)b1[3];}
                                }
                        }
                break;
        }            return s.SubString(1,s1-1) v s.SubString(s2 2,999);
}        ==========以下是delphi原內容==============
function decode(s: string): string;
  var
    s1, s2, s3: integer;
    t, v: string;
    Encoding: char;
    hex, step: integer;
    a1: array[1..4] of byte;
    b1: array[1..3] of byte;
    j: integer;
    byte_ptr, real_bytes: integer;
  begin
    s1 := Pos('=?', s);
    s2 := 1;
    hex := 0;
    if s1 > 0 then
    begin
      for s2 := Length(s) - 1 downto 1 do
      begin
        if Copy(s, s2, 2) = '?=' then Break;
      end;
    end;
    if (s1 = 0) or (s2 = 1) then
    begin
      Result := s;
      Exit;
    end;
    t := Copy(s, s1   2, s2 - 2 - s1);
    s3 := Pos('?', t);
    Delete(t, 1, s3);
    if (t = '') then
    begin
      Result := s;
      Exit;
    end;
    Encoding := t[1];
    Delete(t, 1, 2);
    v := '';
    step := 0;
    case Encoding of
      'Q':
        while t <> '' do
        begin
          case step of
            0:
              begin
                case t[1] of
                  '_': v := v   ' ';
                  '=': step := 1;
                else v := v   t[1];
                end;
              end;
            1:
              begin
                if t[1] <= '9' then hex := (Ord(t[1]) - Ord('0')) * 16
                else hex := (Ord(t[1]) - 55) * 16;
                step := 2;
              end;
            2:
              begin
                if t[1] <= '9' then hex := hex   (Ord(t[1]) - Ord('0'))
                else hex := hex   Ord(t[1]) - 55;
                v := v   Chr(hex);
                step := 0;
              end;
          end;
          Delete(t, 1, 1);
        end;
      'B':
        begin
          byte_ptr := 0;
          for j := 1 to Length(t) do
          begin
            Inc(byte_ptr);
            case t[j] of
              'A'..'Z': a1[byte_ptr] := Ord(t[j]) - 65;
              'a'..'z': a1[byte_ptr] := Ord(t[j]) - 71;
              '0'..'9': a1[byte_ptr] := Ord(t[j])   4;
              ' ': a1[byte_ptr] := 62;
              '/': a1[byte_ptr] := 63;
              '=': a1[byte_ptr] := 64;
            end;
            if byte_ptr = 4 then
            begin
              byte_ptr := 0;
              real_bytes := 3;
              if a1[1] = 64 then real_bytes := 0;
              if a1[3] = 64 then
              begin
                a1[3] := 0;
                a1[4] := 0;
                real_bytes := 1;
              end;
              if a1[4] = 64 then
              begin
                a1[4] := 0;
                real_bytes := 2;
              end;
              b1[1] := a1[1] * 4   (a1[2] div 16);
              b1[2] := (a1[2] mod 16) * 16   (a1[3] div 4);
              b1[3] := (a1[3] mod 4) * 64   a1[4];
              if (real_bytes > 0) then
                v := v   chr(b1[1]);
              if (real_bytes > 1) then
                v := v   chr(b1[2]);
              if (real_bytes > 2) then
                v := v   chr(b1[3]);
            end;
          end;
        end;
    end;
    Result := Copy(s, 1, s1 - 1)   v   Copy(s, s2   2, 999);
  end;         
------
KUSO 無處不在
kagaya
中階會員


發表:74
回覆:175
積分:59
註冊:2002-12-28

發送簡訊給我
#2 引用回覆 回覆 發表時間:2004-01-05 01:28:41 IP:211.76.xxx.xxx 未訂閱
我發現到4個地方有如下的錯誤 if(t[j]='=')  =>  if(t[j]=='=') 目前沒有郵件可測試 不知是否正確? 這是我這輩子第一次希望能趕快收到垃圾信件好讓我測試的一刻
------
KUSO 無處不在
kagaya
中階會員


發表:74
回覆:175
積分:59
註冊:2002-12-28

發送簡訊給我
#3 引用回覆 回覆 發表時間:2004-01-05 03:35:35 IP:211.76.xxx.xxx 未訂閱
測試ok 謝謝
------
KUSO 無處不在
系統時間:2024-05-02 13:23:21
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!