Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Translate simple C code to Delphi 5

Posted on 2001-06-10
9
448 Views
Last Modified: 2010-04-06
/*
   Please translate this code to Delphi 5 . I need it as soon as possible.
 
   Requires libdes to compile.
*/

#include <stdlib.h>
#include <stdio.h>
#include "des.h"

int main(int argc, char *argv[])
{
     FILE *infile, *outfile;

     char *header = "Allaire Cold Fusion Template\012Header Size: ";
     char buffer[54];
     int headsize, outlen;
     int skip_header;
     int len, i;

     char *keystr = "Error: cannot open template file--\"%s\". Please, try again!\012\012";
     des_cblock key;
     des_cblock input;
     des_cblock output;
     des_key_schedule schedule;

     switch (argc)
     {
     case 2:
          outfile = stdout;
          break;

     case 3:
          if (!(outfile = fopen(argv[2], "wb")))
          {
               fprintf(stderr, "Error opening output file %s\n", argv[2]);
               return 1;
          }
          break;

     default:
          fprintf(stderr, "Usage: cfdecrypt <encrypted template> [output file]\n");
          return 1;
     }

     if (!(infile = fopen(argv[1], "rb")))
     {
          fprintf(stderr, "Error opening input file %s\n", argv[1]);
          return 1;
     }

     if ((fread(buffer, 1, 54, infile) < 54) || (memcmp(buffer, header, 42)))
     {
          fprintf(stderr, "File is not an encrypted template\n");
          return 1;
     }

     if (!memcmp(&buffer[42], "New Version", 11))
     {
          headsize = 69;
          skip_header = 1;
     }
     else
     {
          headsize = atoi(&buffer[42]);
          skip_header = 0;
     }

     if ((headsize < 54) || (fseek(infile, headsize, SEEK_SET) < 0))
     {
          fprintf(stderr, "Error in file format\n");
          return 1;
     }

     des_string_to_key(keystr, &key);
     des_set_key(&key, schedule);
     outlen = 0;

     while ((len = fread(input, 1, 8, infile)) == 8)
     {
          des_ecb_encrypt(&input, &output, schedule, 0);
          outlen += 8;
          i = 0;

          if (skip_header)
          {
               while (i < 8)
               {
                    if (output[i++] == 0x1A)
                    {
                         skip_header = 0;
                         break;
                    }
               }
          }

          fwrite(output + i, 1, 8 - i, outfile);
     }

     for (i = 0; i < len; i++)
     {
          output[i] = input[i] ^ (outlen + i);
     }

     fwrite(output, 1, len, outfile);

     fclose(outfile);
     fclose(infile);
     return 0;
}
0
Comment
Question by:MrNet
9 Comments
 
LVL 3

Expert Comment

by:alzv
ID: 6172933
Look... This code uses module with DES encryption procedures. I think it is impossible to translate it into Delphi code without having at least des.h header file. And I think it is fully impossible to translate it if des.h is not a header file for DLL which provides functions for working with DES encryption until you have another one for Delphi.

Will be happy to know if I am not right...

Best regards,
Alexey Zverev.
0
 

Author Comment

by:MrNet
ID: 6173125
ok Alexey,

I have exe file already compiled with the prevoius code. I made Delphi project which using the library SellAPI to run the exe with ShellExe() in delphi.

Every thing works fine. The exe run under DOS and the thing that I want happend. BUT I cannot recieve the error message (if error occured) from the exe into my delphi project.

So, You have the C code and you know Delphi. Tell me how to recieve the errors from DOS EXEs.

Thanks
0
 
LVL 20

Accepted Solution

by:
Madshi earned 300 total points
ID: 6173130
I'm not sure at all, what your real problem is. If I understand your last comment correctly, you are executing a DOS application and want to get the output back. If that is the case, then the following function will perhaps help you. However, I don't see any connection of this to your original question...

Regards, Madshi.

function StartPiped(cmdLine: string) : string;
var sa     : TSecurityAttributes;
    si     : TStartupInfo;
    pi     : TProcessInformation;
    pr, pw : dword;
    dw1    : dword;
begin
  result := '';
  with sa do begin
    nLength              := sizeOf(sa);
    bInheritHandle       := true;
    lpSecurityDescriptor := nil;
  end;
  CreatePipe(pr, pw, @sa, 0);
  try
    ZeroMemory(@si, sizeOf(si));
    with si do begin
      cb          := sizeOf(si);
      dwFlags     := STARTF_USESHOWWINDOW or STARTF_USESTDHANDLES;
      wShowWindow := SW_HIDE;
      hStdInput   := GetStdHandle(STD_INPUT_HANDLE);
      hStdOutput  := pw;
      hStdError   := pw;
    end;
    if CreateProcess(nil, pchar(cmdLine), nil, nil, true, 0, nil, nil, si, pi) then
      try
        WaitForSingleObject(pi.hProcess, INFINITE);
        if PeekNamedPipe(pr, nil, 0, nil, @dw1, nil) and (dw1 > 0) then begin
          SetLength(result, dw1);
          if (not ReadFile(pr, pointer(result)^, dw1, dw1, nil)) or (dw1 = 0) then
            result := '';
        end;
      finally
        CloseHandle(pi.hThread);
        CloseHandle(pi.hProcess);
      end;
  finally
    CloseHandle(pr);
    CloseHandle(pw);
  end;
