egedit: do not save cursor movement in undo -- this is my stupid habit, and it comple...
[iv.d.git] / dlzma / test / dlzmatest_onecall.d
blobe9fd10c859ac6883a68cba0306d203557d827879
1 /* converted by Ketmar // Invisible Vector <ketmar@ketmar.no-ip.org>
2 * Understanding is not required. Only obedience.
4 * This program is free software. It comes without any warranty, to
5 * the extent permitted by applicable law. You can redistribute it
6 * and/or modify it under the terms of the Do What The Fuck You Want
7 * To Public License, Version 2, as published by Sam Hocevar. See
8 * http://www.wtfpl.net/txt/copying/ for more details.
9 */
10 module dlzmatest_onecall;
12 import iv.strex;
13 import iv.vfs;
14 import iv.vfs.io;
16 import iv.dlzma;
19 // ////////////////////////////////////////////////////////////////////////// //
20 void main (string[] args) {
21 if (args.length != 4) {
22 writeln("usage: fuckme <c|x> infile outfile");
23 return;
26 if (args[1] == "c") {
27 auto fi = VFile(args[2]);
28 usize insize = cast(usize)fi.size;
29 ubyte *inbuf = cast(ubyte*)ISzAlloc_Alloc(&lzmaDefAllocator, insize);
30 fi.rawReadExact(inbuf[0..insize]);
31 fi.close();
33 ubyte *outbuf = cast(ubyte*)ISzAlloc_Alloc(&lzmaDefAllocator, insize);
35 CLzmaEncProps props;
36 LzmaEncProps_Init(&props);
37 props.level = 9;
38 //props.dictSize = 1;
39 //while (props.dictSize < insize) props.dictSize <<= 1;
40 //props.dictSize = 1<<27; //128MB
41 props.dictSize = 1<<22; //4MB
42 props.reduceSize = insize;
44 ubyte[LZMA_PROPS_SIZE+8] header;
45 uint headerSize = cast(uint)header.sizeof;
47 writeln("compressing...");
48 usize destLen = insize;
49 SRes res = LzmaEncode(outbuf, &destLen, inbuf, insize, &props, header.ptr, &headerSize, 0/*writeEndMark*/, null, &lzmaDefAllocator, &lzmaDefAllocator);
50 ISzAlloc_Free(&lzmaDefAllocator, inbuf);
52 switch (res) {
53 case SZ_OK: break;
54 case SZ_ERROR_MEM: stderr.writeln("FUCK: memory"); return;
55 case SZ_ERROR_PARAM: stderr.writeln("FUCK: param"); return;
56 case SZ_ERROR_OUTPUT_EOF: stderr.writeln("FUCK: compressed is bigger"); return;
57 default: stderr.writeln("FUCK: something else"); return;
60 writeln("compressed ", intWithCommas(insize), " to ", intWithCommas(destLen), "; ratio: ", destLen*100U/insize, "%");
62 auto fo = VFile(args[3], "w");
63 fo.writeNum!ubyte(cast(ubyte)usize.sizeof);
64 fo.writeNum!ubyte(cast(ubyte)headerSize);
65 fo.writeNum!usize(insize);
66 fo.writeNum!usize(destLen);
67 fo.rawWriteExact(header[0..headerSize]);
68 fo.rawWriteExact(outbuf[0..destLen]);
69 fo.close();
71 ISzAlloc_Free(&lzmaDefAllocator, outbuf);
72 } else if (args[1] == "x") {
73 usize unsize = 0;
74 usize pksize = 0;
75 ubyte[LZMA_PROPS_SIZE+8] header;
76 uint headerSize = 0;
78 auto fi = VFile(args[2]);
79 if (fi.readNum!ubyte != cast(ubyte)usize.sizeof) throw new Exception("invalid arch");
80 headerSize = fi.readNum!ubyte;
81 if (!headerSize || headerSize > cast(uint)header.sizeof) throw new Exception("invalid header size");
82 unsize = fi.readNum!usize;
83 pksize = fi.readNum!usize;
84 fi.rawReadExact(header[0..headerSize]);
85 ubyte *inbuf = cast(ubyte*)ISzAlloc_Alloc(&lzmaDefAllocator, pksize);
86 fi.rawReadExact(inbuf[0..pksize]);
87 fi.close();
89 ubyte *outbuf = cast(ubyte*)ISzAlloc_Alloc(&lzmaDefAllocator, unsize);
90 usize srcLen = pksize;
91 usize destLen = unsize;
92 ELzmaStatus status = 0;
93 writeln("decompressing...");
94 int res = LzmaDecode(outbuf, &destLen, inbuf, &srcLen, header.ptr, headerSize, /*LZMA_FINISH_END*/LZMA_FINISH_ANY, &status, &lzmaDefAllocator);
95 ISzAlloc_Free(&lzmaDefAllocator, inbuf);
97 switch (res) {
98 case SZ_OK:
99 switch (status) {
100 case LZMA_STATUS_FINISHED_WITH_MARK: break;
101 case LZMA_STATUS_NOT_FINISHED: stderr.writeln("FUCK: unfinished"); return;
102 case LZMA_STATUS_MAYBE_FINISHED_WITHOUT_MARK: break; //stderr.writeln("FUCK: finished w/o mark"); return;
103 default: stderr.writeln("FUCK: fucked status"); return;
105 break;
106 case SZ_ERROR_DATA: stderr.writeln("FUCK: data error"); return;
107 case SZ_ERROR_MEM: stderr.writeln("FUCK: memory"); return;
108 case SZ_ERROR_UNSUPPORTED: stderr.writeln("FUCK: unsupported properties"); return;
109 case SZ_ERROR_INPUT_EOF: stderr.writeln("FUCK: out of compressed data"); return;
110 default: stderr.writeln("FUCK: something else"); return;
113 auto fo = VFile(args[3], "w");
114 fo.rawWriteExact(outbuf[0..destLen]);
115 fo.close();
117 ISzAlloc_Free(&lzmaDefAllocator, outbuf);