File: software.c

package info (click to toggle)
genwqe-user 4.0.20-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,372 kB
  • sloc: ansic: 18,786; sh: 2,382; makefile: 420
file content (855 lines) | stat: -rw-r--r-- 22,178 bytes parent folder | download | duplicates (2)
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
/*
 * Copyright 2015, 2016, International Business Machines
 *
 * 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.
 */

#define _LARGEFILE64_SOURCE
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <zlib.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <malloc.h>
#include <string.h>
#include <dlfcn.h>
#include <wrapper.h>

#undef CONFIG_USE_ZLIB_PATH_ENV_VAR

/* older zlibs might not have this */
#ifndef z_off64_t
#  define z_off64_t off64_t
#endif

#if defined(CONFIG_DLOPEN_MECHANISM)

typedef void * __attribute__ ((__may_alias__)) pvoid_t;

#define register_sym(name)						\
	do {								\
		dlerror();    /* Clear any existing error */		\
		/* sw_trace("loading [%s]\n", #name); */		\
		*(pvoid_t *)(&p_##name) = dlsym(handle, #name);		\
		if ((error = dlerror()) != NULL) {			\
			sw_trace("%s\n", error);			\
			/* exit(EXIT_FAILURE); */			\
		}							\
	} while (0)

#define check_sym(name, rc)						\
	do {								\
		if ((name) == NULL) {					\
			pr_err("%s not loadable, consider using a "	\
			       "newer libz version.\n", #name);		\
			return (rc);					\
		}							\
	} while (0)

static void *handle = NULL;

int (* p_deflateInit2_)(z_streamp strm, int level, int method,
			int windowBits, int memLevel, int strategy,
			const char *version, int stream_size);
int z_deflateInit2_(z_streamp strm, int level, int method,
		    int windowBits, int memLevel, int strategy,
		    const char *version, int stream_size)
{
	int rc;

	check_sym(p_deflateInit2_, Z_STREAM_ERROR);
	rc = (* p_deflateInit2_)(strm, level, method, windowBits, memLevel,
				 strategy, version, stream_size);
	return rc;
}

static int (* p_deflateParams)(z_streamp strm, int level, int strategy);
int z_deflateParams(z_streamp strm, int level, int strategy)
{
	check_sym(p_deflateParams, Z_STREAM_ERROR);
	return (* p_deflateParams)(strm, level, strategy);
}

static uLong (* p_deflateBound)(z_streamp strm, uLong sourceLen);
uLong z_deflateBound(z_streamp strm, uLong sourceLen)
{
	check_sym(p_deflateBound, Z_STREAM_ERROR);
	return (* p_deflateBound)(strm, sourceLen);
}

static int (* p_deflateReset)(z_streamp strm);
int z_deflateReset(z_streamp strm)
{
	check_sym(p_deflateReset, Z_STREAM_ERROR);
	return (* p_deflateReset)(strm);
}

static int (* p_deflateSetDictionary)(z_streamp strm, const Bytef *dictionary,
				      uInt  dictLength);
int z_deflateSetDictionary(z_streamp strm, const Bytef *dictionary,
			   uInt  dictLength)
{
	check_sym(p_deflateSetDictionary, Z_STREAM_ERROR);
	return (* p_deflateSetDictionary)(strm, dictionary, dictLength);
}

static int (* p_deflateSetHeader)(z_streamp strm, gz_headerp head);
int z_deflateSetHeader(z_streamp strm, gz_headerp head)
{
	check_sym(p_deflateSetHeader, Z_STREAM_ERROR);
	return p_deflateSetHeader(strm, head);
}


static int (* p_deflatePrime)(z_streamp strm, int bits, int value);
int z_deflatePrime(z_streamp strm, int bits, int value)
{
	check_sym(p_deflatePrime, Z_STREAM_ERROR);
	return (* p_deflatePrime)(strm, bits, value);
}

static int (* p_deflateCopy)(z_streamp dest, z_streamp source);
int z_deflateCopy(z_streamp dest, z_streamp source)
{
	check_sym(p_deflateCopy, Z_STREAM_ERROR);
	return (* p_deflateCopy)(dest, source);
}

static int (* p_deflate)(z_streamp strm, int flush);
int z_deflate(z_streamp strm, int flush)
{
	check_sym(p_deflate, Z_STREAM_ERROR);
	return (* p_deflate)(strm, flush);
}

static int (* p_deflateEnd)(z_streamp strm);
int z_deflateEnd(z_streamp strm)
{
	check_sym(p_deflateEnd, Z_STREAM_ERROR);
	return (* p_deflateEnd)(strm);
}

static int (* p_inflateInit2_)(z_streamp strm, int  windowBits,
			       const char *version, int stream_size);
int z_inflateInit2_(z_streamp strm, int  windowBits, const char *version,
		    int stream_size)
{
	int rc;

	check_sym(p_inflateInit2_, Z_STREAM_ERROR);
	rc = (* p_inflateInit2_)(strm, windowBits, version, stream_size);
	return rc;
}

static int (* p_inflateReset)(z_streamp strm);
int z_inflateReset(z_streamp strm)
{
	check_sym(p_inflateReset, Z_STREAM_ERROR);
	return (* p_inflateReset)(strm);
}

static int (* p_inflateReset2)(z_streamp strm, int windowBits);
int z_inflateReset2(z_streamp strm, int windowBits)
{
	check_sym(p_inflateReset2, Z_STREAM_ERROR);
	return (* p_inflateReset2)(strm, windowBits);
}

static int (* p_inflateSetDictionary)(z_streamp strm, const Bytef *dictionary,
				 uInt  dictLength);
int z_inflateSetDictionary(z_streamp strm, const Bytef *dictionary,
			   uInt  dictLength)
{
	check_sym(p_inflateSetDictionary, Z_STREAM_ERROR);
	return (* p_inflateSetDictionary)(strm, dictionary, dictLength);
}

/**
 * No warning in this case since we try to emulate this in the
 * functions above.
 */
static int (* p_inflateGetDictionary)(z_streamp strm, const Bytef *dictionary,
				      uInt *dictLength);
int z_inflateGetDictionary(z_streamp strm, const Bytef *dictionary,
			   uInt *dictLength)
{
	if (p_inflateGetDictionary == NULL)
		return Z_STREAM_ERROR;
	return (* p_inflateGetDictionary)(strm, dictionary, dictLength);
}

bool z_hasGetDictionary(void)
{
	return (p_inflateGetDictionary != NULL);
}

static int (* p_inflateGetHeader)(z_streamp strm, gz_headerp head);
int z_inflateGetHeader(z_streamp strm, gz_headerp head)
{
	check_sym(p_inflateGetHeader, Z_STREAM_ERROR);
	return (* p_inflateGetHeader)(strm, head);
}

static int (* p_inflatePrime)(z_streamp strm, int bits, int value);
int z_inflatePrime(z_streamp strm, int bits, int value)
{
	check_sym(p_inflatePrime, Z_STREAM_ERROR);
	return (* p_inflatePrime)(strm, bits, value);
}

static int (* p_inflateSync)(z_streamp strm);
int z_inflateSync(z_streamp strm)
{
	check_sym(p_inflateSync, Z_STREAM_ERROR);
	return (* p_inflateSync)(strm);
}

static int (* p_inflate)(z_streamp strm, int flush);
int z_inflate(z_streamp strm, int flush)
{
	check_sym(p_inflate, Z_STREAM_ERROR);
	return (* p_inflate)(strm, flush);
}

static int (* p_inflateEnd)(z_streamp strm);
int z_inflateEnd(z_streamp strm)
{
	check_sym(p_inflateEnd, Z_STREAM_ERROR);
	return (* p_inflateEnd)(strm);
}

static int (* p_inflateBackInit_)(z_streamp strm, int windowBits,
				  unsigned char *window, const char *version,
				  int stream_size);
int z_inflateBackInit_(z_streamp strm, int windowBits,
		       unsigned char *window, const char *version,
		       int stream_size)
{
	check_sym(p_inflateBackInit_, Z_STREAM_ERROR);
	return (* p_inflateBackInit_)(strm, windowBits, window, version,
				      stream_size);
}

static int (* p_inflateBack)(z_streamp strm, in_func in, void *in_desc,
			     out_func out, void *out_desc);
int z_inflateBack(z_streamp strm, in_func in, void *in_desc,
		      out_func out, void *out_desc)
{
	check_sym(p_inflateBack, Z_STREAM_ERROR);
	return (* p_inflateBack)(strm, in, in_desc, out, out_desc);
}

static int (* p_inflateBackEnd)(z_streamp strm);
int z_inflateBackEnd(z_streamp strm)
{
	check_sym(p_inflateBackEnd, Z_STREAM_ERROR);
	return (* p_inflateBackEnd)(strm);
}

static uLong (* p_adler32)(uLong adler, const Bytef *buf, uInt len);
uLong z_adler32(uLong adler, const Bytef *buf, uInt len)
{
	check_sym(p_adler32, Z_STREAM_ERROR);
	return (* p_adler32)(adler, buf, len);
}

static uLong (* p_adler32_combine)(uLong adler1, uLong adler2, z_off_t len2);
uLong z_adler32_combine(uLong adler1, uLong adler2, z_off_t len2)
{
	check_sym(p_adler32_combine, Z_STREAM_ERROR);
	return (* p_adler32_combine)(adler1, adler2, len2);
}

static uLong (* p_crc32)(uLong crc, const Bytef *buf, uInt len);
uLong z_crc32(uLong crc, const Bytef *buf, uInt len)
{
	check_sym(p_crc32, Z_STREAM_ERROR);
	return (* p_crc32)(crc, buf, len);
}

static uLong (* p_crc32_combine)(uLong crc1, uLong crc2, z_off_t len2);
uLong z_crc32_combine(uLong crc1, uLong crc2, z_off_t len2)
{
	check_sym(p_crc32_combine, Z_STREAM_ERROR);
	return (* p_crc32_combine)(crc1, crc2, len2);
}

static const char *(* p_zError)(int err);
const char *z_zError(int err)
{
	check_sym(p_zError, NULL);
	return (* p_zError)(err);
}

static uLong (* p_zlibCompileFlags)(void);
uLong z_zlibCompileFlags(void)
{
	return p_zlibCompileFlags();
}

static const char * (* p_zlibVersion)(void);
const char *z_zlibVersion(void)
{
	check_sym(p_zlibVersion, NULL);
	return (* p_zlibVersion)();
}

static gzFile (* p_gzopen)(const char *path, const char *mode);
gzFile gzopen(const char *path, const char *mode)
{
	zlib_stats_inc(&zlib_stats.gzopen);
	check_sym(p_gzopen, NULL);
	return (* p_gzopen)(path, mode);
}

static gzFile (* p_gzdopen)(int fd, const char *mode);
gzFile gzdopen(int fd, const char *mode)
{
	zlib_stats_inc(&zlib_stats.gzdopen);
	check_sym(p_gzdopen, NULL);
	return (* p_gzdopen)(fd, mode);
}

static int (* p_gzwrite)(gzFile file, voidpc buf, unsigned len);
int gzwrite(gzFile file, voidpc buf, unsigned len)

{
	zlib_stats_inc(&zlib_stats.gzwrite);
	check_sym(p_gzwrite, -1);
	return (* p_gzwrite)(file, buf, len);
}

static int (* p_gzread)(gzFile file, voidp buf, unsigned len);
int gzread(gzFile file, voidp buf, unsigned len)
{
	zlib_stats_inc(&zlib_stats.gzread);
	check_sym(p_gzread, -1);
	return (* p_gzread)(file, buf, len);
}

static int (* p_gzclose)(gzFile file);
int gzclose(gzFile file)
{
	zlib_stats_inc(&zlib_stats.gzclose);
	check_sym(p_gzread, Z_STREAM_ERROR);
	return (* p_gzclose)(file);
}

static int (* p_gzungetc)(int c, gzFile file);
int gzungetc(int c, gzFile file)
{
	zlib_stats_inc(&zlib_stats.gzungetc);
	check_sym(p_gzungetc, -1);
	return (* p_gzungetc)(c, file);
}

static int (* p_gzflush)(gzFile file, int flush);
int gzflush(gzFile file, int flush)
{
	zlib_stats_inc(&zlib_stats.gzflush);
	check_sym(p_gzflush, Z_STREAM_ERROR);
	return (* p_gzflush)(file, flush);
}

static int (* p_gzeof)(gzFile file);
int gzeof(gzFile file)
{
	zlib_stats_inc(&zlib_stats.gzeof);
	check_sym(p_gzeof, 0);
	return (* p_gzeof)(file);
}

static z_off_t (* p_gztell)(gzFile file);
z_off_t gztell(gzFile file)
{
	zlib_stats_inc(&zlib_stats.gztell);
	check_sym(p_gztell, -1ll);
	return (* p_gztell)(file);
}

static const char * (* p_gzerror)(gzFile file, int *errnum);
const char *gzerror(gzFile file, int *errnum)
{
	zlib_stats_inc(&zlib_stats.gzerror);
	check_sym(p_gzerror, NULL);
	return (* p_gzerror)(file, errnum);
}

static z_off_t (* p_gzseek)(gzFile file, z_off_t offset, int whence);
z_off_t gzseek(gzFile file, z_off_t offset, int whence)
{
	zlib_stats_inc(&zlib_stats.gzseek);
	check_sym(p_gzseek, -1ll);
	return (* p_gzseek)(file, offset, whence);
}

static int (* p_gzrewind)(gzFile file);
int gzrewind(gzFile file)
{
	zlib_stats_inc(&zlib_stats.gzrewind);
	check_sym(p_gzrewind, -1);
	return (* p_gzrewind)(file);
}

static char * (* p_gzgets)(gzFile file, char *buf, int len);
char * gzgets(gzFile file, char *buf, int len)
{
	zlib_stats_inc(&zlib_stats.gzgets);
	check_sym(p_gzgets, NULL);
	return (* p_gzgets)(file, buf, len);
}

static int (* p_gzputc)(gzFile file, int c);
int gzputc(gzFile file, int c)
{
	zlib_stats_inc(&zlib_stats.gzputc);
	check_sym(p_gzputc, -1);
	return (* p_gzputc)(file, c);
}

/*FIXME gzgetc is potentially a macro ... */
static int (* p_gzgetc)(gzFile file);
#undef gzgetc
int gzgetc(gzFile file)
{
	zlib_stats_inc(&zlib_stats.gzgetc);
	check_sym(p_gzgetc, -1);
	return (* p_gzgetc)(file);
}

static int (* p_gzputs)(gzFile file, const char *s);
int gzputs(gzFile file, const char *s)
{
	zlib_stats_inc(&zlib_stats.gzputs);
	check_sym(p_gzputs, -1);
	return (* p_gzputs)(file, s);
}

static int (* p_gzprintf)(gzFile file, const char *format, ...);
int gzprintf(gzFile file, const char *format, ...)
{
	int count;
	va_list ap;

	zlib_stats_inc(&zlib_stats.gzprintf);
	check_sym(p_gzprintf, -1);

	va_start(ap, format);
	count = (* p_gzprintf)(file, format, ap);
	va_end(ap);

	return count;
}

static int (* p_compress)(Bytef *dest, uLongf *destLen,
			  const Bytef *source, uLong sourceLen);
int compress(Bytef *dest, uLongf *destLen, const Bytef *source,
	     uLong sourceLen)
{
	zlib_stats_inc(&zlib_stats.compress);
	check_sym(p_compress, Z_STREAM_ERROR);
	return (* p_compress)(dest, destLen, source, sourceLen);
}

static int (* p_compress2)(Bytef *dest, uLongf *destLen,
			   const Bytef *source, uLong sourceLen, int level);
int compress2(Bytef *dest, uLongf *destLen, const Bytef *source,
	      uLong sourceLen, int level)
{
	zlib_stats_inc(&zlib_stats.compress2);
	check_sym(p_compress2, Z_STREAM_ERROR);
	return (* p_compress2)(dest, destLen, source, sourceLen, level);
}

static uLong (* p_compressBound)(uLong sourceLen);
uLong z_compressBound(uLong sourceLen)
{
	check_sym(p_compressBound, Z_STREAM_ERROR);
	return (* p_compressBound)(sourceLen);
}

static int (* p_uncompress)(Bytef *dest, uLongf *destLen,
			    const Bytef *source, uLong sourceLen);
int uncompress(Bytef *dest, uLongf *destLen, const Bytef *source,
	       uLong sourceLen)
{
	zlib_stats_inc(&zlib_stats.uncompress);
	check_sym(p_uncompress, Z_STREAM_ERROR);
	return (* p_uncompress)(dest, destLen, source, sourceLen);
}

#if ZLIB_VERNUM >= 0x1270
static int (* p_gzbuffer)(gzFile file, unsigned size);
int gzbuffer(gzFile file, unsigned size)
{
	zlib_stats_inc(&zlib_stats.gzbuffer);
	check_sym(p_gzbuffer, -1);
	return (* p_gzbuffer)(file, size);
}

static uLong (* p_adler32_combine64)(uLong adler1, uLong adler2,
				     z_off64_t len2);
uLong adler32_combine64(uLong adler1, uLong adler2, z_off64_t len2)
{
	zlib_stats_inc(&zlib_stats.adler32_combine64);
	check_sym(p_adler32_combine64, Z_STREAM_ERROR);
	return (* p_adler32_combine64)(adler1, adler2, len2);
}

static uLong (* p_crc32_combine64)(uLong crc1, uLong crc2, z_off64_t len2);
uLong crc32_combine64(uLong crc1, uLong crc2, z_off64_t len2)
{
	zlib_stats_inc(&zlib_stats.crc32_combine64);
	check_sym(p_crc32_combine64, Z_STREAM_ERROR);
	return (* p_crc32_combine64)(crc1, crc2, len2);
}

static gzFile (* p_gzopen64)(const char *path, const char *mode);
gzFile gzopen64(const char *path, const char *mode)
{
	zlib_stats_inc(&zlib_stats.gzopen64);
	check_sym(p_gzopen64, NULL);
	return (* p_gzopen64)(path, mode);
}

static z_off64_t (* p_gztell64)(gzFile file);
z_off64_t gztell64(gzFile file)
{
	zlib_stats_inc(&zlib_stats.gztell64);
	check_sym(p_gztell64, -1ll);
	return (* p_gztell64)(file);
}

static z_off64_t (* p_gzseek64)(gzFile file, z_off64_t offset, int whence);
z_off64_t gzseek64(gzFile file, z_off64_t offset, int whence)
{
	zlib_stats_inc(&zlib_stats.gzseek64);
	check_sym(p_gzseek64, -1ll);
	return (* p_gzseek64)(file, offset, whence);
}

static z_off_t (* p_gzoffset)(gzFile file);
z_off_t gzoffset(gzFile file)
{
	zlib_stats_inc(&zlib_stats.gzoffset);
	check_sym(p_gzoffset, -1ll);
	return (* p_gzoffset)(file);
}

static z_off64_t (* p_gzoffset64)(gzFile file);
z_off64_t gzoffset64(gzFile file)
{
	zlib_stats_inc(&zlib_stats.gzoffset64);
	check_sym(p_gzoffset64, -1ll);
	return (* p_gzoffset64)(file);
}

static const z_crc_t *(* p_get_crc_table)(void);
const z_crc_t *get_crc_table()
{
	zlib_stats_inc(&zlib_stats.get_crc_table);
	check_sym(p_get_crc_table, NULL);
	return (* p_get_crc_table)();
}

#endif

/**
 * NOTE: We had different variants trying to find the right libz.so.1
 * in our system. Unfortunately this can differ for the various
 * distributions:
 *
 * RHEL7.2:
 *   $ ldconfig -p | grep libz.so.1 | cut -d' ' -f4 | head -n1
 *   /lib64/libz.so.1
 *
 * Ubuntu 15.10:
 *   $ ldconfig -p | grep libz.so.1 | cut -d' ' -f4 | head -n1
 *   /lib/powerpc64le-linux-gnu/libz.so.1
 *
 * Intel with RHEL6.7:
 *   $ ldconfig -p | grep libz.so.1 | cut -d' ' -f4 | head -n1
 *   /lib64/libz.so.1
 *
 * We are setting this via config.mk option and allow the
 * distributions to overwrite this via rpm spec file.
 *
 * NOTE: We tried loading "libz.so.1" without full path, but that
 * turned out to be dangerous. We tried to load our own lib, which was
 * leading to endless recursive calls and segfaults as results.
 */
void zedc_sw_init(void)
{
	char *error;

#ifdef CONFIG_USE_ZLIB_PATH_ENV_VAR
	const char *zlib_path = getenv("ZLIB_PATH");

	/* User has setup environment variable to find libz.so.1 */
	/*
	 * This should be for debugging only. We got a report that
	 * this mechanism is critical from a security perspective,
	 * since it allows to execute arbitrary code if being misused.
	 *
	 * See also:
	 *   https://github.com/ibm-genwqe/genwqe-user/issues/156
	 */
	if (zlib_path != NULL) {
		sw_trace("Loading software zlib \"%s\"\n", zlib_path);
		dlerror();
		handle = dlopen(zlib_path, RTLD_LAZY);
		if (handle != NULL)
			goto load_syms;
	}
#endif

	/* We saw dlopen returning non NULL value in case of passing ""! */
	if (strcmp(CONFIG_ZLIB_PATH, "") == 0) {
		pr_err("  Empty CONFIG_ZLIB_PATH \"%s\"\n",
		       CONFIG_ZLIB_PATH);
		return;
	}

	/* Loading private zlib.so.1 using CONFIG_ZLIB_PATH */
	sw_trace("Loading software zlib \"%s\"\n", CONFIG_ZLIB_PATH);
	dlerror();
	handle = dlopen(CONFIG_ZLIB_PATH, RTLD_LAZY);
	if (handle == NULL) {
		pr_err("  %s\n", dlerror());
		return;
	}

#ifdef CONFIG_USE_ZLIB_PATH_ENV_VAR
load_syms:
#endif
	register_sym(zlibVersion);

	sw_trace("  ZLIB_VERSION=%s (header) zlibVersion()=%s (code)\n",
		 ZLIB_VERSION, z_zlibVersion());

	if (strcmp(ZLIB_VERSION, z_zlibVersion()) != 0) {
		pr_err("libz.so.1=%s and zlib.h=%s do not match!\n",
		       z_zlibVersion(), ZLIB_VERSION);
		return;
	}

	register_sym(deflateInit2_);
	register_sym(deflateParams);
	register_sym(deflateBound);
	register_sym(deflateReset);
	register_sym(deflatePrime);
	register_sym(deflateCopy);
	register_sym(deflate);
	register_sym(deflateSetDictionary);
	register_sym(deflateSetHeader);
	register_sym(deflateEnd);

	register_sym(inflateInit2_);
	register_sym(inflateSync);
	register_sym(inflatePrime);
	register_sym(inflate);
	register_sym(inflateReset);
	register_sym(inflateReset2);
	register_sym(inflateSetDictionary);
	register_sym(inflateGetDictionary);
	register_sym(inflateGetHeader);
	register_sym(inflateEnd);

	register_sym(inflateBackInit_);
	register_sym(inflateBack);
	register_sym(inflateBackEnd);

	register_sym(gzopen);
	register_sym(gzdopen);
	register_sym(gzwrite);
	register_sym(gzread);
	register_sym(gzclose);
	register_sym(gzflush);
	register_sym(gzungetc);
	register_sym(gzeof);
	register_sym(gztell);
	register_sym(gzerror);
	register_sym(gzseek);
	register_sym(gzrewind);
	register_sym(gzputs);
	register_sym(gzputc);
	register_sym(gzgetc);
	register_sym(gzputs);
	register_sym(gzprintf);

	register_sym(compress);
	register_sym(compress2);
	register_sym(compressBound);
	register_sym(uncompress);

	register_sym(zError);
	register_sym(zlibCompileFlags);

	register_sym(adler32);
	register_sym(adler32_combine);
	register_sym(crc32);
	register_sym(crc32_combine);


#if ZLIB_VERNUM >= 0x1270
	register_sym(gzbuffer);
	register_sym(gzopen64);
	register_sym(gzseek64);
	register_sym(gztell64);
	register_sym(gzoffset);
	register_sym(gzoffset64);
	register_sym(adler32_combine64);
	register_sym(crc32_combine64);
	register_sym(get_crc_table);
#endif
}

void zedc_sw_done(void)
{
	if (handle != NULL) {
		sw_trace("Closing software zlib\n");
		dlclose(handle);
	}
}

#else

/*
 * Prefixing symbols has nasty side effects. One of them is that libc
 * symbols get prefixed too. Such that we see z_free and not free
 * anymore. Let us fix this up here to see if it works in general.
 */
void *z_malloc(size_t size);
void *z_malloc(size_t size)
{
	return malloc(size);
}

void z_free(void *ptr);
void z_free(void *ptr)
{
	free(ptr);
}

void *z_memcpy(void *dest, const void *src, size_t n);
void *z_memcpy(void *dest, const void *src, size_t n)
{
	return memcpy(dest, src, n);
}

size_t z_strlen(const char *s);
size_t z_strlen(const char *s)
{
	return strlen(s);
}

void *z_memset(void *s, int c, size_t n);
void *z_memset(void *s, int c, size_t n)
{
	return memset(s, c, n);
}

int z_close(int fd);
int z_close(int fd)
{
	return close(fd);
}

int z_open(const char *pathname, int flags, mode_t mode);
int z_open(const char *pathname, int flags, mode_t mode)
{
	return open(pathname, flags, mode);
}

ssize_t z_read(int fd, void *buf, size_t count);
ssize_t z_read(int fd, void *buf, size_t count)
{
	return read(fd, buf, count);
}

ssize_t z_write(int fd, const void *buf, size_t count);
ssize_t z_write(int fd, const void *buf, size_t count)
{
	return write(fd, buf, count);
}

long long z_lseek64(int fd, long long offset, int whence);
long long z_lseek64(int fd, long long offset, int whence)
{
	return lseek64(fd, offset, whence);
}

int z_snprintf(char *str, size_t size, const char *format, ...);
int z_snprintf(char *str, size_t size, const char *format, ...)
{
	int rc;
	va_list ap;

	va_start(ap, format);
	rc = snprintf(str, size, format, ap);
	va_end(ap);
	return rc;
}

int z_vsnprintf(char *str, size_t size, const char *format, va_list ap);
int z_vsnprintf(char *str, size_t size, const char *format, va_list ap)
{
	int rc;

	rc = vsnprintf(str, size, format, ap);
	return rc;
}

extern int *z___errno_location (void);
extern int *z___errno_location (void)
{
	return __errno_location();
}


void *z_memchr(const void *s, int c, size_t n);
void *z_memchr(const void *s, int c, size_t n)
{
	return memchr(s, c, n);
}

char *z_strerror(int errnum);
char *z_strerror(int errnum)
{
	return strerror(errnum);
}

void zedc_sw_init(void)
{
	sw_trace("Using z_ prefixed libz.a\n");
	sw_trace("  ZLIB_VERSION %s (header version)\n", ZLIB_VERSION);
	sw_trace("  zlibVersion  %s (libz.so version)\n", z_zlibVersion());

	if (strcmp(ZLIB_VERSION, z_zlibVersion()) != 0) {
		pr_err("libz.so %s and zlib.h %s do not match!\n",
		       z_zlibVersion(), ZLIB_VERSION);
		return;
	}
}

void zedc_sw_done(void)
{
	sw_trace("Closing software zlib\n");
}

#endif	/* CONFIG_DLOPEN_MECHANSIM */