Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
const zip = await unzipBuffer(buffer);
if (zip.entryCount < 1)
throw new TypeError(`Provided ZIP buffer contains no entries`);
let template = new Template();
for await (const entry of zip) {
if (entry.fileName.endsWith('/')) continue;
if (/\/?pass\.json$/i.test(entry.fileName)) {
if (template.style)
throw new TypeError(
`Archive contains more than one pass.json - found ${entry.fileName}`,
);
const buf = await zip.getBuffer(entry);
if (crc32(buf) !== entry.crc32)
throw new Error(
`CRC32 does not match for ${entry.fileName}, expected ${
entry.crc32
}, got ${crc32(buf)}`,
);
const passJSON = JSON.parse(stripJsonComments(buf.toString('utf8')));
template = new Template(
undefined,
passJSON,
template.images,
template.localization,
);
} else {
// test if it's an image
const img = template.images.parseFilename(entry.fileName);
if (img) {
entry.crc32
}, got ${crc32(buf)}`,
);
const passJSON = JSON.parse(stripJsonComments(buf.toString('utf8')));
template = new Template(
undefined,
passJSON,
template.images,
template.localization,
);
} else {
// test if it's an image
const img = template.images.parseFilename(entry.fileName);
if (img) {
const imgBuffer = await zip.getBuffer(entry);
if (crc32(imgBuffer) !== entry.crc32)
throw new Error(
`CRC32 does not match for ${entry.fileName}, expected ${
entry.crc32
}, got ${crc32(imgBuffer)}`,
);
await template.images.add(
img.imageType,
imgBuffer,
img.density,
img.lang,
);
} else {
// the only option lest is 'pass.strings' file in localization folder
const test = /(^|\/)(?[-_a-z]+)\.lproj\/pass\.strings$/i.exec(
entry.fileName,
);
function sendData(req, res, data, statusCode) {
var body = JSON.stringify(data)
req.removeAllListeners()
res.statusCode = statusCode || 200
res.setHeader('x-amz-crc32', crc32.unsigned(body))
res.setHeader('Content-Type', res.contentType)
res.setHeader('Content-Length', Buffer.byteLength(body, 'utf8'))
// AWS doesn't send a 'Connection' header but seems to use keep-alive behaviour
// res.setHeader('Connection', '')
// res.shouldKeepAlive = false
res.end(body)
}
PngWriter.prototype._addChunk = function (name, data, len) {
var buf = new Buffer(len + 12, 'binary'); // 4 bytes for name + 4 bytes for palette length + 4 bytes for crc
buf.writeUInt32BE(len, 0);
buf.write(name, 4, 4);
if (typeof data === 'string') {
buf.write(data, 8, len, 'binary');
} else {
data.copy(buf, 8, 0, len);
}
var crc32 = crc.unsigned(buf.slice(4, 8 + len));
buf.writeUInt32BE(crc32, len + 8);
this.chunks.push(buf);
};
wetag: function(body, encoding) {
if (body.length === 0) {
// fast-path empty body
return 'W/"0-0"';
} else {
var buf = Buffer.isBuffer(body) ? body : new Buffer(body, encoding);
return 'W/"' + buf.length.toString(16) + '-' + crc32.unsigned(buf) + '"';
}
},
exports.wetag = function wetag(body, encoding){
if (body.length === 0) {
// fast-path empty body
return 'W/"0-0"'
}
var buf = Buffer.isBuffer(body)
? body
: new Buffer(body, encoding)
var len = buf.length
return 'W/"' + len.toString(16) + '-' + crc32.unsigned(buf) + '"'
};
ZipArchiveOutputStream.prototype._appendBuffer = function(ae, source, callback) {
if (source.length === 0) {
ae.setMethod(constants.METHOD_STORED);
}
var method = ae.getMethod();
if (method === constants.METHOD_STORED) {
ae.setSize(source.length);
ae.setCompressedSize(source.length);
ae.setCrc(crc32.unsigned(source));
}
this._writeLocalFileHeader(ae);
if (method === constants.METHOD_STORED) {
this.write(source);
this._afterAppend(ae);
callback(null, ae);
return;
} else if (method === constants.METHOD_DEFLATED) {
this._smartStream(ae, callback).end(source);
return;
} else {
callback(new Error('compression method ' + method + ' not implemented'));
return;
}
async checkCrc32(position, length, recordedCrc32, description) {
const b = Buffer.allocUnsafe(length)
await this.file.read(b, 0, length, position)
const calculatedCrc32 = crc32.unsigned(b)
if (calculatedCrc32 !== recordedCrc32) {
throw new CramMalformedError(
`crc mismatch in ${description}: recorded CRC32 = ${recordedCrc32}, but calculated CRC32 = ${calculatedCrc32}`,
)
}
}
ZipArchiveOutputStream.prototype._appendBuffer = function(ae, source, callback) {
if (source.length === 0) {
ae.setMethod(constants.METHOD_STORED);
}
var method = ae.getMethod();
if (method === constants.METHOD_STORED) {
ae.setSize(source.length);
ae.setCompressedSize(source.length);
ae.setCrc(crc32.unsigned(source));
}
this._writeLocalFileHeader(ae);
if (method === constants.METHOD_STORED) {
this.write(source);
this._afterAppend(ae);
callback(null, ae);
return;
} else if (method === constants.METHOD_DEFLATED) {
this._smartStream(ae, callback).end(source);
return;
} else {
callback(new Error('compression method ' + method + ' not implemented'));
return;
}
function onend(err, sourceBuffer) {
if (err) {
callback(err);
return;
}
data.size = sourceBuffer.length || 0;
data.crc32 = crc32.unsigned(sourceBuffer);
self.files.push(data);
callback(null, data);
}