-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathCompressZlib.java
More file actions
617 lines (521 loc) · 21.8 KB
/
CompressZlib.java
File metadata and controls
617 lines (521 loc) · 21.8 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
package org.perlonjava.runtime.perlmodule;
import org.perlonjava.runtime.operators.ReferenceOperators;
import org.perlonjava.runtime.runtimetypes.*;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.zip.*;
import static org.perlonjava.runtime.runtimetypes.RuntimeScalarCache.scalarUndef;
public class CompressZlib extends PerlModuleBase {
private static final String INFLATER_KEY = "_inflater";
private static final String DEFLATER_KEY = "_deflater";
public CompressZlib() {
super("Compress::Zlib", false);
}
public static void initialize() {
CompressZlib cz = new CompressZlib();
try {
cz.registerMethod("inflateInit", null);
cz.registerMethod("deflateInit", null);
cz.registerMethod("compress", null);
cz.registerMethod("uncompress", null);
cz.registerMethod("memGzip", null);
cz.registerMethod("memGunzip", null);
cz.registerMethod("crc32", null);
cz.registerMethod("adler32", null);
cz.registerMethod("Z_OK", null);
cz.registerMethod("Z_STREAM_END", null);
cz.registerMethod("Z_STREAM_ERROR", null);
cz.registerMethod("Z_DATA_ERROR", null);
cz.registerMethod("Z_BUF_ERROR", null);
cz.registerMethod("Z_NO_FLUSH", null);
cz.registerMethod("Z_SYNC_FLUSH", null);
cz.registerMethod("Z_FULL_FLUSH", null);
cz.registerMethod("Z_FINISH", null);
cz.registerMethod("Z_DEFAULT_COMPRESSION", null);
cz.registerMethod("Z_BEST_SPEED", null);
cz.registerMethod("Z_BEST_COMPRESSION", null);
cz.registerMethod("Z_FILTERED", null);
cz.registerMethod("Z_HUFFMAN_ONLY", null);
cz.registerMethod("Z_DEFAULT_STRATEGY", null);
cz.registerMethod("Z_DEFLATED", null);
cz.registerMethod("WANT_GZIP", null);
cz.registerMethod("WANT_GZIP_OR_ZLIB", null);
cz.registerMethod("MAX_WBITS", null);
cz.registerMethod("inflate", "inflateMethod", null);
cz.registerMethod("deflate", "deflateMethod", null);
cz.registerMethod("flush", null);
cz.registerMethod("gzopen", null);
} catch (NoSuchMethodException e) {
System.err.println("Warning: Missing Compress::Zlib method: " + e.getMessage());
}
// Initialize gzFile methods (gzread, gzwrite, gzreadline, gzeof, gzclose)
CompressZlibGzFile.initialize();
}
public static RuntimeList Z_OK(RuntimeArray args, int ctx) {
return new RuntimeScalar(0).getList();
}
public static RuntimeList Z_STREAM_END(RuntimeArray args, int ctx) {
return new RuntimeScalar(1).getList();
}
public static RuntimeList Z_STREAM_ERROR(RuntimeArray args, int ctx) {
return new RuntimeScalar(-2).getList();
}
public static RuntimeList Z_DATA_ERROR(RuntimeArray args, int ctx) {
return new RuntimeScalar(-3).getList();
}
public static RuntimeList Z_BUF_ERROR(RuntimeArray args, int ctx) {
return new RuntimeScalar(-5).getList();
}
public static RuntimeList Z_NO_FLUSH(RuntimeArray args, int ctx) {
return new RuntimeScalar(0).getList();
}
public static RuntimeList Z_SYNC_FLUSH(RuntimeArray args, int ctx) {
return new RuntimeScalar(2).getList();
}
public static RuntimeList Z_FULL_FLUSH(RuntimeArray args, int ctx) {
return new RuntimeScalar(3).getList();
}
public static RuntimeList Z_FINISH(RuntimeArray args, int ctx) {
return new RuntimeScalar(4).getList();
}
public static RuntimeList Z_DEFAULT_COMPRESSION(RuntimeArray args, int ctx) {
return new RuntimeScalar(-1).getList();
}
public static RuntimeList Z_BEST_SPEED(RuntimeArray args, int ctx) {
return new RuntimeScalar(1).getList();
}
public static RuntimeList Z_BEST_COMPRESSION(RuntimeArray args, int ctx) {
return new RuntimeScalar(9).getList();
}
public static RuntimeList Z_FILTERED(RuntimeArray args, int ctx) {
return new RuntimeScalar(1).getList();
}
public static RuntimeList Z_HUFFMAN_ONLY(RuntimeArray args, int ctx) {
return new RuntimeScalar(2).getList();
}
public static RuntimeList Z_DEFAULT_STRATEGY(RuntimeArray args, int ctx) {
return new RuntimeScalar(0).getList();
}
public static RuntimeList Z_DEFLATED(RuntimeArray args, int ctx) {
return new RuntimeScalar(8).getList();
}
public static RuntimeList WANT_GZIP(RuntimeArray args, int ctx) {
return new RuntimeScalar(16).getList();
}
public static RuntimeList WANT_GZIP_OR_ZLIB(RuntimeArray args, int ctx) {
return new RuntimeScalar(32).getList();
}
public static RuntimeList MAX_WBITS(RuntimeArray args, int ctx) {
return new RuntimeScalar(15).getList();
}
/**
* Helper to extract byte data from a scalar or scalar reference.
*/
private static byte[] getInputBytes(RuntimeScalar dataScalar) {
RuntimeScalar actual = dataScalar;
if (dataScalar.type == RuntimeScalarType.REFERENCE) {
actual = dataScalar.scalarDeref();
}
return actual.toString().getBytes(StandardCharsets.ISO_8859_1);
}
/**
* compress($data [, $level])
* One-shot zlib compression (RFC 1950 format).
* Returns compressed data or undef on error.
*/
public static RuntimeList compress(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
return scalarUndef.getList();
}
int level = Deflater.DEFAULT_COMPRESSION;
byte[] input = getInputBytes(args.get(0));
if (args.size() > 1) {
level = args.get(1).getInt();
}
try {
Deflater deflater = new Deflater(level, false); // nowrap=false for zlib format
deflater.setInput(input);
deflater.finish();
ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length + 64);
byte[] buf = new byte[Math.max(input.length + 64, 1024)];
while (!deflater.finished()) {
int count = deflater.deflate(buf);
if (count > 0) {
baos.write(buf, 0, count);
} else {
break;
}
}
deflater.end();
RuntimeScalar result = new RuntimeScalar(baos.toString(StandardCharsets.ISO_8859_1));
result.type = RuntimeScalarType.BYTE_STRING;
return result.getList();
} catch (Exception e) {
return scalarUndef.getList();
}
}
/**
* uncompress($data)
* One-shot zlib decompression (RFC 1950 format).
* Returns decompressed data or undef on error.
*/
public static RuntimeList uncompress(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
return scalarUndef.getList();
}
byte[] input = getInputBytes(args.get(0));
try {
Inflater inflater = new Inflater(false); // nowrap=false for zlib format
inflater.setInput(input);
ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length * 4);
byte[] buf = new byte[Math.max(input.length * 4, 1024)];
while (!inflater.finished()) {
int count = inflater.inflate(buf);
if (count > 0) {
baos.write(buf, 0, count);
} else if (inflater.needsInput()) {
break;
}
}
if (!inflater.finished()) {
inflater.end();
return scalarUndef.getList();
}
inflater.end();
RuntimeScalar result = new RuntimeScalar(baos.toString(StandardCharsets.ISO_8859_1));
result.type = RuntimeScalarType.BYTE_STRING;
return result.getList();
} catch (DataFormatException | IllegalArgumentException e) {
return scalarUndef.getList();
}
}
/**
* memGzip($data)
* Compress data in gzip format (RFC 1952).
* Returns gzipped data or undef on error.
*/
public static RuntimeList memGzip(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
return scalarUndef.getList();
}
byte[] input = getInputBytes(args.get(0));
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length + 64);
try (GZIPOutputStream gos = new GZIPOutputStream(baos)) {
gos.write(input);
}
RuntimeScalar result = new RuntimeScalar(baos.toString(StandardCharsets.ISO_8859_1));
result.type = RuntimeScalarType.BYTE_STRING;
return result.getList();
} catch (IOException e) {
return scalarUndef.getList();
}
}
/**
* memGunzip($data)
* Decompress gzip format data (RFC 1952).
* Returns decompressed data or undef on error.
*/
public static RuntimeList memGunzip(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
return scalarUndef.getList();
}
byte[] input = getInputBytes(args.get(0));
try {
ByteArrayInputStream bais = new ByteArrayInputStream(input);
GZIPInputStream gis = new GZIPInputStream(bais);
ByteArrayOutputStream baos = new ByteArrayOutputStream(input.length * 4);
byte[] buf = new byte[4096];
int count;
while ((count = gis.read(buf)) != -1) {
baos.write(buf, 0, count);
}
gis.close();
RuntimeScalar result = new RuntimeScalar(baos.toString(StandardCharsets.ISO_8859_1));
result.type = RuntimeScalarType.BYTE_STRING;
return result.getList();
} catch (IOException e) {
return scalarUndef.getList();
}
}
/**
* crc32($data [, $crc])
* Calculate CRC-32 checksum. If $crc is provided, continue from that value.
* Returns unsigned 32-bit integer.
*/
public static RuntimeList crc32(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
return new RuntimeScalar(0).getList();
}
byte[] input = getInputBytes(args.get(0));
CRC32 crc = new CRC32();
if (args.size() > 1 && args.get(1).getDefinedBoolean()) {
// Continue from previous CRC value — CRC32 doesn't support direct seeding,
// but Perl's crc32() with a second arg does a running checksum.
// Java's CRC32 doesn't allow seeding, so we use Adler32-style workaround:
// For compatibility, we use the combine approach if needed.
// Actually, Perl's Compress::Zlib::crc32 with initial value works by
// calling zlib's crc32() C function which supports seeding.
// Java's CRC32 doesn't support this directly, but we can use the
// crc32_combine concept. For simplicity, if the initial value is 0,
// just compute normally. For non-zero seed, we need a different approach.
long seed = args.get(1).getLong() & 0xFFFFFFFFL;
if (seed != 0) {
// Use reflection or a manual CRC32 table for seeding.
// Simpler: compute via direct table calculation.
long crcVal = crc32WithSeed(input, seed);
return new RuntimeScalar(crcVal).getList();
}
}
crc.update(input);
return new RuntimeScalar(crc.getValue()).getList();
}
/**
* CRC-32 computation with an initial seed value.
* Implements the CRC-32 algorithm directly with a lookup table.
*/
private static final long[] CRC32_TABLE = new long[256];
static {
for (int i = 0; i < 256; i++) {
long c = i;
for (int j = 0; j < 8; j++) {
if ((c & 1) != 0) {
c = 0xEDB88320L ^ (c >>> 1);
} else {
c >>>= 1;
}
}
CRC32_TABLE[i] = c;
}
}
private static long crc32WithSeed(byte[] data, long seed) {
long crc = seed ^ 0xFFFFFFFFL;
for (byte b : data) {
crc = CRC32_TABLE[(int) ((crc ^ b) & 0xFF)] ^ (crc >>> 8);
}
return (crc ^ 0xFFFFFFFFL) & 0xFFFFFFFFL;
}
/**
* adler32($data [, $adler])
* Calculate Adler-32 checksum. If $adler is provided, continue from that value.
* Returns unsigned 32-bit integer.
*/
public static RuntimeList adler32(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
return new RuntimeScalar(1).getList(); // Adler-32 of empty data is 1
}
byte[] input = getInputBytes(args.get(0));
Adler32 adler = new Adler32();
if (args.size() > 1 && args.get(1).getDefinedBoolean()) {
long seed = args.get(1).getLong() & 0xFFFFFFFFL;
if (seed != 1) {
// Adler32 with seed: compute manually
long s1 = seed & 0xFFFF;
long s2 = (seed >>> 16) & 0xFFFF;
for (byte b : input) {
s1 = (s1 + (b & 0xFF)) % 65521;
s2 = (s2 + s1) % 65521;
}
return new RuntimeScalar((s2 << 16) | s1).getList();
}
}
adler.update(input);
return new RuntimeScalar(adler.getValue()).getList();
}
public static RuntimeList inflateInit(RuntimeArray args, int ctx) {
boolean nowrap = false;
for (int i = 0; i < args.size() - 1; i++) {
String key = args.get(i).toString();
if (key.equals("-WindowBits") || key.equals("WindowBits")) {
int wbits = args.get(i + 1).getInt();
if (wbits < 0) {
nowrap = true;
}
break;
}
}
try {
Inflater inflater = new Inflater(nowrap);
RuntimeHash self = new RuntimeHash();
self.put(INFLATER_KEY, new RuntimeScalar(inflater));
RuntimeScalar ref = self.createReference();
ReferenceOperators.bless(ref, new RuntimeScalar("Compress::Zlib"));
return ref.getList();
} catch (Exception e) {
return scalarUndef.getList();
}
}
public static RuntimeList deflateInit(RuntimeArray args, int ctx) {
int level = Deflater.DEFAULT_COMPRESSION;
boolean nowrap = false;
for (int i = 0; i < args.size() - 1; i++) {
String key = args.get(i).toString();
if (key.equals("-Level") || key.equals("Level")) {
level = args.get(i + 1).getInt();
i++;
} else if (key.equals("-WindowBits") || key.equals("WindowBits")) {
int wbits = args.get(i + 1).getInt();
if (wbits < 0) {
nowrap = true; // raw deflate
}
// wbits >= 16 would mean gzip format, but Java's Deflater
// doesn't support that directly — use memGzip instead
i++;
}
}
try {
Deflater deflater = new Deflater(level, nowrap);
RuntimeHash self = new RuntimeHash();
self.put(DEFLATER_KEY, new RuntimeScalar(deflater));
RuntimeScalar ref = self.createReference();
ReferenceOperators.bless(ref, new RuntimeScalar("Compress::Zlib"));
return ref.getList();
} catch (Exception e) {
return scalarUndef.getList();
}
}
public static RuntimeList inflateMethod(RuntimeArray args, int ctx) {
if (args.size() < 2) {
RuntimeList result = new RuntimeList();
result.add(scalarUndef);
result.add(new RuntimeScalar(-2));
return result;
}
RuntimeHash self = args.get(0).hashDeref();
RuntimeScalar dataScalar = args.get(1);
RuntimeScalar inflaterScalar = self.get(INFLATER_KEY);
if (inflaterScalar == null || inflaterScalar.type != RuntimeScalarType.JAVAOBJECT
|| !(inflaterScalar.value instanceof Inflater inflater)) {
RuntimeList result = new RuntimeList();
result.add(scalarUndef);
result.add(new RuntimeScalar(-2));
return result;
}
String dataStr = dataScalar.toString();
byte[] input = dataStr.getBytes(StandardCharsets.ISO_8859_1);
inflater.setInput(input);
byte[] outputBuf = new byte[input.length * 4 + 1024];
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
int status = 0; // Z_OK
try {
while (!inflater.finished() && !inflater.needsInput()) {
int count = inflater.inflate(outputBuf);
if (count > 0) {
baos.write(outputBuf, 0, count);
} else if (count == 0 && !inflater.finished()) {
break;
}
}
if (inflater.finished()) {
status = 1; // Z_STREAM_END
}
} catch (DataFormatException e) {
RuntimeList result = new RuntimeList();
result.add(scalarUndef);
result.add(new RuntimeScalar(-3)); // Z_DATA_ERROR
return result;
}
String outputStr = baos.toString(StandardCharsets.ISO_8859_1);
RuntimeList result = new RuntimeList();
RuntimeScalar outputScalar = new RuntimeScalar(outputStr);
outputScalar.type = RuntimeScalarType.BYTE_STRING;
result.add(outputScalar);
result.add(new RuntimeScalar(status));
return result;
}
public static RuntimeList deflateMethod(RuntimeArray args, int ctx) {
if (args.size() < 2) {
return scalarUndef.getList();
}
RuntimeHash self = args.get(0).hashDeref();
RuntimeScalar dataScalar = args.get(1);
RuntimeScalar deflaterScalar = self.get(DEFLATER_KEY);
if (deflaterScalar == null || deflaterScalar.type != RuntimeScalarType.JAVAOBJECT
|| !(deflaterScalar.value instanceof Deflater deflater)) {
return scalarUndef.getList();
}
String dataStr = dataScalar.toString();
byte[] input = dataStr.getBytes(StandardCharsets.ISO_8859_1);
deflater.setInput(input);
byte[] outputBuf = new byte[input.length + 256];
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
int count;
while ((count = deflater.deflate(outputBuf, 0, outputBuf.length, Deflater.SYNC_FLUSH)) > 0) {
baos.write(outputBuf, 0, count);
}
String outputStr = baos.toString(StandardCharsets.ISO_8859_1);
RuntimeScalar outputScalar = new RuntimeScalar(outputStr);
outputScalar.type = RuntimeScalarType.BYTE_STRING;
return outputScalar.getList();
}
public static RuntimeList flush(RuntimeArray args, int ctx) {
if (args.isEmpty()) {
return scalarUndef.getList();
}
RuntimeHash self = args.get(0).hashDeref();
RuntimeScalar deflaterScalar = self.get(DEFLATER_KEY);
if (deflaterScalar == null || deflaterScalar.type != RuntimeScalarType.JAVAOBJECT
|| !(deflaterScalar.value instanceof Deflater deflater)) {
return scalarUndef.getList();
}
deflater.finish();
byte[] outputBuf = new byte[1024];
java.io.ByteArrayOutputStream baos = new java.io.ByteArrayOutputStream();
while (!deflater.finished()) {
int count = deflater.deflate(outputBuf);
if (count > 0) {
baos.write(outputBuf, 0, count);
} else {
break;
}
}
String outputStr = baos.toString(StandardCharsets.ISO_8859_1);
RuntimeScalar outputScalar = new RuntimeScalar(outputStr);
outputScalar.type = RuntimeScalarType.BYTE_STRING;
return outputScalar.getList();
}
/**
* gzopen($filename, $mode)
* Opens a gzip file for reading ('rb') or writing ('wb').
* Returns a blessed Compress::Zlib::gzFile object, or undef on error.
*/
public static RuntimeList gzopen(RuntimeArray args, int ctx) {
if (args.size() < 2) {
return scalarUndef.getList();
}
// Skip 'self' if called as Compress::Zlib->gzopen() (class method)
int argOffset = 0;
String firstArg = args.get(0).toString();
if (firstArg.equals("Compress::Zlib") || firstArg.contains("::")) {
argOffset = 1;
}
if (args.size() < argOffset + 2) {
return scalarUndef.getList();
}
String filename = args.get(argOffset).toString();
String mode = args.get(argOffset + 1).toString();
try {
RuntimeHash self = new RuntimeHash();
self.put("_mode", new RuntimeScalar(mode));
self.put("_eof", new RuntimeScalar(0));
if (mode.startsWith("r")) {
// Read mode
InputStream fis = new FileInputStream(filename);
GZIPInputStream gis = new GZIPInputStream(fis);
self.put("_stream", new RuntimeScalar(gis));
} else if (mode.startsWith("w")) {
// Write mode - check for compression level
OutputStream fos = new FileOutputStream(filename);
GZIPOutputStream gos = new GZIPOutputStream(fos);
self.put("_stream", new RuntimeScalar(gos));
} else {
return scalarUndef.getList();
}
RuntimeScalar ref = self.createReference();
ReferenceOperators.bless(ref, new RuntimeScalar("Compress::Zlib::gzFile"));
return ref.getList();
} catch (IOException e) {
return scalarUndef.getList();
}
}
}