-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEnvironment.cpp
More file actions
executable file
·1850 lines (1631 loc) · 58.6 KB
/
Copy pathEnvironment.cpp
File metadata and controls
executable file
·1850 lines (1631 loc) · 58.6 KB
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
#define _WIN32_WINNT 0x0500
#include <stdio.h>
#include <sys/stat.h>
#include <utime.h>
#include <limits.h>
#include <memory.h>
#include <pthread.h>
#include <stdlib.h>
#include <stdint.h>
#include "Environment.h"
#include "Compression/Compression.h"
// Изменим настройки RTS, включив compacting GC начиная с 40 mb:
char *ghc_rts_opts = "-c1 -M4000m";
/* ********************************************************************************************************
* Find largest contiguous memory block available and dump information about all available memory blocks
***********************************************************************************************************/
void memstat(void);
struct LargestMemoryBlock
{
void *p;
size_t size;
LargestMemoryBlock();
~LargestMemoryBlock() {free();}
void alloc(size_t n);
void free();
void test();
};
LargestMemoryBlock::LargestMemoryBlock() : p(NULL)
{
size_t a=0, b=UINT_MAX;
while (b-a>1) {
free();
size_t c=(a+b)/2;
alloc(c);
if(p) a=c; else b=c;
}
}
void LargestMemoryBlock::test()
{
if ((size>>20)>0) {
printf("Allocated %4d mb, addr=%p\n", size>>20, p);
LargestMemoryBlock next;
next.test();
} else {
memstat();
}
}
void TestMalloc (void)
{
memstat();
printf("\n");
LargestMemoryBlock m;
m.test();
}
#ifdef FREEARC_WIN
#include <windows.h>
#include <stdio.h>
#include <conio.h>
#include <time.h>
// Provide VirtualAlloc operations for testing
void LargestMemoryBlock::alloc(size_t n) {p = VirtualAlloc (0, size=n, MEM_RESERVE, PAGE_READWRITE);};
void LargestMemoryBlock::free () {VirtualFree (p, 0, MEM_RELEASE); p=NULL;};
// Use to convert bytes to MB
#define DIV (1024*1024)
// Specify the width of the field in which to print the numbers.
// The asterisk in the format specifier "%*I64d" takes an integer
// argument and uses it to pad and right justify the number.
#define WIDTH 4
void memstat (void)
{
MEMORYSTATUSEX statex;
statex.dwLength = sizeof (statex);
GlobalMemoryStatusEx (&statex);
printf ("There is %*ld percent of memory in use.\n",
WIDTH, statex.dwMemoryLoad);
printf ("There are %*I64d total Mbytes of physical memory.\n",
WIDTH, statex.ullTotalPhys/DIV);
printf ("There are %*I64d free Mbytes of physical memory.\n",
WIDTH, statex.ullAvailPhys/DIV);
printf ("There are %*I64d total Mbytes of paging file.\n",
WIDTH, statex.ullTotalPageFile/DIV);
printf ("There are %*I64d free Mbytes of paging file.\n",
WIDTH, statex.ullAvailPageFile/DIV);
printf ("There are %*I64d total Mbytes of virtual memory.\n",
WIDTH, statex.ullTotalVirtual/DIV);
printf ("There are %*I64d free Mbytes of virtual memory.\n",
WIDTH, statex.ullAvailVirtual/DIV);
// Show the amount of extended memory available.
printf ("There are %*I64d free Mbytes of extended memory.\n",
WIDTH, statex.ullAvailExtendedVirtual/DIV);
}
#else
// Provide malloc operations for testing
void LargestMemoryBlock::alloc(size_t n) {p=malloc(size=n);};
void LargestMemoryBlock::free () {::free(p); p=NULL;};
void memstat (void)
{
}
#endif
#ifdef FREEARC_WIN
/*
void SetDateTimeAttr(const char* Filename, time_t t)
{
struct tm* t2 = gmtime(&t);
SYSTEMTIME t3;
t3.wYear = t2->tm_year+1900;
t3.wMonth = t2->tm_mon+1;
t3.wDay = t2->tm_mday;
t3.wHour = t2->tm_hour;
t3.wMinute = t2->tm_min;
t3.wSecond = t2->tm_sec;
t3.wMilliseconds = 0;
FILETIME ft;
SystemTimeToFileTime(&t3, &ft);
HANDLE hndl=CreateFile(Filename,GENERIC_WRITE,0,NULL,OPEN_EXISTING,0,0);
SetFileTime(hndl,NULL,NULL,&ft); //creation, last access, modification times
CloseHandle(hndl);
//SetFileAttributes(Filename,ai.attrib);
}
*/
CFILENAME GetExeName (CFILENAME buf, int bufsize)
{
GetModuleFileNameA (NULL, buf, bufsize);
return buf;
}
unsigned GetPhysicalMemory (void)
{
MEMORYSTATUS buf;
GlobalMemoryStatus (&buf);
return buf.dwTotalPhys;
}
unsigned GetMaxMemToAlloc (void)
{
LargestMemoryBlock block;
return block.size - 5*mb;
}
unsigned GetAvailablePhysicalMemory (void)
{
MEMORYSTATUS buf;
GlobalMemoryStatus (&buf);
return buf.dwAvailPhys;
}
int GetProcessorsCount (void)
{
SYSTEM_INFO si;
GetSystemInfo (&si);
return si.dwNumberOfProcessors;
}
void SetFileDateTime (const CFILENAME Filename, time_t mtime)
{
struct _stat st;
_stat (Filename, &st);
struct _utimbuf times;
times.actime = st.st_atime;
times.modtime = mtime;
_utime (Filename, ×);
}
// Execute program `filename` in the directory `curdir` optionally waiting until it finished
void RunProgram (const CFILENAME filename, const CFILENAME curdir, int wait_finish)
{
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory (&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory (&pi, sizeof(pi));
BOOL process_created = CreateProcessA (filename, NULL, NULL, NULL, FALSE, 0, NULL, curdir, &si, &pi);
if (process_created && wait_finish)
WaitForSingleObject (pi.hProcess, INFINITE);
}
// Execute file `filename` in the directory `curdir` optionally waiting until it finished
void RunFile (const CFILENAME filename, const CFILENAME curdir, int wait_finish)
{
SHELLEXECUTEINFO sei;
ZeroMemory(&sei, sizeof(SHELLEXECUTEINFO));
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = (wait_finish? SEE_MASK_NOCLOSEPROCESS : 0);
sei.hwnd = GetActiveWindow();
sei.lpFile = filename;
sei.lpDirectory = curdir;
sei.nShow = SW_SHOW;
DWORD rc = ShellExecuteEx(&sei);
if (rc && wait_finish)
WaitForSingleObject(sei.hProcess, INFINITE);
}
#else // For Unix:
#include <unistd.h>
#include <sys/sysinfo.h>
unsigned GetPhysicalMemory (void)
{
struct sysinfo si;
sysinfo(&si);
return si.totalram*si.mem_unit;
}
unsigned GetMaxMemToAlloc (void)
{
//struct sysinfo si;
// sysinfo(&si);
return UINT_MAX;
}
unsigned GetAvailablePhysicalMemory (void)
{
struct sysinfo si;
sysinfo(&si);
return si.freeram*si.mem_unit;
}
int GetProcessorsCount (void)
{
return get_nprocs();
}
void SetFileDateTime(const CFILENAME Filename, time_t mtime)
{
#undef stat
struct stat st;
stat (Filename, &st);
struct utimbuf times;
times.actime = st.st_atime;
times.modtime = mtime;
utime (Filename, ×);
}
// Execute file `filename` in the directory `curdir` optionally waiting until it finished
void RunFile (const CFILENAME filename, const CFILENAME curdir, int wait_finish)
{
char *olddir = (char*) malloc(MY_FILENAME_MAX*4),
*cmd = (char*) malloc(strlen(filename)+10);
getcwd(olddir, MY_FILENAME_MAX*4);
chdir(curdir);
sprintf(cmd, "./%s%s", filename, wait_finish? "" : " &");
system(cmd);
chdir(olddir);
free(cmd);
free(olddir);
}
#endif // Windows/Unix
void FormatDateTime (char *buf, int bufsize, time_t t)
{
struct tm *p;
if (t==-1) t=0; // Иначе получим вылет :(
p = localtime(&t);
strftime( buf, bufsize, "%Y-%m-%d %H:%M:%S", p);
}
// Максимальная длина имени файла
int long_path_size (void)
{
return MY_FILENAME_MAX;
}
/************************************************************************
************* CRC-32 subroutines ***************************************
************************************************************************/
uint CRCTab[256];
static uint CRCTab8[8][256];
static int crc_slice8_initialized = 0;
void InitCRC()
{
for (int I=0;I<256;I++)
{
uint C=I;
for (int J=0;J<8;J++)
C=(C & 1) ? (C>>1)^0xEDB88320L : (C>>1);
CRCTab[I]=C;
}
}
// Build the 8 tables used by the slice-by-8 inner loop. Each CRCTab8[k][i]
// is the CRC of the one-byte value i followed by k zero bytes.
static void InitCRCSlice8()
{
if (CRCTab[1]==0) InitCRC();
for (int i=0; i<256; i++) CRCTab8[0][i] = CRCTab[i];
for (int i=0; i<256; i++) {
uint c = CRCTab8[0][i];
for (int k=1; k<8; k++) {
c = CRCTab8[0][c & 0xff] ^ (c >> 8);
CRCTab8[k][i] = c;
}
}
crc_slice8_initialized = 1;
}
// Slice-by-8 CRC-32 (polynomial 0xEDB88320, zlib/gzip compatible).
// Processes 8 input bytes per iteration with 8 parallel table lookups,
// replacing the previous sequential byte-at-a-time inner loop. ~3-5x faster
// on large buffers; binary-identical output.
uint UpdateCRC( void *Addr, uint Size, uint StartCRC)
{
if (!crc_slice8_initialized)
InitCRCSlice8();
uint8 *Data = (uint8 *)Addr;
uint crc = StartCRC;
#if defined(FREEARC_INTEL_BYTE_ORDER)
while (Size >= 8) {
uint32_t lo = crc ^ *(uint32_t *)Data;
uint32_t hi = *(uint32_t *)(Data + 4);
crc = CRCTab8[7][ lo & 0xff]
^ CRCTab8[6][(lo >> 8) & 0xff]
^ CRCTab8[5][(lo >> 16) & 0xff]
^ CRCTab8[4][ lo >> 24]
^ CRCTab8[3][ hi & 0xff]
^ CRCTab8[2][(hi >> 8) & 0xff]
^ CRCTab8[1][(hi >> 16) & 0xff]
^ CRCTab8[0][ hi >> 24];
Data += 8;
Size -= 8;
}
#endif
for (uint I=0; I<Size; I++)
crc = CRCTab[(uint8)(crc ^ Data[I])] ^ (crc >> 8);
return crc;
}
// Вычислить CRC блока данных
uint CalcCRC( void *Addr, uint Size)
{
return UpdateCRC (Addr, Size, INIT_CRC) ^ INIT_CRC;
}
// От-xor-ить два блока данных
void memxor (char *dest, char *src, uint size)
{
if (size) do
*dest++ ^= *src++;
while (--size);
}
// Вернуть имя файла без имени каталога
FILENAME arc_basename (FILENAME fullname)
{
char *p = fullname;
for (char* q=fullname; *q; q++)
if (in_set (*q, ALL_PATH_DELIMITERS))
p = q+1;
return p;
}
// Создать каталоги на пути к name
void BuildPathTo (CFILENAME name)
{
CFILENAME path_ptr = NULL;
for (CFILENAME p = _tcschr(name,0); --p >= name;)
if (_tcschr (_T(DIRECTORY_DELIMITERS), *p))
{path_ptr=p; break;}
if (path_ptr==NULL) return;
TCHAR oldc = *path_ptr;
*path_ptr = 0;
if (! file_exists (name))
{
BuildPathTo (name);
create_dir (name);
}
*path_ptr = oldc;
}
/* ***************************************************************************
* *
* Random system values collection routine from CryptLib by Peter Gutmann *
* [ftp://ftp.franken.de/pub/crypt/cryptlib/cl331.zip] *
* *
*****************************************************************************/
/* The size of the intermediate buffer used to accumulate polled data */
#define RANDOM_BUFSIZE 4096
// Handling random data buffer
#define initRandomData(rand_buf, rand_size) \
char *rand_ptr=(rand_buf), *rand_end=(rand_buf)+(rand_size)
#define addRandomData(ptr,size) (memcpy (rand_ptr, (ptr), mymin((size),rand_end-rand_ptr)), rand_ptr+=mymin((size),rand_end-rand_ptr))
#define addRandomLong(value) {long n=(value); addRandomData(&n, sizeof(long));}
#define addRandomValue(value) addRandomLong((long) value)
/* Map a value that may be 32 or 64 bits depending on the platform to a long */
#if defined( _WIN64 ) || ( defined( _MSC_VER ) && ( _MSC_VER >= 1400 ) )
#define addRandomHandle( handle ) \
addRandomLong( PtrToUlong( handle ) )
#else
#define addRandomHandle addRandomValue
#endif /* 32- vs. 64-bit VC++ */
// This routine fills buffer with system-generated pseudo-random data
// and returns number of bytes filled
int systemRandomData (char *rand_buf, int rand_size)
{
#ifdef FREEARC_WIN
FILETIME creationTime, exitTime, kernelTime, userTime;
SIZE_T minimumWorkingSetSize, maximumWorkingSetSize;
LARGE_INTEGER performanceCount;
MEMORYSTATUS memoryStatus;
HANDLE handle;
POINT point;
initRandomData (rand_buf, rand_size);
/* Get various basic pieces of system information: Handle of active
window, handle of window with mouse capture, handle of clipboard owner
handle of start of clpboard viewer list, pseudohandle of current
process, current process ID, pseudohandle of current thread, current
thread ID, handle of desktop window, handle of window with keyboard
focus, whether system queue has any events, cursor position for last
message, 1 ms time for last message, handle of window with clipboard
open, handle of process heap, handle of procs window station, types of
events in input queue, and milliseconds since Windows was started.
Since a HWND/HANDLE can be a 64-bit value on a 64-bit platform, we
have to use a mapping macro that discards the high 32 bits (which
presumably won't be of much interest anyway) */
addRandomHandle( GetActiveWindow() );
addRandomHandle( GetCapture() );
addRandomHandle( GetClipboardOwner() );
addRandomHandle( GetClipboardViewer() );
addRandomHandle( GetCurrentProcess() );
addRandomValue( GetCurrentProcessId() );
addRandomHandle( GetCurrentThread() );
addRandomValue( GetCurrentThreadId() );
addRandomHandle( GetDesktopWindow() );
addRandomHandle( GetFocus() );
addRandomValue( GetInputState() );
addRandomValue( GetMessagePos() );
addRandomValue( GetMessageTime() );
addRandomHandle( GetOpenClipboardWindow() );
addRandomHandle( GetProcessHeap() );
addRandomHandle( GetProcessWindowStation() );
addRandomValue( GetTickCount() );
/* Get multiword system information: Current caret position, current
mouse cursor position */
GetCaretPos( &point );
addRandomData( &point, sizeof( POINT ) );
GetCursorPos( &point );
addRandomData( &point, sizeof( POINT ) );
/* Get percent of memory in use, bytes of physical memory, bytes of free
physical memory, bytes in paging file, free bytes in paging file, user
bytes of address space, and free user bytes */
memoryStatus.dwLength = sizeof( MEMORYSTATUS );
GlobalMemoryStatus( &memoryStatus );
addRandomData( &memoryStatus, sizeof( MEMORYSTATUS ) );
/* Get thread and process creation time, exit time, time in kernel mode,
and time in user mode in 100ns intervals */
handle = GetCurrentThread();
GetThreadTimes( handle, &creationTime, &exitTime, &kernelTime, &userTime );
addRandomData( &creationTime, sizeof( FILETIME ) );
addRandomData( &exitTime, sizeof( FILETIME ) );
addRandomData( &kernelTime, sizeof( FILETIME ) );
addRandomData( &userTime, sizeof( FILETIME ) );
handle = GetCurrentProcess();
GetProcessTimes( handle, &creationTime, &exitTime, &kernelTime, &userTime );
addRandomData( &creationTime, sizeof( FILETIME ) );
addRandomData( &exitTime, sizeof( FILETIME ) );
addRandomData( &kernelTime, sizeof( FILETIME ) );
addRandomData( &userTime, sizeof( FILETIME ) );
/* Get the minimum and maximum working set size for the current process */
GetProcessWorkingSetSize( handle, &minimumWorkingSetSize, &maximumWorkingSetSize );
addRandomValue( minimumWorkingSetSize );
addRandomValue( maximumWorkingSetSize );
/* The following are fixed for the lifetime of the process */
/* Get name of desktop, console window title, new window position and
size, window flags, and handles for stdin, stdout, and stderr */
STARTUPINFO startupInfo;
startupInfo.cb = sizeof( STARTUPINFO );
GetStartupInfo( &startupInfo );
addRandomData( &startupInfo, sizeof( STARTUPINFO ) );
/* The performance of QPC varies depending on the architecture it's
running on and on the OS, the MS documentation is vague about the
details because it varies so much. Under Win9x/ME it reads the
1.193180 MHz PIC timer. Under NT/Win2K/XP it may or may not read the
64-bit TSC depending on the HAL and assorted other circumstances,
generally on machines with a uniprocessor HAL
KeQueryPerformanceCounter() uses a 3.579545MHz timer and on machines
with a multiprocessor or APIC HAL it uses the TSC (the exact time
source is controlled by the HalpUse8254 flag in the kernel). That
choice of time sources is somewhat peculiar because on a
multiprocessor machine it's theoretically possible to get completely
different TSC readings depending on which CPU you're currently
running on, while for uniprocessor machines it's not a problem.
However, the kernel appears to synchronise the TSCs across CPUs at
boot time (it resets the TSC as part of its system init), so this
shouldn't really be a problem. Under WinCE it's completely platform-
dependant, if there's no hardware performance counter available, it
uses the 1ms system timer.
Another feature of the TSC (although it doesn't really affect us here)
is that mobile CPUs will turn off the TSC when they idle, Pentiums
will change the rate of the counter when they clock-throttle (to
match the current CPU speed), and hyperthreading Pentiums will turn
it off when both threads are idle (this more or less makes sense,
since the CPU will be in the halted state and not executing any
instructions to count).
To make things unambiguous, we detect a CPU new enough to call RDTSC
directly by checking for CPUID capabilities, and fall back to QPC if
this isn't present */
if( QueryPerformanceCounter( &performanceCount ) )
addRandomData( &performanceCount,
sizeof( LARGE_INTEGER ) );
else
/* Millisecond accuracy at best... */
addRandomValue( GetTickCount() );
return rand_ptr-rand_buf;
#else // For Unix:
FILE *f = fopen ("/dev/urandom", "rb");
if (f == NULL)
{
perror ("Cannot open /dev/urandom");
return 0;
}
if (file_read (f, rand_buf, rand_size) != rand_size)
{
perror ("Read from /dev/urandom failed");
fclose (f);
return 0;
}
fclose (f);
return rand_size;
#endif // Windows/Unix
}
/****************************************************************************
*
* Random system values collection *
*
****************************************************************************/
/****************************************************************************
* SIGINT helpers for the System.Posix.Signals MicroHs shim *
* darc_install_sigint / darc_check_sigint / darc_clear_sigint *
****************************************************************************/
#ifndef FREEARC_WIN
#include <signal.h>
#include <stdint.h>
static volatile int darc_sigint_fired = 0;
static void darc_sigint_handler(int) {
darc_sigint_fired = 1;
/* Reinstall so the next Ctrl-C also fires (mirrors CatchOnce behaviour
managed from the Haskell side). */
signal(SIGINT, darc_sigint_handler);
}
extern "C" void darc_install_sigint(void) {
signal(SIGINT, darc_sigint_handler);
}
extern "C" int darc_check_sigint(void) {
return darc_sigint_fired;
}
extern "C" void darc_clear_sigint(void) {
darc_sigint_fired = 0;
}
#else /* FREEARC_WIN: stub sigint handlers on Windows */
extern "C" void darc_install_sigint(void) {}
extern "C" int darc_check_sigint(void) { return 0; }
extern "C" void darc_clear_sigint(void) {}
#endif // !FREEARC_WIN
/****************************************************************************
* MicroHs compat helpers: stat accessors and processor count *
****************************************************************************/
#ifndef FREEARC_WIN
#include <sys/stat.h>
#include <unistd.h>
extern "C" int darc_sizeof_stat(void) {
return (int)sizeof(struct stat);
}
extern "C" unsigned int darc_st_mode(struct stat *p) {
return (unsigned int)p->st_mode;
}
// realpath wrapper: returns 0 on success, -1 on failure
extern "C" int darc_realpath(const char *path, char *out) {
char *r = realpath(path, out);
return r ? 0 : -1;
}
extern "C" int darc_utimes(const char *path, long atime, long mtime) {
struct utimbuf ut;
ut.actime = (time_t)atime;
ut.modtime = (time_t)mtime;
return utime(path, &ut);
}
extern "C" long darc_st_size(struct stat *p) {
return (long)p->st_size;
}
extern "C" long darc_st_mtime(struct stat *p) {
return (long)p->st_mtime;
}
/* MicroHs workaround: FFI return values are truncated to 32 bits.
These _w variants write 64-bit results via pointer instead. */
extern "C" void darc_st_size_w(struct stat *p, long *out) {
*out = (long)p->st_size;
}
extern "C" void darc_st_mtime_w(struct stat *p, long *out) {
*out = (long)p->st_mtime;
}
#endif // !FREEARC_WIN (stat/realpath/utime POSIX block)
/****************************************************************************
* Windows compat helpers for POSIX APIs used by the portable blocks below *
****************************************************************************/
#ifdef FREEARC_WIN
#include <windows.h>
#include <io.h> /* _chsize_s, _fullpath */
#include <sys/stat.h>
#include <sys/utime.h>
#include <time.h>
#include <wincrypt.h>
#ifndef ftruncate
static inline int ftruncate(int fd, long long size) {
return _chsize_s(fd, (__int64)size);
}
#endif
static inline int darc_win_nprocs(void) {
SYSTEM_INFO si; GetSystemInfo(&si);
return si.dwNumberOfProcessors > 0 ? (int)si.dwNumberOfProcessors : 1;
}
/* localtime_r / gmtime_r fallbacks for Windows (MSVC uses localtime_s; MinGW-w64
has localtime_s too but not the POSIX _r variants in default headers). */
static inline struct tm* darc_localtime_r_win(const time_t *t, struct tm *out) {
return localtime_s(out, t) == 0 ? out : NULL;
}
static inline struct tm* darc_gmtime_r_win(const time_t *t, struct tm *out) {
return gmtime_s(out, t) == 0 ? out : NULL;
}
#define localtime_r darc_localtime_r_win
#define gmtime_r darc_gmtime_r_win
extern "C" int darc_sizeof_stat(void) { return (int)sizeof(struct stat); }
extern "C" unsigned int darc_st_mode(struct stat *p) { return (unsigned int)p->st_mode; }
extern "C" int darc_realpath(const char *path, char *out) {
return _fullpath(out, path, MAX_PATH) ? 0 : -1;
}
extern "C" int darc_utimes(const char *path, long atime, long mtime) {
struct _utimbuf ut; ut.actime = (time_t)atime; ut.modtime = (time_t)mtime;
return _utime(path, &ut);
}
extern "C" long darc_st_size(struct stat *p) { return (long)p->st_size; }
extern "C" long darc_st_mtime(struct stat *p) { return (long)p->st_mtime; }
extern "C" void darc_st_size_w(struct stat *p, long *out) { *out = (long)p->st_size; }
extern "C" void darc_st_mtime_w(struct stat *p, long *out) { *out = (long)p->st_mtime; }
#endif // FREEARC_WIN
/****************************************************************************
* Handle IO helpers for MicroHs (hSeek, hTell, hFileSize, hSetFileSize) *
* BFILE_file layout: BFILE (7 fn ptrs = 56 bytes) + FILE* at offset 56 *
****************************************************************************/
#include <stdio.h>
static FILE* bfile_to_file(void *bf) {
/* The FILE* is at offset 56 (sizeof(BFILE) = 7 * sizeof(void*)) */
return *(FILE**)((char*)bf + 7 * sizeof(void*));
}
extern "C" int darc_bfile_seek(void *bf, long offset, int whence) {
FILE *f = bfile_to_file(bf);
if (!f) return -1;
return fseek(f, offset, whence);
}
extern "C" long darc_bfile_tell(void *bf) {
FILE *f = bfile_to_file(bf);
if (!f) return -1;
return ftell(f);
}
extern "C" long darc_bfile_size(void *bf) {
FILE *f = bfile_to_file(bf);
if (!f) return -1;
long pos = ftell(f);
fseek(f, 0, SEEK_END);
long size = ftell(f);
fseek(f, pos, SEEK_SET);
return size;
}
/* MicroHs workaround: write 64-bit results via pointer. */
extern "C" void darc_bfile_tell_w(void *bf, long *out) {
FILE *f = bfile_to_file(bf);
*out = f ? ftell(f) : -1;
}
extern "C" void darc_bfile_size_w(void *bf, long *out) {
FILE *f = bfile_to_file(bf);
if (!f) { *out = -1; return; }
long pos = ftell(f);
fseek(f, 0, SEEK_END);
*out = ftell(f);
fseek(f, pos, SEEK_SET);
}
extern "C" void darc_bfile_read_w(void *bf, void *buf, long size, long *out) {
FILE *f = bfile_to_file(bf);
*out = f ? (long)fread(buf, 1, (size_t)size, f) : -1;
}
extern "C" void darc_bfile_write_w(void *bf, const void *buf, long size, long *out) {
FILE *f = bfile_to_file(bf);
*out = f ? (long)fwrite(buf, 1, (size_t)size, f) : -1;
}
extern "C" int darc_bfile_truncate(void *bf, long size) {
FILE *f = bfile_to_file(bf);
if (!f) return -1;
fflush(f);
int fd = fileno(f);
return ftruncate(fd, (off_t)size);
}
extern "C" long darc_bfile_read(void *bf, void *buf, long size) {
FILE *f = bfile_to_file(bf);
if (!f) return -1;
return (long)fread(buf, 1, (size_t)size, f);
}
extern "C" long darc_bfile_write(void *bf, const void *buf, long size) {
FILE *f = bfile_to_file(bf);
if (!f) return -1;
return (long)fwrite(buf, 1, (size_t)size, f);
}
extern "C" int darc_get_nprocs(void) {
#ifdef FREEARC_WIN
return darc_win_nprocs();
#else
long n = sysconf(_SC_NPROCESSORS_ONLN);
return (n > 0) ? (int)n : 1;
#endif
}
/* Random bytes: /dev/urandom on POSIX, CryptGenRandom on Windows. */
extern "C" long darc_urandom_read(void *buf, long size) {
#ifdef FREEARC_WIN
HCRYPTPROV h;
if (!CryptAcquireContextA(&h, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) return -1;
BOOL ok = CryptGenRandom(h, (DWORD)size, (BYTE*)buf);
CryptReleaseContext(h, 0);
return ok ? size : -1;
#else
FILE *f = fopen("/dev/urandom", "rb");
if (!f) return -1;
long n = (long)fread(buf, 1, (size_t)size, f);
fclose(f);
return n;
#endif
}
extern "C" void darc_urandom_read_w(void *buf, long size, long *out) {
*out = darc_urandom_read(buf, size);
}
/* FreeArc 0.67 --shutdown / -ioff: power off the machine. */
extern "C" void PowerOffComputer(void) {
#ifdef FREEARC_WIN
HANDLE hToken;
TOKEN_PRIVILEGES tkp;
if (!OpenProcessToken(GetCurrentProcess(),
TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken)) return;
LookupPrivilegeValue(NULL, SE_SHUTDOWN_NAME, &tkp.Privileges[0].Luid);
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
AdjustTokenPrivileges(hToken, FALSE, &tkp, 0, (PTOKEN_PRIVILEGES)NULL, 0);
ExitWindowsEx(EWX_POWEROFF | EWX_FORCE, 0);
#else
int r = system("shutdown -h now");
(void)r;
#endif
}
/****************************************************************************
* System.Time helpers for the MicroHs shim *
* Uses a flat int[10] layout: sec,min,hour,mday,mon,year,wday,yday,isdst,gmtoff_min
****************************************************************************/
#include <time.h>
static void tm_to_flat(struct tm *t, int *out) {
out[0] = t->tm_sec;
out[1] = t->tm_min;
out[2] = t->tm_hour;
out[3] = t->tm_mday;
out[4] = t->tm_mon;
out[5] = t->tm_year;
out[6] = t->tm_wday;
out[7] = t->tm_yday;
out[8] = t->tm_isdst;
#ifdef __linux__
out[9] = (int)(t->tm_gmtoff / 60);
#else
out[9] = 0;
#endif
}
static void flat_to_tm(int *in, struct tm *t) {
t->tm_sec = in[0];
t->tm_min = in[1];
t->tm_hour = in[2];
t->tm_mday = in[3];
t->tm_mon = in[4];
t->tm_year = in[5];
t->tm_wday = in[6];
t->tm_yday = in[7];
t->tm_isdst = in[8];
}
extern "C" long darc_time(void) {
return (long)time(NULL);
}
extern "C" void darc_time_w(long *out) {
*out = (long)time(NULL);
}
extern "C" void darc_localtime(long secs, int *out) {
time_t t = (time_t)secs;
struct tm buf;
struct tm *r = localtime_r(&t, &buf);
if (r) tm_to_flat(r, out);
}
extern "C" void darc_gmtime(long secs, int *out) {
time_t t = (time_t)secs;
struct tm buf;
struct tm *r = gmtime_r(&t, &buf);
if (r) tm_to_flat(r, out);
}
extern "C" long darc_mktime_tz(int year, int mon, int mday, int hour, int min, int sec, int gmtoff_min) {
struct tm t = {};
t.tm_year = year;
t.tm_mon = mon;
t.tm_mday = mday;
t.tm_hour = hour;
t.tm_min = min;
t.tm_sec = sec;
t.tm_isdst = -1;
/* Adjust for timezone offset */
time_t r = mktime(&t);
r -= (time_t)(gmtoff_min * 60);
/* Add local UTC offset back */
struct tm local_check;
localtime_r(&r, &local_check);
#ifdef __linux__
r += local_check.tm_gmtoff;
#endif
return (long)r;
}
extern "C" void darc_mktime_tz_w(int year, int mon, int mday, int hour, int min, int sec, int gmtoff_min, long *out) {
*out = darc_mktime_tz(year, mon, mday, hour, min, sec, gmtoff_min);
}
extern "C" void darc_fill_tm(int *out, int sec, int min_, int hour, int mday, int mon,
int year, int wday, int yday, int isdst, int gmtoff_min) {
out[0] = sec; out[1] = min_; out[2] = hour; out[3] = mday;
out[4] = mon; out[5] = year; out[6] = wday; out[7] = yday;
out[8] = isdst; out[9] = gmtoff_min;
}
extern "C" int darc_strftime(char *buf, size_t size, const char *fmt, int *flat_tm) {
struct tm t = {};
flat_to_tm(flat_tm, &t);
return (int)strftime(buf, size, fmt, &t);
}
// ============================================================
// MHS C-side compression/decompression pipeline
// Bypasses slow MHS Haskell pipe iteration for large data.
// Data is accumulated in a C-managed growing buffer, then
// compressed/decompressed using streaming Compress/Decompress.
// ============================================================
#ifdef __cplusplus
extern "C" {
#endif
// Growing buffer for pipeline input/output
static char *g_pipeline_buf = NULL;
static long g_pipeline_size = 0;
static long g_pipeline_cap = 0;
void darc_pipeline_init(long initial_cap) {
free(g_pipeline_buf);
if (initial_cap < 65536) initial_cap = 65536;
g_pipeline_buf = (char *)malloc(initial_cap);
g_pipeline_size = 0;
g_pipeline_cap = initial_cap;
}
void darc_pipeline_append(const void *data, long len) {
if (!g_pipeline_buf) return;
if (g_pipeline_size + len > g_pipeline_cap) {
while (g_pipeline_size + len > g_pipeline_cap)
g_pipeline_cap *= 2;
g_pipeline_buf = (char *)realloc(g_pipeline_buf, g_pipeline_cap);
}
memcpy(g_pipeline_buf + g_pipeline_size, data, len);
g_pipeline_size += len;
}
void darc_pipeline_get_buf_w(void **out_buf, long *out_size) {
*out_buf = g_pipeline_buf;
*out_size = g_pipeline_size;
g_pipeline_buf = NULL;
g_pipeline_size = 0;
g_pipeline_cap = 0;
}
void darc_pipeline_free(void) {
free(g_pipeline_buf);
g_pipeline_buf = NULL;
g_pipeline_size = 0;
g_pipeline_cap = 0;
}
// Streaming callback that reads from/writes to memory buffers.