Q&A

  • 'Base64: Incorrect string format'라는 에러?
nmpop3컴포넌트를 이용해 데이터베이스에 15초마다저장하는 소스입니다.

영문은 저장이 잘 되는데 한글은 글자가 깨져서 저장이 됩니다.

base64decode소스를 구해 적용시켜보았는데 'Base64: Incorrect string format'라는

메시지가 나오면서 에러가 납니다. 어떻게 해야 하는지 답답합니다.





-------------------------------------

unit Unit1;



interface



uses

Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,

Psock, NMpop3, StdCtrls, ExtCtrls, Db, ADODB;



type

TForm1 = class(TForm)

Label4: TLabel;

NMPOP31: TNMPOP3;

Timer1: TTimer;

ADOQuery1: TADOQuery;

ADOQuery2: TADOQuery;

procedure FormCreate(Sender: TObject);

procedure Timer1Timer(Sender: TObject);

function Base64Decode(S: string): string;

private

{ Private declarations }

public



{ Public declarations }

end;



var

Form1: TForm1;



implementation



{$R *.DFM}





const

B64Table = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';







procedure TForm1.FormCreate(Sender: TObject);

var

i : integer;

j : integer;

messagebody : string;

messagebodylist : tstringlist;

check : string;

s : string;

begin

nmpop31.attachfilepath := '.';

nmpop31.deleteonread := false;

nmpop31.ReportLevel := status_basic;

nmpop31.TimeOut := 20000;

nmpop31.Host := 'mail.hibi.or.kr';

nmpop31.port := 110;

nmpop31.userid := 'ns';

nmpop31.Password := 'wmfrjdns';

nmpop31.Connect;

label4.caption := 'count of messages: ' +inttostr(nmpop31.mailcount);

if nmpop31.mailcount>= 1 then

begin

for i :=1 to nmpop31.mailcount do

begin

nmpop31.getmailmessage(i);

check := nmpop31.mailmessage.subject;

check := copy(check,2,8);

if check='Returned' then continue;

s := nmpop31.mailmessage.Subject;

adoquery1.parameters.paramvalues['m_subject'] := base64decode(s);

adoquery1.parameters.paramvalues['m_name'] := nmpop31.mailmessage.from;

messagebodylist := nmpop31.mailmessage.body;

for j:=0 to messagebodylist.count-1 do

begin

s := messagebodylist[j];

messagebody := messagebody+base64decode(s);

end;

adoquery1.parameters.paramvalues['m_content'] := messagebody;

adoquery2.parameters.paramvalues['m_content'] := '%'+messagebody+'%';

adoquery2.open;

if adoquery2.recordcount=0 then

begin

adoquery1.execsql;

end;

adoquery2.close;

messagebody:='';

end;

end;

nmpop31.disconnect;

end;



procedure TForm1.Timer1Timer(Sender: TObject);

var

i : integer;

j : integer;

messagebody : string;

messagebodylist : tstringlist;

check : string;

s : string;

begin

nmpop31.attachfilepath := '.';

nmpop31.deleteonread := false;

nmpop31.ReportLevel := status_basic;

nmpop31.TimeOut := 20000;

nmpop31.Host := 'mail.hibi.or.kr';

nmpop31.port := 110;

nmpop31.userid := 'ns';

nmpop31.Password := 'wmfrjdns';

nmpop31.Connect;

label4.caption := 'count of messages: ' +inttostr(nmpop31.mailcount);

label4.Caption :='ontimer';

if nmpop31.mailcount>= 1 then

begin

for i :=1 to nmpop31.mailcount do

begin

nmpop31.getmailmessage(i);

check := nmpop31.mailmessage.subject;

check := copy(check,2,8);

if check='Returned' then continue;

s := nmpop31.mailmessage.subject;

adoquery1.parameters.paramvalues['m_subject'] := base64decode(s);

adoquery1.parameters.paramvalues['m_name'] := nmpop31.mailmessage.from;

messagebodylist := nmpop31.mailmessage.body;

for j:=0 to messagebodylist.count-1 do

begin

s := messagebodylist[j];

messagebody := messagebody+base64decode(s);

end;

adoquery1.parameters.paramvalues['m_content'] := messagebody;

adoquery2.parameters.paramvalues['m_content'] := '%'+messagebody+'%';

adoquery2.open;

if adoquery2.recordcount=0 then

begin

adoquery1.execsql;

end;

adoquery2.close;

messagebody:='';

end;

end;

nmpop31.disconnect;

end;



function tform1.Base64Decode(S: string): string;

var

OutBuf: array[0..2] of Byte;

InBuf : array[0..3] of Byte;

iI, iJ: Integer;

begin

if Length(S) mod 4 <> 0 then

raise Exception.Create('Base64: Incorrect string format');

SetLength(Result, ((Length(S) div 4) - 1) * 3);

for iI := 1 to (Length(S) div 4) - 1 do begin

Move(S[(iI - 1) * 4 + 1], InBuf, 4);

for iJ := 0 to 3 do

case InBuf[iJ] of

43: InBuf[iJ] := 62;

48..57: Inc(InBuf[iJ], 4);

65..90: Dec(InBuf[iJ], 65);

97..122: Dec(InBuf[iJ], 71);

else

InBuf[iJ] := 63;

end;

OutBuf[0] := (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $3);

OutBuf[1] := (InBuf[1] shl 4) or ((InBuf[2] shr 2) and $F);

OutBuf[2] := (InBuf[2] shl 6) or (InBuf[3] and $3F);

Move(OutBuf, Result[(iI - 1) * 3 + 1], 3);

end;

if Length(S) <> 0 then begin

Move(S[Length(S) - 3], InBuf, 4);

if InBuf[2] = 61 then begin

for iJ := 0 to 1 do

case InBuf[iJ] of

43: InBuf[iJ] := 62;

48..57: Inc(InBuf[iJ], 4);

65..90: Dec(InBuf[iJ], 65);

97..122: Dec(InBuf[iJ], 71);

else

InBuf[iJ] := 63;

end;

OutBuf[0] := (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $3);

Result := Result + Char(OutBuf[0]);

end else if InBuf[3] = 61 then begin

for iJ := 0 to 2 do

case InBuf[iJ] of

43: InBuf[iJ] := 62;

48..57: Inc(InBuf[iJ], 4);

65..90: Dec(InBuf[iJ], 65);

97..122: Dec(InBuf[iJ], 71);

else

InBuf[iJ] := 63;

end;

OutBuf[0] := (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $3);

OutBuf[1] := (InBuf[1] shl 4) or ((InBuf[2] shr 2) and $F);

Result := Result + Char(OutBuf[0]) + Char(OutBuf[1]);

end else begin

for iJ := 0 to 3 do

case InBuf[iJ] of

43: InBuf[iJ] := 62;

48..57: Inc(InBuf[iJ], 4);

65..90: Dec(InBuf[iJ], 65);

97..122: Dec(InBuf[iJ], 71);

else

InBuf[iJ] := 63;

end;

OutBuf[0] := (InBuf[0] shl 2) or ((InBuf[1] shr 4) and $3);

OutBuf[1] := (InBuf[1] shl 4) or ((InBuf[2] shr 2) and $F);

OutBuf[2] := (InBuf[2] shl 6) or (InBuf[3] and $3F);

Result := Result + Char(OutBuf[0]) + Char(OutBuf[1]) + Char(OutBuf[2]);

end;

end;

end;







end.

0  COMMENTS