Debian Codegraph @

Source/exehead/fileform.c

raw
1 /*
2 * fileform.c
3 *
4 * This file is a part of NSIS.
5 *
6 * Copyright (C) 1999-2026 Nullsoft and Contributors
7 *
8 * Licensed under the zlib/libpng license (the "License");
9 * you may not use this file except in compliance with the License.
10 *
11 * Licence details can be found in the file COPYING.
12 *
13 * This software is provided 'as-is', without any express or implied
14 * warranty.
15 *
16 * Unicode support by Jim Park -- 08/13/2007
17 */
18
19 #include "../Platform.h"
20 #include "fileform.h"
21 #include "util.h"
22 #include "state.h"
23 #include "resource.h"
24 #include "lang.h"
25 #include "ui.h"
26 #include "exec.h"
27 #include "../crc32.h"
28 #include "../tchar.h"
29
30 #ifdef NSIS_CONFIG_COMPRESSION_SUPPORT
31 #ifdef NSIS_COMPRESS_USE_ZLIB
32 #include "../zlib/ZLIB.H"
33 #endif
34
35 #ifdef NSIS_COMPRESS_USE_LZMA
36 #include "../7zip/LZMADecode.h"
37 #define z_stream lzma_stream
38 #define inflateInit(x) lzmaInit(x)
39 #define inflateReset(x) lzmaInit(x)
40 #define inflate(x) lzmaDecode(x)
41 #define Z_OK LZMA_OK
42 #define Z_STREAM_END LZMA_STREAM_END
43 #endif
44
45 #ifdef NSIS_COMPRESS_USE_BZIP2
46 #include "../bzip2/bzlib.h"
47
48 #define z_stream DState
49 #define inflateInit(x) BZ2_bzDecompressInit(x)
50 #define inflateReset(x) BZ2_bzDecompressInit(x)
51
52 #define inflate(x) BZ2_bzDecompress(x)
53 #define Z_OK BZ_OK
54 #define Z_STREAM_END BZ_STREAM_END
55 #endif//NSIS_COMPRESS_USE_BZIP2
56 #endif//NSIS_CONFIG_COMPRESSION_SUPPORT
57
58 struct block_header g_blocks[BLOCKS_NUM];
59 header *g_header;
60 int g_flags;
61 UINT g_filehdrsize;
62 int g_is_uninstaller;
63
64 HANDLE g_db_hFile=INVALID_HANDLE_VALUE;
65
66 #if defined(NSIS_CONFIG_COMPRESSION_SUPPORT) && defined(NSIS_COMPRESS_WHOLE)
67 HANDLE dbd_hFile=INVALID_HANDLE_VALUE;
68 static int dbd_size, dbd_pos, dbd_srcpos, dbd_fulllen;
69 #endif//NSIS_COMPRESS_WHOLE
70
71 static MAXSIZETYPE m_length;
72 static UINT m_pos;
73
74 #define _calc_percent() (MulDiv(min(m_pos,m_length),100,m_length))
75 #ifdef NSIS_COMPRESS_WHOLE
76 static int NSISCALL calc_percent()
77 {
78 return _calc_percent();
79 }
80 #else
81 #define calc_percent() _calc_percent()
82 #endif
83
84 #ifdef NSIS_CONFIG_VISIBLE_SUPPORT
85 #if defined(NSIS_CONFIG_CRC_SUPPORT) || defined(NSIS_COMPRESS_WHOLE)
86 INT_PTR CALLBACK verProc(HWND hwndDlg, UINT uMsg, WPARAM wParam, LPARAM lParam)
87 {
88 if (uMsg == WM_INITDIALOG)
89 {
90 SetTimer(hwndDlg,1,250,NULL);
91 uMsg = WM_TIMER;
92 }
93 if (uMsg == WM_TIMER)
94 {
95 TCHAR bt[64];
96 int percent=calc_percent();
97 #ifdef NSIS_COMPRESS_WHOLE
98 TCHAR *msg=g_header?_LANG_UNPACKING:_LANG_VERIFYINGINST;
99 #else
100 TCHAR *msg=_LANG_VERIFYINGINST;
101 #endif
102 wsprintf(bt,msg,percent);
103 my_SetWindowText(hwndDlg,bt);
104 my_SetDialogItemText(hwndDlg,IDC_STR,bt);
105 }
106 return FALSE;
107 }
108
109 DWORD verify_time;
110
111 void handle_ver_dlg(BOOL kill)
112 {
113 static HWND hwnd;
114
115 if (kill)
116 {
117 if (hwnd) DestroyWindow(hwnd);
118 hwnd = NULL;
119
120 return;
121 }
122
123 if (hwnd)
124 {
125 MessageLoop(0);
126 }
127 else if (GetTickCount() > verify_time)
128 {
129 #ifdef NSIS_COMPRESS_WHOLE
130 if (g_hwnd)
131 {
132 if (g_exec_flags.status_update & 1)
133 {
134 TCHAR bt[64];
135 wsprintf(bt, _T("... %d%%"), calc_percent());
136 update_status_text(0, bt);
137 }
138 }
139 else
140 #endif
141 {
142 hwnd = CreateDialog(
143 g_hInstance,
144 MAKEINTRESOURCE(IDD_VERIFY),
145 0,
146 verProc
147 );
148 ShowWindow(hwnd, SW_SHOW);
149 }
150 }
151 }
152
153 #endif//NSIS_CONFIG_CRC_SUPPORT || NSIS_COMPRESS_WHOLE
154 #endif//NSIS_CONFIG_VISIBLE_SUPPORT
155
156 #ifdef NSIS_CONFIG_COMPRESSION_SUPPORT
157 static z_stream g_inflate_stream;
158 #endif
159
160 const TCHAR * NSISCALL loadHeaders(int cl_flags)
161 {
162 MAXSIZETYPE left;
163 #ifdef NSIS_CONFIG_CRC_SUPPORT
164 crc32_t crc = 0;
165 int do_crc = 0;
166 #endif//NSIS_CONFIG_CRC_SUPPORT
167
168 void *data;
169 firstheader h;
170 header *header;
171
172 HANDLE db_hFile;
173 enum { opentotalwait = 1500, opentrywait = 250, maxopentries = opentotalwait / opentrywait };
174 UINT opentries = 0;
175
176 #ifdef C_ASSERT
177 {C_ASSERT(sizeof(firstheader) == sizeof(int) * 7);}
178 {C_ASSERT(sizeof(struct block_header) == sizeof(UINT_PTR) + sizeof(int));}
179 {C_ASSERT(LASIF_FITCTLW >> LASIS_FITCTLW == 1);}
180 {C_ASSERT(LASIF_LR_LOADFROMFILE == LR_LOADFROMFILE);}
181 {C_ASSERT(opentotalwait == opentrywait * maxopentries);}
182 #endif
183
184 #ifdef NSIS_CONFIG_CRC_SUPPORT
185 #ifdef NSIS_CONFIG_VISIBLE_SUPPORT
186 verify_time = GetTickCount() + 1000;
187 #endif
188 #endif//NSIS_CONFIG_CRC_SUPPORT
189
190 GetModuleFileName(NULL, state_exe_path, NSIS_MAX_STRLEN);
191 retry:
192 g_db_hFile = db_hFile = myOpenFile(state_exe_path, GENERIC_READ, OPEN_EXISTING);
193 if (db_hFile == INVALID_HANDLE_VALUE)
194 {
195 UINT error = GetLastError();
196 if (error == ERROR_SHARING_VIOLATION && ++opentries <= maxopentries)
197 {
198 Sleep(opentrywait);
199 goto retry;
200 }
201 return _LANG_CANTOPENSELF;
202 }
203
204 mystrcpy(state_exe_directory, state_exe_path);
205 mystrcpy(state_exe_file, trimslashtoend(state_exe_directory));
206
207 left = m_length = GetFileSize(db_hFile,NULL);
208 while (left > 0)
209 {
210 static char temp[32768];
211 DWORD l = min(left, (g_filehdrsize ? 32768UL : 512UL));
212 if (!ReadSelfFile(temp, l))
213 {
214 #if defined(NSIS_CONFIG_CRC_SUPPORT) && defined(NSIS_CONFIG_VISIBLE_SUPPORT)
215 handle_ver_dlg(TRUE);
216 #endif//NSIS_CONFIG_CRC_SUPPORT
217 return _LANG_INVALIDCRC;
218 }
219
220 if (!g_filehdrsize)
221 {
222 mini_memcpy(&h, temp, sizeof(firstheader));
223 if (
224 (h.flags & (~FH_FLAGS_MASK)) == 0 &&
225 h.siginfo == FH_SIG &&
226 h.nsinst[2] == FH_INT3 &&
227 h.nsinst[1] == FH_INT2 &&
228 h.nsinst[0] == FH_INT1
229 )
230 {
231 g_filehdrsize = m_pos;
232
233 #if defined(NSIS_CONFIG_CRC_SUPPORT) || defined(NSIS_CONFIG_SILENT_SUPPORT)
234 cl_flags |= h.flags;
235 #endif
236
237 #ifdef NSIS_CONFIG_SILENT_SUPPORT
238 g_exec_flags.silent |= cl_flags & FH_FLAGS_SILENT;
239 #endif
240
241 if ((MAXEXEDATASIZETYPE) h.length_of_all_following_data > left)
242 return _LANG_INVALIDCRC;
243
244 #ifdef NSIS_CONFIG_CRC_SUPPORT
245 if ((cl_flags & FH_FLAGS_FORCE_CRC) == 0)
246 {
247 if (cl_flags & FH_FLAGS_NO_CRC)
248 break;
249 }
250
251 do_crc++;
252
253 #ifndef NSIS_CONFIG_CRC_ANAL
254 left = h.length_of_all_following_data - 4;
255 // end crc checking at crc :) this means you can tack stuff on the end and it'll still work.
256 #else //!NSIS_CONFIG_CRC_ANAL
257 left -= 4;
258 #endif//NSIS_CONFIG_CRC_ANAL
259 // this is in case the end part is < 512 bytes.
260 if (l > (DWORD)left) l=(DWORD)left;
261
262 #else//!NSIS_CONFIG_CRC_SUPPORT
263 // no crc support, no need to keep on reading
264 break;
265 #endif//!NSIS_CONFIG_CRC_SUPPORT
266 }
267 }
268 #ifdef NSIS_CONFIG_CRC_SUPPORT
269
270 #ifdef NSIS_CONFIG_VISIBLE_SUPPORT
271
272 #ifdef NSIS_CONFIG_SILENT_SUPPORT
273 else if ((cl_flags & FH_FLAGS_SILENT) == 0)
274 #endif//NSIS_CONFIG_SILENT_SUPPORT
275 {
276 handle_ver_dlg(FALSE);
277 }
278 #endif//NSIS_CONFIG_VISIBLE_SUPPORT
279
280 #ifndef NSIS_CONFIG_CRC_ANAL
281 if (left < m_length)
282 #endif//NSIS_CONFIG_CRC_ANAL
283 crc = CRC32(crc, (unsigned char*)temp, l);
284
285 #endif//NSIS_CONFIG_CRC_SUPPORT
286 m_pos += l;
287 left -= l;
288 }
289 #ifdef NSIS_CONFIG_VISIBLE_SUPPORT
290 #ifdef NSIS_CONFIG_CRC_SUPPORT
291 handle_ver_dlg(TRUE);
292 #endif//NSIS_CONFIG_CRC_SUPPORT
293 #endif//NSIS_CONFIG_VISIBLE_SUPPORT
294 if (!g_filehdrsize)
295 return _LANG_INVALIDCRC;
296
297 #ifdef NSIS_CONFIG_CRC_SUPPORT
298 if (do_crc)
299 {
300 crc32_t fcrc;
301 SetSelfFilePointer(m_pos);
302 if (!ReadSelfFile(&fcrc, sizeof(crc32_t)) || crc != fcrc)
303 return _LANG_INVALIDCRC;
304 }
305 #endif//NSIS_CONFIG_CRC_SUPPORT
306
307 data = (void *)GlobalAlloc(GPTR,h.length_of_header);
308
309 #ifdef NSIS_COMPRESS_WHOLE
310 inflateReset(&g_inflate_stream);
311
312 {
313 TCHAR fno[MAX_PATH];
314 my_GetTempFileName(fno, state_temp_dir);
315 dbd_hFile=CreateFile(fno,GENERIC_WRITE|GENERIC_READ,0,NULL,CREATE_ALWAYS,FILE_ATTRIBUTE_TEMPORARY|FILE_FLAG_DELETE_ON_CLOSE,NULL);
316 if (dbd_hFile == INVALID_HANDLE_VALUE)
317 return _LANG_ERRORWRITINGTEMP;
318 }
319 dbd_srcpos = SetSelfFilePointer(g_filehdrsize + sizeof(firstheader));
320 #ifdef NSIS_CONFIG_CRC_SUPPORT
321 dbd_fulllen = dbd_srcpos - sizeof(h) + h.length_of_all_following_data - ((h.flags & FH_FLAGS_NO_CRC) ? 0 : sizeof(crc32_t));
322 #else
323 dbd_fulllen = dbd_srcpos - sizeof(h) + h.length_of_all_following_data;
324 #endif//NSIS_CONFIG_CRC_SUPPORT
325 #else
326 SetSelfFilePointer(g_filehdrsize + sizeof(firstheader));
327 #endif//NSIS_COMPRESS_WHOLE
328
329 if (GetCompressedDataFromDataBlockToMemory(-1, data, h.length_of_header) != h.length_of_header)
330 {
331 return _LANG_INVALIDCRC;
332 }
333
334 header = g_header = data;
335
336 g_flags = header->flags;
337
338 #ifdef NSIS_CONFIG_UNINSTALL_SUPPORT
339 if (h.flags & FH_FLAGS_UNINSTALL)
340 g_is_uninstaller++;
341 #endif
342
343 // set offsets to real memory offsets rather than installer's header offset
344 left = BLOCKS_NUM;
345 while (left--)
346 {
347 #ifdef DEBUG
348 if ((UINT_PTR) h.length_of_header < header->blocks[left].offset)
349 return _LANG_GENERIC_ERROR; // Should never happen
350 #endif
351 header->blocks[left].offset += (UINT_PTR) data;
352 }
353
354 #ifdef NSIS_COMPRESS_WHOLE
355 header->blocks[NB_DATA].offset = dbd_pos;
356 #else
357 header->blocks[NB_DATA].offset = SetFilePointer(db_hFile,0,NULL,FILE_CURRENT);
358 #endif
359
360 mini_memcpy(&g_blocks, &header->blocks, sizeof(g_blocks));
361
362 return 0;
363 }
364
365 #define IBUFSIZE 16384
366 #define OBUFSIZE 32768
367
368 // returns -3 if compression error/eof/etc
369
370 #if !defined(NSIS_COMPRESS_WHOLE) || !defined(NSIS_CONFIG_COMPRESSION_SUPPORT)
371
372 // Decompress data.
373 int NSISCALL _dodecomp(int offset, HANDLE hFileOut, unsigned char *outbuf, int outbuflen)
374 {
375 static char inbuffer[IBUFSIZE+OBUFSIZE];
376 char *outbuffer;
377 int outbuffer_len=outbuf?outbuflen:OBUFSIZE;
378 int retval=0;
379 int input_len;
380
381 outbuffer = outbuf?(char*)outbuf:(inbuffer+IBUFSIZE);
382
383 if (offset>=0)
384 {
385 UINT_PTR datofs=g_blocks[NB_DATA].offset+offset;
386 #if (NSIS_MAX_EXEDATASIZE+0) > 0x7fffffffUL
387 #error "SetFilePointer is documented to only support signed 32-bit offsets in lDistanceToMove"
388 #endif
389 const int pos=(int)datofs;
390 SetSelfFilePointer(pos);
391 }
392
393 if (!ReadSelfFile((LPVOID)&input_len,sizeof(int))) return -3;
394
395 #ifdef NSIS_CONFIG_COMPRESSION_SUPPORT
396 if (input_len & 0x80000000) // compressed
397 {
398 TCHAR progress[64];
399 int input_len_total;
400 DWORD ltc = GetTickCount(), tc;
401
402 inflateReset(&g_inflate_stream);
403 input_len_total = input_len &= 0x7fffffff; // take off top bit.
404
405 while (input_len > 0)
406 {
407 int l=min(input_len,IBUFSIZE);
408 int err;
409
410 if (!ReadSelfFile((LPVOID)inbuffer,l))
411 return -3;
412
413 g_inflate_stream.next_in = (unsigned char*) inbuffer;
414 g_inflate_stream.avail_in = l;
415 input_len-=l;
416
417 for (;;)
418 {
419 int u;
420
421 g_inflate_stream.next_out = (unsigned char*) outbuffer;
422 g_inflate_stream.avail_out = (unsigned int)outbuffer_len;
423
424 err=inflate(&g_inflate_stream);
425
426 if (err<0) return -4;
427
428 u=BUGBUG64TRUNCATE(int, (size_t)((char*)g_inflate_stream.next_out - outbuffer));
429
430 tc=GetTickCount();
431 if (g_exec_flags.status_update & 1 && (tc - ltc > 200 || !input_len))
432 {
433 wsprintf(progress, _T("... %d%%"), MulDiv(input_len_total - input_len, 100, input_len_total));
434 update_status_text(0, progress);
435 ltc=tc;
436 }
437
438 // if there's no output, more input is needed
439 if (!u)
440 break;
441
442 if (!outbuf)
443 {
444 if (!myWriteFile(hFileOut,outbuffer,u)) return -2;
445 retval+=u;
446 }
447 else
448 {
449 retval+=u;
450 outbuffer_len-=u;
451 outbuffer=(char*)g_inflate_stream.next_out;
452 }
453 if (err==Z_STREAM_END) return retval;
454 }
455 }
456 }
457 else
458 #endif//NSIS_CONFIG_COMPRESSION_SUPPORT
459 {
460 if (!outbuf)
461 {
462 while (input_len > 0)
463 {
464 DWORD l=min(input_len,outbuffer_len);
465 if (!ReadSelfFile((LPVOID)inbuffer,l)) return -3;
466 if (!myWriteFile(hFileOut,inbuffer,l)) return -2;
467 retval+=l;
468 input_len-=l;
469 }
470 }
471 else
472 {
473 int l=min(input_len,outbuflen);
474 if (!ReadSelfFile((LPVOID)outbuf,l)) return -3;
475 retval=l;
476 }
477 }
478 return retval;
479 }
480 #else//NSIS_COMPRESS_WHOLE
481
482 static char _inbuffer[IBUFSIZE];
483 static char _outbuffer[OBUFSIZE];
484 extern MAXSIZETYPE m_length;
485 extern UINT m_pos;
486 extern INT_PTR CALLBACK verProc(HWND, UINT, WPARAM, LPARAM);
487 extern INT_PTR CALLBACK DialogProc(HWND, UINT, WPARAM, LPARAM);
488 static int NSISCALL __ensuredata(int amount)
489 {
490 int needed=amount-(dbd_size-dbd_pos);
491 #ifdef NSIS_CONFIG_VISIBLE_SUPPORT
492 verify_time=GetTickCount()+500;
493 #endif
494 if (needed>0)
495 {
496 SetSelfFilePointer(dbd_srcpos);
497 SetFilePointer(dbd_hFile,dbd_size,NULL,FILE_BEGIN);
498 m_length=needed;
499 m_pos=0;
500 for (;;)
501 {
502 int err;
503 int l=min(IBUFSIZE,dbd_fulllen-dbd_srcpos);
504 if (!ReadSelfFile((LPVOID)_inbuffer,l)) return -1;
505 dbd_srcpos+=l;
506 g_inflate_stream.next_in=(unsigned char*)_inbuffer;
507 g_inflate_stream.avail_in=l;
508 do
509 {
510 DWORD r;
511 #ifdef NSIS_CONFIG_VISIBLE_SUPPORT
512 if (g_header)
513 #ifdef NSIS_CONFIG_SILENT_SUPPORT
514 if (!g_exec_flags.silent)
515 #endif
516 {
517 m_pos=m_length-(amount-(dbd_size-dbd_pos));
518
519 handle_ver_dlg(FALSE);
520 }
521 #endif//NSIS_CONFIG_VISIBLE_SUPPORT
522 g_inflate_stream.next_out=(unsigned char*)_outbuffer;
523 g_inflate_stream.avail_out=OBUFSIZE;
524 err=inflate(&g_inflate_stream);
525 if (err<0)
526 {
527 return -3;
528 }
529 r=BUGBUG64TRUNCATE(DWORD,(UINT_PTR)g_inflate_stream.next_out)-BUGBUG64TRUNCATE(DWORD,(UINT_PTR)_outbuffer);
530 if (r)
531 {
532 if (!myWriteFile(dbd_hFile,_outbuffer,r))
533 {
534 return -2;
535 }
536 dbd_size+=r;
537 }
538 else if (g_inflate_stream.avail_in || !l) return -3;
539 else break;
540 }
541 while (g_inflate_stream.avail_in);
542 if (amount-(dbd_size-dbd_pos) <= 0) break;
543 }
544 SetFilePointer(dbd_hFile,dbd_pos,NULL,FILE_BEGIN);
545 }
546 #ifdef NSIS_CONFIG_VISIBLE_SUPPORT
547 handle_ver_dlg(TRUE);
548 #endif//NSIS_CONFIG_VISIBLE_SUPPORT
549 return 0;
550 }
551
552
553 int NSISCALL _dodecomp(int offset, HANDLE hFileOut, unsigned char *outbuf, int outbuflen)
554 {
555 DWORD r;
556 int input_len;
557 int retval;
558 if (offset>=0)
559 {
560 UINT_PTR datofs=g_blocks[NB_DATA].offset+offset;
561 #if (NSIS_MAX_EXEDATASIZE+0) > 0x7fffffffUL
562 #error "SetFilePointer is documented to only support signed 32-bit offsets in lDistanceToMove"
563 #endif
564 dbd_pos=(int)datofs;
565 SetFilePointer(dbd_hFile,dbd_pos,NULL,FILE_BEGIN);
566 }
567 retval=__ensuredata(sizeof(int));
568 if (retval<0) return retval;
569
570 if (!myReadFile(dbd_hFile,(LPVOID)&input_len,sizeof(int))) return -3;
571 dbd_pos+=sizeof(int);
572
573 retval=__ensuredata(input_len);
574 if (retval < 0) return retval;
575
576 if (!outbuf)
577 {
578 while (input_len > 0)
579 {
580 DWORD l=min(input_len,IBUFSIZE);
581 if (!myReadFile(dbd_hFile,(LPVOID)_inbuffer,r=l)) return -3;
582 if (!myWriteFile(hFileOut,_inbuffer,r)) return -2;
583 retval+=r;
584 input_len-=r;
585 dbd_pos+=r;
586 }
587 }
588 else
589 {
590 if (!ReadFile(dbd_hFile,(LPVOID)outbuf,min(input_len,outbuflen),&r,NULL)) return -3;
591 retval=r;
592 dbd_pos+=r;
593 }
594 return retval;
595 }
596 #endif//NSIS_COMPRESS_WHOLE
597
598 BOOL NSISCALL ReadSelfFile(LPVOID lpBuffer, DWORD nNumberOfBytesToRead)
599 {
600 return myReadFile(g_db_hFile,lpBuffer,nNumberOfBytesToRead);
601 }
602
603 DWORD NSISCALL SetSelfFilePointer(LONG lDistanceToMove)
604 {
605 return SetFilePointer(g_db_hFile,lDistanceToMove,NULL,FILE_BEGIN);
606 }