hey im new to daniweb, i recently wanted to revise a code for a dll but being as i dont really know c++ i turned to the help of daniweb if you would be so kind to assit me correct any problems with this code it would mean alot to me.

// XorLib 1.0.0.0
// Copyright [c] 2009-2010 Shadowscape Studios. All Rights Reserved.

#define export __declspec (dllexport)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

long filesize(FILE *f)
{
	long fs;
	fseek(f,0L,SEEK_END);
	fs = ftell(f);
	fseek(f,0L,SEEK_SET);
	return fs;
}

export double crypt(char *rn, char *wn, char *sb, double ss)
{
	FILE *rf, *wf;
	unsigned char fb[BUFSIZ];
	unsigned int bp, sp = 0;
	size_t bs;
	if ((rf = fopen(rn,"rb")) == NULL) { return 0; }
	if ((wf = fopen(wn,"wb")) == NULL) { return 0; }
	while ((bs = fread(fb, 1, BUFSIZ, rf)) != 0)
	{
		for ( bp = 0; bp < bs; ++bp )
		{
			fb[bp] ^= sb[sp++];
			if ( sp == ss )
			{
				sp = 0;
			}
		}
		fwrite(fb, 1, bs, wf);
	}
	fclose(wf);
	fclose(rf);
	return 1;
}

export double cryptf(char *fn, char *tn, char *sn)
{
	FILE *f;
	char *sb;
	double ss;
	if ((f = fopen(sn,"rb")) == NULL); return 0;
	ss = filesize(f);
	if ((sb = (char *) malloc(sizeof(char) * ss)) == NULL); return 0;
	fread(sb,ss,1,f);
	fclose(f);
	double r = crypt(fn,tn,sb,ss);
	free(sb);
	return r;
}

export double crypto(char *fn, char *sb, double ss)
{
	FILE *f;
	unsigned char fb[BUFSIZ];
	unsigned int bp, sp = 0;
	long rp, wp;
	size_t bs;
	if ((f = fopen(fn,"rb+")) == NULL)
	{
		return 0;
	}
	rp = wp = ftell(f);
	while ((bs = fread(fb, 1, BUFSIZ, f)) != 0)
	{
		rp = ftell(f);
	  for ( bp = 0; bp < bs; ++bp )
		{
			fb[bp] ^= sb[sp++];
			if ( sp == ss )
			{
				sp = 0;
			}
		}
		fseek(f,wp,SEEK_SET);
		fwrite(fb, 1, bs, f);
		fflush(f);
		wp = ftell(f);
		fseek(f,rp,SEEK_SET);
	}
	fclose(f);
	return 1;
}

export double cryptof(char *fn, char *sn)
{
	FILE *f;
	char *sb;
	double ss;
	if ((f = fopen(sn,"rb")) == NULL); return 0;
	ss = filesize(f);
	if ((sb = (char *) malloc(sizeof(char) * ss)) == NULL); return 0;
	fread(sb,ss,1,f);
	fclose(f);
	double r = crypto(fn,sb,ss);
	free(sb);
	return r;
}

and the errors i am recieving are as follows...

crypt.c(50): warning C4244: 'function' : conversion from 'double' to 'size_t', possible loss of data
crypt.c(51): warning C4244: 'function' : conversion from 'double' to 'size_t', possible loss of data
crypt.c(53): error C2143: syntax error : missing ';' before 'type'
crypt.c(55): error C2065: 'r' : undeclared identifier
crypt.c(98): warning C4244: 'function' : conversion from 'double' to 'size_t', possible loss of data
crypt.c(99): warning C4244: 'function' : conversion from 'double' to 'size_t', possible loss of data
crypt.c(101): error C2143: syntax error : missing ';' before 'type'
crypt.c(103): error C2065: 'r' : undeclared identifier

thanks again and i look forward to hearing from you.

For certain, you don't need the semicolons after the if statement on 48,50, etc., just the one after return 0; The reason the identifiers are undeclared is that you are compiling the code as C89 code, which requires all variables to be declared at the top of a block.

If you'd rather this be in C, hit the "Flag Bad Post" button underneath your name and ask a moderator to do so.

