|
| 1 | +const request = require('request'); |
| 2 | +const fs = require("fs"); |
| 3 | +PNG = require("pngjs").PNG; |
| 4 | + |
| 5 | +function stegDecode(dataIn){ |
| 6 | + const magicWord = 0x12345678; |
| 7 | + const version = 0x00000001; |
| 8 | + |
| 9 | + var image8 = new Uint8Array(dataIn); |
| 10 | + var data8 = new Uint8Array(900*900); |
| 11 | + var data32 = new Uint32Array(data8.buffer); |
| 12 | + |
| 13 | + let i = 0; |
| 14 | + |
| 15 | + for (let d = 0; d<image8.length; d++){ |
| 16 | + var b = 0; |
| 17 | + for (let j = 0; j<4; j++){ |
| 18 | + if (i % 4 == 3) {i++;} // skip alpha bytes |
| 19 | + b |= (image8[i] & 3) << (j*2); |
| 20 | + i++; |
| 21 | + } |
| 22 | + data8[d] = b; |
| 23 | + } |
| 24 | + |
| 25 | + // Check header |
| 26 | + if (data32[0] != magicWord) { |
| 27 | + console.log('not a recognised steg file '+data32[0x00].toString(16));return null; |
| 28 | + } |
| 29 | + |
| 30 | + var fileLength = data32[2]; |
| 31 | + var headerLength = data32[3]; |
| 32 | + var checksum = data32[4]; |
| 33 | + var fileData = new Uint8Array(data8.slice(headerLength, fileLength+headerLength)); |
| 34 | + |
| 35 | + // Perform checksum |
| 36 | + //var crc = crc32.buf(fileData); |
| 37 | + //if (checksum != crc) {console.log("*** checksum failed ***");return null;} else {console.log("*** checksum passed ***");} |
| 38 | + return fileData; |
| 39 | +} |
| 40 | + |
| 41 | +async function getTweetDisk(imageURL){ |
| 42 | + return new Promise(function(resolve, reject){ |
| 43 | + request({ uri:imageURL, encoding: null }, function (error, response, body) { |
| 44 | + if (error) return reject(error); |
| 45 | + try { |
| 46 | + var png = PNG.sync.read(body); |
| 47 | + var diskData = stegDecode(png.data); |
| 48 | + resolve(diskData); |
| 49 | + } catch(e) { |
| 50 | + reject(e); |
| 51 | + } |
| 52 | + }); |
| 53 | + }); |
| 54 | +} |
| 55 | + |
| 56 | +module.exports = { |
| 57 | + get: getTweetDisk |
| 58 | +}; |
0 commit comments