piclaw 0.0.20 → 0.0.21

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (245) hide show
  1. package/.output/nitro.json +1 -1
  2. package/.output/public/assets/defult-D5RLDUrI.js +1 -0
  3. package/.output/public/assets/{dist-D-Hc5HbQ.js → dist-BH_oa-kv.js} +1 -1
  4. package/.output/public/assets/index-7JvURuHy.js +204 -0
  5. package/.output/public/assets/index-K43slwjJ.css +1 -0
  6. package/.output/public/index.html +11 -2
  7. package/.output/server/_chunks/app.mjs +138 -104
  8. package/.output/server/_chunks/config.mjs +4 -0
  9. package/.output/server/_chunks/dummy.mjs +1 -1
  10. package/.output/server/_chunks/logger.mjs +1 -1
  11. package/.output/server/_chunks/notes.mjs +1 -3
  12. package/.output/server/_chunks/renderer-template.mjs +1 -1
  13. package/.output/server/_chunks/sandbox.mjs +217 -0
  14. package/.output/server/_chunks/server.mjs +411 -291
  15. package/.output/server/_chunks/terminal.mjs +47 -8
  16. package/.output/server/_chunks/virtual.mjs +192 -54
  17. package/.output/server/_id_.delete.mjs +5 -2
  18. package/.output/server/_id_2.delete.mjs +8 -0
  19. package/.output/server/_jid_.delete.mjs +0 -1
  20. package/.output/server/_jid_.patch.mjs +21 -3
  21. package/.output/server/_jid_2.delete.mjs +0 -1
  22. package/.output/server/_libs/@acemir/cssom+[...].mjs +2269 -1137
  23. package/.output/server/_libs/@google/genai.mjs +348 -284
  24. package/.output/server/_libs/@mariozechner/pi-agent-core+[...].mjs +381 -2073
  25. package/.output/server/_libs/@mariozechner/pi-coding-agent+[...].mjs +236 -136
  26. package/.output/server/_libs/_.mjs +3 -2
  27. package/.output/server/_libs/_10.mjs +2 -4
  28. package/.output/server/_libs/_11.mjs +2 -4
  29. package/.output/server/_libs/_12.mjs +2 -3
  30. package/.output/server/_libs/_13.mjs +2 -3
  31. package/.output/server/_libs/_14.mjs +2 -4
  32. package/.output/server/_libs/_15.mjs +2 -4
  33. package/.output/server/_libs/_16.mjs +2 -3
  34. package/.output/server/_libs/_17.mjs +2 -4
  35. package/.output/server/_libs/_18.mjs +2 -2
  36. package/.output/server/_libs/_19.mjs +2 -2
  37. package/.output/server/_libs/_2.mjs +3 -3
  38. package/.output/server/_libs/_20.mjs +2 -2
  39. package/.output/server/_libs/_21.mjs +2 -2
  40. package/.output/server/_libs/_22.mjs +2 -2
  41. package/.output/server/_libs/_23.mjs +2 -2
  42. package/.output/server/_libs/_24.mjs +2 -2
  43. package/.output/server/_libs/_25.mjs +2 -2
  44. package/.output/server/_libs/_26.mjs +2 -2
  45. package/.output/server/_libs/_27.mjs +2 -2
  46. package/.output/server/_libs/_28.mjs +2 -2
  47. package/.output/server/_libs/_29.mjs +2 -2
  48. package/.output/server/_libs/_3.mjs +3 -3
  49. package/.output/server/_libs/_30.mjs +2 -2
  50. package/.output/server/_libs/_31.mjs +2 -2
  51. package/.output/server/_libs/_32.mjs +2 -2
  52. package/.output/server/_libs/_33.mjs +2 -2
  53. package/.output/server/_libs/_34.mjs +2 -2
  54. package/.output/server/_libs/_35.mjs +2 -2
  55. package/.output/server/_libs/_36.mjs +2 -2
  56. package/.output/server/_libs/_37.mjs +2 -2
  57. package/.output/server/_libs/_38.mjs +2 -2
  58. package/.output/server/_libs/_39.mjs +2 -2
  59. package/.output/server/_libs/_4.mjs +4 -3
  60. package/.output/server/_libs/_40.mjs +2 -2
  61. package/.output/server/_libs/_41.mjs +2 -2
  62. package/.output/server/_libs/_42.mjs +2 -2
  63. package/.output/server/_libs/_43.mjs +2 -2
  64. package/.output/server/_libs/_44.mjs +2 -2
  65. package/.output/server/_libs/_45.mjs +2 -2
  66. package/.output/server/_libs/_46.mjs +2 -2
  67. package/.output/server/_libs/_47.mjs +2 -2
  68. package/.output/server/_libs/_48.mjs +2 -2
  69. package/.output/server/_libs/_49.mjs +2 -2
  70. package/.output/server/_libs/_5.mjs +2 -3
  71. package/.output/server/_libs/_50.mjs +2 -2
  72. package/.output/server/_libs/_51.mjs +2 -2
  73. package/.output/server/_libs/_52.mjs +2 -2
  74. package/.output/server/_libs/_53.mjs +2 -2
  75. package/.output/server/_libs/_54.mjs +2 -2
  76. package/.output/server/_libs/_55.mjs +2 -2
  77. package/.output/server/_libs/_56.mjs +2 -2
  78. package/.output/server/_libs/_57.mjs +2 -2
  79. package/.output/server/_libs/_58.mjs +2 -2
  80. package/.output/server/_libs/_59.mjs +2 -2
  81. package/.output/server/_libs/_6.mjs +2 -3
  82. package/.output/server/_libs/_60.mjs +2 -2
  83. package/.output/server/_libs/_61.mjs +2 -2
  84. package/.output/server/_libs/_62.mjs +2 -2
  85. package/.output/server/_libs/_63.mjs +2 -2
  86. package/.output/server/_libs/_64.mjs +2 -2
  87. package/.output/server/_libs/_65.mjs +2 -2
  88. package/.output/server/_libs/_66.mjs +2 -2
  89. package/.output/server/_libs/_67.mjs +2 -2
  90. package/.output/server/_libs/_68.mjs +2 -2
  91. package/.output/server/_libs/_69.mjs +2 -2
  92. package/.output/server/_libs/_7.mjs +2 -5
  93. package/.output/server/_libs/_70.mjs +2 -2
  94. package/.output/server/_libs/_71.mjs +2 -2
  95. package/.output/server/_libs/_72.mjs +2 -2
  96. package/.output/server/_libs/_73.mjs +2 -2
  97. package/.output/server/_libs/_74.mjs +2 -2
  98. package/.output/server/_libs/_75.mjs +2 -2
  99. package/.output/server/_libs/_76.mjs +2 -2
  100. package/.output/server/_libs/_77.mjs +2 -2
  101. package/.output/server/_libs/_78.mjs +2 -2
  102. package/.output/server/_libs/_79.mjs +2 -2
  103. package/.output/server/_libs/_8.mjs +2 -3
  104. package/.output/server/_libs/_80.mjs +2 -2
  105. package/.output/server/_libs/_81.mjs +2 -2
  106. package/.output/server/_libs/_82.mjs +2 -2
  107. package/.output/server/_libs/_83.mjs +2 -2
  108. package/.output/server/_libs/_84.mjs +2 -2
  109. package/.output/server/_libs/_85.mjs +2 -2
  110. package/.output/server/_libs/_86.mjs +2 -2
  111. package/.output/server/_libs/_87.mjs +2 -2
  112. package/.output/server/_libs/_88.mjs +2 -2
  113. package/.output/server/_libs/_89.mjs +2 -2
  114. package/.output/server/_libs/_9.mjs +2 -4
  115. package/.output/server/_libs/_90.mjs +5 -2
  116. package/.output/server/_libs/_91.mjs +3 -2
  117. package/.output/server/_libs/_92.mjs +2 -2
  118. package/.output/server/_libs/_93.mjs +2 -2
  119. package/.output/server/_libs/_94.mjs +2 -2
  120. package/.output/server/_libs/agent-base.mjs +1 -1
  121. package/.output/server/_libs/cheerio+[...].mjs +1 -1
  122. package/.output/server/_libs/data-uri-to-buffer.mjs +2 -67
  123. package/.output/server/_libs/data-urls+[...].mjs +1 -1
  124. package/.output/server/_libs/diff.mjs +1 -1
  125. package/.output/server/_libs/exodus__bytes.mjs +99 -81
  126. package/.output/server/_libs/fetch-blob+node-domexception.mjs +1 -1
  127. package/.output/server/_libs/h3+rou3+srvx.mjs +34 -4
  128. package/.output/server/_libs/html-encoding-sniffer.mjs +1 -1
  129. package/.output/server/_libs/https-proxy-agent.mjs +2 -2
  130. package/.output/server/_libs/jsdom.mjs +1 -1
  131. package/.output/server/_libs/just-bash+[...].mjs +4676 -3916
  132. package/.output/server/_libs/mariozechner__jiti.mjs +1 -1
  133. package/.output/server/_libs/mariozechner__pi-ai.mjs +1472 -0
  134. package/.output/server/_libs/md4x.mjs +1 -1
  135. package/.output/server/_libs/node-fetch.mjs +14 -14
  136. package/.output/server/_libs/node-liblzma.mjs +1 -1
  137. package/.output/server/_libs/silvia-odwyer__photon-node.mjs +1 -1
  138. package/.output/server/_routes/api/auth/status.mjs +25 -6
  139. package/.output/server/_routes/api/config2.mjs +2 -0
  140. package/.output/server/_routes/api/files/groups.mjs +0 -1
  141. package/.output/server/_routes/api/groups.mjs +4 -2
  142. package/.output/server/_routes/api/groups2.mjs +14 -5
  143. package/.output/server/_routes/api/health.mjs +0 -1
  144. package/.output/server/_routes/api/pi/apikey.mjs +0 -1
  145. package/.output/server/_routes/api/pi/apikey_providers.mjs +0 -1
  146. package/.output/server/_routes/api/pi/commands.mjs +1 -2
  147. package/.output/server/_routes/api/pi/login/events.mjs +0 -1
  148. package/.output/server/_routes/api/pi/login/respond.mjs +0 -1
  149. package/.output/server/_routes/api/pi/login.mjs +0 -1
  150. package/.output/server/_routes/api/pi/logout.mjs +0 -1
  151. package/.output/server/_routes/api/pi/models.mjs +0 -1
  152. package/.output/server/_routes/api/pi/status.mjs +0 -1
  153. package/.output/server/_routes/api/sandbox.mjs +26 -0
  154. package/.output/server/_routes/api/sandbox2.mjs +17 -0
  155. package/.output/server/_routes/api/send.mjs +12 -12
  156. package/.output/server/_routes/api/status.mjs +0 -1
  157. package/.output/server/_routes/api/stop.mjs +0 -1
  158. package/.output/server/_routes/api/tasks2.mjs +0 -1
  159. package/.output/server/_routes/api/telegram/setup.mjs +0 -1
  160. package/.output/server/_routes/api/telegram/status.mjs +0 -1
  161. package/.output/server/_routes/api/terminal2.mjs +2 -1
  162. package/.output/server/_routes/api/tunnel/setup.mjs +0 -1
  163. package/.output/server/_runtime.mjs +1 -2
  164. package/.output/server/index.mjs +1 -1
  165. package/.output/server/node_modules/amdefine/amdefine.js +301 -0
  166. package/.output/server/node_modules/amdefine/package.json +16 -0
  167. package/.output/server/node_modules/compressjs/lib/BWT.js +420 -0
  168. package/.output/server/node_modules/compressjs/lib/BWTC.js +234 -0
  169. package/.output/server/node_modules/compressjs/lib/BitStream.js +108 -0
  170. package/.output/server/node_modules/compressjs/lib/Bzip2.js +936 -0
  171. package/.output/server/node_modules/compressjs/lib/CRC32.js +105 -0
  172. package/.output/server/node_modules/compressjs/lib/Context1Model.js +56 -0
  173. package/.output/server/node_modules/compressjs/lib/DefSumModel.js +152 -0
  174. package/.output/server/node_modules/compressjs/lib/DeflateDistanceModel.js +55 -0
  175. package/.output/server/node_modules/compressjs/lib/Dmc.js +197 -0
  176. package/.output/server/node_modules/compressjs/lib/DummyRangeCoder.js +81 -0
  177. package/.output/server/node_modules/compressjs/lib/FenwickModel.js +194 -0
  178. package/.output/server/node_modules/compressjs/lib/Huffman.js +514 -0
  179. package/.output/server/node_modules/compressjs/lib/HuffmanAllocator.js +227 -0
  180. package/.output/server/node_modules/compressjs/lib/LogDistanceModel.js +46 -0
  181. package/.output/server/node_modules/compressjs/lib/Lzjb.js +300 -0
  182. package/.output/server/node_modules/compressjs/lib/LzjbR.js +241 -0
  183. package/.output/server/node_modules/compressjs/lib/Lzp3.js +273 -0
  184. package/.output/server/node_modules/compressjs/lib/MTFModel.js +208 -0
  185. package/.output/server/node_modules/compressjs/lib/NoModel.js +46 -0
  186. package/.output/server/node_modules/compressjs/lib/PPM.js +343 -0
  187. package/.output/server/node_modules/compressjs/lib/RangeCoder.js +238 -0
  188. package/.output/server/node_modules/compressjs/lib/Simple.js +111 -0
  189. package/.output/server/node_modules/compressjs/lib/Stream.js +53 -0
  190. package/.output/server/node_modules/compressjs/lib/Util.js +324 -0
  191. package/.output/server/node_modules/compressjs/lib/freeze.js +14 -0
  192. package/.output/server/node_modules/compressjs/main.js +29 -0
  193. package/.output/server/node_modules/compressjs/package.json +35 -0
  194. package/.output/server/package.json +2 -1
  195. package/README.md +10 -1
  196. package/lib/index.d.mts +1 -0
  197. package/lib/index.mjs +1 -0
  198. package/lib/piclaw.mjs +100 -0
  199. package/lib/utils.mjs +96 -0
  200. package/package.json +16 -11
  201. package/.output/public/assets/defult-DtwgaiMA.js +0 -1
  202. package/.output/public/assets/index-B5n0eraW.css +0 -1
  203. package/.output/public/assets/index-DUbn6fuj.js +0 -205
  204. package/.output/server/_libs/@aws-crypto/crc32+[...].mjs +0 -299
  205. package/.output/server/_libs/@aws-sdk/client-bedrock-runtime+[...].mjs +0 -17828
  206. package/.output/server/_libs/@aws-sdk/credential-provider-http+[...].mjs +0 -122
  207. package/.output/server/_libs/@aws-sdk/credential-provider-ini+[...].mjs +0 -417
  208. package/.output/server/_libs/@aws-sdk/credential-provider-process+[...].mjs +0 -54
  209. package/.output/server/_libs/@aws-sdk/credential-provider-sso+[...].mjs +0 -1151
  210. package/.output/server/_libs/@aws-sdk/credential-provider-web-identity+[...].mjs +0 -50
  211. package/.output/server/_libs/@smithy/credential-provider-imds+[...].mjs +0 -369
  212. package/.output/server/_libs/@tootallnate/quickjs-emscripten+[...].mjs +0 -3011
  213. package/.output/server/_libs/_100.mjs +0 -2
  214. package/.output/server/_libs/_101.mjs +0 -2
  215. package/.output/server/_libs/_102.mjs +0 -2
  216. package/.output/server/_libs/_103.mjs +0 -5
  217. package/.output/server/_libs/_104.mjs +0 -3
  218. package/.output/server/_libs/_105.mjs +0 -2
  219. package/.output/server/_libs/_106.mjs +0 -3
  220. package/.output/server/_libs/_107.mjs +0 -2
  221. package/.output/server/_libs/_108.mjs +0 -2
  222. package/.output/server/_libs/_95.mjs +0 -2
  223. package/.output/server/_libs/_96.mjs +0 -2
  224. package/.output/server/_libs/_97.mjs +0 -2
  225. package/.output/server/_libs/_98.mjs +0 -2
  226. package/.output/server/_libs/_99.mjs +0 -2
  227. package/.output/server/_libs/amdefine.mjs +0 -188
  228. package/.output/server/_libs/ast-types.mjs +0 -2270
  229. package/.output/server/_libs/aws-sdk__nested-clients.mjs +0 -3141
  230. package/.output/server/_libs/basic-ftp.mjs +0 -1906
  231. package/.output/server/_libs/compressjs.mjs +0 -50
  232. package/.output/server/_libs/degenerator+[...].mjs +0 -9964
  233. package/.output/server/_libs/get-uri.mjs +0 -413
  234. package/.output/server/_libs/http-proxy-agent.mjs +0 -123
  235. package/.output/server/_libs/ip-address.mjs +0 -1423
  236. package/.output/server/_libs/lru-cache.mjs +0 -732
  237. package/.output/server/_libs/netmask.mjs +0 -139
  238. package/.output/server/_libs/pac-proxy-agent+[...].mjs +0 -3104
  239. package/.output/server/_libs/proxy-agent+proxy-from-env.mjs +0 -204
  240. package/.output/server/_libs/smithy__core.mjs +0 -192
  241. package/.output/server/node_modules/tslib/modules/index.js +0 -70
  242. package/.output/server/node_modules/tslib/modules/package.json +0 -3
  243. package/.output/server/node_modules/tslib/package.json +0 -47
  244. package/.output/server/node_modules/tslib/tslib.js +0 -484
  245. package/bin/piclaw.mjs +0 -195
