+ const char*filename;
+ struct _filelist*next;
+ char type;
+} filelist_t;
+
+static filelist_t* registerFile(filelist_t*next, const char*filename, char type)
+{
+ filelist_t*file = malloc(sizeof(filelist_t));
+ file->filename = strdup(filename);
+ file->type = type;
+ file->next = next;
+ return file;
+}
+
+static filelist_t* readFileList(char*filename)
+{
+ FILE*fi = fopen(filename, "rb");
+ if(!fi) {
+ return 0;
+ }
+ fseek(fi, 0, SEEK_END);
+ int len = ftell(fi);
+ fseek(fi, 0, SEEK_SET);
+ char*data = malloc(len);
+ fread(data, len, 1, fi);
+ fclose(fi);
+ int t=0;
+ char*line = data;
+ filelist_t*list = 0,*lpos=0;;
+ while(t<len) {
+ if(data[t]=='\r' || data[t]=='\n') {
+ data[t++] = 0;
+ if(strchr("DFI", line[0]) && line[1]==' ' && line[2]!=' ') {
+ filelist_t*f = malloc(sizeof(filelist_t));
+ f->type=line[0];
+ f->filename=strdup(line+2);
+ f->next = 0;
+ if(!list) {
+ list = lpos = f;
+ } else {
+ lpos->next = f;
+ lpos = f;
+ }
+ } else {
+ // skip line- this usually only happens if somebody tampered
+ // with the file
+ }
+ while(t<len && (data[t]=='\0' || data[t]=='\r' || data[t]=='\n'))
+ t++;
+ line = &data[t];
+ } else {
+ t++;
+ }
+ }
+ return list;
+}
+
+static void writeFileList(filelist_t*file, const char*filename)
+{
+ FILE*fi = fopen(filename, "wb");
+ fprintf(fi, "[%s installed files]\r\n", SOFTWARE_NAME);
+ if(!fi) {
+ char buf[1024];
+ sprintf(buf, "Couldn't write file %s", filename);
+ MessageBox(0, buf, INSTALLER_NAME, MB_OK|MB_ICONERROR);
+ do_abort=1;
+ exit(1);
+ }
+ while(file) {
+ fprintf(fi, "%c %s\r\n", file->type, file->filename);
+ file = file->next;
+ }
+ fclose(fi);
+}
+
+static filelist_t*installedFiles = 0;
+
+static void addFile(const char*filename)
+{
+ installedFiles = registerFile(installedFiles, filename, 'F');
+}
+static void addDir(const char*filename)
+{
+ installedFiles = registerFile(installedFiles, filename, 'D');
+}
+
+static void handleTemplateFile(const char*filename)
+{
+ FILE*fi = fopen(filename, "rb");
+ fseek(fi, 0, SEEK_END);
+ int len = ftell(fi);
+ fseek(fi, 0, SEEK_SET);
+ char*file = malloc(len);
+ fread(file, len, 1, fi);
+ fclose(fi);
+ int l = strlen(install_path);
+ fi = fopen(filename, "wb");
+ char*pos = file;
+ char*lastpos = file;
+ while(1) {
+ pos = strstr(pos, "%%PATH%%");
+ if(!pos) {
+ pos = &file[len];
+ break;
+ }
+ if(pos!=lastpos)
+ fwrite(lastpos, pos-lastpos, 1, fi);
+ fwrite(install_path, l, 1, fi);
+ pos+=8; // length of "%%PATH%%"
+ lastpos = pos;
+ }
+ fwrite(lastpos, pos-lastpos, 1, fi);
+ fclose(fi);
+ free(file);
+}
+
+static int setRegistryEntry(char*key,char*value)
+{
+ HKEY hkey1;
+ HKEY hkey2;
+ int ret1 = -1, ret2= -1;
+ ret1 = RegCreateKey(HKEY_CURRENT_USER, key, &hkey1);
+ if(config_forAllUsers) {
+ ret2 = RegCreateKey(HKEY_LOCAL_MACHINE, key, &hkey2);
+ }
+
+ if(ret1 && ret2) {
+ fprintf(stderr, "registry: CreateKey %s failed\n", key);
+ return 0;
+ }
+ if(ret1) {
+ installedFiles = registerFile(installedFiles, key, 'k');
+ }
+ if(ret2) {
+ installedFiles = registerFile(installedFiles, key, 'K');
+ }
+
+ if(!ret1)
+ ret1 = RegSetValue(hkey1, NULL, REG_SZ, value, strlen(value)+1);
+ if(!ret2)
+ ret2 = RegSetValue(hkey2, NULL, REG_SZ, value, strlen(value)+1);
+ if(ret1 && ret2) {
+ fprintf(stderr, "registry: SetValue %s failed\n", key);
+ return 0;
+ }
+ return 1;
+}
+
+
+static char* getRegistryEntry(char*path)
+{
+ int res = 0;
+ HKEY key;
+ long rc;
+ long size = 0;
+ DWORD type;
+ char*buf;
+ rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, path, 0, KEY_ALL_ACCESS/* KEY_READ*/, &key);
+ if (rc != ERROR_SUCCESS) {
+ fprintf(stderr, "RegOpenKeyEx failed\n");
+ return 0;
+ }
+ rc = RegQueryValueEx(key, NULL, 0, 0, 0, (LPDWORD)&size) ;
+ if(rc != ERROR_SUCCESS) {
+ fprintf(stderr, "RegQueryValueEx(1) failed: %d\n", rc);
+ return 0;
+ }
+ buf = (char*)malloc(size+1);
+ rc = RegQueryValueEx(key, NULL, 0, &type, (BYTE*)buf, (LPDWORD)&size);
+ if(rc != ERROR_SUCCESS) {
+ fprintf(stderr, "RegQueryValueEx(2) failed: %d\n", rc);
+ return 0;
+ }
+ if(type == REG_SZ || type == REG_EXPAND_SZ) {
+ while(size && buf[size-1] == '\0')
+ --size;
+ buf[size] = 0;
+ /* TODO: convert */
+ return buf;
+ } else if(type == REG_BINARY) {
+ return buf;
+ }
+}
+
+void processMessages()
+{
+ MSG msg;
+ while(PeekMessage(&msg,NULL,0,0,0))
+ {
+ GetMessage(&msg, NULL, 0, 0);
+ TranslateMessage(&msg);
+ DispatchMessage(&msg);
+ }
+}
+
+int addRegistryEntries(char*install_dir)
+{
+ int ret;
+ ret = setRegistryEntry(registry_path, install_dir);
+ if(!ret) return 0;
+ return 1;
+}
+
+int CreateShortcut(char*path, char*description, char*filename, char*arguments, int iconindex, char*iconpath, char*workdir)
+{
+ printf("Creating %s -> %s\n", filename, path);
+ WCHAR wszFilename[MAX_PATH];
+ IShellLink *ps1 = NULL;
+ IPersistFile *pPf = NULL;
+ HRESULT hr;
+ hr = CoCreateInstance(&CLSID_ShellLink, NULL, CLSCTX_INPROC_SERVER, &IID_IShellLink, (void*)&ps1);
+ if(FAILED(hr)) return 0;
+ hr = ps1->lpVtbl->QueryInterface(ps1, &IID_IPersistFile, (void **)&pPf);
+ if(FAILED(hr)) return 0;
+ hr = ps1->lpVtbl->SetPath(ps1, path);
+ if(FAILED(hr)) return 0;
+ hr = ps1->lpVtbl->SetDescription(ps1, description);
+
+ if (arguments) {
+ hr = ps1->lpVtbl->SetArguments(ps1, arguments);
+ if(FAILED(hr)) return 0;
+ }
+ if (iconpath) {
+ hr = ps1->lpVtbl->SetIconLocation(ps1, iconpath, iconindex);
+ if (FAILED(hr)) return 0;
+ }
+ if (workdir) {
+ hr = ps1->lpVtbl->SetWorkingDirectory(ps1, workdir);
+ if (FAILED(hr)) return 0;
+ }
+ MultiByteToWideChar(CP_ACP, 0, filename, -1, wszFilename, MAX_PATH);
+ hr = pPf->lpVtbl->Save(pPf, wszFilename, TRUE);
+ if(FAILED(hr)) {
+ return 0;
+ }
+ pPf->lpVtbl->Release(pPf);
+ ps1->lpVtbl->Release(ps1);
+ addFile(filename);
+ return 1;
+}
+
+static int CreateURL(const char*url, const char*path)
+{
+ FILE*fi = fopen(path, "wb");
+ if(!fi)
+ return 0;
+ fprintf(fi, "[InternetShortcut]\r\n");
+ fprintf(fi, "URL=http://localhost:8081/\r\n");
+ fclose(fi);
+ addFile(path);
+ return 1;
+}
+
+
+BOOL CALLBACK PropertySheetFuncCommon(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, int buttons)
+{
+ LPNMHDR lpnm;
+
+ HWND dialog = GetParent(hwnd);
+
+ if(message == WM_INITDIALOG) {
+ if(logo)
+ SendDlgItemMessage(hwnd, IDC_BITMAP, STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)logo);
+
+ RECT rc;
+ GetWindowRect(dialog, &rc);
+ int width = rc.right - rc.left;
+ int height = rc.bottom - rc.top;
+ MoveWindow(dialog, (GetSystemMetrics(SM_CXSCREEN) - width)/2, (GetSystemMetrics(SM_CYSCREEN) - height)/2, width, height, FALSE);
+ return FALSE;
+ }
+
+ if(message == WM_NOTIFY && (((LPNMHDR)lParam)->code == PSN_SETACTIVE)) {
+ PropSheet_SetWizButtons(dialog, buttons);
+ return FALSE;
+ }
+ return FALSE;
+}
+
+#ifndef DEINSTALL
+BOOL CALLBACK PropertySheetFunc1(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
+ if(message == WM_INITDIALOG) {
+ SetDlgItemText(hwnd, IDC_LICENSE, license_text);
+ }
+ return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_NEXT);
+}
+BOOL CALLBACK PropertySheetFunc2(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
+ if(message == WM_INITDIALOG) {
+ SetDlgItemText(hwnd, IDC_INSTALL_PATH, install_path);
+
+ config_forAllUsers = 0;
+ SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
+ SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
+ }
+ if(message == WM_COMMAND) {
+ if((wParam&0xffff) == IDC_BROWSE) {
+ BROWSEINFOA browse;
+ memset(&browse, 0, sizeof(browse));
+ browse.ulFlags = BIF_EDITBOX | BIF_NEWDIALOGSTYLE | BIF_USENEWUI;// | BIF_RETURNONLYFSDIRS; //BIF_VALIDATE
+ browse.pszDisplayName = (CHAR*)malloc(MAX_PATH);
+ memset(browse.pszDisplayName, 0, MAX_PATH);
+ browse.lpszTitle = "Select installation directory";
+ browse.pidlRoot = SHBrowseForFolder(&browse);
+ if(browse.pszDisplayName) {
+ if(SHGetPathFromIDList(browse.pidlRoot, browse.pszDisplayName)) {
+ install_path = browse.pszDisplayName;
+ int l = strlen(install_path);
+ while(l && install_path[l-1]=='\\') {
+ install_path[--l]=0;
+ }
+ }
+ }
+ SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_SETTEXT, 0, (LPARAM)install_path);
+ return 0;
+
+ }
+ else if((wParam&0xffff) == IDC_ALLUSERS) {
+ config_forAllUsers = 1;
+ SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
+ SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
+ }
+ else if((wParam&0xffff) == IDC_CURRENTUSER) {
+ config_forAllUsers = 0;
+ SendDlgItemMessage(hwnd, IDC_ALLUSERS, BM_SETCHECK, config_forAllUsers, 0);
+ SendDlgItemMessage(hwnd, IDC_CURRENTUSER, BM_SETCHECK, config_forAllUsers^1, 0);
+ }
+ else if((wParam&0xffff) == IDC_INSTALL_PATH) {
+ SendDlgItemMessage(hwnd, IDC_INSTALL_PATH, WM_GETTEXT, sizeof(pathBuf), (LPARAM)&(pathBuf[0]));
+ if(pathBuf[0]) {
+ install_path = pathBuf;
+ int l = strlen(install_path);
+ while(l && install_path[l-1]=='\\') {
+ install_path[--l]=0;
+ }
+ }
+ return 0;
+ }
+ }
+ return PropertySheetFuncCommon(hwnd, message, wParam, lParam, PSWIZB_BACK|PSWIZB_NEXT);
+}
+HWND statuswnd;
+static int progress_pos = 0;
+
+void PropertyArchiveError(char*text)
+{
+ while(1) {
+ int ret = MessageBox(0, text, "Error", MB_RETRYCANCEL|MB_ICONERROR);
+ if(ret==IDRETRY) continue;
+ else break;
+ }
+}
+
+void PropertyArchive_NewFile(char*filename)
+{
+ addFile(filename);
+ processMessages();
+}
+void PropertyArchive_NewDirectory(char*filename)
+{
+ addDir(filename);
+ processMessages();
+}
+
+static int lastlen = 0;
+void PropertyArchiveStatus(int pos, int len)
+{
+ if(len!=lastlen) {
+ SendDlgItemMessage(statuswnd, IDC_PROGRESS, PBM_SETRANGE, 0, (LPARAM)MAKELONG(0,len));
+ lastlen = len;
+ }
+ SendDlgItemMessage(statuswnd, IDC_PROGRESS, PBM_SETPOS, pos, 0);
+ processMessages();
+ Sleep(30);
+}
+void PropertyArchiveMessage(char*text)
+{
+ if(text && text[0]=='[') {
+ return;
+ }
+ SetDlgItemText(statuswnd, IDC_INFO, strdup(text));
+ processMessages();
+}
+
+void print_space(char*dest, char*msg, ULONGLONG size)
+{
+ if(size < 1024)
+ sprintf(dest, "%s%d Bytes", msg, size);
+ else if(size < 1048576l)
+ sprintf(dest, "%s%.2f Kb", msg, size/1024.0);
+ else if(size < 1073741824l)
+ sprintf(dest, "%s%.2f Mb", msg, size/1048576.0);
+ else if(size < 1099511627776ll)
+ sprintf(dest, "%s%.2f Gb", msg, size/1073741824.0);
+ else
+ sprintf(dest, "%s%.2f Tb", msg, size/1125899906842624.0);
+}
+
+BOOL CALLBACK PropertySheetFunc3(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam) {
+ HWND dialog = GetParent(hwnd);
+ if(message == WM_INITDIALOG) {
+ SetDlgItemText(hwnd, IDC_INFO, "Ready to install");
+
+ char buf[256];
+ print_space(buf, "Space required: ", crn_decompressed_size);
+ SetDlgItemText(hwnd, IDC_SPACE1, buf);
+ ULARGE_INTEGER available,total,totalfree;
+ available.QuadPart=0;
+ total.QuadPart=0;
+ totalfree.QuadPart=0;
+ if(GetDiskFreeSpaceEx(install_path, &available, &total, &totalfree)) {
+ print_space(buf, "Space available: ", available.QuadPart);
+ } else {
+ sprintf(buf, "Space available: [Error %d]", GetLastError());
+ if(GetLastError() == ERROR_FILE_NOT_FOUND && install_path[0] && install_path[1]==':') {
+ /* installation directory does not yet exist */
+ char path[3]={'c',':',0};
+ path[0] = install_path[0];
+ if(GetDiskFreeSpaceEx(path, &available, &total, &totalfree)) {
+ print_space(buf, "Space available: ", available.QuadPart);
+ }
+ }