Cipher program error : redifinition/previous definition
Hello everyone, Im trying to run a program PassWeb by compiling three source files together (PassWeb, Cipher and Menu) but every time I run it the compiler crashes(I use both dev C++ and gcc).
The error redefinition/previous definition of all the funtions is repeatedly displayed and I dont know what to do with it. Im posting the Cipher.c program. Thanks for the help
Code:
/* "cipher.c – containing the Caesar cipher program."
"Use the simple Caesar cipher algorithm covered in class. Make the integer
number a constant, built into the program. The program must not ask the user
for an integer number. Make this Caesar cipher encrypt over the entire ASCII
character space and not encrypt only letters as in the previous assignment."
"Neil: Do we encode the entire file, just the lines, or the values.
Mirza Abdel Baig: It's better to just decrypt the whole file first (including
newlines) and then convert comma-separated data into a linked-list.
Neil: Thanks."
This is a wrapper for IO fn's using 'encryption.' */
#include <stdlib.h> /* malloc free */
#include <stdio.h> /* fprintf fputs */
#include <stdarg.h> /* va_* vsnprintf */
#include <string.h> /* strlen */
#include "Cipher.c"
#include "PassWeb.c"
/* Caesar Cipher is probably the easiest ever to break, but I can choose a
number that is good (primes < 256: 2 3 5 7 11 13 17 19 23 29 31 37 41 43 47
53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157
163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251;) it
doesn't matter though, 10 is the last byte in the file, so any attacker could
decrypt (and know your key) trivially; a better way would be to encrypt the
values; md5() anyone? */
const int debug = 0;
const int key = 113; /* default key */
const int bufsize = 2048; /* sufficient to hold user info? */
/* public */
struct Cipher {
FILE *fp;
enum Mode {WRITE, READ} mode;
char *buf;
};
static int encrypt(const int c, const int key);
static int flushin(FILE *fp);
/* public */
void Cipher_(struct Cipher **cipherPtr) {
struct Cipher *cipher;
if(!cipherPtr || !(cipher = *cipherPtr)) return;
if(cipher->fp && fclose(cipher->fp)) perror("file");
if(debug) fprintf(stderr, "~Cipher: erase, #%p.\n", (void *)cipher);
free(cipher);
*cipherPtr = cipher = 0;
}
struct Cipher *Cipher(char *fn, enum Mode m) {
struct Cipher *cipher;
if(!fn || !*fn) return 0;
if(!(cipher = malloc(sizeof(struct Cipher) + bufsize))) {
perror("Cipher constructor");
Cipher_(&cipher);
return 0;
}
cipher->fp = 0;
cipher->mode = m;
cipher->buf = (char *)(cipher + 1);
if(debug) fprintf(stderr, "Cipher: new <%s> for %s, #%p.\n", fn,
m == WRITE ? "writing" : "reading", (void *)cipher);
/* binary files: warning: no automatic \n conversion */
if(!(cipher->fp = fopen(fn, m == WRITE ? "wb" : "rb"))) {
perror(fn);
Cipher_(&cipher);
return 0;
}
return cipher;
}
/** gets as much of the line as possible (up to bufsize,) discards the rest,
never returns a \n */
char *CipherGetLine(struct Cipher *c) {
int i, ch;
if(!c || c->mode != READ) return 0;
/* read char by char; slower but needs unencryping to tell where \n's are */
for(i = 0; i < bufsize; i++) {
if((ch = fgetc(c->fp)) == EOF) {
if(ferror(c->fp)) perror("Cipher::getLine");
break;
}
ch = encrypt(ch, -key);
if(ch == '\n') break;
c->buf[i] = ch;
}
if(i < bufsize) {
c->buf[i] = '\0';
} else {
fprintf(stderr, "Cipher::getLine: truncating input at column %d.\n", bufsize - 1);
flushin(c->fp);
c->buf[bufsize - 1] = '\0';
}
/* very scetch returning decrypted memory */
return c->buf;
}
/** everything is sexy in this fn (except it needs a \n, getLine does not, so
this fn outputs one less char then getLine) */
void CipherPrintf(struct Cipher *c, const char *fmt, ...) {
char *chPtr;
va_list ap; /* macro */
/* what happens to the stack if this is true? should be okay */
if(!c || c->mode != WRITE) return;
/* transfer to temporary buffer */
va_start(ap, fmt);
va_end(ap);
if(vsnprintf(c->buf, bufsize, fmt, ap) >= bufsize && c->buf[bufsize - 2] != '\n') {
c->buf[bufsize - 2] = '\n';
fprintf(stderr, "Cipher::printf: truncating output at column %d.\n", bufsize - 2);
}
/* encrypt, shredding the buffer (fput[s->c] because it's binary) */
for(chPtr = c->buf; *chPtr != '\0'; chPtr++) {
*chPtr = encrypt(*chPtr, key);
if(fputc(*chPtr, c->fp) == EOF) {
perror("Cipher::printf");
return;
}
}
}
/* private */
/** it doesn't take much code */
static int encrypt(const int c, const int key) {
return c + key;
}
/** fflush isn't defined on input buffers; returns zero if it got an EOF */
static int flushin(FILE *fp) {
int c;
do { c = fgetc(fp); } while(c != EOF && c != '\n');
return (c == EOF) ? 0 : -1;
}