wok-6.x view xz/stuff/lzma @ rev 7616
lzma, xz: add unlzma & lzcat
author | Pascal Bellard <pascal.bellard@slitaz.org> |
---|---|
date | Sun Dec 12 13:55:48 2010 +0100 (2010-12-12) |
parents | 6a8dae495834 |
children | b07e53f0d08e |
line source
1 #!/bin/sh
2 #
3 # Wrapper to accept lzma_alone or lzma_utils arguments
4 #
6 usage()
7 {
8 cat 1>&2 <<EOT
9 Usage: LZMA <e|d> inputFile outputFile [<switches>...]
10 e: encode file
11 d: decode file
12 <Switches>
13 -a{N}: set compression mode - [0, 2], default: 1 (max)
14 -d{N}: set dictionary - [12,30], default: 23 (8MB)
15 -fb{N}: set number of fast bytes - [2, 273], default: 64
16 -mc{N}: set number of cycles for match finder
17 -lc{N}: set number of literal context bits - [0, 3], default: 3
18 -lp{N}: set number of literal pos bits - [0, 4], default: 0
19 -pb{N}: set number of pos bits - [0, 4], default: 2
20 -mf{MF_ID}: set Match Finder: [bt2, bt3, bt4, hc3, hc4], default: bt4
21 -eos: write End Of Stream marker
22 -si: read data from stdin
23 -so: write data to stdout
24 EOT
25 exit 0
26 }
28 # Get lzma_alone arg
29 getarg()
30 {
31 case "$1" in
32 -a0) mode="lzma1=mode=fast";;
33 -a1) mode="lzma1=mode=normal";;
34 -a2) mode="lzma2=mode=normal";;
35 -d*) extra="$extra,dict=$((2 ** ${1#-d}))";;
36 -fb*) extra="$extra,fb=${1#-fb}";;
37 -mc*) extra="$extra,depth=${1#-mc}";;
38 -lc*) extra="$extra,lc=${1#-lc}";;
39 -lp*) extra="$extra,lp=${1#-lp}";;
40 -pb*) extra="$extra,pb=${1#-pb}";;
41 -mf*) extra="$extra,mf=${1#-mf}";;
42 -eos|-mt*) ;;
43 -si) output="> ${input#< }"; input="";;
44 -so) output="";;
45 *) return 1;;
46 esac
47 return 0
48 }
50 lzma_utils()
51 {
52 args="--format=lzma"
53 files=""
54 suffix=lzma
55 while [ -n "$1" ]; do
56 case "$1" in
57 -e) args="$args -z";;
58 -d|-k|-[0-9]) args="$args $1";;
59 --fast) args="$args -1";;
60 --best) args="$args -9";;
61 -c|--stdout|--to-stdout) args="$args -c";;
62 -S) suffix=${2#.}; shift;;
63 -*);;
64 *) files="$files $1";;
65 esac
66 shift
67 done
68 for i in $files; do
69 xz $args $i || exit
70 [ "lzma" == "$suffix" ] || mv ${i%.*}.lzma ${i%.*}.$suffix
71 done
72 exit
73 }
75 case "$0" in
76 *unlzma) lzma_utils -d $@ ;;
77 *lzcat) lzma_utils -c -d $@ ;;
78 esac
80 # lzma_utils or lzma_alone ?
81 [ -n "$3" ] || lzma_utils $@
82 case "$1" in
83 d) args="-d";;
84 e) args="-z";;
85 --version|-V|--help|-h|-\?|'') usage;;
86 *) lzma_utils $@;;
87 esac
89 # it's lzma_alone
90 # get filenames
91 input="< $2"
92 output="> $2"
93 shift
94 if ! getarg "$1"; then
95 output="> $2"
96 getarg "$2"
97 shift
98 fi
100 # get arguments
101 extra=""
102 mode="lzma1=mode=normal"
103 while [ -n "$2" ]; do
104 getarg "$2" || usage
105 shift
106 done
108 # fake lzma_alone (-eos -mt1)
109 eval xz $args --format=lzma --$mode$extra --stdout $input $output
110 status=$?
111 if [ -n "$input" -a -n "$output" ]; then
112 size=$(stat -c "%s" ${input#< })
113 ascii="00000 "
114 for i in 1 2 3 4 5 6 7 8; do
115 ascii="$ascii $(printf "%02x" $((size % 256)) )"
116 size=$(( $size / 256 ))
117 done
118 echo "$ascii | .." | hexdump -R | \
119 dd of=${output#> } seek=5 bs=1 conv=notrunc 2> /dev/null
120 fi
121 exit $status