forked from IlyaGrebnov/libbsc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bsc.cpp
885 lines (751 loc) · 30.9 KB
/
bsc.cpp
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
/*-----------------------------------------------------------*/
/* Block Sorting, Lossless Data Compression Library. */
/* Block Sorting Compressor */
/*-----------------------------------------------------------*/
/*--
This file is a part of bsc and/or libbsc, a program and a library for
lossless, block-sorting data compression.
Copyright (c) 2009-2021 Ilya Grebnov <[email protected]>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Please see the file LICENSE for full copyright information and file AUTHORS
for full list of contributors.
See also the bsc and libbsc web site:
http://libbsc.com/ for more information.
--*/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <math.h>
#include <memory.h>
#include "libbsc/libbsc.h"
#include "libbsc/filters.h"
#include "libbsc/platform/platform.h"
#pragma pack(push, 1)
#define LIBBSC_CONTEXTS_AUTODETECT 3
unsigned char bscFileSign[4] = {'b', 's', 'c', 0x31};
typedef struct BSC_BLOCK_HEADER
{
long long blockOffset;
signed char recordSize;
signed char sortingContexts;
} BSC_BLOCK_HEADER;
#pragma pack(pop)
int paramBlockSize = 25 * 1024 * 1024;
int paramBlockSorter = LIBBSC_BLOCKSORTER_BWT;
int paramCoder = LIBBSC_CODER_QLFC_STATIC;
int paramSortingContexts = LIBBSC_CONTEXTS_FOLLOWING;
int paramEnableParallelProcessing = 1;
int paramEnableMultiThreading = 1;
int paramEnableFastMode = 1;
int paramEnableLargePages = 0;
int paramEnableCUDA = 0;
int paramEnableSegmentation = 0;
int paramEnableReordering = 0;
int paramEnableLZP = 1;
int paramLZPHashSize = 15;
int paramLZPMinLen = 128;
int paramFeatures()
{
int features =
(paramEnableFastMode ? LIBBSC_FEATURE_FASTMODE : LIBBSC_FEATURE_NONE) |
(paramEnableMultiThreading ? LIBBSC_FEATURE_MULTITHREADING : LIBBSC_FEATURE_NONE) |
(paramEnableLargePages ? LIBBSC_FEATURE_LARGEPAGES : LIBBSC_FEATURE_NONE) |
(paramEnableCUDA ? LIBBSC_FEATURE_CUDA : LIBBSC_FEATURE_NONE)
;
return features;
}
#if defined(__GNUC__) && (defined(_GLIBCXX_USE_LFS) || defined(__MINGW32__))
#define BSC_FSEEK fseeko64
#define BSC_FTELL ftello64
#define BSC_FILEOFFSET off64_t
#elif defined(_MSC_VER) && _MSC_VER >= 1400
#define BSC_FSEEK _fseeki64
#define BSC_FTELL _ftelli64
#define BSC_FILEOFFSET __int64
#else
#define BSC_FSEEK fseek
#define BSC_FTELL ftell
#define BSC_FILEOFFSET long
#endif
#if defined(WIN32) || defined(_WIN32) || defined(__CYGWIN__) || defined(__MINGW32__) || defined(__BORLANDC__) || defined(_MSC_VER)
#include <windows.h>
double BSC_CLOCK() { return 0.001 * GetTickCount(); }
#elif defined (__unix) || defined (__linux__) || defined (__QNX__) || defined (_AIX) || defined (__NetBSD__) || defined(macintosh) || defined (_MAC)
#include <sys/time.h>
double BSC_CLOCK() { timeval tv; gettimeofday(&tv, 0); return tv.tv_sec + tv.tv_usec * 0.000001; }
#else
double BSC_CLOCK() { return (double)clock() / CLOCKS_PER_SEC; }
#endif
int segmentedBlock[256];
void Compression(char * argv[])
{
if (!paramEnableLZP)
{
paramLZPHashSize = 0;
paramLZPMinLen = 0;
}
FILE * fInput = fopen(argv[2], "rb");
if (fInput == NULL)
{
fprintf(stderr, "Can't open input file: %s!\n", argv[2]);
exit(1);
}
FILE * fOutput = fopen(argv[3], "wb");
if (fOutput == NULL)
{
fprintf(stderr, "Can't create output file: %s!\n", argv[3]);
exit(1);
}
if (BSC_FSEEK(fInput, 0, SEEK_END))
{
fprintf(stderr, "IO error on file: %s!\n", argv[2]);
exit(1);
}
BSC_FILEOFFSET fileSize = BSC_FTELL(fInput);
if (fileSize < 0)
{
fprintf(stderr, "IO error on file: %s!\n", argv[2]);
exit(1);
}
if (BSC_FSEEK(fInput, 0, SEEK_SET))
{
fprintf(stderr, "IO error on file: %s!\n", argv[2]);
exit(1);
}
if (paramBlockSize > fileSize)
{
paramBlockSize = (int)fileSize;
}
if (fwrite(bscFileSign, sizeof(bscFileSign), 1, fOutput) != 1)
{
fprintf(stderr, "IO error on file: %s!\n", argv[3]);
exit(1);
}
int nBlocks = paramBlockSize > 0 ? (int)((fileSize + paramBlockSize - 1) / paramBlockSize) : 0;
if (fwrite(&nBlocks, sizeof(nBlocks), 1, fOutput) != 1)
{
fprintf(stderr, "IO error on file: %s!\n", argv[3]);
exit(1);
}
double startTime = BSC_CLOCK();
#ifdef LIBBSC_OPENMP
int numThreads = 1;
if (paramEnableParallelProcessing)
{
numThreads = omp_get_max_threads();
if (numThreads <= nBlocks) paramEnableMultiThreading = 0;
if (numThreads >= nBlocks) numThreads = nBlocks;
}
#endif
int segmentationStart = 0, segmentationEnd = 0;
#ifdef LIBBSC_OPENMP
#pragma omp parallel num_threads(numThreads) if(numThreads > 1)
#endif
{
unsigned char * buffer = (unsigned char *)bsc_malloc(paramBlockSize + LIBBSC_HEADER_SIZE);
if (buffer == NULL)
{
#ifdef LIBBSC_OPENMP
#pragma omp critical(print)
#endif
{
fprintf(stderr, "Not enough memory! Please check README file for more information.\n");
exit(2);
}
}
while (true)
{
BSC_FILEOFFSET blockOffset = 0;
int dataSize = 0;
#ifdef LIBBSC_OPENMP
#pragma omp critical(input)
#endif
{
if ((feof(fInput) == 0) && (BSC_FTELL(fInput) != fileSize))
{
#ifdef LIBBSC_OPENMP
#pragma omp master
#endif
{
double progress = (100.0 * (double)BSC_FTELL(fInput)) / fileSize;
fprintf(stdout, "\rCompressing %.55s(%02d%%)", argv[2], (int)progress);
fflush(stdout);
}
blockOffset = BSC_FTELL(fInput);
int currentBlockSize = paramBlockSize;
if (paramEnableSegmentation)
{
if (segmentationEnd - segmentationStart > 1) currentBlockSize = segmentedBlock[segmentationStart];
}
dataSize = (int)fread(buffer, 1, currentBlockSize, fInput);
if (dataSize <= 0)
{
fprintf(stderr, "\nIO error on file: %s!\n", argv[2]);
exit(1);
}
if (paramEnableSegmentation)
{
bool bSegmentation = false;
if (segmentationStart == segmentationEnd) bSegmentation = true;
if ((segmentationEnd - segmentationStart == 1) && (dataSize != segmentedBlock[segmentationStart])) bSegmentation = true;
if (bSegmentation)
{
segmentationStart = 0; segmentationEnd = bsc_detect_segments(buffer, dataSize, segmentedBlock, 256, paramFeatures());
if (segmentationEnd <= LIBBSC_NO_ERROR)
{
switch (segmentationEnd)
{
case LIBBSC_NOT_ENOUGH_MEMORY : fprintf(stderr, "\nNot enough memory! Please check README file for more information.\n"); break;
default : fprintf(stderr, "\nInternal program error, please contact the author!\n");
}
exit(2);
}
}
int newDataSize = segmentedBlock[segmentationStart++];
if (dataSize != newDataSize)
{
BSC_FILEOFFSET pos = BSC_FTELL(fInput) - dataSize + newDataSize;
BSC_FSEEK(fInput, pos, SEEK_SET);
dataSize = newDataSize;
}
}
}
}
if (dataSize == 0) break;
signed char recordSize = 1;
if (paramEnableReordering)
{
recordSize = bsc_detect_recordsize(buffer, dataSize, paramFeatures());
if (recordSize < LIBBSC_NO_ERROR)
{
#ifdef LIBBSC_OPENMP
#pragma omp critical(print)
#endif
{
switch (recordSize)
{
case LIBBSC_NOT_ENOUGH_MEMORY : fprintf(stderr, "\nNot enough memory! Please check README file for more information.\n"); break;
default : fprintf(stderr, "\nInternal program error, please contact the author!\n");
}
exit(2);
}
}
if (recordSize > 1)
{
int result = bsc_reorder_forward(buffer, dataSize, recordSize, paramFeatures());
if (result != LIBBSC_NO_ERROR)
{
#ifdef LIBBSC_OPENMP
#pragma omp critical(print)
#endif
{
switch (result)
{
case LIBBSC_NOT_ENOUGH_MEMORY : fprintf(stderr, "\nNot enough memory! Please check README file for more information.\n"); break;
default : fprintf(stderr, "\nInternal program error, please contact the author!\n");
}
exit(2);
}
}
}
}
signed char sortingContexts = paramSortingContexts;
if (paramSortingContexts == LIBBSC_CONTEXTS_AUTODETECT)
{
sortingContexts = bsc_detect_contextsorder(buffer, dataSize, paramFeatures());
if (sortingContexts < LIBBSC_NO_ERROR)
{
#ifdef LIBBSC_OPENMP
#pragma omp critical(print)
#endif
{
switch (sortingContexts)
{
case LIBBSC_NOT_ENOUGH_MEMORY : fprintf(stderr, "\nNot enough memory!\n"); break;
default : fprintf(stderr, "\nInternal program error, please contact the author!\n");
}
exit(2);
}
}
}
if (sortingContexts == LIBBSC_CONTEXTS_PRECEDING)
{
int result = bsc_reverse_block(buffer, dataSize, paramFeatures());
if (result != LIBBSC_NO_ERROR)
{
#ifdef LIBBSC_OPENMP
#pragma omp critical(print)
#endif
{
fprintf(stderr, "\nInternal program error, please contact the author!\n");
exit(2);
}
}
}
int blockSize = bsc_compress(buffer, buffer, dataSize, paramLZPHashSize, paramLZPMinLen, paramBlockSorter, paramCoder, paramFeatures());
if (blockSize == LIBBSC_NOT_COMPRESSIBLE)
{
#ifdef LIBBSC_OPENMP
#pragma omp critical(input)
#endif
{
sortingContexts = LIBBSC_CONTEXTS_FOLLOWING; recordSize = 1;
BSC_FILEOFFSET pos = BSC_FTELL(fInput);
{
BSC_FSEEK(fInput, blockOffset, SEEK_SET);
if (dataSize != (int)fread(buffer, 1, dataSize, fInput))
{
fprintf(stderr, "\nInternal program error, please contact the author!\n");
exit(2);
}
}
BSC_FSEEK(fInput, pos, SEEK_SET);
}
blockSize = bsc_store(buffer, buffer, dataSize, paramFeatures());
}
if (blockSize < LIBBSC_NO_ERROR)
{
#ifdef LIBBSC_OPENMP
#pragma omp critical(print)
#endif
{
switch (blockSize)
{
case LIBBSC_NOT_ENOUGH_MEMORY : fprintf(stderr, "\nNot enough memory! Please check README file for more information.\n"); break;
case LIBBSC_NOT_SUPPORTED : fprintf(stderr, "\nSpecified compression method is not supported on this platform!\n"); break;
case LIBBSC_GPU_ERROR : fprintf(stderr, "\nGeneral GPU failure! Please check README file for more information.\n"); break;
case LIBBSC_GPU_NOT_SUPPORTED : fprintf(stderr, "\nYour GPU is not supported! Please check README file for more information.\n"); break;
case LIBBSC_GPU_NOT_ENOUGH_MEMORY : fprintf(stderr, "\nNot enough GPU memory! Please check README file for more information.\n"); break;
default : fprintf(stderr, "\nInternal program error, please contact the author!\n");
}
exit(2);
}
}
#ifdef LIBBSC_OPENMP
#pragma omp critical(output)
#endif
{
BSC_BLOCK_HEADER header = {blockOffset, recordSize, sortingContexts};
if (fwrite(&header, sizeof(BSC_BLOCK_HEADER), 1, fOutput) != 1)
{
fprintf(stderr, "\nIO error on file: %s!\n", argv[3]);
exit(1);
}
if ((int)fwrite(buffer, 1, blockSize, fOutput) != blockSize)
{
fprintf(stderr, "\nIO error on file: %s!\n", argv[3]);
exit(1);
}
}
}
bsc_free(buffer);
}
fprintf(stdout, "\r%.55s compressed %.0f into %.0f in %.3f seconds.\n", argv[2], (double)fileSize, (double)BSC_FTELL(fOutput), BSC_CLOCK() - startTime);
fclose(fInput); fclose(fOutput);
}
void Decompression(char * argv[])
{
FILE * fInput = fopen(argv[2], "rb");
if (fInput == NULL)
{
fprintf(stderr, "Can't open input file: %s!\n", argv[2]);
exit(1);
}
FILE * fOutput = fopen(argv[3], "wb");
if (fOutput == NULL)
{
fprintf(stderr, "Can't create output file: %s!\n", argv[3]);
exit(1);
}
if (BSC_FSEEK(fInput, 0, SEEK_END))
{
fprintf(stderr, "IO error on file: %s!\n", argv[2]);
exit(1);
}
BSC_FILEOFFSET fileSize = BSC_FTELL(fInput);
if (fileSize < 0)
{
fprintf(stderr, "IO error on file: %s!\n", argv[2]);
exit(1);
}
if (BSC_FSEEK(fInput, 0, SEEK_SET))
{
fprintf(stderr, "IO error on file: %s!\n", argv[2]);
exit(1);
}
unsigned char inputFileSign[sizeof(bscFileSign)];
if (fread(inputFileSign, sizeof(bscFileSign), 1, fInput) != 1)
{
fprintf(stderr, "This is not bsc archive!\n");
exit(1);
}
if (memcmp(inputFileSign, bscFileSign, sizeof(bscFileSign)) != 0)
{
fprintf(stderr, "This is not bsc archive or invalid compression method!\n");
exit(2);
}
int nBlocks = 0;
if (fread(&nBlocks, sizeof(nBlocks), 1, fInput) != 1)
{
fprintf(stderr, "This is not bsc archive!\n");
exit(1);
}
double startTime = BSC_CLOCK();
#ifdef LIBBSC_OPENMP
int numThreads = 1;
if (paramEnableParallelProcessing)
{
numThreads = omp_get_max_threads();
if (numThreads <= nBlocks) paramEnableMultiThreading = 0;
if (numThreads >= nBlocks) numThreads = nBlocks;
}
#pragma omp parallel num_threads(numThreads) if(numThreads > 1)
#endif
{
int bufferSize = -1; unsigned char * buffer = NULL;
while (true)
{
BSC_FILEOFFSET blockOffset = 0;
signed char sortingContexts = 0;
signed char recordSize = 0;
int blockSize = 0;
int dataSize = 0;
#ifdef LIBBSC_OPENMP
#pragma omp critical(input)
#endif
{
if ((feof(fInput) == 0) && (BSC_FTELL(fInput) != fileSize))
{
#ifdef LIBBSC_OPENMP
#pragma omp master
#endif
{
double progress = (100.0 * (double)BSC_FTELL(fInput)) / fileSize;
fprintf(stdout, "\rDecompressing %.55s(%02d%%)", argv[2], (int)progress);
fflush(stdout);
}
BSC_BLOCK_HEADER header = {0, 0, 0};
if (fread(&header, sizeof(BSC_BLOCK_HEADER), 1, fInput) != 1)
{
fprintf(stderr, "\nUnexpected end of file: %s!\n", argv[2]);
exit(1);
}
recordSize = header.recordSize;
if (recordSize < 1)
{
fprintf(stderr, "\nThis is not bsc archive or invalid compression method!\n");
exit(2);
}
sortingContexts = header.sortingContexts;
if ((sortingContexts != LIBBSC_CONTEXTS_FOLLOWING) && (sortingContexts != LIBBSC_CONTEXTS_PRECEDING))
{
fprintf(stderr, "\nThis is not bsc archive or invalid compression method!\n");
exit(2);
}
blockOffset = (BSC_FILEOFFSET)header.blockOffset;
unsigned char bscBlockHeader[LIBBSC_HEADER_SIZE];
if (fread(bscBlockHeader, LIBBSC_HEADER_SIZE, 1, fInput) != 1)
{
fprintf(stderr, "\nUnexpected end of file: %s!\n", argv[2]);
exit(1);
}
if (bsc_block_info(bscBlockHeader, LIBBSC_HEADER_SIZE, &blockSize, &dataSize, paramFeatures()) != LIBBSC_NO_ERROR)
{
fprintf(stderr, "\nThis is not bsc archive or invalid compression method!\n");
exit(2);
}
if ((blockSize > bufferSize) || (dataSize > bufferSize))
{
if (blockSize > bufferSize) bufferSize = blockSize;
if (dataSize > bufferSize) bufferSize = dataSize;
if (buffer != NULL) { bsc_free(buffer); } buffer = (unsigned char *)bsc_malloc(bufferSize);
}
if (buffer == NULL)
{
fprintf(stderr, "\nNot enough memory! Please check README file for more information.\n");
exit(2);
}
memcpy(buffer, bscBlockHeader, LIBBSC_HEADER_SIZE);
if (fread(buffer + LIBBSC_HEADER_SIZE, blockSize - LIBBSC_HEADER_SIZE, 1, fInput) != 1)
{
fprintf(stderr, "\nUnexpected end of file: %s!\n", argv[2]);
exit(1);
}
}
}
if (dataSize == 0) break;
int result = bsc_decompress(buffer, blockSize, buffer, dataSize, paramFeatures());
if (result < LIBBSC_NO_ERROR)
{
#ifdef LIBBSC_OPENMP
#pragma omp critical(print)
#endif
{
switch (result)
{
case LIBBSC_DATA_CORRUPT : fprintf(stderr, "\nThe compressed data is corrupted!\n"); break;
case LIBBSC_NOT_ENOUGH_MEMORY : fprintf(stderr, "\nNot enough memory! Please check README file for more information.\n"); break;
case LIBBSC_GPU_ERROR : fprintf(stderr, "\nGeneral GPU failure! Please check README file for more information.\n"); break;
case LIBBSC_GPU_NOT_SUPPORTED : fprintf(stderr, "\nYour GPU is not supported! Please check README file for more information.\n"); break;
case LIBBSC_GPU_NOT_ENOUGH_MEMORY : fprintf(stderr, "\nNot enough GPU memory! Please check README file for more information.\n"); break;
default : fprintf(stderr, "\nInternal program error, please contact the author!\n");
}
exit(2);
}
}
if (sortingContexts == LIBBSC_CONTEXTS_PRECEDING)
{
result = bsc_reverse_block(buffer, dataSize, paramFeatures());
if (result != LIBBSC_NO_ERROR)
{
#ifdef LIBBSC_OPENMP
#pragma omp critical(print)
#endif
{
fprintf(stderr, "\nInternal program error, please contact the author!\n");
exit(2);
}
}
}
if (recordSize > 1)
{
result = bsc_reorder_reverse(buffer, dataSize, recordSize, paramFeatures());
if (result != LIBBSC_NO_ERROR)
{
#ifdef LIBBSC_OPENMP
#pragma omp critical(print)
#endif
{
switch (result)
{
case LIBBSC_NOT_ENOUGH_MEMORY : fprintf(stderr, "\nNot enough memory! Please check README file for more information.\n"); break;
default : fprintf(stderr, "\nInternal program error, please contact the author!\n");
}
exit(2);
}
}
}
#ifdef LIBBSC_OPENMP
#pragma omp critical(output)
#endif
{
if (BSC_FSEEK(fOutput, blockOffset, SEEK_SET))
{
fprintf(stderr, "\nIO error on file: %s!\n", argv[3]);
exit(1);
}
if ((int)fwrite(buffer, 1, dataSize, fOutput) != dataSize)
{
fprintf(stderr, "\nIO error on file: %s!\n", argv[3]);
exit(1);
}
}
}
if (buffer != NULL) bsc_free(buffer);
}
if (BSC_FSEEK(fOutput, 0, SEEK_END))
{
fprintf(stderr, "IO error on file: %s!\n", argv[3]);
exit(1);
}
fprintf(stdout, "\r%.55s decompressed %.0f into %.0f in %.3f seconds.\n", argv[2], (double)fileSize, (double)BSC_FTELL(fOutput), BSC_CLOCK() - startTime);
fclose(fInput); fclose(fOutput);
}
void ShowUsage(void)
{
#if !defined(BSC_DECOMPRESSION_ONLY)
fprintf(stdout, "Usage: bsc <e|d> inputfile outputfile <options>\n\n");
#elif defined(LIBBSC_CUDA_SUPPORT) || defined(_WIN32) || defined(LIBBSC_OPENMP)
fprintf(stdout, "Usage: bsc d inputfile outputfile <options>\n\n");
#else
fprintf(stdout, "Usage: bsc d inputfile outputfile\n\n");
#endif
#if !defined(BSC_DECOMPRESSION_ONLY)
fprintf(stdout, "Block sorting options:\n");
fprintf(stdout, " -b<size> Block size in megabytes, default: -b25\n");
fprintf(stdout, " minimum: -b1, maximum: -b2047\n");
fprintf(stdout, " -m<algo> Block sorting algorithm, default: -m0\n");
fprintf(stdout, " -m0 Burrows Wheeler Transform (default)\n");
#ifdef LIBBSC_SORT_TRANSFORM_SUPPORT
fprintf(stdout, " -m3..8 Sort Transform of order n\n");
#endif
fprintf(stdout, " -c<ctx> Contexts for sorting, default: -cf\n");
fprintf(stdout, " -cf Following contexts (default)\n");
fprintf(stdout, " -cp Preceding contexts\n");
fprintf(stdout, " -ca Autodetect (experimental)\n");
fprintf(stdout, " -e<algo> Entropy encoding algorithm, default: -e1\n");
fprintf(stdout, " -e0 Fast Quantized Local Frequency Coding\n");
fprintf(stdout, " -e1 Static Quantized Local Frequency Coding (default)\n");
fprintf(stdout, " -e2 Adaptive Quantized Local Frequency Coding (best compression)\n");
fprintf(stdout, "\nPreprocessing options:\n");
fprintf(stdout, " -p Disable all preprocessing techniques\n");
fprintf(stdout, " -s Enable segmentation (adaptive block size), default: disable\n");
fprintf(stdout, " -r Enable structured data reordering, default: disable\n");
fprintf(stdout, " -l Enable Lempel-Ziv preprocessing, default: enable\n");
fprintf(stdout, " -H<size> LZP dictionary size in bits, default: -H15\n");
fprintf(stdout, " minimum: -H10, maximum: -H28\n");
fprintf(stdout, " -M<size> LZP minimum match length, default: -M128\n");
fprintf(stdout, " minimum: -M4, maximum: -M255\n\n");
#endif
#if defined(LIBBSC_CUDA_SUPPORT) || defined(_WIN32) || defined(LIBBSC_OPENMP)
fprintf(stdout, "Platform specific options:\n");
#ifdef LIBBSC_CUDA_SUPPORT
fprintf(stdout, " -G Enable Sort Transform acceleration on NVIDIA GPU, default: disable\n");
#endif
#ifdef _WIN32
fprintf(stdout, " -P Enable large 2MB RAM pages, default: disable\n");
#endif
#ifdef LIBBSC_OPENMP
fprintf(stdout, " -t Disable parallel blocks processing, default: enable\n");
fprintf(stdout, " -T Disable multi-core systems support, default: enable\n");
#endif
fprintf(stdout, "\n");
#endif
#if !defined(BSC_DECOMPRESSION_ONLY) || defined(LIBBSC_CUDA_SUPPORT) || defined(_WIN32) || defined(LIBBSC_OPENMP)
fprintf(stdout,"Options may be combined into one, like -b128p -m5e1\n");
#endif
exit(0);
}
void ProcessSwitch(char * s)
{
if (*s == 0)
{
ShowUsage();
}
for (; *s != 0; )
{
switch (*s++)
{
case 'b':
{
char * strNum = s; while ((*s >= '0') && (*s <= '9')) s++;
paramBlockSize = atoi(strNum) * 1024 * 1024;
if ((paramBlockSize < 1024 * 1024) || (paramBlockSize > 2047 * 1024 * 1024)) ShowUsage();
break;
}
case 'm':
{
char * strNum = s; while ((*s >= '0') && (*s <= '9')) s++;
switch (atoi(strNum))
{
case 0 : paramBlockSorter = LIBBSC_BLOCKSORTER_BWT; break;
#ifdef LIBBSC_SORT_TRANSFORM_SUPPORT
case 3 : paramBlockSorter = LIBBSC_BLOCKSORTER_ST3; break;
case 4 : paramBlockSorter = LIBBSC_BLOCKSORTER_ST4; break;
case 5 : paramBlockSorter = LIBBSC_BLOCKSORTER_ST5; break;
case 6 : paramBlockSorter = LIBBSC_BLOCKSORTER_ST6; break;
case 7 : paramBlockSorter = LIBBSC_BLOCKSORTER_ST7; paramEnableCUDA = 1; break;
case 8 : paramBlockSorter = LIBBSC_BLOCKSORTER_ST8; paramEnableCUDA = 1; break;
#endif
default : ShowUsage();
}
break;
}
case 'c':
{
switch (*s++)
{
case 'f' : paramSortingContexts = LIBBSC_CONTEXTS_FOLLOWING; break;
case 'p' : paramSortingContexts = LIBBSC_CONTEXTS_PRECEDING; break;
case 'a' : paramSortingContexts = LIBBSC_CONTEXTS_AUTODETECT; break;
default : ShowUsage();
}
break;
}
case 'e':
{
switch (*s++)
{
case '0' : paramCoder = LIBBSC_CODER_QLFC_FAST; break;
case '1' : paramCoder = LIBBSC_CODER_QLFC_STATIC; break;
case '2' : paramCoder = LIBBSC_CODER_QLFC_ADAPTIVE; break;
default : ShowUsage();
}
break;
}
case 'H':
{
char * strNum = s; while ((*s >= '0') && (*s <= '9')) s++;
paramLZPHashSize = atoi(strNum);
if ((paramLZPHashSize < 10) || (paramLZPHashSize > 28)) ShowUsage();
break;
}
case 'M':
{
char * strNum = s; while ((*s >= '0') && (*s <= '9')) s++;
paramLZPMinLen = atoi(strNum);
if ((paramLZPMinLen < 4) || (paramLZPMinLen > 255)) ShowUsage();
break;
}
case 'l': paramEnableLZP = 1; break;
case 's': paramEnableSegmentation = 1; break;
case 'r': paramEnableReordering = 1; break;
case 'p': paramEnableLZP = paramEnableSegmentation = paramEnableReordering = 0; break;
#ifdef LIBBSC_OPENMP
case 't': paramEnableParallelProcessing = 0; break;
case 'T': paramEnableParallelProcessing = paramEnableMultiThreading = 0; break;
#endif
#ifdef LIBBSC_CUDA_SUPPORT
case 'G': paramEnableCUDA = 1; break;
#endif
#ifdef _WIN32
case 'P': paramEnableLargePages = 1; break;
#endif
default : ShowUsage();
}
}
}
void ProcessCommandline(int argc, char * argv[])
{
if (argc < 4 || strlen(argv[1]) != 1)
{
ShowUsage();
}
for (int i = 4; i < argc; ++i)
{
if (argv[i][0] == '-')
{
ProcessSwitch(&argv[i][1]);
}
else
{
ShowUsage();
}
}
}
int main(int argc, char * argv[])
{
fprintf(stdout, "This is bsc, Block Sorting Compressor. Version 3.2.4. 18 January 2022.\n");
fprintf(stdout, "Copyright (c) 2009-2021 Ilya Grebnov <[email protected]>.\n\n");
#if defined(_OPENMP) && defined(__INTEL_COMPILER)
kmp_set_warnings_off();
#endif
ProcessCommandline(argc, argv);
if (bsc_init(paramFeatures()) != LIBBSC_NO_ERROR)
{
fprintf(stderr, "\nInternal program error, please contact the author!\n");
exit(2);
}
switch (*argv[1])
{
#if !defined(BSC_DECOMPRESSION_ONLY)
case 'e' : case 'E' : Compression(argv); break;
#endif
case 'd' : case 'D' : Decompression(argv); break;
default : ShowUsage();
}
return 0;
}
/*-----------------------------------------------------------*/
/* End bsc.cpp */
/*-----------------------------------------------------------*/