wok-6.x view xz/stuff/lzma @ rev 12985

Move from undigest enna gnaughty open-cobol
author Pascal Bellard <pascal.bellard@slitaz.org>
date Sat Jun 02 17:09:50 2012 +0200 (2012-06-02)
parents 885c330a75ba
children 02c70d036ea0
line source
1 #!/bin/sh
2 #
3 # Wrapper to accept lzma_alone or lzma_utils arguments
4 #
6 usage()
7 {
8 status=$?
9 cat 1>&2 <<EOT
10 Usage: LZMA <e|d> inputFile outputFile [<switches>...]
11 e: encode file
12 d: decode file
13 <Switches>
14 -a{N}: set compression mode - [0, 2], default: 1 (max)
15 -d{N}: set dictionary - [12,30], default: 23 (8MB)
16 -fb{N}: set number of fast bytes - [2, 273], default: 64
17 -mc{N}: set number of cycles for match finder
18 -lc{N}: set number of literal context bits - [0, 3], default: 3
19 -lp{N}: set number of literal pos bits - [0, 4], default: 0
20 -pb{N}: set number of pos bits - [0, 4], default: 2
21 -mf{MF_ID}: set Match Finder: [bt2, bt3, bt4, hc3, hc4], default: bt4
22 -eos: write End Of Stream marker
23 -si: read data from stdin
24 -so: write data to stdout
25 EOT
26 if [ "$1" = error ]; then
27 echo -e "\nError"
28 exit $status
29 else
30 exit 0
31 fi
32 }
34 # Get lzma_alone arg
35 getarg()
36 {
37 case "$1" in
38 -a0) mode="lzma1=mode=fast";;
39 -a1) mode="lzma1=mode=normal";;
40 -a2) mode="lzma2=mode=normal";;
41 -d*) extra="$extra,dict=$((2 ** ${1#-d}))";;
42 -fb*) extra="$extra,fb=${1#-fb}";;
43 -mc*) extra="$extra,depth=${1#-mc}";;
44 -lc*) extra="$extra,lc=${1#-lc}";;
45 -lp*) extra="$extra,lp=${1#-lp}";;
46 -pb*) extra="$extra,pb=${1#-pb}";;
47 -mf*) extra="$extra,mf=${1#-mf}";;
48 -eos|-mt*) ;;
49 -si) output="> ${input#< }"; input="";;
50 -so) output="";;
51 *) return 1;;
52 esac
53 return 0
54 }
56 lzma_utils()
57 {
58 args="--format=lzma"
59 files=""
60 suffix=lzma
61 while [ -n "$1" ]; do
62 case "$1" in
63 -e) args="$args -z";;
64 -d|-k|-[0-9]) args="$args $1";;
65 --fast) args="$args -1";;
66 --best) args="$args -9";;
67 -c|--stdout|--to-stdout) args="$args -c";;
68 -S) suffix=${2#.}; shift;;
69 -*);;
70 *) files="$files $1";;
71 esac
72 shift
73 done
74 for i in $files; do
75 xz $args $i || usage error
76 [ "lzma" == "$suffix" ] || mv ${i%.*}.lzma ${i%.*}.$suffix
77 done
78 exit
79 }
81 fix_size()
82 {
83 n=$1
84 for i in $(seq 1 8); do
85 printf '\\\\x%02X' $(($n & 255))
86 n=$(($n >> 8))
87 done | xargs echo -en | dd of=$2 conv=notrunc bs=1 seek=5 2> /dev/null
88 }
90 case "$0" in
91 *unlzma) lzma_utils -d $@ ;;
92 *lzcat) lzma_utils -c -d $@ ;;
93 esac
95 # First look if help is needed.
96 case "$1" in
97 --version|-V|--help|-h|-\?|'') usage;;
98 --size) size=$(od -t d8 -j5 -N8 < $2 | awk '{ print $2; exit }')
99 if [ $size -eq -1 ]; then
100 size=$(unlzma -c $2 | wc -c)
101 fix_size $size $2
102 fi
103 echo $size
104 exit ;;
105 esac
107 # lzma_utils or lzma_alone ?
108 [ -n "$3" ] || lzma_utils $@
109 case "$1" in
110 d) args="-d";;
111 e) args="-z";;
112 *) lzma_utils $@;;
113 esac
115 # it's lzma_alone
116 # get filenames
117 input="< $2"
118 output="> $2"
119 shift
120 if ! getarg "$1"; then
121 output="> $2"
122 getarg "$2"
123 shift
124 fi
126 # get arguments
127 extra=""
128 mode="lzma1=mode=normal"
129 while [ -n "$2" ]; do
130 getarg "$2" || usage error
131 shift
132 done
134 # fake lzma_alone (-eos -mt1)
135 eval xz $args --format=lzma --$mode$extra --stdout $input $output || usage error
136 [ -n "$input" -a -n "$output" ] &&
137 fix_size $(stat -c "%s" ${input#< }) ${output#> }
138 exit 0