omega-zip
omega-zip: ZIP archive reader in seeded Omega (store and deflate, CRC-32, fail-closed refusals)
Brought by drakestapleton
| 1 | note ======== src/main.om: the ozip command line ======== |
| 2 | |
| 3 | text tusage "usage: ozip crc32|inflate|zlib (stdin to stdout); ozip l|v|t ZIP; ozip p ZIP [NAME]; ozip x ZIP DIR [NAME]" |
| 4 | text tbadarg "bad arguments" |
| 5 | text topen "cannot open the zip file" |
| 6 | text tcrc32 "crc32" |
| 7 | text tinfl "inflate" |
| 8 | text tzlib "zlib" |
| 9 | text tl "l" |
| 10 | text tv "v" |
| 11 | text tt "t" |
| 12 | text tp "p" |
| 13 | text tx "x" |
| 14 | |
| 15 | note wout: the output sink that io.om's wflush calls with win[0 .. n). ozip writes the window to the |
| 16 | note output descriptor; a program that reuses io.om, inflate.om and unzip.om defines its own wout. |
| 17 | note It returns n (wflush assigns the result, so a missing wout is a compile error, not silence). |
| 18 | fn wout n ulong to ulong |
| 19 | woutfd n |
| 20 | ret n |
| 21 | end |
| 22 | |
| 23 | fn usage to int |
| 24 | warn tpref |
| 25 | warn tusage |
| 26 | warn tnl |
| 27 | ret 1 |
| 28 | end |
| 29 | |
| 30 | note argnm: argument i into want (the entry name asked for). |
| 31 | fn argnm i ulong to void |
| 32 | k = argread i want 0 4096 |
| 33 | if lt k 0 |
| 34 | fail 1 tbadarg |
| 35 | end |
| 36 | if gt k long NAMEMAX |
| 37 | fail 1 tbadarg |
| 38 | end |
| 39 | put wantn 0 ulong k |
| 40 | put wantf 0 1 |
| 41 | end |
| 42 | |
| 43 | note argdir: argument i into opath[0 .. dirn), the output directory; a trailing "/" is dropped. |
| 44 | fn argdir i ulong to void |
| 45 | k = argread i opath 0 4096 |
| 46 | if le k 0 |
| 47 | fail 1 tbadarg |
| 48 | end |
| 49 | n = ulong k |
| 50 | if gt n NAMEMAX |
| 51 | fail 1 tbadarg |
| 52 | end |
| 53 | if gt n 1 |
| 54 | if eq get opath sub n 1 47 |
| 55 | n = sub n 1 |
| 56 | end |
| 57 | end |
| 58 | put dirn 0 n |
| 59 | end |
| 60 | |
| 61 | note zipcmd: c 0 l, 1 v, 2 t, 3 p, 4 x; n arguments in all (the program name included). |
| 62 | fn zipcmd c ulong n ulong to int |
| 63 | zp = new uchar 4096 0 |
| 64 | k = argread 2 zp 0 4096 |
| 65 | if le k 0 |
| 66 | fail 1 tbadarg |
| 67 | end |
| 68 | if gt k long NAMEMAX |
| 69 | fail 1 tbadarg |
| 70 | end |
| 71 | fd = fopen zp 0 ulong k 0 0 |
| 72 | if lt fd 0 |
| 73 | fail 2 topen |
| 74 | end |
| 75 | put zfd 0 ulong fd |
| 76 | if lt c 3 |
| 77 | if ne n 3 |
| 78 | ret usage |
| 79 | end |
| 80 | end |
| 81 | if eq c 3 |
| 82 | if gt n 4 |
| 83 | ret usage |
| 84 | end |
| 85 | end |
| 86 | if eq c 4 |
| 87 | if lt n 4 |
| 88 | ret usage |
| 89 | end |
| 90 | if gt n 5 |
| 91 | ret usage |
| 92 | end |
| 93 | end |
| 94 | zopen |
| 95 | if eq c 0 |
| 96 | ret listing false part zp 0 ulong k |
| 97 | end |
| 98 | if eq c 1 |
| 99 | ret listing true part zp 0 ulong k |
| 100 | end |
| 101 | if eq c 2 |
| 102 | ret extract 2 |
| 103 | end |
| 104 | if eq c 3 |
| 105 | if eq n 4 |
| 106 | argnm 3 |
| 107 | end |
| 108 | ret extract 0 |
| 109 | end |
| 110 | argdir 3 |
| 111 | if eq n 5 |
| 112 | argnm 4 |
| 113 | end |
| 114 | ret extract 1 |
| 115 | end |
| 116 | |
| 117 | fn main to int |
| 118 | crcinit |
| 119 | tabinit |
| 120 | n = ulong argcnt |
| 121 | if lt n 2 |
| 122 | ret usage |
| 123 | end |
| 124 | a = new uchar 16 0 |
| 125 | k = argread 1 a 0 16 |
| 126 | if lt k 0 |
| 127 | ret usage |
| 128 | end |
| 129 | m = ulong k |
| 130 | if tx_eq part a 0 m tcrc32 |
| 131 | if ne n 2 |
| 132 | ret usage |
| 133 | end |
| 134 | ret crcrun |
| 135 | end |
| 136 | if tx_eq part a 0 m tinfl |
| 137 | if ne n 2 |
| 138 | ret usage |
| 139 | end |
| 140 | ret rawrun |
| 141 | end |
| 142 | if tx_eq part a 0 m tzlib |
| 143 | if ne n 2 |
| 144 | ret usage |
| 145 | end |
| 146 | ret zlibrun |
| 147 | end |
| 148 | if lt n 3 |
| 149 | ret usage |
| 150 | end |
| 151 | if tx_eq part a 0 m tl |
| 152 | ret zipcmd 0 n |
| 153 | end |
| 154 | if tx_eq part a 0 m tv |
| 155 | ret zipcmd 1 n |
| 156 | end |
| 157 | if tx_eq part a 0 m tt |
| 158 | ret zipcmd 2 n |
| 159 | end |
| 160 | if tx_eq part a 0 m tp |
| 161 | ret zipcmd 3 n |
| 162 | end |
| 163 | if tx_eq part a 0 m tx |
| 164 | ret zipcmd 4 n |
| 165 | end |
| 166 | ret usage |
| 167 | end |