Best JavaScript code snippet using redwood
jsunzip.js
Source:jsunzip.js
...293 d.tag >>= 1;294 return bit;295}296/* read a num bit value from a stream and add base */297function read_bits_direct(source, bitcount, tag, idx, num)298{299 var val = 0;300 while (bitcount < 24) {301 tag = tag | (source[idx++] & 0xff) << bitcount;302 bitcount += 8;303 }304 val = tag & (0xffff >> (16 - num));305 tag >>= num;306 bitcount -= num;307 return [bitcount, tag, idx, val];308}309this.read_bits = function(d, num, base)310{311 if (!num)312 return base;313 var ret = read_bits_direct(d.source, d.bitcount, d.tag, d.sourceIndex, num);314 d.bitcount = ret[0];315 d.tag = ret[1];316 d.sourceIndex = ret[2];317 return ret[3] + base;318}319/* given a data stream and a tree, decode a symbol */320this.decode_symbol = function(d, t)321{322 while (d.bitcount < 16) {323 d.tag = d.tag | (d.source[d.sourceIndex++] & 0xff) << d.bitcount;324 d.bitcount += 8;325 }326 327 var sum = 0, cur = 0, len = 0;...
tinf.js
Source:tinf.js
...158 this.read_bits = function(d, num, base) {159 if (!num) {160 return base;161 }162 var ret = read_bits_direct(d.source, d.bitcount, d.tag, d.sourceIndex, num);163 d.bitcount = ret[0];164 d.tag = ret[1];165 d.sourceIndex = ret[2];166 return ret[3] + base;167 };168 /* given a data stream and a tree, decode a symbol */169 this.decode_symbol = function(d, t) {170 while (d.bitcount < 16) {171 d.tag = d.tag | (d.source[d.sourceIndex++] & 0xff) << d.bitcount;172 d.bitcount += 8;173 }174 var sum = 0, cur = 0, len = 0;175 do {176 cur = 2 * cur + ((d.tag & (1 << len)) >> len);177 ++len;178 sum += t.table[len];179 cur -= t.table[len];180 } while (cur >= 0);181 d.tag >>= len;182 d.bitcount -= len;183 return t.trans[sum + cur];184 };185 /* given a data stream, decode dynamic trees from it */186 this.decode_trees = function(d, lt, dt) {187 var code_tree = new this.TREE();188 var lengths = new Array(288+32);189 var hlit, hdist, hclen;190 var i, num, length;191 /* get 5 bits HLIT (257-286) */192 hlit = this.read_bits(d, 5, 257);193 /* get 5 bits HDIST (1-32) */194 hdist = this.read_bits(d, 5, 1);195 /* get 4 bits HCLEN (4-19) */196 hclen = this.read_bits(d, 4, 4);197 for (i = 0; i < 19; ++i) { lengths[i] = 0; }198 /* read code lengths for code length alphabet */199 for (i = 0; i < hclen; ++i) {200 /* get 3 bits code length (0-7) */201 var clen = this.read_bits(d, 3, 0);202 lengths[this.clcidx[i]] = clen;203 }204 /* build code length tree */205 this.build_tree(code_tree, lengths, 0, 19);206 /* decode code lengths for the dynamic trees */207 for (num = 0; num < hlit + hdist;) {208 var sym = this.decode_symbol(d, code_tree);209 switch (sym) {210 case 16:211 /* copy previous code length 3-6 times (read 2 bits) */212 {213 var prev = lengths[num - 1];214 for (length = this.read_bits(d, 2, 3); length; --length) {215 lengths[num++] = prev;216 }217 }218 break;219 case 17:220 /* repeat code length 0 for 3-10 times (read 3 bits) */221 for (length = this.read_bits(d, 3, 3); length; --length) {222 lengths[num++] = 0;223 }224 break;225 case 18:226 /* repeat code length 0 for 11-138 times (read 7 bits) */227 for (length = this.read_bits(d, 7, 11); length; --length) {228 lengths[num++] = 0;229 }230 break;231 default:232 /* values 0-15 represent the actual code lengths */233 lengths[num++] = sym;234 break;235 }236 }237 /* build dynamic trees */238 this.build_tree(lt, lengths, 0, hlit);239 this.build_tree(dt, lengths, hlit, hdist);240 };241 /* ----------------------------- *242 * -- block inflate functions -- *243 * ----------------------------- */244 /* given a stream and two trees, inflate a block of data */245 this.inflate_block_data = function(d, lt, dt) {246 // js optimization.247 var ddest = d.dest;248 var ddestlength = ddest.length;249 while (1) {250 var sym = this.decode_symbol(d, lt);251 /* check for end of block */252 if (sym === 256) {253 return this.OK;254 }255 if (sym < 256) {256 ddest[ddestlength++] = sym; // ? String.fromCharCode(sym);257 d.history.push(sym);258 } else {259 var length, dist, offs;260 var i;261 sym -= 257;262 /* possibly get more bits from length code */263 length = this.read_bits(d, this.length_bits[sym], this.length_base[sym]);264 dist = this.decode_symbol(d, dt);265 /* possibly get more bits from distance code */266 offs = d.history.length - this.read_bits(d, this.dist_bits[dist], this.dist_base[dist]);267 if (offs < 0) {268 throw new Error('Invalid zlib offset ' + offs);269 }270 /* copy match */271 for (i = offs; i < offs + length; ++i) {272 //ddest[ddestlength++] = ddest[i];273 ddest[ddestlength++] = d.history[i];274 d.history.push(d.history[i]);275 }276 }277 }278 };279 /* inflate an uncompressed block of data */280 this.inflate_uncompressed_block = function(d) {281 var length, invlength;282 var i;283 if (d.bitcount > 7) {284 var overflow = Math.floor(d.bitcount / 8);285 d.sourceIndex -= overflow;286 d.bitcount = 0;287 d.tag = 0;288 }289 /* get length */290 length = d.source[d.sourceIndex+1];291 length = 256*length + d.source[d.sourceIndex];292 /* get one's complement of length */293 invlength = d.source[d.sourceIndex+3];294 invlength = 256*invlength + d.source[d.sourceIndex+2];295 /* check length */296 if (length !== (~invlength & 0x0000ffff)) {297 return this.DATA_ERROR;298 }299 d.sourceIndex += 4;300 /* copy block */301 for (i = length; i; --i) {302 d.history.push(d.source[d.sourceIndex]);303 d.dest[d.dest.length] = d.source[d.sourceIndex++];304 }305 /* make sure we start next block on a byte boundary */306 d.bitcount = 0;307 return this.OK;308 };309 /* inflate a block of data compressed with fixed huffman trees */310 this.inflate_fixed_block = function(d) {311 /* decode block using fixed trees */312 return this.inflate_block_data(d, this.sltree, this.sdtree);313 };314 /* inflate a block of data compressed with dynamic huffman trees */315 this.inflate_dynamic_block = function(d) {316 /* decode trees from stream */317 this.decode_trees(d, d.ltree, d.dtree);318 /* decode block using decoded trees */319 return this.inflate_block_data(d, d.ltree, d.dtree);320 };321 /* ---------------------- *322 * -- public functions -- *323 * ---------------------- */324 /* initialize global (static) data */325 this.init = function() {326 /* build fixed huffman trees */327 this.build_fixed_trees(this.sltree, this.sdtree);328 /* build extra bits and base tables */329 this.build_bits_base(this.length_bits, this.length_base, 4, 3);330 this.build_bits_base(this.dist_bits, this.dist_base, 2, 1);331 /* fix a special case */332 this.length_bits[28] = 0;333 this.length_base[28] = 258;334 this.reset();335 };336 this.reset = function() {337 this.d = new this.DATA(this);338 delete this.header;339 };340 /* inflate stream from source to dest */341 this.uncompress = function(source, offset) {342 var d = this.d;343 var bfinal;344 /* initialise data */345 d.source = source;346 d.sourceIndex = offset;347 d.bitcount = 0;348 d.dest = [];349 // Skip zlib header at start of stream350 if (typeof this.header === 'undefined') {351 this.header = this.read_bits(d, 16, 0);352 /* byte 0: 0x78, 7 = 32k window size, 8 = deflate */353 /* byte 1: check bits for header and other flags */354 }355 var blocks = 0;356 do {357 var btype;358 var res;359 /* read final block flag */360 bfinal = this.getbit(d);361 /* read block type (2 bits) */362 btype = this.read_bits(d, 2, 0);363 /* decompress block */364 switch (btype) {365 case 0:366 /* decompress uncompressed block */367 res = this.inflate_uncompressed_block(d);368 break;369 case 1:370 /* decompress block with fixed huffman trees */371 res = this.inflate_fixed_block(d);372 break;373 case 2:374 /* decompress block with dynamic huffman trees */375 res = this.inflate_dynamic_block(d);376 break;377 default:378 return { 'status' : this.DATA_ERROR };379 }380 if (res !== this.OK) {381 return { 'status' : this.DATA_ERROR };382 }383 blocks++;384 } while (!bfinal && d.sourceIndex < d.source.length);385 d.history = d.history.slice(-this.WINDOW_SIZE);386 return { 'status' : this.OK, 'data' : d.dest };387 };388}389/**390 * Private API.391 */392/* read a num bit value from a stream and add base */393function read_bits_direct(source, bitcount, tag, idx, num) {394 var val = 0;395 while (bitcount < 24) {396 tag = tag | (source[idx++] & 0xff) << bitcount;397 bitcount += 8;398 }399 val = tag & (0xffff >> (16 - num));400 tag >>= num;401 bitcount -= num;402 return [bitcount, tag, idx, val];...
Using AI Code Generation
1var redwood = require('./build/Release/redwood');2var fs = require('fs');3var path = require('path');4var file_path = path.join(__dirname, "test_file");5var fd = fs.openSync(file_path, 'r');6var file_size = fs.fstatSync(fd).size;7console.log("file size: " + file_size);8var buffer = new Buffer(8);9var offset = 0;10var length = 8;11var bit_offset = 0;12var bit_length = 64;13var read_bits = redwood.read_bits_direct(fd, bit_offset, bit_length, buffer, offset, length);14console.log("read_bits: " + read_bits);15var data = buffer.readUInt32BE(0);16console.log("data: " + data);17fs.closeSync(fd);
Using AI Code Generation
1var redwood = require('redwood');2var fs = require('fs');3var buf = new Buffer(10);4fs.open('test.dat', 'r', function(err, fd) {5 redwood.read_bits_direct(fd, 0, 10, buf, 0, function(err, bytesRead, buffer) {6 console.log(buffer);7 });8});9var redwood = require('redwood');10var fs = require('fs');11var buf = new Buffer('Hello');12fs.open('test.dat', 'w', function(err, fd) {13 redwood.write_bits_direct(fd, 0, 10, buf, 0, function(err, bytesWritten, buffer) {14 console.log(buffer);15 });16});17var redwood = require('redwood');18var fs = require('fs');19var buf = new Buffer(10);20fs.open('test.dat', 'r', function(err, fd) {21 redwood.read_bits(fd, 0, 10, buf, 0, function(err, bytesRead, buffer) {22 console.log(buffer);23 });24});25var redwood = require('redwood');26var fs = require('fs');27var buf = new Buffer('Hello');28fs.open('test.dat', 'w', function(err, fd) {29 redwood.write_bits(fd, 0, 10, buf, 0, function(err, bytesWritten, buffer) {30 console.log(buffer);31 });32});
Using AI Code Generation
1var redwood = require('bindings')('redwood.node');2var fs = require('fs');3var path = require('path');4var assert = require('assert');5var util = require('util');6var f = path.join(__dirname, 'test.txt');7var f2 = path.join(__dirname, 'test2.txt');8var f3 = path.join(__dirname, 'test3.txt');9var f4 = path.join(__dirname, 'test4.txt');10var f5 = path.join(__dirname, 'test5.txt');11var f6 = path.join(__dirname, 'test6.txt');12var f7 = path.join(__dirname, 'test7.txt');13var f8 = path.join(__dirname, 'test8.txt');14var f9 = path.join(__dirname, 'test9.txt');15var f10 = path.join(__dirname, 'test10.txt');16var f11 = path.join(__dirname, 'test11.txt');17var f12 = path.join(__dirname, 'test12.txt');18var f13 = path.join(__dirname, 'test13.txt');19var f14 = path.join(__dirname, 'test14.txt');20var f15 = path.join(__dirname, 'test15.txt');21var f16 = path.join(__dirname, 'test16.txt');22var f17 = path.join(__dirname, 'test17.txt');23var f18 = path.join(__dirname, 'test18.txt');24var f19 = path.join(__dirname, 'test19.txt');25var f20 = path.join(__dirname, 'test20.txt');26var f21 = path.join(__dirname, 'test21.txt');27var f22 = path.join(__dirname, 'test22.txt');28var f23 = path.join(__dirname, 'test23.txt');29var f24 = path.join(__dirname, 'test24.txt');30var f25 = path.join(__dirname, 'test25.txt');31var f26 = path.join(__dirname, 'test26.txt');32var f27 = path.join(__dirname, 'test27.txt');33var f28 = path.join(__dirname, 'test28.txt');34var f29 = path.join(__dirname, 'test29.txt');35var f30 = path.join(__dirname, 'test30.txt');36var f31 = path.join(__dirname, 'test31.txt');37var f32 = path.join(__dirname, 'test32.txt');38var f33 = path.join(__dirname, 'test
Using AI Code Generation
1var redwood = require('./redwood.js');2var fs = require('fs');3var file = fs.openSync("test.txt", 'r');4var buf = new Buffer(8);5var pos = 0;6var bit_pos = 0;7var num_bits = 8;8var result = redwood.read_bits_direct(file, buf, pos, bit_pos, num_bits);9console.log("read_bits_direct result = " + result);10fs.closeSync(file);11var redwood = require('./redwood.js');12var fs = require('fs');13var file = fs.openSync("test.txt", 'r');14var buf = new Buffer(8);15var pos = 0;16var bit_pos = 0;17var num_bits = 8;18var result = redwood.read_bits_direct(file, buf, pos, bit_pos, num_bits);19console.log("read_bits_direct result = " + result);20fs.closeSync(file);21var redwood = require('./redwood.js');22var fs = require('fs');23var file = fs.openSync("test.txt", 'r');24var buf = new Buffer(8);25var pos = 0;26var bit_pos = 0;
Using AI Code Generation
1var redwood = require('redwood');2var file = redwood.open("testfile.txt");3var len = file.length();4var buffer = new Buffer(100);5file.read_bits_direct(0, 100, buffer, 0);6console.log(buffer.toString());7file.close();8### `read_bits_direct_sync(offset, length, buffer, buffer_offset)`9### `read_bits_sync(offset, length)`10### `read_sync(offset, length)`11### `write_bits_direct(offset, buffer, buffer_offset, length)`12### `write_bits_direct_sync(offset, buffer, buffer_offset, length)`13### `write_bits(offset, buffer, buffer_offset, length)`14### `write_bits_sync(offset, buffer, buffer_offset, length)`15### `write(offset, buffer, buffer_offset, length)`16### `write_sync(offset, buffer, buffer_offset, length)`17### `write_buffer(buffer)`18### `write_buffer_sync(buffer)`19### `close()`20### `close_sync()`21### `length()`22### `length_sync()`23### `truncate(length)`24### `truncate_sync(length)`25### `flush()`26### `flush_sync()`
Using AI Code Generation
1const redwood = require("redwood");2const buffer = redwood.read_file("test.bin");3const bits = redwood.read_bits_direct(buffer, 8);4console.log(bits);5const redwood = require("redwood");6const buffer = redwood.read_file("test.bin");7const bits = redwood.read_bits(buffer, 8);8console.log(bits);9const redwood = require("redwood");10const buffer = redwood.read_file("test.bin");11const bits = redwood.read_bits_reverse(buffer, 8);12console.log(bits);13const redwood = require("redwood");14const buffer = redwood.read_file("test.bin");15const bits = redwood.read_bits_reverse_direct(buffer, 8);16console.log(bits);17const redwood = require("redwood");18const buffer = redwood.read_file("test.bin");19const bits = redwood.read_bits_reverse_direct(buffer, 8);20console.log(bits);21const redwood = require("redwood");22const buffer = redwood.read_file("test.bin");23const bits = redwood.read_bits_reverse_direct(buffer, 8);24console.log(bits);25const redwood = require("redwood");
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!