N
Nobody
Let me start off with a brief overview...
This part is not really important, just saying what its for...
I had been working on a Windows GUI library (DLL) when suddenly my boss told
he wanted a static .lib version so he could give a customer a single .exe
file for the "free version".
Certain parts of my library are required to live inside a DLL.
My solution was to move those parts into a seperate small DLL that contained
only those functions and encode it into the DLL or .lib as a byte array.
Then at runtime write out the DLL to a temporary location and delete it
afterwards...
Yes, dirty I know...
Anyways, I've got it mostly working, but my binary encoder seems to be
adding an extra byte onto the end of the file. Does anyone see a problem
with this code:
Its just supposed to open the .dll file, read it one byte at a time and
format it like:
"\x00\x01\x02" etc
so I can link it into my code... nByteCount at the end comes out to the
correct value, but I get a file thats one byte too big????
Also, while you are at it, here is an interesting question... my DLL was
initially 745k in debug mode. I encoded a 40k DLL and ended with obviously a
40k array of bytes. This added ZERO size to my DLL version and only 2k to my
static lib version.
After correctly the extra byte by hand, I have confirmed that all the data
is there because I did a compare on the original DLL and the DLL I wrote out
and they are exactly the same.
And since I am doing no compression on this data... where the hell is the
compiler sticking 40k of data? surely there isn't THAT much padding??
Anyways, here is the code... does any one see why it is writing an extra
byte??
int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 3)
{
printf("\nUsage: BINENC inputfile outputfile\n");
return 0;
}
FILE* fpIn = fopen(argv[1], "rb");
if (!fpIn)
{
printf("\nError: Cannot open input file \"%s\"\n", argv[1]);
return 0;
}
FILE* fpOut = fopen(argv[2], "wt");
if (!fpOut)
{
printf("\nError: Cannot open output file \"%s\"\n", argv[2]);
fclose(fpIn);
return 0;
}
int nByteCount = 0;
int nCount = 0;
unsigned char chBuf;
while (fread(&chBuf, 1, 1, fpIn) != 0)
{
// beginning of line
if (nCount == 0)
fprintf(fpOut, "\"");
// write the formatted hex character
fprintf(fpOut, "\\x%02x", chBuf);
// next position
nCount++;
nByteCount++;
// end of line
if (nCount == 35)
{
fprintf(fpOut, "\"\n");
nCount = 0;
}
}
fprintf(fpOut, "\"\n");
fclose(fpIn);
fclose(fpOut);
printf("\nEncoded %d bytes\n", nByteCount);
return 0;
}
This part is not really important, just saying what its for...
I had been working on a Windows GUI library (DLL) when suddenly my boss told
he wanted a static .lib version so he could give a customer a single .exe
file for the "free version".
Certain parts of my library are required to live inside a DLL.
My solution was to move those parts into a seperate small DLL that contained
only those functions and encode it into the DLL or .lib as a byte array.
Then at runtime write out the DLL to a temporary location and delete it
afterwards...
Yes, dirty I know...
Anyways, I've got it mostly working, but my binary encoder seems to be
adding an extra byte onto the end of the file. Does anyone see a problem
with this code:
Its just supposed to open the .dll file, read it one byte at a time and
format it like:
"\x00\x01\x02" etc
so I can link it into my code... nByteCount at the end comes out to the
correct value, but I get a file thats one byte too big????
Also, while you are at it, here is an interesting question... my DLL was
initially 745k in debug mode. I encoded a 40k DLL and ended with obviously a
40k array of bytes. This added ZERO size to my DLL version and only 2k to my
static lib version.
After correctly the extra byte by hand, I have confirmed that all the data
is there because I did a compare on the original DLL and the DLL I wrote out
and they are exactly the same.
And since I am doing no compression on this data... where the hell is the
compiler sticking 40k of data? surely there isn't THAT much padding??
Anyways, here is the code... does any one see why it is writing an extra
byte??
int _tmain(int argc, _TCHAR* argv[])
{
if (argc < 3)
{
printf("\nUsage: BINENC inputfile outputfile\n");
return 0;
}
FILE* fpIn = fopen(argv[1], "rb");
if (!fpIn)
{
printf("\nError: Cannot open input file \"%s\"\n", argv[1]);
return 0;
}
FILE* fpOut = fopen(argv[2], "wt");
if (!fpOut)
{
printf("\nError: Cannot open output file \"%s\"\n", argv[2]);
fclose(fpIn);
return 0;
}
int nByteCount = 0;
int nCount = 0;
unsigned char chBuf;
while (fread(&chBuf, 1, 1, fpIn) != 0)
{
// beginning of line
if (nCount == 0)
fprintf(fpOut, "\"");
// write the formatted hex character
fprintf(fpOut, "\\x%02x", chBuf);
// next position
nCount++;
nByteCount++;
// end of line
if (nCount == 35)
{
fprintf(fpOut, "\"\n");
nCount = 0;
}
}
fprintf(fpOut, "\"\n");
fclose(fpIn);
fclose(fpOut);
printf("\nEncoded %d bytes\n", nByteCount);
return 0;
}