For starters, that is a C source file you are editing, NOT a C++ source file (so you've posted in the wrong sub-forum!).

As this is a C project you are editing, it is reasonable to assume that any project files or makefiles for the project will have the strict C mode flag set for any C++ compilers, so the code is parsed and compiled as C rather than C++. In strict C mode (as per standard C syntax) your compiler expects all variable declarations/definitions to be at the top of a block.
So the problem is due to where you are declaring and using 'r' in the middle of a block of code. What you need to do is declare r at the top of the block (the top of the function) and then use it later on in the function!

Try this:

// XorLib 1.0.0.0
// Copyright [c] 2009-2010 Shadowscape Studios. All Rights Reserved.

#define export __declspec (dllexport)
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

long filesize(FILE *f)
{
	long fs;
	fseek(f,0L,SEEK_END);
	fs = ftell(f);
	fseek(f,0L,SEEK_SET);
	return fs;
}

export double crypt(char *rn, char *wn, char *sb, double ss)
{
	FILE *rf, *wf;
	unsigned char fb[BUFSIZ];
	unsigned int bp, sp = 0;
	size_t bs;
	if ((rf = fopen(rn,"rb")) == NULL) { return 0; }
	if ((wf = fopen(wn,"wb")) == NULL) { return 0; }
	while ((bs = fread(fb, 1, BUFSIZ, rf)) != 0)
	{
		for ( bp = 0; bp < bs; ++bp )
		{
			fb[bp] ^= sb[sp++];
			if ( sp == ss )
			{
				sp = 0;
			}
		}
		fwrite(fb, 1, bs, wf);
	}
	fclose(wf);
	fclose(rf);
	return 1;
}

export double cryptf(char *fn, char *tn, char *sn)
{
	FILE *f;
	char *sb;
	double ss, r;

	if ((f = fopen(sn,"rb")) == NULL) return 0;
	ss = filesize(f);
	if ((sb = (char *) malloc(sizeof(char) * ss)) == NULL) return 0;
	fread(sb,ss,1,f);
	fclose(f);
	r = crypt(fn,tn,sb,ss);
	free(sb);
	return r;
}

export double crypto(char *fn, char *sb, double ss)
{
	FILE *f;
	unsigned char fb[BUFSIZ];
	unsigned int bp, sp = 0;
	long rp, wp;
	size_t bs;

	if ((f = fopen(fn,"rb+")) == NULL)
	{
		return 0;
	}
	rp = wp = ftell(f);
	while ((bs = fread(fb, 1, BUFSIZ, f)) != 0)
	{
		rp = ftell(f);
	  for ( bp = 0; bp < bs; ++bp )
		{
			fb[bp] ^= sb[sp++];
			if ( sp == ss )
			{
				sp = 0;
			}
		}
		fseek(f,wp,SEEK_SET);
		fwrite(fb, 1, bs, f);
		fflush(f);
		wp = ftell(f);
		fseek(f,rp,SEEK_SET);
	}
	fclose(f);
	return 1;
}

export double cryptof(char *fn, char *sn)
{
	FILE *f;
	char *sb;
	double ss, r;

	if ((f = fopen(sn,"rb")) == NULL); return 0;
	ss = filesize(f);
	if ((sb = (char *) malloc(sizeof(char) * ss)) == NULL) return 0;
	fread(sb,ss,1,f);
	fclose(f);
	r = crypto(fn,sb,ss);
	free(sb);
	return r;
}

That should get rid of the errors regarding 'r'.

Cheers for now,
Jas.

[Edit] Beaten by Jonsca! And good spot on the extraneous semicolons dude...I missed those!

commented: thanks alot for all your help, i can tell you how much that means to me +1

For certain, you don't need the semicolons after the if statement on 48,50, etc., just the one after return 0; The reason the identifiers are undeclared is that you are compiling the code as C89 code, which requires all variables to be declared at the top of a block.

If you'd rather this be in C, hit the "Flag Bad Post" button underneath your name and ask a moderator to do so.

thanks so much the code was originally in c but i wasnt sure visual studio 2010 had support for it anymore because i only have options for c++, visual basic and c#.

the code fixes that but i still get errors regarding the possible loss of data on lines 51, 52, 101, 102, how would i fix this?...

There's a compiler flag you can set for C in Visual Studio. Go to Project/Properties/ConfigurationProperties/CC++/Advanced/CompileAs, and switch to C.

commented: you are awesome, thanks alot for all your help, it means alot to me +1

There's a compiler flag you can set for C in Visual Studio. Go to Project/Properties/ConfigurationProperties/CC++/Advanced/CompileAs, and switch to C.

oh awesome thanks so much, i have been killing myself trying to find out how to fix the errors, this means alot to me thanks alot :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.