Recent

Author Topic: DCPCrypt2.pas throws exception when compiling for WinCE  (Read 9191 times)

The ZipGenius Team

  • New member
  • *
  • Posts: 7
DCPCrypt2.pas throws exception when compiling for WinCE
« on: May 19, 2010, 10:49:47 am »
Hello.
I'm using Lazarus 0.9.29 + FPC 2.4.1.
I'm trying to compile a project for Windows Mobile and everything works fine, except when it comes to use the InitStr method of DCPCrypt cypher.
When I click a button that executes

Code: [Select]
DCP_blowfish1.InitStr('test',TDCP_sha1);
The program shows a message error stating "Unable to allocate memory for hash digest".
I looked into dcpcrypt2.pas source code and found the InitStr code to be:
Code: [Select]
procedure TDCP_cipher.InitStr(const Key: string; HashType: TDCP_hashclass);
var
  Hash: TDCP_hash;
  Digest: pointer;
begin
  if fInitialized then
    Burn;
  try
    GetMem(Digest,HashType.GetHashSize div 8);
    Hash:= HashType.Create(Self);
    Hash.Init;
    Hash.UpdateStr(Key);

    Hash.Final(Digest^);

    Hash.Free;
    if MaxKeySize< HashType.GetHashSize then
    begin
      Init(Digest^,MaxKeySize,nil);
    end
    else
    begin
      Init(Digest^,HashType.GetHashSize,nil);
    end;

    FillChar(Digest^,HashType.GetHashSize div 8,$FF);
    FreeMem(Digest);
  except
    raise EDCP_cipher.Create('Unable to allocate sufficient memory for hash digest');
  end;
end;          

If I replace the "except"  part with
Code: [Select]
...
finally

end;
I can finally see what exception is being fired: it is the "Bus error or misaligned data access".
I read the wili about this error message but I can't understand what should I do.

Any help, please?
« Last Edit: May 19, 2010, 11:05:05 am by ZipGenius »
Matteo Riso
http://www.zipgenius.it
Happy ZipGenius User #1

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: DCPCrypt2.pas throws exception when compiling for WinCE
« Reply #1 on: May 26, 2010, 05:49:08 am »
The error is most likely in one of the routines which TDCP_cipher.InitStr calls and not in itself ... maybe you could add debug information, such as DebugLn from the LCLProc unit, to check which exact instruction causes the exception, you can starting putting DebugLN between every instruction in this function and then check the method which it calls and so on.

The ZipGenius Team

  • New member
  • *
  • Posts: 7
Re: DCPCrypt2.pas throws exception when compiling for WinCE
« Reply #2 on: May 26, 2010, 03:04:34 pm »
Thank you Felipe :)
But... I am a total newbie in Lazarus+FreePascal development for WinCE: how could I debug the application by using the Windows Mobile emulator?

Thank you (again) for your patience ;)
Matteo Riso
http://www.zipgenius.it
Happy ZipGenius User #1

felipemdc

  • Administrator
  • Hero Member
  • *
  • Posts: 3538
Re: DCPCrypt2.pas throws exception when compiling for WinCE
« Reply #3 on: May 26, 2010, 05:33:24 pm »
Just use DebugLN. It recent Lazarus 0.9.29+ this function will write the debug information to a file 1.log or 1.txt in the same directory as  the executable. You just run the program and do whatever is necessary to make the code you want to see executed gets run and close the app and you can see the debug info in the log file which you can open in the PDA or copy to your computer and open there.

Code: [Select]
uses LCLProc; // Could be another unit too, can't remember

procedure TDCP_cipher.InitStr(const Key: string; HashType: TDCP_hashclass);
var
  Hash: TDCP_hash;
  Digest: pointer;
begin
  if fInitialized then
    Burn;
  DebugLn('Checkpoint 1');
  try
    GetMem(Digest,HashType.GetHashSize div 8);
    DebugLn('Checkpoint 2');
    Hash:= HashType.Create(Self);
    Hash.Init;
    Hash.UpdateStr(Key);
    DebugLn('Checkpoint 3');

    Hash.Final(Digest^);

    DebugLn('Checkpoint 4');
    Hash.Free;
    if MaxKeySize< HashType.GetHashSize then
    begin
      DebugLn('Checkpoint 5');
      Init(Digest^,MaxKeySize,nil);
    end
    else
    begin
      DebugLn('Checkpoint 6');
      Init(Digest^,HashType.GetHashSize,nil);
    end;

    DebugLn('Checkpoint 7');
    FillChar(Digest^,HashType.GetHashSize div 8,$FF);
    DebugLn('Checkpoint 8');
    FreeMem(Digest);
    DebugLn('Checkpoint 9');
  except
    raise EDCP_cipher.Create('Unable to allocate sufficient memory for hash digest');
  end;
end;          

 

TinyPortal © 2005-2018