Hi,
I’m writing a program that will encode and decode uue files and I’ve finished the encoder and I am now on to the decoder. I normal encoded uue file will look something like this…
begin 644 foo.txt
$8V%T<P``
`
end
where “foo.txt” should be the name of the file after decoding. Currently I am at the point where I am trying to take out the “foo.txt” by creating a char * and using malloc on whatever size the filename is. Unfortunately when I run my debugger it appears that malloc isn’t correctly allocating the amount of memory I want. Here’s the code I’ve got right now.
Code:
#include <stdio.h>
#include <malloc.h>
#include "uucode.h"
#define MAX_BUFFER 45
#define ERROR -1
#define CARRIAGE_RETURN 13
#define START_OF_FILENAME 10
/* encoding function taken out to conserve space */
int uudecode(const char *InputFilename)
{
FILE* eFile = fopen(InputFilename, "r");
FILE* oFile = NULL;
char buffer[MAX_BUFFER];
char *outFile;
int i, chRead, endOfFirstLine = 0;
/* if there was an error openning the encoded file */
if(!eFile)
return ERROR;
chRead = fread(buffer, 1, MAX_BUFFER, eFile);
/* finding the end of the first line of encoded text to find the filename */
for(i = 0; i < MAX_BUFFER && *(buffer + i) != '\n' && *(buffer + i) != CARRIAGE_RETURN; i++)
endOfFirstLine++;
/* setting the filename size */
outFile = (char *)malloc(endOfFirstLine - START_OF_FILENAME);
/* if malloc didn't work return an error */
if(outFile == NULL)
return ERROR;
/* copying the filename from the buffer to outFile */
for(i = START_OF_FILENAME; i < endOfFirstLine; i++)
{
*(outFile + i) = *(buffer + i);
}
oFile = fopen(outFile, "w");
if(!oFile)
return ERROR;
fclose(eFile);
fclose(oFile);
/* free is returning a memory error
free(outFile); */
return 0;
}
If I put the encoded file I showed above through this code I get endOfFirstLine = 17 while START_OF_FILENAME is #defined to be 10 which means a malloc(7) which should be correct I’m assuming, although I’m starting to doubt it since when I run free(outFile) I get a “DAMAGE: after normal block (#52) at …..” error.
Also looking at my debugger the contents of outFile are "ÍÍÍÍÍÍÍýýýý««««««««þîþîþ" before putting anything in to it which is way too big.
If I comment out the free(outFile) line it is able to create a file by the name of “ÍÍÍÍÍÍÍýýýfoo.txt««þîþîþ” so I know I’m able to copy the filename in there.
My problem is that I don’t understand what is going on with malloc() or why “foo.txt” appears in the middle of outFile. Or why free is returning an error for that matter.
Any help that someone could bring to me would be greatly appreciated, thanks for taking the time to read this.
-Peter
PS I'm using Visual Studio 2003 .net