1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
|
// Archive/NsisIn.cpp
#include "StdAfx.h"
// #include <stdio.h>
#include "NsisIn.h"
#include "NsisDecode.h"
#include "Windows/Defs.h"
#include "../../Common/StreamUtils.h"
#include "Common/StringConvert.h"
#include "Common/IntToString.h"
namespace NArchive {
namespace NNsis {
Byte kSignature[kSignatureSize] = { 0xEF + 1, 0xBE, 0xAD, 0xDE,
0x4E, 0x75, 0x6C, 0x6C, 0x73, 0x6F, 0x66, 0x74, 0x49, 0x6E, 0x73, 0x74};
class SignatureInitializer
{
public:
SignatureInitializer() { kSignature[0]--; };
} g_SignatureInitializer;
#ifdef NSIS_SCRIPT
static const char *kCrLf = "\x0D\x0A";
#endif
#define NS_UN_SKIP_CODE 0xE000
#define NS_UN_VAR_CODE 0xE001
#define NS_UN_SHELL_CODE 0xE002
#define NS_UN_LANG_CODE 0xE003
#define NS_UN_CODES_START NS_UN_SKIP_CODE
#define NS_UN_CODES_END NS_UN_LANG_CODE
UInt32 GetUInt32FromMemLE(const Byte *p)
{
return p[0] | (((UInt32)p[1]) << 8) | (((UInt32)p[2]) << 16) | (((UInt32)p[3]) << 24);
}
Byte CInArchive::ReadByte()
{
if (_posInData >= _size)
throw 1;
return _data[_posInData++];
}
UInt32 CInArchive::ReadUInt32()
{
UInt32 value = 0;
for (int i = 0; i < 4; i++)
value |= ((UInt32)(ReadByte()) << (8 * i));
return value;
}
void CInArchive::ReadBlockHeader(CBlockHeader &bh)
{
bh.Offset = ReadUInt32();
bh.Num = ReadUInt32();
}
#define RINOZ(x) { int __tt = (x); if (__tt != 0) return __tt; }
static int CompareItems(void *const *p1, void *const *p2, void * /* param */)
{
const CItem &i1 = **(CItem **)p1;
const CItem &i2 = **(CItem **)p2;
RINOZ(MyCompare(i1.Pos, i2.Pos));
if (i1.IsUnicode)
{
RINOZ(i1.PrefixU.Compare(i2.PrefixU));
RINOZ(i1.NameU.Compare(i2.NameU));
}
else
{
RINOZ(i1.PrefixA.Compare(i2.PrefixA));
RINOZ(i1.NameA.Compare(i2.NameA));
}
return 0;
}
AString CInArchive::ReadStringA(UInt32 pos)
{
AString s;
UInt32 offset = GetOffset() + _stringsPos + pos;
for (;;)
{
if (offset >= _size)
throw 1;
char c = _data[offset++];
if (c == 0)
break;
s += c;
}
return s;
}
UString CInArchive::ReadStringU(UInt32 pos)
{
UString s;
UInt32 offset = GetOffset() + _stringsPos + (pos * 2);
for (;;)
{
if (offset >= _size || offset + 1 >= _size)
throw 1;
char c0 = _data[offset++];
char c1 = _data[offset++];
wchar_t c = (c0 | ((wchar_t)c1 << 8));
if (c == 0)
break;
s += c;
}
return s;
}
/*
static AString ParsePrefix(const AString &prefix)
{
AString res = prefix;
if (prefix.Length() >= 3)
{
if ((Byte)prefix[0] == 0xFD && (Byte)prefix[1] == 0x95 && (Byte)prefix[2] == 0x80)
res = "$INSTDIR" + prefix.Mid(3);
else if ((Byte)prefix[0] == 0xFD && (Byte)prefix[1] == 0x96 && (Byte)prefix[2] == 0x80)
res = "$OUTDIR" + prefix.Mid(3);
}
return res;
}
*/
#define SYSREGKEY "Software\\Microsoft\\Windows\\CurrentVersion"
/*
# define CSIDL_PROGRAMS 0x2
# define CSIDL_PRINTERS 0x4
# define CSIDL_PERSONAL 0x5
# define CSIDL_FAVORITES 0x6
# define CSIDL_STARTUP 0x7
# define CSIDL_RECENT 0x8
# define CSIDL_SENDTO 0x9
# define CSIDL_STARTMENU 0xB
# define CSIDL_MYMUSIC 0xD
# define CSIDL_MYVIDEO 0xE
# define CSIDL_DESKTOPDIRECTORY 0x10
# define CSIDL_NETHOOD 0x13
# define CSIDL_FONTS 0x14
# define CSIDL_TEMPLATES 0x15
# define CSIDL_COMMON_STARTMENU 0x16
# define CSIDL_COMMON_PROGRAMS 0x17
# define CSIDL_COMMON_STARTUP 0x18
# define CSIDL_COMMON_DESKTOPDIRECTORY 0x19
# define CSIDL_APPDATA 0x1A
# define CSIDL_PRINTHOOD 0x1B
# define CSIDL_LOCAL_APPDATA 0x1C
# define CSIDL_ALTSTARTUP 0x1D
# define CSIDL_COMMON_ALTSTARTUP 0x1E
# define CSIDL_COMMON_FAVORITES 0x1F
# define CSIDL_INTERNET_CACHE 0x20
# define CSIDL_COOKIES 0x21
# define CSIDL_HISTORY 0x22
# define CSIDL_COMMON_APPDATA 0x23
# define CSIDL_WINDOWS 0x24
# define CSIDL_SYSTEM 0x25
# define CSIDL_PROGRAM_FILES 0x26
# define CSIDL_MYPICTURES 0x27
# define CSIDL_PROFILE 0x28
# define CSIDL_PROGRAM_FILES_COMMON 0x2B
# define CSIDL_COMMON_TEMPLATES 0x2D
# define CSIDL_COMMON_DOCUMENTS 0x2E
# define CSIDL_COMMON_ADMINTOOLS 0x2F
# define CSIDL_ADMINTOOLS 0x30
# define CSIDL_COMMON_MUSIC 0x35
# define CSIDL_COMMON_PICTURES 0x36
# define CSIDL_COMMON_VIDEO 0x37
# define CSIDL_RESOURCES 0x38
# define CSIDL_RESOURCES_LOCALIZED 0x39
# define CSIDL_CDBURN_AREA 0x3B
*/
struct CCommandPair
{
int NumParams;
const char *Name;
};
enum
{
// 0
EW_INVALID_OPCODE, // zero is invalid. useful for catching errors. (otherwise an all zeroes instruction
// does nothing, which is easily ignored but means something is wrong.
EW_RET, // return from function call
EW_NOP, // Nop/Jump, do nothing: 1, [?new address+1:advance one]
EW_ABORT, // Abort: 1 [status]
EW_QUIT, // Quit: 0
EW_CALL, // Call: 1 [new address+1]
EW_UPDATETEXT, // Update status text: 2 [update str, ui_st_updateflag=?ui_st_updateflag:this]
EW_SLEEP, // Sleep: 1 [sleep time in milliseconds]
EW_BRINGTOFRONT, // BringToFront: 0
EW_CHDETAILSVIEW, // SetDetailsView: 2 [listaction,buttonaction]
// 10
EW_SETFILEATTRIBUTES, // SetFileAttributes: 2 [filename, attributes]
EW_CREATEDIR, // Create directory: 2, [path, ?update$INSTDIR]
EW_IFFILEEXISTS, // IfFileExists: 3, [file name, jump amount if exists, jump amount if not exists]
EW_SETFLAG, // Sets a flag: 2 [id, data]
EW_IFFLAG, // If a flag: 4 [on, off, id, new value mask]
EW_GETFLAG, // Gets a flag: 2 [output, id]
EW_RENAME, // Rename: 3 [old, new, rebootok]
EW_GETFULLPATHNAME, // GetFullPathName: 2 [output, input, ?lfn:sfn]
EW_SEARCHPATH, // SearchPath: 2 [output, filename]
EW_GETTEMPFILENAME, // GetTempFileName: 2 [output, base_dir]
// 20
EW_EXTRACTFILE, // File to extract: 6 [overwriteflag, output filename, compressed filedata, filedatetimelow, filedatetimehigh, allow ignore]
// overwriteflag: 0x1 = no. 0x0=force, 0x2=try, 0x3=if date is newer
EW_DELETEFILE, // Delete File: 2, [filename, rebootok]
EW_MESSAGEBOX, // MessageBox: 5,[MB_flags,text,retv1:retv2,moveonretv1:moveonretv2]
EW_RMDIR, // RMDir: 2 [path, recursiveflag]
EW_STRLEN, // StrLen: 2 [output, input]
EW_ASSIGNVAR, // Assign: 4 [variable (0-9) to assign, string to assign, maxlen, startpos]
EW_STRCMP, // StrCmp: 5 [str1, str2, jump_if_equal, jump_if_not_equal, case-sensitive?]
EW_READENVSTR, // ReadEnvStr/ExpandEnvStrings: 3 [output, string_with_env_variables, IsRead]
EW_INTCMP, // IntCmp: 6 [val1, val2, equal, val1<val2, val1>val2, unsigned?]
EW_INTOP, // IntOp: 4 [output, input1, input2, op] where op: 0=add, 1=sub, 2=mul, 3=div, 4=bor, 5=band, 6=bxor, 7=bnot input1, 8=lnot input1, 9=lor, 10=land], 11=1%2
// 30
EW_INTFMT, // IntFmt: [output, format, input]
EW_PUSHPOP, // Push/Pop/Exchange: 3 [variable/string, ?pop:push, ?exch]
EW_FINDWINDOW, // FindWindow: 5, [outputvar, window class,window name, window_parent, window_after]
EW_SENDMESSAGE, // SendMessage: 6 [output, hwnd, msg, wparam, lparam, [wparamstring?1:0 | lparamstring?2:0 | timeout<<2]
EW_ISWINDOW, // IsWindow: 3 [hwnd, jump_if_window, jump_if_notwindow]
EW_GETDLGITEM, // GetDlgItem: 3: [outputvar, dialog, item_id]
EW_SETCTLCOLORS, // SerCtlColors: 3: [hwnd, pointer to struct colors]
EW_SETBRANDINGIMAGE, // SetBrandingImage: 1: [Bitmap file]
EW_CREATEFONT, // CreateFont: 5: [handle output, face name, height, weight, flags]
EW_SHOWWINDOW, // ShowWindow: 2: [hwnd, show state]
// 40
EW_SHELLEXEC, // ShellExecute program: 4, [shell action, complete commandline, parameters, showwindow]
EW_EXECUTE, // Execute program: 3,[complete command line,waitflag,>=0?output errorcode]
EW_GETFILETIME, // GetFileTime; 3 [file highout lowout]
EW_GETDLLVERSION, // GetDLLVersion: 3 [file highout lowout]
EW_REGISTERDLL, // Register DLL: 3,[DLL file name, string ptr of function to call, text to put in display (<0 if none/pass parms), 1 - no unload, 0 - unload]
EW_CREATESHORTCUT, // Make Shortcut: 5, [link file, target file, parameters, icon file, iconindex|show mode<<8|hotkey<<16]
EW_COPYFILES, // CopyFiles: 3 [source mask, destination location, flags]
EW_REBOOT, // Reboot: 0
EW_WRITEINI, // Write INI String: 4, [Section, Name, Value, INI File]
EW_READINISTR, // ReadINIStr: 4 [output, section, name, ini_file]
// 50
EW_DELREG, // DeleteRegValue/DeleteRegKey: 4, [root key(int), KeyName, ValueName, delkeyonlyifempty]. ValueName is -1 if delete key
EW_WRITEREG, // Write Registry value: 5, [RootKey(int),KeyName,ItemName,ItemData,typelen]
// typelen=1 for str, 2 for dword, 3 for binary, 0 for expanded str
EW_READREGSTR, // ReadRegStr: 5 [output, rootkey(int), keyname, itemname, ==1?int::str]
EW_REGENUM, // RegEnum: 5 [output, rootkey, keyname, index, ?key:value]
EW_FCLOSE, // FileClose: 1 [handle]
EW_FOPEN, // FileOpen: 4 [name, openmode, createmode, outputhandle]
EW_FPUTS, // FileWrite: 3 [handle, string, ?int:string]
EW_FGETS, // FileRead: 4 [handle, output, maxlen, ?getchar:gets]
EW_FSEEK, // FileSeek: 4 [handle, offset, mode, >=0?positionoutput]
EW_FINDCLOSE, // FindClose: 1 [handle]
// 60
EW_FINDNEXT, // FindNext: 2 [output, handle]
EW_FINDFIRST, // FindFirst: 2 [filespec, output, handleoutput]
EW_WRITEUNINSTALLER, // WriteUninstaller: 3 [name, offset, icon_size]
EW_LOG, // LogText: 2 [0, text] / LogSet: [1, logstate]
EW_SECTIONSET, // SectionSetText: 3: [idx, 0, text]
// SectionGetText: 3: [idx, 1, output]
// SectionSetFlags: 3: [idx, 2, flags]
// SectionGetFlags: 3: [idx, 3, output]
EW_INSTTYPESET, // InstTypeSetFlags: 3: [idx, 0, flags]
// InstTypeGetFlags: 3: [idx, 1, output]
// instructions not actually implemented in exehead, but used in compiler.
EW_GETLABELADDR, // both of these get converted to EW_ASSIGNVAR
EW_GETFUNCTIONADDR,
EW_LOCKWINDOW
};
#ifdef NSIS_SCRIPT
static CCommandPair kCommandPairs[] =
{
{ 0, "Invalid" },
{ 0, "Return" },
{ 1, "Goto" },
{ 0, "Abort" },
{ 0, "Quit" },
{ 1, "Call" },
{ 2, "UpdateSatusText" },
{ 1, "Sleep" },
{ 0, "BringToFront" },
{ 2, "SetDetailsView" },
{ 2, "SetFileAttributes" },
{ 2, "SetOutPath" },
{ 3, "IfFileExists" },
{ 2, "SetFlag" },
{ 4, "IfFlag" },
{ 2, "GetFlag" },
{ 3, "Rename" },
{ 2, "GetFullPathName" },
{ 2, "SearchPath" },
{ 2, "GetTempFileName" },
{ 6, "File" },
{ 2, "Delete" },
{ 5, "MessageBox" },
{ 2, "RMDir" },
{ 2, "Assign" },
{ 4, "StrCpy" },
{ 5, "StrCmp" },
{ 3, "ReadEnvStr" },
{ 6, "IntCmp" },
{ 4, "IntOp" },
{ 3, "IntFmt" },
{ 3, "PushPop" },
{ 5, "FindWindow" },
{ 6, "SendMessage" },
{ 3, "IsWindow" },
{ 3, "GetDlgItem" },
{ 3, "SerCtlColors" },
{ 1, "SetBrandingImage" },
{ 5, "CreateFont" },
{ 2, "ShowWindow" },
{ 4, "ShellExecute" },
{ 3, "Execute" },
{ 3, "GetFileTime" },
{ 3, "GetDLLVersion" },
{ 3, "RegisterDLL" },
{ 5, "CreateShortCut" },
{ 3, "CopyFiles" },
{ 0, "Reboot" },
{ 4, "WriteINIStr" },
{ 4, "ReadINIStr" },
{ 4, "DelReg" },
{ 5, "WriteReg" },
{ 5, "ReadRegStr" },
{ 5, "RegEnum" },
{ 1, "FileClose" },
{ 4, "FileOpen" },
{ 3, "FileWrite" },
{ 4, "FileRead" },
{ 4, "FileSeek" },
{ 1, "FindClose" },
{ 2, "FindNext" },
{ 2, "FindFirst" },
{ 3, "WriteUninstaller" },
{ 2, "LogText" },
{ 3, "Section?etText" },
{ 3, "InstType?etFlags" },
{ 6, "GetLabelAddr" },
{ 2, "GetFunctionAddress" },
{ 6, "LockWindow" }
};
#endif
static const char *kShellStrings[] =
{
"",
"",
"SMPROGRAMS",
"",
"PRINTERS",
"DOCUMENTS",
"FAVORITES",
"SMSTARTUP",
"RECENT",
"SENDTO",
"",
"STARTMENU",
"",
"MUSIC",
"VIDEO",
"",
"DESKTOP",
"",
"",
"NETHOOD",
"FONTS",
"TEMPLATES",
"COMMONSTARTMENU",
"COMMONFILES",
"COMMON_STARTUP",
"COMMON_DESKTOPDIRECTORY",
"QUICKLAUNCH",
"PRINTHOOD",
"LOCALAPPDATA",
"ALTSTARTUP",
"ALTSTARTUP",
"FAVORITES",
"INTERNET_CACHE",
"COOKIES",
"HISTORY",
"APPDATA",
"WINDIR",
"SYSDIR",
"PROGRAMFILES",
"PICTURES",
"PROFILE",
"",
"",
"COMMONFILES",
"",
"TEMPLATES",
"DOCUMENTS",
"ADMINTOOLS",
"ADMINTOOLS",
"",
"",
"",
"",
"MUSIC",
"PICTURES",
"VIDEO",
"RESOURCES",
"RESOURCES_LOCALIZED",
"",
"CDBURN_AREA"
};
static const int kNumShellStrings = sizeof(kShellStrings) / sizeof(kShellStrings[0]);
/*
# define CMDLINE 20 // everything before here doesn't have trailing slash removal
# define INSTDIR 21
# define OUTDIR 22
# define EXEDIR 23
# define LANGUAGE 24
# define TEMP 25
# define PLUGINSDIR 26
# define HWNDPARENT 27
# define _CLICK 28
# define _OUTDIR 29
*/
static const char *kVarStrings[] =
{
"CMDLINE",
"INSTDIR",
"OUTDIR",
"EXEDIR",
"LANGUAGE",
"TEMP",
"PLUGINSDIR",
"HWNDPARENT",
"_CLICK",
"_OUTDIR"
};
static const int kNumVarStrings = sizeof(kVarStrings) / sizeof(kVarStrings[0]);
static AString UIntToString(UInt32 v)
{
char sz[32];
ConvertUInt64ToString(v, sz);
return sz;
}
static AString IntToString(Int32 v)
{
char sz[32];
ConvertInt64ToString(v, sz);
return sz;
}
static AString GetVar(UInt32 index)
{
AString res = "$";
if (index < 10)
res += UIntToString(index);
else if (index < 20)
{
res += "R";
res += UIntToString(index - 10);
}
else if (index < 20 + kNumVarStrings)
res += kVarStrings[index - 20];
else
{
res += "[";
res += UIntToString(index);
res += "]";
}
return res;
}
#define NS_SKIP_CODE 252
#define NS_VAR_CODE 253
#define NS_SHELL_CODE 254
#define NS_LANG_CODE 255
#define NS_CODES_START NS_SKIP_CODE
static AString GetShellString(int index)
{
AString res = "$";
if (index < kNumShellStrings)
{
const char *sz = kShellStrings[index];
if (sz[0] != 0)
return res + sz;
}
res += "SHELL[";
res += UIntToString(index);
res += "]";
return res;
}
// Based on Dave Laundon's simplified process_string
AString GetNsisString(const AString &s)
{
AString res;
for (int i = 0; i < s.Length();)
{
unsigned char nVarIdx = s[i++];
if (nVarIdx > NS_CODES_START && i + 2 <= s.Length())
{
int nData = s[i++] & 0x7F;
unsigned char c1 = s[i++];
nData |= (((int)(c1 & 0x7F)) << 7);
if (nVarIdx == NS_SHELL_CODE)
res += GetShellString(c1);
else if (nVarIdx == NS_VAR_CODE)
res += GetVar(nData);
else if (nVarIdx == NS_LANG_CODE)
res += "NS_LANG_CODE";
}
else if (nVarIdx == NS_SKIP_CODE)
{
if (i < s.Length())
res += s[i++];
}
else // Normal char
res += (char)nVarIdx;
}
return res;
}
UString GetNsisString(const UString &s)
{
UString res;
for (int i = 0; i < s.Length();)
{
wchar_t nVarIdx = s[i++];
if (nVarIdx > NS_UN_CODES_START && nVarIdx <= NS_UN_CODES_END)
{
if (i == s.Length())
break;
int nData = s[i++] & 0x7FFF;
if (nVarIdx == NS_UN_SHELL_CODE)
res += GetUnicodeString(GetShellString(nData >> 8));
else if (nVarIdx == NS_UN_VAR_CODE)
res += GetUnicodeString(GetVar(nData));
else if (nVarIdx == NS_UN_LANG_CODE)
res += L"NS_LANG_CODE";
}
else if (nVarIdx == NS_UN_SKIP_CODE)
{
if (i == s.Length())
break;
res += s[i++];
}
else // Normal char
res += (char)nVarIdx;
}
return res;
}
AString CInArchive::ReadString2A(UInt32 pos)
{
return GetNsisString(ReadStringA(pos));
}
UString CInArchive::ReadString2U(UInt32 pos)
{
return GetNsisString(ReadStringU(pos));
}
AString CInArchive::ReadString2(UInt32 pos)
{
if (IsUnicode)
return UnicodeStringToMultiByte(ReadString2U(pos));
else
return ReadString2A(pos);
}
#define DEL_DIR 1
#define DEL_RECURSE 2
#define DEL_REBOOT 4
// #define DEL_SIMPLE 8
static const int kNumEntryParams = 6;
struct CEntry
{
UInt32 Which;
UInt32 Params[kNumEntryParams];
AString GetParamsString(int numParams);
CEntry()
{
Which = 0;
for (UInt32 j = 0; j < kNumEntryParams; j++)
Params[j] = 0;
}
};
AString CEntry::GetParamsString(int numParams)
{
AString s;
for (int i = 0; i < numParams; i++)
{
s += " ";
UInt32 v = Params[i];
if (v > 0xFFF00000)
s += IntToString((Int32)Params[i]);
else
s += UIntToString(Params[i]);
}
return s;
}
HRESULT CInArchive::ReadEntries(const CBlockHeader &bh)
{
_posInData = bh.Offset + GetOffset();
AString prefixA;
UString prefixU;
for (UInt32 i = 0; i < bh.Num; i++)
{
CEntry e;
e.Which = ReadUInt32();
for (UInt32 j = 0; j < kNumEntryParams; j++)
e.Params[j] = ReadUInt32();
#ifdef NSIS_SCRIPT
if (e.Which != EW_PUSHPOP && e.Which < sizeof(kCommandPairs) / sizeof(kCommandPairs[0]))
{
const CCommandPair &pair = kCommandPairs[e.Which];
Script += pair.Name;
}
#endif
switch (e.Which)
{
case EW_CREATEDIR:
{
if (IsUnicode)
{
prefixU.Empty();
prefixU = ReadString2U(e.Params[0]);
}
else
{
prefixA.Empty();
prefixA = ReadString2A(e.Params[0]);
}
#ifdef NSIS_SCRIPT
Script += " ";
if (IsUnicode)
Script += UnicodeStringToMultiByte(prefixU);
else
Script += prefixA;
#endif
break;
}
case EW_EXTRACTFILE:
{
CItem item;
item.IsUnicode = IsUnicode;
if (IsUnicode)
{
item.PrefixU = prefixU;
item.NameU = ReadString2U(e.Params[1]);
}
else
{
item.PrefixA = prefixA;
item.NameA = ReadString2A(e.Params[1]);
}
/* UInt32 overwriteFlag = e.Params[0]; */
item.Pos = e.Params[2];
item.DateTime.dwLowDateTime = e.Params[3];
item.DateTime.dwHighDateTime = e.Params[4];
/* UInt32 allowIgnore = e.Params[5]; */
if (Items.Size() > 0)
{
/*
if (item.Pos == Items.Back().Pos)
continue;
*/
}
Items.Add(item);
#ifdef NSIS_SCRIPT
Script += " ";
if (IsUnicode)
Script += UnicodeStringToMultiByte(item.NameU);
else
Script += item.NameA;
#endif
break;
}
#ifdef NSIS_SCRIPT
case EW_UPDATETEXT:
{
Script += " ";
Script += ReadString2(e.Params[0]);
Script += " ";
Script += UIntToString(e.Params[1]);
break;
}
case EW_SETFILEATTRIBUTES:
{
Script += " ";
Script += ReadString2(e.Params[0]);
Script += " ";
Script += UIntToString(e.Params[1]);
break;
}
case EW_IFFILEEXISTS:
{
Script += " ";
Script += ReadString2(e.Params[0]);
Script += " ";
Script += UIntToString(e.Params[1]);
Script += " ";
Script += UIntToString(e.Params[2]);
break;
}
case EW_RENAME:
{
Script += " ";
Script += ReadString2(e.Params[0]);
Script += " ";
Script += ReadString2(e.Params[1]);
Script += " ";
Script += UIntToString(e.Params[2]);
break;
}
case EW_GETFULLPATHNAME:
{
Script += " ";
Script += ReadString2(e.Params[0]);
Script += " ";
Script += ReadString2(e.Params[1]);
Script += " ";
Script += UIntToString(e.Params[2]);
break;
}
case EW_SEARCHPATH:
{
Script += " ";
Script += ReadString2(e.Params[0]);
Script += " ";
Script += ReadString2(e.Params[1]);
break;
}
case EW_GETTEMPFILENAME:
{
AString s;
Script += " ";
Script += ReadString2(e.Params[0]);
Script += " ";
Script += ReadString2(e.Params[1]);
break;
}
case EW_DELETEFILE:
{
UInt64 flag = e.Params[1];
if (flag != 0)
{
Script += " ";
if (flag == DEL_REBOOT)
Script += "/REBOOTOK";
else
Script += UIntToString(e.Params[1]);
}
Script += " ";
Script += ReadString2(e.Params[0]);
break;
}
case EW_RMDIR:
{
UInt64 flag = e.Params[1];
if (flag != 0)
{
if ((flag & DEL_REBOOT) != 0)
Script += " /REBOOTOK";
if ((flag & DEL_RECURSE) != 0)
Script += " /r";
}
Script += " ";
Script += ReadString2(e.Params[0]);
break;
}
case EW_ASSIGNVAR:
{
Script += " ";
Script += GetVar(e.Params[0]);;
Script += " \"";
AString maxLen, startOffset;
Script += ReadString2(e.Params[1]);
Script += "\"";
if (e.Params[2] != 0)
maxLen = ReadString(e.Params[2]);
if (e.Params[3] != 0)
startOffset = ReadString(e.Params[3]);
if (!maxLen.IsEmpty() || !startOffset.IsEmpty())
{
Script += " ";
if (maxLen.IsEmpty())
Script += "\"\"";
else
Script += maxLen;
if (!startOffset.IsEmpty())
{
Script += " ";
Script += startOffset;
}
}
break;
}
case EW_STRCMP:
{
Script += " ";
Script += " \"";
Script += ReadString2(e.Params[0]);
Script += "\"";
Script += " \"";
Script += ReadString2(e.Params[1]);
Script += "\"";
for (int j = 2; j < 5; j++)
{
Script += " ";
Script += UIntToString(e.Params[j]);
}
break;
}
case EW_PUSHPOP:
{
int isPop = (e.Params[1] != 0);
if (isPop)
{
Script += "Pop";
Script += " ";
Script += GetVar(e.Params[0]);;
}
else
{
int isExch = (e.Params[2] != 0);
if (isExch)
{
Script += "Exch";
}
else
{
Script += "Push";
Script += " ";
Script += ReadString2(e.Params[0]);
}
}
break;
}
/*
case EW_SENDMESSAGE:
{
Script += " ";
Script += IntToString(e.Params[0]);
Script += " ";
Script += GetVar(e.Params[1]);
Script += " ";
Script += ReadString2(e.Params[2]);
Script += " ";
Script += UIntToString(e.Params[3]);
Script += " ";
Script += IntToString(e.Params[4]);
Script += " ";
Script += UIntToString(e.Params[5]);
break;
}
*/
case EW_REGISTERDLL:
{
Script += " ";
Script += ReadString2(e.Params[0]);
Script += " ";
Script += ReadString2(e.Params[1]);
Script += " ";
Script += UIntToString(e.Params[2]);
break;
}
case EW_CREATESHORTCUT:
{
AString s;
Script += " ";
Script += " \"";
Script += ReadString2(e.Params[0]);
Script += " \"";
Script += " ";
Script += " \"";
Script += ReadString2(e.Params[1]);
Script += " \"";
for (int j = 2; j < 5; j++)
{
Script += " ";
Script += UIntToString(e.Params[j]);
}
break;
}
/*
case EW_DELREG:
{
AString keyName, valueName;
keyName = ReadString2(e.Params[1]);
bool isValue = (e.Params[2] != -1);
if (isValue)
{
valueName = ReadString2(e.Params[2]);
Script += "Key";
}
else
Script += "Value";
Script += " ";
Script += UIntToString(e.Params[0]);
Script += " ";
Script += keyName;
if (isValue)
{
Script += " ";
Script += valueName;
}
Script += " ";
Script += UIntToString(e.Params[3]);
break;
}
*/
case EW_WRITEUNINSTALLER:
{
Script += " ";
Script += ReadString2(e.Params[0]);
for (int j = 1; j < 3; j++)
{
Script += " ";
Script += UIntToString(e.Params[j]);
}
break;
}
default:
{
int numParams = kNumEntryParams;
if (e.Which < sizeof(kCommandPairs) / sizeof(kCommandPairs[0]))
{
const CCommandPair &pair = kCommandPairs[e.Which];
// Script += pair.Name;
numParams = pair.NumParams;
}
else
{
Script += "Unknown";
Script += UIntToString(e.Which);
}
Script += e.GetParamsString(numParams);
}
#endif
}
#ifdef NSIS_SCRIPT
Script += kCrLf;
#endif
}
{
Items.Sort(CompareItems, 0);
int i;
// if (IsSolid)
for (i = 0; i + 1 < Items.Size();)
{
bool sameName = IsUnicode ?
(Items[i].NameU == Items[i + 1].NameU) :
(Items[i].NameA == Items[i + 1].NameA);
if (Items[i].Pos == Items[i + 1].Pos && (IsSolid || sameName))
Items.Delete(i + 1);
else
i++;
}
for (i = 0; i + 1 < Items.Size(); i++)
{
CItem &item = Items[i];
item.EstimatedSizeIsDefined = true;
item.EstimatedSize = Items[i + 1].Pos - item.Pos - 4;
}
if (!IsSolid)
{
for (i = 0; i < Items.Size(); i++)
{
CItem &item = Items[i];
RINOK(_stream->Seek(GetPosOfNonSolidItem(i), STREAM_SEEK_SET, NULL));
const UInt32 kSigSize = 4 + 1 + 5;
BYTE sig[kSigSize];
size_t processedSize = kSigSize;
RINOK(ReadStream(_stream, sig, &processedSize));
if (processedSize < 4)
return S_FALSE;
UInt32 size = GetUInt32FromMemLE(sig);
if ((size & 0x80000000) != 0)
{
item.IsCompressed = true;
// is compressed;
size &= ~0x80000000;
if (Method == NMethodType::kLZMA)
{
if (processedSize < 9)
return S_FALSE;
if (FilterFlag)
item.UseFilter = (sig[4] != 0);
item.DictionarySize = GetUInt32FromMemLE(sig + 5 + (FilterFlag ? 1 : 0));
}
}
else
{
item.IsCompressed = false;
item.Size = size;
item.SizeIsDefined = true;
}
item.CompressedSize = size;
item.CompressedSizeIsDefined = true;
}
}
}
return S_OK;
}
HRESULT CInArchive::Parse()
{
// UInt32 offset = ReadUInt32();
// ???? offset == FirstHeader.HeaderLength
/* UInt32 ehFlags = */ ReadUInt32();
CBlockHeader bhPages, bhSections, bhEntries, bhStrings, bhLangTables, bhCtlColors, bhData;
// CBlockHeader bgFont;
ReadBlockHeader(bhPages);
ReadBlockHeader(bhSections);
ReadBlockHeader(bhEntries);
ReadBlockHeader(bhStrings);
ReadBlockHeader(bhLangTables);
ReadBlockHeader(bhCtlColors);
// ReadBlockHeader(bgFont);
ReadBlockHeader(bhData);
_stringsPos = bhStrings.Offset;
UInt32 pos = GetOffset() + _stringsPos;
int numZeros0 = 0;
int numZeros1 = 0;
int i;
const kBlockSize = 256;
for (i = 0; i < kBlockSize; i++)
{
if (pos >= _size || pos + 1 >= _size)
break;
char c0 = _data[pos++];
char c1 = _data[pos++];
wchar_t c = (c0 | ((wchar_t)c1 << 8));
if (c >= NS_UN_CODES_START && c < NS_UN_CODES_END)
{
if (pos >= _size || pos + 1 >= _size)
break;
pos += 2;
numZeros1++;
}
else
{
if (c0 == 0 && c1 != 0)
numZeros0++;
if (c1 == 0)
numZeros1++;
}
// printf("\nnumZeros0 = %2x %2x", _data[pos + 0], _data[pos + 1]);
}
IsUnicode = (numZeros1 > numZeros0 * 3 + kBlockSize / 16);
// printf("\nnumZeros0 = %3d numZeros1 = %3d", numZeros0, numZeros1);
return ReadEntries(bhEntries);
}
static bool IsLZMA(const Byte *p, UInt32 &dictionary)
{
dictionary = GetUInt32FromMemLE(p + 1);
return (p[0] == 0x5D && p[1] == 0x00 && p[2] == 0x00 && p[5] == 0x00);
}
static bool IsLZMA(const Byte *p, UInt32 &dictionary, bool &thereIsFlag)
{
if (IsLZMA(p, dictionary))
{
thereIsFlag = false;
return true;
}
if (IsLZMA(p + 1, dictionary))
{
thereIsFlag = true;
return true;
}
return false;
}
HRESULT CInArchive::Open2(
DECL_EXTERNAL_CODECS_LOC_VARS2
)
{
RINOK(_stream->Seek(0, STREAM_SEEK_CUR, &StreamOffset));
const UInt32 kSigSize = 4 + 1 + 5 + 1; // size, flag, lzma props, lzma first byte
BYTE sig[kSigSize];
RINOK(ReadStream_FALSE(_stream, sig, kSigSize));
UInt64 position;
RINOK(_stream->Seek(StreamOffset, STREAM_SEEK_SET, &position));
_headerIsCompressed = true;
IsSolid = true;
FilterFlag = false;
UInt32 compressedHeaderSize = GetUInt32FromMemLE(sig);
if (compressedHeaderSize == FirstHeader.HeaderLength)
{
_headerIsCompressed = false;
IsSolid = false;
Method = NMethodType::kCopy;
}
else if (IsLZMA(sig, DictionarySize, FilterFlag))
{
Method = NMethodType::kLZMA;
}
else if (IsLZMA(sig + 4, DictionarySize, FilterFlag))
{
IsSolid = false;
Method = NMethodType::kLZMA;
}
else if (sig[3] == 0x80)
{
IsSolid = false;
Method = NMethodType::kDeflate;
}
else
{
Method = NMethodType::kDeflate;
}
_posInData = 0;
if (!IsSolid)
{
_headerIsCompressed = ((compressedHeaderSize & 0x80000000) != 0);
if (_headerIsCompressed)
compressedHeaderSize &= ~0x80000000;
_nonSolidStartOffset = compressedHeaderSize;
RINOK(_stream->Seek(StreamOffset + 4, STREAM_SEEK_SET, NULL));
}
UInt32 unpackSize = FirstHeader.HeaderLength;
if (_headerIsCompressed)
{
// unpackSize = (1 << 23);
_data.SetCapacity(unpackSize);
RINOK(Decoder.Init(
EXTERNAL_CODECS_LOC_VARS
_stream, Method, FilterFlag, UseFilter));
size_t processedSize = unpackSize;
RINOK(Decoder.Read(_data, &processedSize));
if (processedSize != unpackSize)
return S_FALSE;
_size = processedSize;
if (IsSolid)
{
UInt32 size2 = ReadUInt32();
if (size2 < _size)
_size = size2;
}
}
else
{
_data.SetCapacity(unpackSize);
_size = (size_t)unpackSize;
RINOK(ReadStream_FALSE(_stream, (Byte *)_data, unpackSize));
}
return Parse();
}
/*
NsisExe =
{
ExeStub
Archive // must start from 512 * N
#ifndef NSIS_CONFIG_CRC_ANAL
{
Some additional data
}
}
Archive
{
FirstHeader
Data
#ifdef NSIS_CONFIG_CRC_SUPPORT && FirstHeader.ThereIsCrc()
{
CRC
}
}
FirstHeader
{
UInt32 Flags;
Byte Signature[16];
// points to the header+sections+entries+stringtable in the datablock
UInt32 HeaderLength;
UInt32 ArchiveSize;
}
*/
HRESULT CInArchive::Open(
DECL_EXTERNAL_CODECS_LOC_VARS
IInStream *inStream, const UInt64 *maxCheckStartPosition)
{
Clear();
UInt64 pos;
RINOK(inStream->Seek(0, STREAM_SEEK_CUR, &pos));
RINOK(inStream->Seek(0, STREAM_SEEK_END, &_archiveSize));
UInt64 position;
RINOK(inStream->Seek(pos, STREAM_SEEK_SET, &position));
UInt64 maxSize = (maxCheckStartPosition != 0) ? *maxCheckStartPosition : (1 << 20);
const UInt32 kStep = 512;
const UInt32 kStartHeaderSize = 4 * 7;
Byte buffer[kStep];
bool found = false;
UInt64 headerPosition = 0;
while (position <= maxSize)
{
RINOK(ReadStream_FALSE(inStream, buffer, kStartHeaderSize));
headerPosition = position;
position += kStartHeaderSize;
if(memcmp(buffer + 4, kSignature, kSignatureSize) == 0)
{
found = true;
break;
}
const UInt32 kRem = kStep - kStartHeaderSize;
RINOK(ReadStream_FALSE(inStream, buffer + kStartHeaderSize, kRem));
position += kRem;
}
if (!found)
return S_FALSE;
FirstHeader.Flags = GetUInt32FromMemLE(buffer);
FirstHeader.HeaderLength = GetUInt32FromMemLE(buffer + kSignatureSize + 4);
FirstHeader.ArchiveSize = GetUInt32FromMemLE(buffer + kSignatureSize + 8);
if (_archiveSize - headerPosition < FirstHeader.ArchiveSize)
return S_FALSE;
_stream = inStream;
HRESULT res = S_FALSE;
try
{
res = Open2(
EXTERNAL_CODECS_LOC_VARS2
);
}
catch(...) { Clear(); res = S_FALSE; }
_stream.Release();
return res;
}
void CInArchive::Clear()
{
#ifdef NSIS_SCRIPT
Script.Empty();
#endif
Items.Clear();
}
}}
|