UNPKG

corrupt-something

Version:

Let's corrupt some datas

52 lines (50 loc) 1.28 kB
const fs = require('fs') const minimist = require('minimist') const getRandomIntInclusive = (min, max) => { return Math.floor(Math.random() * (max - min + 1)) + min } module.exports = () => { const args = minimist(process.argv.slice(2)) let fileBuffer = fs.readFileSync(args['_'][0]) let offset let end let bit if (typeof args.offset !== 'undefined') { offset = args.offset } else { offset = getRandomIntInclusive(0, fileBuffer.length) } if (typeof args.end !== 'undefined') { if (args.end === 'end') { end = fileBuffer.length - 3 } else { end = args.end } } else { end = getRandomIntInclusive(0, fileBuffer.length) } if (typeof args.subEnd !== 'undefined') { end = fileBuffer.length - args.subEnd } if (typeof args.bit !== 'undefined') { bit = args.bit if (bit > 8) { throw new Error('Bit that over 8 is not allowed') } } else { bit = getRandomIntInclusive(0, 8) } if (offset > end) { let _ = parseInt(end) let __ = parseInt(offset) offset = _ end = __ } let position = offset while (end > position) { let a = fileBuffer.readInt32BE(position) fileBuffer.writeInt32BE(a | bit, position) position++ } fs.writeFileSync(args['_'][0], fileBuffer) }