@@ -0,0 +1,241 @@
1
+ /* Tweaked version of LZJB, using range coder. */
2
+ if (typeof define !== 'function') { var define = require('amdefine')(module); }
3
+ define(['./Context1Model','./FenwickModel','./LogDistanceModel','./NoModel','./RangeCoder','./Stream','./Util'],function(Context1Model,FenwickModel,LogDistanceModel,NoModel,RangeCoder,Stream,Util){
4
+
5
+ var LzjbR = Object.create(null);
6
+ LzjbR.MAGIC = 'lzjR';
7
+
8
+ // Constants was used for compress/decompress function.
9
+ var NBBY = 8,
10
+ MATCH_BITS = 6,
11
+ MATCH_MIN = 3,
12
+ MATCH_MAX = ((1 << MATCH_BITS) + (MATCH_MIN - 1)),
13
+ OFFSET_MASK = ((1 << (16 - MATCH_BITS)) - 1),
14
+ LEMPEL_SIZE_BASE = 1024;
15
+ var LENGTH_MODEL_CUTOFF = 32;
16
+
17
+
18
+ /**
19
+ * Compress using modified LZJB algorithm. Instead of using the simple
20
+ * 9-bit literal / 17-bit match format of the original, use a range
21
+ * coder for the literal/match bit and for the offset and length.
22
+ */
23
+ LzjbR.compressFile = Util.compressFileHelper(LzjbR.MAGIC, function(inStream, outStream, fileSize, props, finalByte) {
24
+ var sstart, dstart = [], slen,
25
+ src = 0, dst = 0,
26
+ cpy, copymap,
27
+ mlen, offset,
28
+ hash, hp,
29
+ lempel,
30
+ i, j;
31
+
32
+ // in an improvement over the original C implementation of LZJB, we expand
33
+ // the hash table to track a number of potential matches, not just the
34
+ // most recent. This doesn't require any changes to the decoder.
35
+ var LEMPEL_SIZE = LEMPEL_SIZE_BASE;
36
+ var EXPAND = 1; // default to original C impl
37
+ if (typeof(props)==='number') {
38
+ LEMPEL_SIZE *= 2;
39
+ props = Math.max(1, Math.min(9, props)) - 1;
40
+ EXPAND = 1<<Math.floor(props/2);
41
+ if (props&1) EXPAND = Math.round(EXPAND * 1.5);
42
+ if (props >=2 && props <= 4) EXPAND++;
43
+ }
44
+
45
+ var encoder = new RangeCoder(outStream);
46
+ encoder.encodeStart(finalByte, 1);
47
+
48
+ // use Uint16Array if available (zero-filled)
49
+ lempel = Util.makeU16Buffer(LEMPEL_SIZE * EXPAND);
50
+
51
+ var window = Util.makeU8Buffer(OFFSET_MASK+1);
52
+ var windowpos = 0;
53
+ var winput = function(_byte) {
54
+ window[windowpos++] = _byte;
55
+ if (windowpos >= window.length) {
56
+ windowpos = 0;
57
+ }
58
+ return _byte;
59
+ };
60
+
61
+ var unbuffer = [];
62
+ var get = function() {
63
+ if (unbuffer.length)
64
+ return unbuffer.pop();
65
+ return inStream.readByte();
66
+ };
67
+ var unget = function(_byte) {
68
+ unbuffer.push(_byte);
69
+ };
70
+
71
+ var matchpossibility = [];
72
+ var MATCH = 256;
73
+ var EOF_SYM = 257;
74
+ var noModelFactory = NoModel.factory(encoder);
75
+ var modelFactory = FenwickModel.factory(encoder, 0xFF00, 0x100);
76
+ var literalModel = new Context1Model(modelFactory, 256,
77
+ ((fileSize<0) ? EOF_SYM : MATCH) + 1);
78
+ var sparseModelFactory = function(size) {
79
+ if (size <= LENGTH_MODEL_CUTOFF) { return modelFactory(size); }
80
+ return noModelFactory(size);
81
+ };
82
+ var lenModel = new LogDistanceModel((MATCH_MAX-MATCH_MIN)+1, 0,
83
+ modelFactory, sparseModelFactory);
84
+ var posModel = new LogDistanceModel(OFFSET_MASK+1, 1,
85
+ modelFactory, sparseModelFactory);
86
+ var lastChar = 0x20, lastOffset = 0;
87
+ while (true) {
88
+ var initialPos = windowpos;
89
+ var c1 = get();
90
+ if (c1 === Stream.EOF) break;
91
+
92
+ var c2 = get();
93
+ if (c2 === Stream.EOF) {
94
+ literalModel.encode(winput(c1), lastChar); // literal, not a match
95
+ break;
96
+ }
97
+ var c3 = get();
98
+ if (c3 === Stream.EOF) {
99
+ literalModel.encode(winput(c1), lastChar); // literal, not a match
100
+ unget(c2); lastChar = c1;
101
+ continue;
102
+ }
103
+
104
+ hash = (c1 << 16) + (c2 << 8) + c3;
105
+ hash ^= (hash >> 9);
106
+ hash += (hash >> 5);
107
+ hash ^= c1;
108
+ hp = (hash & (LEMPEL_SIZE - 1)) * EXPAND;
109
+ matchpossibility.length = 0;
110
+ for (j=0; j<EXPAND; j++) {
111
+ offset = (windowpos - lempel[hp+j]) & OFFSET_MASK;
112
+ cpy = window.length + windowpos - offset;
113
+ var w1 = window[cpy & OFFSET_MASK];
114
+ var w2 = window[(cpy+1) & OFFSET_MASK];
115
+ var w3 = window[(cpy+2) & OFFSET_MASK];
116
+ // if offset is small, we might not have copied the tentative
117
+ // bytes into the window yet. (Note that offset=0 really means
118
+ // offset=(OFFSET_MASK+1).)
119
+ if (offset==1) { w2 = c1; w3 = c2; }
120
+ else if (offset==2) { w3 = c1; }
121
+ if (c1 === w1 && c2 === w2 && c3 === w3) {
122
+ matchpossibility.push(offset);
123
+ }
124
+ }
125
+ // store this location in the hash, move the others over to make room
126
+ // oldest match drops off
127
+ for (j=EXPAND-1; j>0; j--)
128
+ lempel[hp+j] = lempel[hp+j-1];
129
+ lempel[hp] = windowpos;
130
+ // did we find any matches?
131
+ if (matchpossibility.length === 0) {
132
+ literalModel.encode(winput(c1), lastChar); // literal, not a match
133
+ unget(c3);
134
+ unget(c2);
135
+ lastChar = c1;
136
+ } else {
137
+ literalModel.encode(MATCH, lastChar); // a match!
138
+ // find the longest of the possible matches
139
+ winput(c1); winput(c2); winput(c3); lastChar = c3;
140
+ var c4 = get(), last = matchpossibility[0];
141
+ var base = window.length + windowpos;
142
+ for (mlen = MATCH_MIN; mlen < MATCH_MAX; mlen++, base++) {
143
+ if (c4 === Stream.EOF) break;
144
+ for (j=0; j < matchpossibility.length; ) {
145
+ var w4 = window[(base - matchpossibility[j]) & OFFSET_MASK];
146
+ if (c4 !== w4) {
147
+ last = matchpossibility[j];
148
+ matchpossibility.splice(j, 1);
149
+ } else {
150
+ j++;
151
+ }
152
+ }
153
+ if (matchpossibility.length===0) break; // no more matches
154
+ winput(c4); lastChar = c4;
155
+ c4 = get();
156
+ }
157
+ if (matchpossibility.length !== 0) {
158
+ // maximum length match, rock on!
159
+ last = matchpossibility[0];
160
+ }
161
+ unget(c4);
162
+
163
+ // encode match length
164
+ // XXX we could get a bit more compression if we allowed
165
+ // the length to predict the offset (or vice-versa)
166
+ lenModel.encode(mlen - MATCH_MIN);
167
+ offset = (initialPos - last) & OFFSET_MASK;
168
+ if (offset === lastOffset) {
169
+ posModel.encode(-1); // common case!
170
+ } else {
171
+ posModel.encode(offset);
172
+ lastOffset = offset;
173
+ }
174
+ }
175
+ }
176
+ if (fileSize < 0) {
177
+ literalModel.encode(EOF_SYM, lastChar); // end of file (streaming)
178
+ }
179
+ encoder.encodeFinish();
180
+ }, true);
181
+
182
+ /**
183
+ * Decompress using modified LZJB algorithm.
184
+ */
185
+ LzjbR.decompressFile = Util.decompressFileHelper(LzjbR.MAGIC, function(inStream, outStream, outSize) {
186
+ var sstart, dstart = [], slen,
187
+ src = 0, dst = 0,
188
+ cpy, copymap,
189
+ mlen, offset,
190
+ i, c;
191
+
192
+ var window = Util.makeU8Buffer(OFFSET_MASK+1);
193
+ var windowpos = 0;
194
+
195
+ var decoder = new RangeCoder(inStream);
196
+ decoder.decodeStart(true/* we already read the 'free' byte*/);
197
+
198
+ var MATCH = 256;
199
+ var EOF_SYM = 257;
200
+ var noModelFactory = NoModel.factory(decoder);
201
+ var modelFactory = FenwickModel.factory(decoder, 0xFF00, 0x100);
202
+ var literalModel = new Context1Model(modelFactory, 256,
203
+ ((outSize<0) ? EOF_SYM : MATCH) + 1);
204
+ var sparseModelFactory = function(size) {
205
+ if (size <= LENGTH_MODEL_CUTOFF) { return modelFactory(size); }
206
+ return noModelFactory(size);
207
+ };
208
+ var lenModel = new LogDistanceModel((MATCH_MAX-MATCH_MIN)+1, 0,
209
+ modelFactory, sparseModelFactory);
210
+ var posModel = new LogDistanceModel(OFFSET_MASK+1, 1,
211
+ modelFactory, sparseModelFactory);
212
+ var lastChar = 0x20, lastOffset = 0;
213
+ while (outSize !== 0) {
214
+ c = literalModel.decode(lastChar);
215
+ if (c === EOF_SYM) {
216
+ break;
217
+ } else if (c === MATCH) {
218
+ mlen = lenModel.decode() + MATCH_MIN;
219
+ cpy = posModel.decode();
220
+ if (cpy<0) { cpy = lastOffset; }
221
+ else { lastOffset = cpy; }
222
+ if (outSize >= 0) outSize -= mlen;
223
+ while (--mlen >= 0) {
224
+ c = lastChar = window[windowpos++] = window[cpy++];
225
+ outStream.writeByte(c);
226
+ if (windowpos >= window.length) { windowpos=0; }
227
+ if (cpy >= window.length) { cpy = 0; }
228
+ }
229
+ } else {
230
+ outStream.writeByte(c);
231
+ window[windowpos++] = lastChar = c;
232
+ if (windowpos >= window.length) { windowpos=0; }
233
+ if (outSize >= 0) outSize--;
234
+ }
235
+ }
236
+ decoder.decodeFinish();
237
+ });
238
+
239
+
240
+ return LzjbR;
241
+ });
@@ -0,0 +1,273 @@
1
+ /* Implementation of LZP3(ish), with an adaptive Huffman code or a range
2
+ * coder (instead of LZP3's original static Huffman code).
3
+ * See: http://www.cbloom.com/papers/lzp.pdf
4
+ */
5
+ if (typeof define !== 'function') { var define = require('amdefine')(module); }
6
+ define(['./BitStream', './Context1Model', './DefSumModel', './FenwickModel', './Huffman', './LogDistanceModel', './NoModel', './RangeCoder', './Stream', './Util'],function(BitStream, Context1Model, DefSumModel, FenwickModel, Huffman, LogDistanceModel, NoModel, RangeCoder, Stream, Util){
7
+
8
+ var Lzp3 = Object.create(null);
9
+ Lzp3.MAGIC = 'lzp3';
10
+
11
+ // use Huffman coder (fast) or else use range coder (slow)
12
+ var USE_HUFFMAN_CODE = false;
13
+ // use deferred-sum model, which is supposed to be faster (but compresses worse)
14
+ var USE_DEFSUM = false;
15
+ // when to give up attempting to model the length
16
+ var LENGTH_MODEL_CUTOFF = 256;
17
+ var MODEL_MAX_PROB = 0xFF00;
18
+ var MODEL_INCREMENT = 0x100;
19
+
20
+ // Constants was used for compress/decompress function.
21
+ var CTXT4_TABLE_SIZE = 1 << 16;
22
+ var CTXT3_TABLE_SIZE = 1 << 12;
23
+ var CTXT2_TABLE_SIZE = 1 << 16;
24
+ var CONTEXT_LEN = 4;
25
+ var LOG_WINDOW_SIZE = 20;
26
+ var WINDOW_SIZE = 1 << LOG_WINDOW_SIZE;
27
+ var MAX_MATCH_LEN = WINDOW_SIZE-1;
28
+ var MATCH_LEN_CONTEXTS = 16;
29
+
30
+ var MAX32 = 0xFFFFFFFF;
31
+ var MAX24 = 0x00FFFFFF;
32
+ var MAX16 = 0x0000FFFF;
33
+ var MAX8 = 0x000000FF;
34
+
35
+
36
+ var Window = function(maxSize) {
37
+ this.buffer = Util.makeU8Buffer(Math.min(maxSize+4, WINDOW_SIZE));
38
+ this.pos = 0;
39
+ // context-4 hash table.
40
+ this.ctxt4 = Util.makeU32Buffer(CTXT4_TABLE_SIZE);
41
+ // context-3 hash table
42
+ this.ctxt3 = Util.makeU32Buffer(CTXT3_TABLE_SIZE);
43
+ // context-2 table (not really a hash any more)
44
+ this.ctxt2 = Util.makeU32Buffer(CTXT2_TABLE_SIZE);
45
+ // initial context
46
+ this.put(0x63); this.put(0x53); this.put(0x61); this.put(0x20);
47
+ };
48
+ Window.prototype.put = function(_byte) {
49
+ this.buffer[this.pos++] = _byte;
50
+ if (this.pos >= WINDOW_SIZE) { this.pos = 0; }
51
+ return _byte;
52
+ };
53
+ Window.prototype.get = function(pos) {
54
+ return this.buffer[pos & (WINDOW_SIZE-1)];
55
+ };
56
+ Window.prototype.context = function(pos, n) {
57
+ var c = 0, i;
58
+ pos = (pos - n) & (WINDOW_SIZE-1);
59
+ for (i=0; i<n; i++) {
60
+ c = (c << 8) | this.buffer[pos++];
61
+ if (pos >= WINDOW_SIZE) { pos = 0; }
62
+ }
63
+ return c;
64
+ };
65
+ // if matchLen !== 0, update the index; otherwise get index value.
66
+ Window.prototype.getIndex = function(s, matchLen) {
67
+ var c = this.context(s, 4);
68
+ // compute context hashes
69
+ var h4 = ((c>>>15) ^ c) & (CTXT4_TABLE_SIZE-1);
70
+ var h3 = ((c>>>11) ^ c) & (CTXT3_TABLE_SIZE-1);
71
+ var h2 = c & MAX16;
72
+ // check order-4 context
73
+ var p = 0, checkc;
74
+ // only do context confirmation if matchLen==0 (that is, if we're not just
75
+ // doing an update)
76
+ if (matchLen===0) {
77
+ p = this.ctxt4[h4];
78
+ if (p !== 0 && c !== this.context(p-1, 4)) {
79
+ p = 0; // context confirmation failed
80
+ }
81
+ if (p === 0) {
82
+ // check order-3 context
83
+ p = this.ctxt3[h3];
84
+ if (p !== 0 && (c & MAX24) !== this.context(p-1, 3)) {
85
+ p = 0; // context confirmation failed
86
+ }
87
+ if (p === 0) {
88
+ // check order-2 context
89
+ p = this.ctxt2[h2];
90
+ if (p !== 0 && (c && MAX16) !== this.context(p-1, 2)) {
91
+ p = 0; // context confirmation failed
92
+ }
93
+ }
94
+ }
95
+ }
96
+ // update context index
97
+ if (matchLen) { matchLen--; }
98
+ this.ctxt4[h4] = this.ctxt3[h3] = this.ctxt2[h2] =
99
+ (s | (matchLen << LOG_WINDOW_SIZE)) + 1;
100
+ // return lookup result.
101
+ return p;
102
+ };
103
+
104
+ /**
105
+ * Compress using modified LZP3 algorithm. Instead of using static
106
+ * Huffman coding, we use an adaptive Huffman code or range encoding.
107
+ */
108
+ Lzp3.compressFile = Util.compressFileHelper(Lzp3.MAGIC, function(inStream, outStream, fileSize, props) {
109
+ // sliding window & hash table
110
+ var window = new Window( (fileSize>=0) ? fileSize : WINDOW_SIZE );
111
+
112
+ var coderFactory, sparseCoderFactory, flush;
113
+
114
+ if (USE_HUFFMAN_CODE) {
115
+ // Huffman contexts
116
+ outStream.writeByte(0x80); // mark that this is Huffman coded.
117
+ var bitstream = new BitStream(outStream);
118
+ flush = bitstream.flush.bind(bitstream);
119
+ coderFactory = Huffman.factory(bitstream, MAX16);
120
+ sparseCoderFactory = NoModel.factory(bitstream);
121
+
122
+ } else { // range encoder
123
+ var range = new RangeCoder(outStream);
124
+ range.encodeStart(0x00, 0); // 0x00 == range encoded
125
+
126
+ coderFactory = FenwickModel.factory(range, MODEL_MAX_PROB, MODEL_INCREMENT);
127
+ if (USE_DEFSUM) {
128
+ coderFactory = DefSumModel.factory(range, false /* encoder */);
129
+ }
130
+ // switch sparseCoderFactory to a NoModel when size > cutoff
131
+ var noCoderFactory = NoModel.factory(range);
132
+ sparseCoderFactory = function(size) {
133
+ if (size > LENGTH_MODEL_CUTOFF) {
134
+ return noCoderFactory(size);
135
+ }
136
+ return coderFactory(size);
137
+ };
138
+ flush = function() { range.encodeFinish(); };
139
+ }
140
+
141
+ var huffLiteral= new Context1Model(coderFactory, 256,
142
+ (fileSize<0) ? 257 : 256);
143
+ var huffLen = [], i;
144
+ for (i=0; i<MATCH_LEN_CONTEXTS; i++) {
145
+ huffLen[i] = new LogDistanceModel(MAX_MATCH_LEN+1, 1,
146
+ coderFactory, sparseCoderFactory);
147
+ }
148
+
149
+ var inSize = 0, s, matchContext = 0;
150
+ while (inSize !== fileSize) {
151
+ var ch = inStream.readByte();
152
+ s = window.pos;
153
+ var p = window.getIndex(s, 0);
154
+ if (p !== 0) {
155
+ // great, a match! how long is it?
156
+ p--; // p=0 is used for 'not here'. p=1 really means WINDOW_SIZE
157
+ var prevMatchLen = (p >>> LOG_WINDOW_SIZE) + 1;
158
+ var matchLen = 0;
159
+ while (window.get(p + matchLen) === ch && matchLen < MAX_MATCH_LEN) {
160
+ matchLen++;
161
+ window.put(ch);
162
+ ch = inStream.readByte();
163
+ }
164
+ // code match length; match len = 0 means "literal"
165
+ // use "extra state" -1 to mean "same as previous match length"
166
+ if (prevMatchLen===matchLen) {
167
+ huffLen[matchContext&(MATCH_LEN_CONTEXTS-1)].encode(-1);
168
+ } else {
169
+ huffLen[matchContext&(MATCH_LEN_CONTEXTS-1)].encode(matchLen);
170
+ }
171
+ // update hash with this match
172
+ window.getIndex(s, matchLen);
173
+ inSize += matchLen;
174
+ matchContext <<= 1;
175
+ if (matchLen > 0) { matchContext |= 1; }
176
+ // XXX: LZMA uses a special "delta match" context here if matchLen==0
177
+ // XXX: it also uses the offset as context for the length (or vice-versa)
178
+ }
179
+ // always encode a literal after a match
180
+ var context1 = window.get(window.pos-1);
181
+ if (ch===Stream.EOF) {
182
+ if (fileSize < 0) {
183
+ huffLiteral.encode(256, context1);
184
+ }
185
+ break;
186
+ }
187
+ huffLiteral.encode(ch, context1);
188
+ window.put(ch);
189
+ inSize++;
190
+ }
191
+ if (flush) flush();
192
+ });
193
+
194
+ /**
195
+ * Decompress using modified LZP3 algorithm.
196
+ */
197
+ Lzp3.decompressFile = Util.decompressFileHelper(Lzp3.MAGIC, function(inStream, outStream, fileSize) {
198
+ var flags = inStream.readByte();
199
+ var use_huffman_code = !!(flags & 0x80);
200
+
201
+ // sliding window & hash table
202
+ var window = new Window( (fileSize>=0) ? fileSize : WINDOW_SIZE );
203
+
204
+ var coderFactory, sparseCoderFactory, finish;
205
+
206
+ if (use_huffman_code) {
207
+ // Huffman contexts
208
+ var bitstream = new BitStream(inStream);
209
+ coderFactory = Huffman.factory(bitstream, MAX16);
210
+ sparseCoderFactory = NoModel.factory(bitstream);
211
+ } else { // range encoder
212
+ var range = new RangeCoder(inStream);
213
+ range.decodeStart(true/* skip initial read */);
214
+ coderFactory = FenwickModel.factory(range, MODEL_MAX_PROB, MODEL_INCREMENT);
215
+ if (USE_DEFSUM) {
216
+ coderFactory = DefSumModel.factory(range, true /* decoder */);
217
+ }
218
+ // switch sparseCoderFactory to a NoModel when size > cutoff
219
+ var noCoderFactory = NoModel.factory(range);
220
+ sparseCoderFactory = function(size) {
221
+ if (size > LENGTH_MODEL_CUTOFF) {
222
+ return noCoderFactory(size);
223
+ }
224
+ return coderFactory(size);
225
+ };
226
+ finish = function() { range.decodeFinish(); };
227
+ }
228
+
229
+ var huffLiteral= new Context1Model(coderFactory, 256,
230
+ (fileSize<0) ? 257 : 256);
231
+ var huffLen = [], i;
232
+ for (i=0; i<MATCH_LEN_CONTEXTS; i++) {
233
+ huffLen[i] = new LogDistanceModel(MAX_MATCH_LEN+1, 1,
234
+ coderFactory, sparseCoderFactory);
235
+ }
236
+
237
+ var s, ch, outSize = 0, matchContext = 0;
238
+ while (outSize !== fileSize) {
239
+ s = window.pos;
240
+ var p = window.getIndex(s, 0);
241
+ if (p !== 0) {
242
+ p--; // p=0 is used for 'not here'. p=1 really means WINDOW_SIZE
243
+ var prevMatchLen = (p >>> LOG_WINDOW_SIZE) + 1;
244
+ var matchLen = huffLen[matchContext&(MATCH_LEN_CONTEXTS-1)].decode();
245
+ if (matchLen < 0) { matchLen = prevMatchLen; }
246
+ // copy characters!
247
+ for (i=0; i<matchLen; i++) {
248
+ ch = window.get(p + i);
249
+ outStream.writeByte(window.put(ch));
250
+ }
251
+ window.getIndex(s, matchLen);
252
+ outSize += matchLen;
253
+ matchContext <<= 1;
254
+ if (matchLen > 0) matchContext |= 1;
255
+ }
256
+ // literal always follows match (or failed match)
257
+ if (outSize === fileSize) {
258
+ break; // EOF
259
+ }
260
+ var context1 = window.get(window.pos-1);
261
+ ch = huffLiteral.decode(context1);
262
+ if (ch === 256) {
263
+ break; // EOF
264
+ }
265
+ outStream.writeByte(window.put(ch));
266
+ outSize++;
267
+ }
268
+ if (finish) finish();
269
+ });
270
+
271
+
272
+ return Lzp3;
273
+ });