diff --git a/Source/build.cpp b/Source/build.cpp index 2312f397..1679330c 100644 --- a/Source/build.cpp +++ b/Source/build.cpp @@ -8,6 +8,10 @@ #include "build.h" #include "util.h" +#include + +using namespace std; + #include "exehead/resource.h" #include "ResourceEditor.h" #include "DialogTemplate.h" @@ -61,8 +65,9 @@ void CEXEBuild::define(const char *p, const char *v) CEXEBuild::~CEXEBuild() { - free(header_data_new); free(m_unicon_data); + + delete [] m_exehead; int nlt = lang_tables.getlen() / sizeof(LanguageTable); LanguageTable *nla = (LanguageTable*)lang_tables.get(); @@ -72,7 +77,9 @@ CEXEBuild::~CEXEBuild() } } -CEXEBuild::CEXEBuild() +CEXEBuild::CEXEBuild() : + m_exehead(0), + m_exehead_size(0) { linecnt = 0; fp = 0; @@ -95,18 +102,8 @@ CEXEBuild::CEXEBuild() ns_func.add("",0); // make sure offset 0 is special on these (i.e. never used by a label) ns_label.add("",0); - header_data_new=(unsigned char*)malloc(zlib_exeheader_size); - exeheader_size_new=zlib_exeheader_size; - exeheader_size=zlib_exeheader_size; - - if (!header_data_new) - { - ERROR_MSG("Internal compiler error #12345: malloc(%d) failed\n",exeheader_size_new); - extern void quit(); quit(); - } - - // Changed by Amir Szekely 31st July 2002 - memcpy(header_data_new,zlib_header_data,zlib_exeheader_size); + exeheader_size = zlib_exehead_size; + update_exehead(zlib_exehead, zlib_exehead_size); #ifdef NSIS_CONFIG_UNINSTALL_SUPPORT // Changed by Amir Szekely 11th July 2002 @@ -2318,7 +2315,7 @@ int CEXEBuild::pack_exe_header() return PS_OK; } - // write out exe header, pack, read back in, align to 512, and + // write out exe header, pack, read back in, and // update the header info FILE *tmpfile=FOPEN(build_packname,"wb"); if (!tmpfile) @@ -2326,7 +2323,7 @@ int CEXEBuild::pack_exe_header() ERROR_MSG("Error: writing temporary file \"%s\" for pack\n",build_packname); return PS_ERROR; } - fwrite(header_data_new,1,exeheader_size_new,tmpfile); + fwrite(m_exehead,1,m_exehead_size,tmpfile); fclose(tmpfile); if (system(build_packcmd) == -1) { @@ -2341,21 +2338,22 @@ int CEXEBuild::pack_exe_header() ERROR_MSG("Error: reading temporary file \"%s\" after pack\n",build_packname); return PS_ERROR; } + + // read header from file + fseek(tmpfile,0,SEEK_END); - exeheader_size_new=ftell(tmpfile); + size_t exehead_size = ftell(tmpfile); + + unsigned char *exehead = new unsigned char[exehead_size]; fseek(tmpfile,0,SEEK_SET); - unsigned char *header_data_older=header_data_new; - header_data_new=(unsigned char *)malloc(exeheader_size_new); - if (!header_data_new) - { - free(header_data_older); - fclose(tmpfile); - ERROR_MSG("Error: malloc(%d) failed (exepack)\n",exeheader_size_new); - return PS_ERROR; - } - memset(header_data_new,0,exeheader_size_new); - fread(header_data_new,1,exeheader_size_new,tmpfile); + fread(exehead,1,exehead_size,tmpfile); fclose(tmpfile); + + update_exehead(exehead, exehead_size); + + // cleanup + // TODO: use resource-controlling classes (e.g. Boost) + delete [] exehead; remove(build_packname); return PS_OK; @@ -2440,40 +2438,21 @@ int CEXEBuild::write_output(void) return PS_ERROR; } - if ((int)fwrite(header_data_new,1,exeheader_size_new,fp) != exeheader_size_new) + if (fwrite(m_exehead,1,m_exehead_size,fp) != m_exehead_size) { - ERROR_MSG("Error: can't write %d bytes to output\n",exeheader_size_new); + ERROR_MSG("Error: can't write %d bytes to output\n",m_exehead_size); fclose(fp); return PS_ERROR; } #ifdef NSIS_CONFIG_CRC_SUPPORT #ifdef NSIS_CONFIG_CRC_ANAL - crc=CRC32(crc,header_data_new,exeheader_size_new); + crc=CRC32(crc,m_exehead,m_exehead_size); #else - crc=CRC32(crc,header_data_new+512,exeheader_size_new-512); + crc=CRC32(crc,m_exehead+512,m_exehead_size-512); #endif #endif - int exeheader_size_new_aligned = (exeheader_size_new + 511) & ~511; - if (exeheader_size_new_aligned != exeheader_size_new) { - // align to 512 - const unsigned char z = 0; - int write_size = exeheader_size_new_aligned - exeheader_size_new; - for (int i=0; iSave(NULL, newsize); - unsigned char *new_header = (unsigned char *) malloc(newsize); + unsigned char *new_header = new unsigned char[newsize]; + // save - res_editor->Save(new_header, newsize); - free(header_data_new); - header_data_new = new_header; - exeheader_size_new = (int) newsize; + int rc = res_editor->Save(new_header, newsize); + assert(rc == 0); + + update_exehead(new_header, newsize); + + // TODO: resource-controlling class + delete [] new_header; + delete res_editor; res_editor=0; } @@ -3425,3 +3412,19 @@ void CEXEBuild::VerifyDeclaredUserVarRefs(UserVarsStringList *pVarsStringList) } } } + +void CEXEBuild::update_exehead(const unsigned char *new_exehead, size_t new_size) { + assert(m_exehead != new_exehead); + + // align exehead to 512 + m_exehead_size = align_to_512(new_size); + + delete [] m_exehead; + m_exehead = new unsigned char[m_exehead_size]; + + // copy exehead + memcpy(m_exehead, new_exehead, new_size); + + // zero rest of exehead + memset(m_exehead + new_size, 0, m_exehead_size - new_size); +} diff --git a/Source/build.h b/Source/build.h index 06adc9ae..859d504c 100644 --- a/Source/build.h +++ b/Source/build.h @@ -1,10 +1,6 @@ #ifndef _BUILD_H_ #define _BUILD_H_ -#include - -using namespace std; - #include "strlist.h" #include "lineparse.h" #include "lang.h" @@ -117,6 +113,8 @@ class CEXEBuild { int prepare_uninstaller(); int pack_exe_header(); + void update_exehead(const unsigned char *new_exehead, size_t new_size); + // tokens.cpp int get_commandtoken(char *s, int *np, int *op, int *pos); int IsTokenPlacedRight(int pos, char *tok); @@ -350,8 +348,9 @@ class CEXEBuild { TinyGrowBuf verbose_stack; - unsigned char *header_data_new; - int exeheader_size_new; + unsigned char *m_exehead; + size_t m_exehead_size; + int icon_offset; bool branding_image_found; WORD branding_image_id; diff --git a/Source/exedata.cpp b/Source/exedata.cpp index b3333eeb..05acf0be 100644 --- a/Source/exedata.cpp +++ b/Source/exedata.cpp @@ -20,10 +20,11 @@ #include "exehead/Debug-lzma/exehead_lzma.h" #endif*/ -int zlib_exeheader_size=sizeof(zlib_header_data); -int bzip2_exeheader_size=sizeof(bzip2_header_data); -int lzma_exeheader_size=sizeof(lzma_header_data); -int exeheader_size=0; +size_t zlib_exehead_size=sizeof(zlib_exehead); +size_t bzip2_exehead_size=sizeof(bzip2_exehead); +size_t lzma_exehead_size=sizeof(lzma_exehead); +// TODO: rename to exehead_original_size +size_t exeheader_size=0; -int icondata_size=sizeof(icon_data)-22; -int unicondata_size=sizeof(unicon_data)-22; +size_t icondata_size=sizeof(icon_data)-22; +size_t unicondata_size=sizeof(unicon_data)-22; diff --git a/Source/exedata.h b/Source/exedata.h index dfd6cb1d..397a84e6 100644 --- a/Source/exedata.h +++ b/Source/exedata.h @@ -1,16 +1,19 @@ #ifndef _EXEDATA_H_ #define _EXEDATA_H_ -extern int zlib_exeheader_size; -extern int bzip2_exeheader_size; -extern int lzma_exeheader_size; -extern int exeheader_size; -extern int icondata_size; -extern int unicondata_size; +#include -extern unsigned char zlib_header_data[]; -extern unsigned char bzip2_header_data[]; -extern unsigned char lzma_header_data[]; +// TODO: these should live in a singleton +extern size_t zlib_exehead_size; +extern size_t bzip2_exehead_size; +extern size_t lzma_exehead_size; +extern size_t exeheader_size; +extern size_t icondata_size; +extern size_t unicondata_size; + +extern unsigned char zlib_exehead[]; +extern unsigned char bzip2_exehead[]; +extern unsigned char lzma_exehead[]; extern unsigned char icon_data[]; extern unsigned char unicon_data[]; extern unsigned char bitmap1_data[630]; diff --git a/Source/exehead/Makefile b/Source/exehead/Makefile index 10b62161..b97ca644 100644 --- a/Source/exehead/Makefile +++ b/Source/exehead/Makefile @@ -73,7 +73,7 @@ Release-zlib/exehead_zlib.exe : $(ZLIB_OBJS) sections_script ifneq ($(USE_PRECOMPILED_EXEHEADS),1) Release-zlib/exehead_zlib.h : Release-zlib/exehead_zlib.exe $(BIN2H) - $(BIN2H) $< $@ zlib_header_data + $(BIN2H) $< $@ zlib_exehead endif Release-zlib/%.o : %.c @@ -92,7 +92,7 @@ Release-bzip2/exehead_bzip2.exe : $(BZIP2_OBJS) sections_script ifneq ($(USE_PRECOMPILED_EXEHEADS),1) Release-bzip2/exehead_bzip2.h : Release-bzip2/exehead_bzip2.exe $(BIN2H) - $(BIN2H) $< $@ bzip2_header_data + $(BIN2H) $< $@ bzip2_exehead endif Release-bzip2/%.o : %.c @@ -108,7 +108,7 @@ Release-lzma/exehead_lzma.exe : $(LZMA_OBJS) sections_script ifneq ($(USE_PRECOMPILED_EXEHEADS),1) Release-lzma/exehead_lzma.h : Release-lzma/exehead_lzma.exe $(BIN2H) - $(BIN2H) $< $@ lzma_header_data + $(BIN2H) $< $@ lzma_exehead endif Release-lzma/%.o : %.c diff --git a/Source/exehead/exehead-bzip2.dsp b/Source/exehead/exehead-bzip2.dsp index cf1cbcd0..4e2029e4 100644 --- a/Source/exehead/exehead-bzip2.dsp +++ b/Source/exehead/exehead-bzip2.dsp @@ -54,7 +54,7 @@ LINK32=link.exe # Begin Special Build Tool SOURCE="$(InputPath)" PostBuild_Desc=generating include file for makenssi -PostBuild_Cmds=bin2h Release-bzip2\exehead_bzip2.exe Release-bzip2\exehead_bzip2.h bzip2_header_data +PostBuild_Cmds=bin2h Release-bzip2\exehead_bzip2.exe Release-bzip2\exehead_bzip2.h bzip2_exehead # End Special Build Tool # Begin Target diff --git a/Source/exehead/exehead-lzma.dsp b/Source/exehead/exehead-lzma.dsp index 6c61fadf..82921b44 100644 --- a/Source/exehead/exehead-lzma.dsp +++ b/Source/exehead/exehead-lzma.dsp @@ -54,7 +54,7 @@ LINK32=link.exe # Begin Special Build Tool SOURCE="$(InputPath)" PostBuild_Desc=generating include file for makenssi -PostBuild_Cmds=bin2h Release-lzma\exehead_lzma.exe Release-lzma\exehead_lzma.h lzma_header_data +PostBuild_Cmds=bin2h Release-lzma\exehead_lzma.exe Release-lzma\exehead_lzma.h lzma_exehead # End Special Build Tool # Begin Target diff --git a/Source/exehead/exehead-zlib.dsp b/Source/exehead/exehead-zlib.dsp index da27ed3c..39530efe 100644 --- a/Source/exehead/exehead-zlib.dsp +++ b/Source/exehead/exehead-zlib.dsp @@ -54,7 +54,7 @@ LINK32=link.exe # Begin Special Build Tool SOURCE="$(InputPath)" PostBuild_Desc=generating include file for makenssi -PostBuild_Cmds=bin2h Release-zlib\exehead_zlib.exe Release-zlib\exehead_zlib.h zlib_header_data bin2h bitmap1.bmp Release-zlib\bitmap1.h bitmap1_data bin2h nsis.ico Release-zlib\icon.h icon_data bin2h uninst.ico Release-zlib\unicon.h unicon_data +PostBuild_Cmds=bin2h Release-zlib\exehead_zlib.exe Release-zlib\exehead_zlib.h zlib_exehead bin2h bitmap1.bmp Release-zlib\bitmap1.h bitmap1_data bin2h nsis.ico Release-zlib\icon.h icon_data bin2h uninst.ico Release-zlib\unicon.h unicon_data # End Special Build Tool # Begin Target diff --git a/Source/makenssi.cpp b/Source/makenssi.cpp index a90f1b51..99e08eef 100644 --- a/Source/makenssi.cpp +++ b/Source/makenssi.cpp @@ -235,9 +235,9 @@ int main(int argc, char **argv) { if (build.display_info) { - fprintf(g_output,"Size of zlib EXE header is %d bytes.\n",zlib_exeheader_size); - fprintf(g_output,"Size of bzip2 EXE header is %d bytes.\n",bzip2_exeheader_size); - fprintf(g_output,"Size of lzma EXE header is %d bytes.\n",lzma_exeheader_size); + fprintf(g_output,"Size of zlib EXE header is %d bytes.\n",zlib_exehead_size); + fprintf(g_output,"Size of bzip2 EXE header is %d bytes.\n",bzip2_exehead_size); + fprintf(g_output,"Size of lzma EXE header is %d bytes.\n",lzma_exehead_size); fprintf(g_output,"Size of first header is %d bytes.\n",sizeof(firstheader)); fprintf(g_output,"Size of main header is %d bytes.\n",sizeof(header)); fprintf(g_output,"Size of each section is %d bytes.\n",sizeof(section)); diff --git a/Source/makenssi.dsp b/Source/makenssi.dsp index 1dd5f414..1dc94417 100644 --- a/Source/makenssi.dsp +++ b/Source/makenssi.dsp @@ -205,6 +205,10 @@ SOURCE=.\exedata.cpp # End Source File # Begin Source File +SOURCE=.\growbuf.cpp +# End Source File +# Begin Source File + SOURCE=.\lang.cpp # End Source File # Begin Source File @@ -214,6 +218,10 @@ SOURCE=.\makenssi.cpp # End Source File # Begin Source File +SOURCE=.\mmap.cpp +# End Source File +# Begin Source File + SOURCE=.\Plugins.cpp # End Source File # Begin Source File @@ -232,6 +240,10 @@ SOURCE=.\script.cpp # End Source File # Begin Source File +SOURCE=.\strlist.cpp +# End Source File +# Begin Source File + SOURCE=.\tokens.cpp # End Source File # Begin Source File @@ -273,6 +285,10 @@ SOURCE=.\exedata.h # End Source File # Begin Source File +SOURCE=.\growbuf.h +# End Source File +# Begin Source File + SOURCE=.\lang.h # End Source File # Begin Source File @@ -281,6 +297,10 @@ SOURCE=.\lineparse.h # End Source File # Begin Source File +SOURCE=.\mmap.h +# End Source File +# Begin Source File + SOURCE=.\Platform.h # End Source File # Begin Source File diff --git a/Source/script.cpp b/Source/script.cpp index f12ac88d..88f43371 100644 --- a/Source/script.cpp +++ b/Source/script.cpp @@ -2451,64 +2451,34 @@ int CEXEBuild::doCommand(int which_token, LineParser &line) switch (k) { case 0: // JF> should handle the state of going from bzip2 back to zlib: compressor = &zlib_compressor; - free(header_data_new); - header_data_new=(unsigned char*)malloc(zlib_exeheader_size); - exeheader_size_new=zlib_exeheader_size; - exeheader_size=zlib_exeheader_size; - - if (!header_data_new) - { - ERROR_MSG("Internal compiler error #12345: malloc(%d) failed\n",exeheader_size_new); - extern void quit(); quit(); - } - - memcpy(header_data_new,zlib_header_data,zlib_exeheader_size); + update_exehead(zlib_exehead, zlib_exehead_size); #ifdef NSIS_ZLIB_COMPRESS_WHOLE build_compress_whole=true; #else build_compress_whole=false; #endif break; + case 1: compressor=&bzip2_compressor; - free(header_data_new); - header_data_new=(unsigned char*)malloc(bzip2_exeheader_size); - exeheader_size_new=bzip2_exeheader_size; - exeheader_size=bzip2_exeheader_size; - - if (!header_data_new) - { - ERROR_MSG("Internal compiler error #12345: malloc(%d) failed\n",exeheader_size_new); - extern void quit(); quit(); - } - - memcpy(header_data_new,bzip2_header_data,bzip2_exeheader_size); + update_exehead(bzip2_exehead, bzip2_exehead_size); #ifdef NSIS_BZIP2_COMPRESS_WHOLE build_compress_whole=true; #else build_compress_whole=false; #endif break; + case 2: compressor = &lzma_compressor; - free(header_data_new); - header_data_new=(unsigned char*)malloc(lzma_exeheader_size); - exeheader_size_new=lzma_exeheader_size; - exeheader_size=lzma_exeheader_size; - - if (!header_data_new) - { - ERROR_MSG("Internal compiler error #12345: malloc(%d) failed\n",exeheader_size_new); - extern void quit(); quit(); - } - - memcpy(header_data_new, lzma_header_data, lzma_exeheader_size); + update_exehead(lzma_exehead, lzma_exehead_size); #ifdef NSIS_LZMA_COMPRESS_WHOLE build_compress_whole=true; #else build_compress_whole=false; #endif break; + default: PRINTHELP(); } diff --git a/Source/util.h b/Source/util.h index 12ca8ade..939622f5 100644 --- a/Source/util.h +++ b/Source/util.h @@ -79,4 +79,9 @@ int my_glob(const char *pattern, int flags, #define OPEN(a, b) open(a, b) #endif +template +inline T align_to_512(const T x) { + return (x+511) & ~511; +} + #endif //_UTIL_H_