end;
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:MrNet
ID: 6173246
OoOoh Madshi, Long time didn't see your answers !! ( I am the absent;) )

In my first question I asked to translate a code, but as alexey said it is impossible I decided to use my already made exe (under DOS) made by the above C code. The problem that I found it that I cannot retrieve the error messages comes out from the exe.

If the exe did his work without any problem, Nothing is output.

If the exe faced a problem, the error message will be the output.

you can see the error messages that may occure on my first question's code.

Regards
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6173465
:-)

Have you tried the function I've posted? It might do exactly what you want, namely return the error string a an empty string (for success).

Regards, Madshi.
0
 

Author Comment

by:MrNet
ID: 6173632
Madshi,

I tried you function. My system got HANG ;)

tell me how to use it.

I have the file decrypt.exe which receive 2 parameters like

d:\decrypt.exe d:\file1.txt d:\file2.txt

Regards
0
 
LVL 20

Expert Comment

by:Madshi
ID: 6173786
Does the DOS exe need input? In that case you can't use SW_HIDE, please try it with:

     wShowWindow := SW_SHOWNORMAL;

Regards, Madshi.
0
 
LVL 1

Expert Comment

by:Moondancer
ID: 6901335
ADMINISTRATION WILL BE CONTACTING YOU SHORTLY.  Moderators Computer101 or Netminder will return to finalize these if still open in seven days.  Please post closing recommendations before that time.

Question(s) below appears to have been abandoned. Your options are:
 
1. Accept a Comment As Answer (use the button next to the Expert's name).
2. Close the question if the information was not useful to you, but may help others. You must tell the participants why you wish to do this, and allow for Expert response.  This choice will include a refund to you, and will move this question to our PAQ (Previously Asked Question) database.  If you found information outside this question thread, please add it.
3. Ask Community Support to help split points between participating experts, or just comment here with details and we'll respond with the process.
4. Delete the question (if it has no potential value for others).
   --> Post comments for expert of your intention to delete and why
   --> You cannot delete a question with comments, special handling by a Moderator is required.

For special handling needs, please post a zero point question in the link below and include the URL (question QID/link) that it regards with details.
http://www.experts-exchange.com/jsp/qList.jsp?ta=commspt
 
Please click the Help Desk link on the left for Member Guidelines, Member Agreement and the Question/Answer process for further information, if needed.  http://www.experts-exchange.com/jsp/cmtyHelpDesk.jsp

Click you Member Profile to view your question history and keep them all current with updates as the collaboration effort continues, to track all your open and locked questions at this site.  If you are an EE Pro user, use the Power Search option to find them.  Anytime you have questions which are LOCKED with a Proposed Answer but does not serve your needs, please reject it and add comments as to why.  In addition, when you do grade the question, if the grade is less than an A, please add a comment as to why.  This helps all involved, as well as future persons who may access this item in the future to seek help.

To view your open questions, please click the following link(s) and keep them all current with updates.
http://www.experts-exchange.com/questions/Q.20027358.html
http://www.experts-exchange.com/questions/Q.20032511.html
http://www.experts-exchange.com/questions/Q.20107804.html
http://www.experts-exchange.com/questions/Q.20124987.html
http://www.experts-exchange.com/questions/Q.20132647.html
http://www.experts-exchange.com/questions/Q.20248142.html
http://www.experts-exchange.com/questions/Q.20273108.html


To view your locked questions, please click the following link(s) and evaluate the proposed answer.
http://www.experts-exchange.com/questions/Q.20124988.html

PLEASE DO NOT AWARD THE POINTS TO ME.  
 
------------>  EXPERTS:  Please leave any comments regarding your closing recommendations if this item remains inactive another seven (7) days.  Also, if you are interested in the cleanup effort, please click this link http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=commspt&qid=20274643

Moderators will finalize this question if still open in 7 days, by either moving this to the PAQ (Previously Asked Questions) at zero points, deleting it or awarding expert(s) when recommendations are made, or an independent determination can be made.  Expert input is always appreciated to determine the fair outcome.
 
Thank you everyone.
 
Moondancer
Moderator @ Experts Exchange

P.S.  For any year 2000 questions, special attention is needed to ensure the first correct response is awarded, since they are not in the comment date order, but rather in Member ID order.
0
 
LVL 14

Expert Comment

by:DragonSlayer
ID: 9285670
MrNet,
No comment has been added lately (526 days), so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area for this question:

RECOMMENDATION: Award points to Madshi http:#6173786

Please leave any comments here within 7 days.

-- Please DO NOT accept this comment as an answer ! --

Thanks,

DragonSlayer
EE Cleanup Volunteer
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question