PDA

Click to See Complete Forum and Search --> : Help needed, C to C#


p_f_t
Feb 27th, 2006, 01:51 AM
Hi everyone, I'm trying to convert some C code to work in C#. I run a Game server and I am writing some management software for handling player files.

These files are encoded with a key. I have the key and I have a working codec written in C so I can decrypt them.

The problem is that I have to read the files into the codec, the codec will only write the results back into a text file and then i have to read them again. There are over 1000 files so you can imagine the time that this takes.

I would like to incorporate this codec into my app to remove this lengthy process but I am relatively new to programming and I cannot get it to work, can anyone offer any assistance with translating it?

const char gene[] = "Gene";

void decode(const char *ifile, const char *ofile) {
int ifd, ofd, i, l, len, rc;
char *mem, *buff, c, k, r;

ifd = open(ifile,O_RDONLY);

if (ifd == -1) {
fprintf(stderr, "Error: open(%s) failed: %s\n", ifile, strerror(errno));
exit(1);
}

len = lseek(ifd, 0, SEEK_END);
lseek(ifd, 0, SEEK_SET);

mem = malloc(len + 1);
if (mem == NULL) {
fprintf(stderr, "Error: malloc(%d) failed: %s\n", len + 1, strerror(errno));
exit(1);
}

rc = read(ifd, mem, len);
if (rc != len) {
fprintf(stderr, "Error: read() failed: %s\n", strerror(errno));
exit(1);
}

close(ifd);

if (strncmp(mem, "FLS1", 4) != 0) {
fprintf(stderr, "Error: file %s is not a FLS1 file.\n", ifile);
exit(1);
}

ofd = open(ofile, O_CREAT | O_TRUNC | O_WRONLY, 0640);
if (ofd == -1) {
fprintf(stderr, "Error: open(%s) failed: %s\n", ofile, strerror(errno));
exit(1);
}

/* skip FLS1 */
buff = mem + 4;
l = len - 4;

i = 0;
while (i < l) {

c = buff[i];
k = (gene[i % 4] + i) % 256;

r = c ^ (k | 0x80);

rc = write(ofd, &r, 1);
if (rc != 1) {
fprintf(stderr, "Error: write() failed: %s\n", strerror(errno));
exit(1);
}

i++;
}
close(ofd);
}

Don't worry about the error handling or the file in and out, it's just the conversion process.

Thankyou in advance

Paul