]>
git.proxmox.com Git - ceph.git/blob - ceph/src/zstd/tests/longmatch.c
6 #define ZSTD_STATIC_LINKING_ONLY
9 int compress(ZSTD_CStream
*ctx
, ZSTD_outBuffer out
, const void *data
, size_t size
) {
10 ZSTD_inBuffer in
= { data
, size
, 0 };
11 while (in
.pos
< in
.size
) {
12 ZSTD_outBuffer tmp
= out
;
13 const size_t rc
= ZSTD_compressStream(ctx
, &tmp
, &in
);
14 if (ZSTD_isError(rc
)) {
19 ZSTD_outBuffer tmp
= out
;
20 const size_t rc
= ZSTD_flushStream(ctx
, &tmp
);
21 if (rc
!= 0) { return 1; }
26 int main(int argc
, const char** argv
) {
28 ZSTD_parameters params
;
34 ctx
= ZSTD_createCStream();
35 if (!ctx
) { return 1; }
37 memset(¶ms
, 0, sizeof(params
));
38 params
.cParams
.windowLog
= 18;
39 params
.cParams
.chainLog
= 13;
40 params
.cParams
.hashLog
= 14;
41 params
.cParams
.searchLog
= 1;
42 params
.cParams
.searchLength
= 7;
43 params
.cParams
.targetLength
= 16;
44 params
.cParams
.strategy
= ZSTD_fast
;
45 windowLog
= params
.cParams
.windowLog
;
46 /* Initialize stream */
47 rc
= ZSTD_initCStream_advanced(ctx
, NULL
, 0, params
, 0);
48 if (ZSTD_isError(rc
)) { return 2; }
51 const U64 toCompress
= ((U64
)1) << 33;
52 const size_t size
= 1 << windowLog
;
54 char *srcBuffer
= (char*) malloc(1 << windowLog
);
55 char *dstBuffer
= (char*) malloc(ZSTD_compressBound(1 << windowLog
));
56 ZSTD_outBuffer out
= { dstBuffer
, ZSTD_compressBound(1 << windowLog
), 0 };
57 const char match
[] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
58 const size_t randomData
= (1 << windowLog
) - 2*sizeof(match
);
60 printf("\n === Long Match Test === \n");
61 printf("Creating random data to produce long matches \n");
62 for (i
= 0; i
< sizeof(match
); ++i
) {
63 srcBuffer
[i
] = match
[i
];
65 for (i
= 0; i
< randomData
; ++i
) {
66 srcBuffer
[sizeof(match
) + i
] = (char)(rand() & 0xFF);
68 for (i
= 0; i
< sizeof(match
); ++i
) {
69 srcBuffer
[sizeof(match
) + randomData
+ i
] = match
[i
];
71 printf("Compressing, trying to generate a segfault \n");
72 if (compress(ctx
, out
, srcBuffer
, size
)) {
76 while (compressed
< toCompress
) {
77 const size_t block
= rand() % (size
- pos
+ 1);
78 if (pos
== size
) { pos
= 0; }
79 if (compress(ctx
, out
, srcBuffer
+ pos
, block
)) {
85 printf("Compression completed successfully (no error triggered)\n");