home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 October / Chip_2001-10_cd1.bin / zkuste / delphi / nastroje / d23456 / NSIS.EXE / Source / exehead / Main.c < prev    next >
C/C++ Source or Header  |  2001-06-11  |  8KB  |  293 lines

  1. const char *verstr="Nullsoft Install System v1.44";
  2.  
  3. /* 
  4.   Nullsoft "SuperPimp" Installation System - main.c - executable header main code
  5.   version 1.44  - June 11, 2001
  6.  
  7.   Copyright (C) 1999-2001 Nullsoft, Inc.
  8.  
  9.   This software is provided 'as-is', without any express or implied
  10.   warranty.  In no event will the authors be held liable for any damages
  11.   arising from the use of this software.
  12.  
  13.   Permission is granted to anyone to use this software for any purpose,
  14.   including commercial applications, and to alter it and redistribute it
  15.   freely, subject to the following restrictions:
  16.  
  17.   1. The origin of this software must not be misrepresented; you must not
  18.      claim that you wrote the original software. If you use this software
  19.      in a product, an acknowledgment in the product documentation would be
  20.      appreciated but is not required.
  21.   2. Altered source versions must be plainly marked as such, and must not be
  22.      misrepresented as being the original software.
  23.   3. This notice may not be removed or altered from any source distribution.
  24.  
  25.   This source distribution includes portions of zlib. see zlib/zlib.h for 
  26.   its license and so forth. Note that this license is also borrowed from zlib.
  27. */
  28.  
  29.  
  30. #include <windows.h>
  31. #include "resource.h"
  32. #include "util.h"
  33. #include "fileform.h"
  34. #include "state.h"
  35. #include "ui.h"
  36. #include "libc.h"
  37. #include "../zlib/zlib.h"
  38.  
  39. char *g_readerrorstr="Error reading data from installer";
  40. char *g_errorcopyinginstall="Error launching uninstaller";
  41.  
  42. char state_exe_directory[1024];
  43. char g_caption[256];
  44. int g_filehdrsize;
  45. HWND g_hwnd;
  46.  
  47. static int m_length;
  48. static int m_pos;
  49.  
  50. static BOOL CALLBACK verProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
  51. {
  52.   static char *t="verifying installer: %d%%";
  53.   if (uMsg == WM_INITDIALOG) 
  54.   {
  55.     SetTimer(hwndDlg,1,250,NULL);
  56.     uMsg = WM_TIMER;
  57.   }
  58.   if (uMsg == WM_TIMER)
  59.   {
  60.     static char bt[64];
  61.     wsprintf(bt,t,m_pos/(m_length/100));
  62.  
  63.     SetWindowText(hwndDlg,bt);
  64.     SetDlgItemText(hwndDlg,IDC_STR,bt);
  65.   }
  66.   return 0;
  67. }
  68.  
  69. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst,LPSTR lpszCmdParam, int nCmdShow)
  70. {
  71.   static HWND hwnd;
  72.   unsigned int verify_time=GetTickCount()+1000;
  73.   static char temp[1024];
  74.   int left;
  75.   static char no_crc;
  76.   static char do_crc;
  77.   static char silent;
  78.   static int ret;
  79.   static char *m_Err;
  80.   static int crc;
  81.   char *cmdline=GetCommandLine();
  82.   if (*cmdline == '\"')
  83.   {
  84.     do cmdline++; while (*cmdline && *cmdline != '\"');
  85.     if (*cmdline) cmdline++;
  86.   }
  87.   else
  88.   {
  89.     while (*cmdline && *cmdline != ' ') cmdline++;
  90.   }
  91.  
  92.   do
  93.   {
  94.     static char v[4]={'N','C','R','C'};
  95.     while (*cmdline == ' ') cmdline++;
  96.     if (cmdline[0] != '/') break;
  97.     cmdline++;
  98.     if (cmdline[0] == 'S' && (cmdline[1] == ' ' || !cmdline[1]))
  99.     {
  100.       silent++;
  101.       cmdline++;
  102.     }
  103.     else if (*(int*)cmdline == *(int*)v && (cmdline[4] == ' ' || !cmdline[4]))
  104.     {
  105.       no_crc++;
  106.       cmdline+=4;
  107.     }
  108.     else if (cmdline[0] == 'D' && cmdline[1] == '=' && cmdline[2])
  109.     {
  110.       cmdline+=2;
  111.       lstrcpy(state_install_directory,cmdline);
  112.       cmdline+=lstrlen(cmdline);
  113.     }
  114.     else break;
  115.   }
  116.   while (*cmdline);
  117.  
  118.   lstrcpy(g_caption,(char*)verstr);
  119.   lstrcat(g_caption," ERROR");
  120.  
  121.   g_hInstance=GetModuleHandle(NULL);
  122.   GetModuleFileName(g_hInstance,temp,sizeof(temp));
  123.  
  124.   {
  125.     char *p=state_exe_directory;
  126.     lstrcpy(state_exe_directory,temp);
  127.     while (*p) p++;
  128.     while (p > state_exe_directory && *p != '\\') p--;
  129.     *p=0;
  130.   }
  131.  
  132.   g_db_hFile=CreateFile(temp,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
  133.   if (g_db_hFile==INVALID_HANDLE_VALUE)
  134.   {
  135.     m_Err = "Can't open self";
  136.     goto end;
  137.   }
  138.  
  139.   left = m_length = GetFileSize(g_db_hFile,NULL);
  140.   while (left > 0)
  141.   {
  142.     DWORD l=left;
  143.     if (l > 512) l=512;
  144.     if (!ReadFile(g_db_hFile,temp,l,&l,NULL))
  145.     {
  146.       m_Err=g_readerrorstr;
  147.       if (hwnd) DestroyWindow(hwnd);
  148.       goto end;
  149.     }
  150.     crc=adler32(crc, temp, l);
  151.  
  152.     if (!g_filehdrsize)
  153.     {
  154.       int dbl;
  155.       dbl=isheader((firstheader*)temp);
  156.       if (dbl)
  157.       {
  158.         int a=*(int*)temp;
  159.         g_filehdrsize=m_pos;
  160.         if (dbl > left)
  161.         {
  162.           m_Err="Installer too small";
  163.           goto end;
  164.         }
  165.  
  166.         if (a&FH_FLAGS_SILENT) silent++;
  167.  
  168.         if (no_crc || !(a&FH_FLAGS_CRC)) break; // if first bit is not set, then no crc checking. 
  169.  
  170.         // end crc checking at crc :) this means you can tack shit on the end 
  171.         // and it'll still work.
  172.         left=dbl-4;
  173.         do_crc++;
  174.       }
  175.     }
  176.     else if (!silent)
  177.     {
  178.       if (hwnd)
  179.       {
  180.         static MSG msg;
  181.         if (PeekMessage(&msg,NULL,0,0,PM_REMOVE)) DispatchMessage(&msg);
  182.       }
  183.       else if (GetTickCount() > verify_time) 
  184.         hwnd=CreateDialog(g_hInstance,MAKEINTRESOURCE(IDD_VERIFY),NULL,verProc);
  185.     }
  186.     m_pos+=l;
  187.     left -= l;
  188.   }
  189.   if (hwnd) DestroyWindow(hwnd);
  190.   if (g_filehdrsize)
  191.   {
  192.     DWORD l;
  193.     int fcrc;
  194.     if (do_crc && (!ReadFile(g_db_hFile,&fcrc,4,&l,NULL) || crc != fcrc))
  195.     {
  196.       m_Err="Installer CRC invalid";
  197.       goto end;
  198.     }
  199.     SetFilePointer(g_db_hFile,g_filehdrsize,NULL,FILE_BEGIN);    
  200.     g_db_offset=g_filehdrsize+sizeof(firstheader);
  201.  
  202.     if (loadHeaders()) m_Err=g_readerrorstr;
  203.   }
  204.   if (m_Err) goto end;
  205.  
  206. #ifdef NSIS_CONFIG_UNINSTALL_SUPPORT
  207.   if (g_inst_uninstheader)
  208.   {
  209.     if (cmdline[0] == '_' && cmdline[1] == '=' && cmdline[2])
  210.     {
  211.       cmdline+=2;
  212.       if (is_valid_instpath(cmdline))
  213.       {
  214.         lstrcpy(state_install_directory,cmdline);
  215.         lstrcpy(state_output_directory,cmdline);
  216.       }
  217.       else
  218.       {
  219.         m_Err = g_errorcopyinginstall;
  220.         goto end;
  221.       }
  222.     }
  223.     else
  224.     {
  225.       int x,done=0;
  226.  
  227.       for (x = 0; x < 26; x ++)
  228.       {
  229.         static char s[]="A~NSISu_.exe";
  230.         static char buf2[1024];
  231.         static char ibuf[1024];
  232.       
  233.         buf2[0]='\"';
  234.         GetTempPath(sizeof(buf2)-1,buf2+1);
  235.         lstrcat(buf2,s);
  236.  
  237.         DeleteFile(buf2+1); // clean up after all the other ones if they are there
  238.         
  239.         if (!done)
  240.         {
  241.           // get current name
  242.           int l=GetModuleFileName(g_hInstance,ibuf,sizeof(ibuf));
  243.           // check if it is ?~NSISu_.exe - if so, fuck it
  244.           if (!lstrcmpi(ibuf+l-(sizeof(s)-2),s+1)) break;
  245.  
  246.           // copy file
  247.           if (CopyFile(ibuf,buf2+1,FALSE))
  248.           {
  249.             PROCESS_INFORMATION ProcInfo={0,};
  250.             STARTUPINFO StartUp={sizeof(STARTUPINFO),};
  251.             char *p=ibuf;
  252.             MoveFileOnReboot(buf2+1,NULL);
  253.             while (*p) p++;
  254.             while (p > ibuf && *p != '\\') p--;
  255.             *p=0;
  256.             if (!is_valid_instpath(ibuf)) break;
  257.             done++;
  258.             lstrcat(buf2,"\" _=");
  259.             lstrcat(buf2,ibuf);
  260.             GetTempPath(sizeof(ibuf),ibuf);
  261.             if (CreateProcess( NULL, buf2, NULL, NULL, FALSE, 0, NULL, ibuf, &StartUp, &ProcInfo) )
  262.             {
  263.               if (ProcInfo.hThread) CloseHandle(ProcInfo.hThread);
  264.               if (ProcInfo.hProcess) CloseHandle(ProcInfo.hProcess);
  265.             }
  266.             else m_Err = g_errorcopyinginstall;
  267.           }
  268.         }
  269.         s[0]++;
  270.       }
  271.       if (!done) m_Err=g_errorcopyinginstall;
  272.       goto end;      
  273.     }
  274.   }
  275. #endif
  276.   if (g_inst_header&&!g_inst_header->silent_install)
  277.     g_inst_header->silent_install=silent;
  278.   ret=ui_doinstall();
  279.   if (g_inst_header) GlobalFree((HGLOBAL)g_inst_header);
  280. #ifdef NSIS_CONFIG_UNINSTALL_SUPPORT
  281.   if (g_inst_uninstheader) GlobalFree((HGLOBAL)g_inst_uninstheader);
  282. #endif
  283.  
  284. #ifdef NSIS_CONFIG_LOG
  285.   log_write(1);
  286. #endif
  287. end:
  288.   if (g_db_hFile!=INVALID_HANDLE_VALUE) CloseHandle(g_db_hFile);
  289.   if (m_Err) MessageBox(NULL,m_Err,g_caption,MB_OK|MB_ICONSTOP);
  290.   ExitProcess(ret);
  291. }
  292.  
  293.