@hiroosy/baileys-lite 0.0.1
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.
- package/README.md +25 -0
- package/WAProto/index.d.ts +15932 -0
- package/WAProto/index.js +115856 -0
- package/engine-requirements.js +11 -0
- package/package.json +27 -0
- package/src/binary/wa-binary-constants.js +1300 -0
- package/src/binary/wa-binary.js +649 -0
- package/src/constants.js +143 -0
- package/src/defaults.js +102 -0
- package/src/foundation/audio-duration.js +530 -0
- package/src/foundation/audio-samples.js +268 -0
- package/src/foundation/boom.js +46 -0
- package/src/foundation/cache.js +142 -0
- package/src/foundation/concurrency.js +87 -0
- package/src/foundation/curve25519.js +221 -0
- package/src/foundation/link-preview.js +320 -0
- package/src/foundation/logger.js +69 -0
- package/src/foundation/long.js +140 -0
- package/src/foundation/protobuf-minimal.js +619 -0
- package/src/foundation/wa-crypto.js +76 -0
- package/src/foundation/zip.js +124 -0
- package/src/index.js +48 -0
- package/src/signal/libsignal.js +1051 -0
- package/src/signal/signal-group.js +487 -0
- package/src/signal/signal-repository.js +646 -0
- package/src/socket/business-communities.js +830 -0
- package/src/socket/chats.js +674 -0
- package/src/socket/groups-newsletter.js +342 -0
- package/src/socket/messages-recv.js +1835 -0
- package/src/socket/messages-send.js +1202 -0
- package/src/socket/socket-core.js +833 -0
- package/src/socket/usync.js +291 -0
- package/src/socket-client/websocket-client.js +124 -0
- package/src/utils/auth-state-core.js +764 -0
- package/src/utils/auth-state-storage.js +316 -0
- package/src/utils/chat-sync.js +1045 -0
- package/src/utils/media.js +841 -0
- package/src/utils/message-compose.js +1609 -0
- package/src/utils/message-processing.js +958 -0
- package/src/utils/wa-protocol-core.js +1283 -0
- package/src/vendor/jpeg-js/LICENSE +25 -0
- package/src/vendor/jpeg-js/decoder.js +1152 -0
- package/src/vendor/jpeg-js/encoder.js +824 -0
- package/src/vendor/pngjs/LICENSE +20 -0
- package/src/vendor/pngjs/bitmapper.js +273 -0
- package/src/vendor/pngjs/bitpacker.js +167 -0
- package/src/vendor/pngjs/constants.js +38 -0
- package/src/vendor/pngjs/crc.js +47 -0
- package/src/vendor/pngjs/filter-pack.js +179 -0
- package/src/vendor/pngjs/filter-parse-sync.js +27 -0
- package/src/vendor/pngjs/filter-parse.js +185 -0
- package/src/vendor/pngjs/format-normaliser.js +101 -0
- package/src/vendor/pngjs/interlace.js +101 -0
- package/src/vendor/pngjs/packer-sync.js +65 -0
- package/src/vendor/pngjs/packer.js +138 -0
- package/src/vendor/pngjs/paeth-predictor.js +22 -0
- package/src/vendor/pngjs/parser-sync.js +121 -0
- package/src/vendor/pngjs/parser.js +299 -0
- package/src/vendor/pngjs/png-sync.js +19 -0
- package/src/vendor/pngjs/sync-inflate.js +174 -0
- package/src/vendor/pngjs/sync-reader.js +54 -0
- package/src/wam/wam-constants.js +22853 -0
- package/src/wam/wam.js +151 -0
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Packer object: builds PNG chunks (IHDR/gAMA/IDAT/IEND) from pixel data using bitpacker+filter-pack+zlib.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export
|
|
4
|
+
* (zlib/util/assert/buffer -> node: specifiers). See ./LICENSE.
|
|
5
|
+
* Algorithm body unchanged vs. upstream md5 bb043be341ec01e2db5b765f7ee86fec.
|
|
6
|
+
* https://github.com/pngjs/pngjs
|
|
7
|
+
*/
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
import constants from './constants.js'
|
|
11
|
+
import CrcStream from './crc.js'
|
|
12
|
+
import bitPacker from './bitpacker.js'
|
|
13
|
+
import filter from './filter-pack.js'
|
|
14
|
+
import zlib from 'node:zlib'
|
|
15
|
+
|
|
16
|
+
let Packer = function (options) {
|
|
17
|
+
this._options = options;
|
|
18
|
+
|
|
19
|
+
options.deflateChunkSize = options.deflateChunkSize || 32 * 1024;
|
|
20
|
+
options.deflateLevel =
|
|
21
|
+
options.deflateLevel != null ? options.deflateLevel : 9;
|
|
22
|
+
options.deflateStrategy =
|
|
23
|
+
options.deflateStrategy != null ? options.deflateStrategy : 3;
|
|
24
|
+
options.inputHasAlpha =
|
|
25
|
+
options.inputHasAlpha != null ? options.inputHasAlpha : true;
|
|
26
|
+
options.deflateFactory = options.deflateFactory || zlib.createDeflate;
|
|
27
|
+
options.bitDepth = options.bitDepth || 8;
|
|
28
|
+
// This is outputColorType
|
|
29
|
+
options.colorType =
|
|
30
|
+
typeof options.colorType === "number"
|
|
31
|
+
? options.colorType
|
|
32
|
+
: constants.COLORTYPE_COLOR_ALPHA;
|
|
33
|
+
options.inputColorType =
|
|
34
|
+
typeof options.inputColorType === "number"
|
|
35
|
+
? options.inputColorType
|
|
36
|
+
: constants.COLORTYPE_COLOR_ALPHA;
|
|
37
|
+
|
|
38
|
+
if (
|
|
39
|
+
[
|
|
40
|
+
constants.COLORTYPE_GRAYSCALE,
|
|
41
|
+
constants.COLORTYPE_COLOR,
|
|
42
|
+
constants.COLORTYPE_COLOR_ALPHA,
|
|
43
|
+
constants.COLORTYPE_ALPHA,
|
|
44
|
+
].indexOf(options.colorType) === -1
|
|
45
|
+
) {
|
|
46
|
+
throw new Error(
|
|
47
|
+
"option color type:" + options.colorType + " is not supported at present"
|
|
48
|
+
);
|
|
49
|
+
}
|
|
50
|
+
if (
|
|
51
|
+
[
|
|
52
|
+
constants.COLORTYPE_GRAYSCALE,
|
|
53
|
+
constants.COLORTYPE_COLOR,
|
|
54
|
+
constants.COLORTYPE_COLOR_ALPHA,
|
|
55
|
+
constants.COLORTYPE_ALPHA,
|
|
56
|
+
].indexOf(options.inputColorType) === -1
|
|
57
|
+
) {
|
|
58
|
+
throw new Error(
|
|
59
|
+
"option input color type:" +
|
|
60
|
+
options.inputColorType +
|
|
61
|
+
" is not supported at present"
|
|
62
|
+
);
|
|
63
|
+
}
|
|
64
|
+
if (options.bitDepth !== 8 && options.bitDepth !== 16) {
|
|
65
|
+
throw new Error(
|
|
66
|
+
"option bit depth:" + options.bitDepth + " is not supported at present"
|
|
67
|
+
);
|
|
68
|
+
}
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
Packer.prototype.getDeflateOptions = function () {
|
|
72
|
+
return {
|
|
73
|
+
chunkSize: this._options.deflateChunkSize,
|
|
74
|
+
level: this._options.deflateLevel,
|
|
75
|
+
strategy: this._options.deflateStrategy,
|
|
76
|
+
};
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
Packer.prototype.createDeflate = function () {
|
|
80
|
+
return this._options.deflateFactory(this.getDeflateOptions());
|
|
81
|
+
};
|
|
82
|
+
|
|
83
|
+
Packer.prototype.filterData = function (data, width, height) {
|
|
84
|
+
// convert to correct format for filtering (e.g. right bpp and bit depth)
|
|
85
|
+
let packedData = bitPacker(data, width, height, this._options);
|
|
86
|
+
|
|
87
|
+
// filter pixel data
|
|
88
|
+
let bpp = constants.COLORTYPE_TO_BPP_MAP[this._options.colorType];
|
|
89
|
+
let filteredData = filter(packedData, width, height, this._options, bpp);
|
|
90
|
+
return filteredData;
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
Packer.prototype._packChunk = function (type, data) {
|
|
94
|
+
let len = data ? data.length : 0;
|
|
95
|
+
let buf = Buffer.alloc(len + 12);
|
|
96
|
+
|
|
97
|
+
buf.writeUInt32BE(len, 0);
|
|
98
|
+
buf.writeUInt32BE(type, 4);
|
|
99
|
+
|
|
100
|
+
if (data) {
|
|
101
|
+
data.copy(buf, 8);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
buf.writeInt32BE(
|
|
105
|
+
CrcStream.crc32(buf.slice(4, buf.length - 4)),
|
|
106
|
+
buf.length - 4
|
|
107
|
+
);
|
|
108
|
+
return buf;
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
Packer.prototype.packGAMA = function (gamma) {
|
|
112
|
+
let buf = Buffer.alloc(4);
|
|
113
|
+
buf.writeUInt32BE(Math.floor(gamma * constants.GAMMA_DIVISION), 0);
|
|
114
|
+
return this._packChunk(constants.TYPE_gAMA, buf);
|
|
115
|
+
};
|
|
116
|
+
|
|
117
|
+
Packer.prototype.packIHDR = function (width, height) {
|
|
118
|
+
let buf = Buffer.alloc(13);
|
|
119
|
+
buf.writeUInt32BE(width, 0);
|
|
120
|
+
buf.writeUInt32BE(height, 4);
|
|
121
|
+
buf[8] = this._options.bitDepth; // Bit depth
|
|
122
|
+
buf[9] = this._options.colorType; // colorType
|
|
123
|
+
buf[10] = 0; // compression
|
|
124
|
+
buf[11] = 0; // filter
|
|
125
|
+
buf[12] = 0; // interlace
|
|
126
|
+
|
|
127
|
+
return this._packChunk(constants.TYPE_IHDR, buf);
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
Packer.prototype.packIDAT = function (data) {
|
|
131
|
+
return this._packChunk(constants.TYPE_IDAT, data);
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
Packer.prototype.packIEND = function () {
|
|
135
|
+
return this._packChunk(constants.TYPE_IEND, null);
|
|
136
|
+
};
|
|
137
|
+
|
|
138
|
+
export default Packer;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Paeth predictor (PNG filter type 4) used by both the parser and packer.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export. See ./LICENSE.
|
|
4
|
+
* Algorithm body unchanged vs. upstream md5 05b33c66f5c16acd9267747c40a24054.
|
|
5
|
+
* https://github.com/pngjs/pngjs
|
|
6
|
+
*/
|
|
7
|
+
"use strict";
|
|
8
|
+
|
|
9
|
+
export default function paethPredictor(left, above, upLeft) {
|
|
10
|
+
let paeth = left + above - upLeft;
|
|
11
|
+
let pLeft = Math.abs(paeth - left);
|
|
12
|
+
let pAbove = Math.abs(paeth - above);
|
|
13
|
+
let pUpLeft = Math.abs(paeth - upLeft);
|
|
14
|
+
|
|
15
|
+
if (pLeft <= pAbove && pLeft <= pUpLeft) {
|
|
16
|
+
return left;
|
|
17
|
+
}
|
|
18
|
+
if (pAbove <= pUpLeft) {
|
|
19
|
+
return above;
|
|
20
|
+
}
|
|
21
|
+
return upLeft;
|
|
22
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Synchronous whole-buffer PNG decode entry point: parse chunks, zlib-inflate IDAT, unfilter, bitmap, normalise.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export
|
|
4
|
+
* (zlib/util/assert/buffer -> node: specifiers). See ./LICENSE.
|
|
5
|
+
* Algorithm body unchanged vs. upstream md5 dc170d369c632b306a208ae9527d9c5a.
|
|
6
|
+
* https://github.com/pngjs/pngjs
|
|
7
|
+
*/
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
let hasSyncZlib = true;
|
|
11
|
+
import zlib from 'node:zlib'
|
|
12
|
+
import inflateSync from './sync-inflate.js'
|
|
13
|
+
if (!zlib.deflateSync) {
|
|
14
|
+
hasSyncZlib = false;
|
|
15
|
+
}
|
|
16
|
+
import SyncReader from './sync-reader.js'
|
|
17
|
+
import * as FilterSync from './filter-parse-sync.js'
|
|
18
|
+
import Parser from './parser.js'
|
|
19
|
+
import * as bitmapper from './bitmapper.js'
|
|
20
|
+
import formatNormaliser from './format-normaliser.js'
|
|
21
|
+
|
|
22
|
+
const parseSync = function (buffer, options) {
|
|
23
|
+
if (!hasSyncZlib) {
|
|
24
|
+
throw new Error(
|
|
25
|
+
"To use the sync capability of this library in old node versions, please pin pngjs to v2.3.0"
|
|
26
|
+
);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
let err;
|
|
30
|
+
function handleError(_err_) {
|
|
31
|
+
err = _err_;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
let metaData;
|
|
35
|
+
function handleMetaData(_metaData_) {
|
|
36
|
+
metaData = _metaData_;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function handleTransColor(transColor) {
|
|
40
|
+
metaData.transColor = transColor;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function handlePalette(palette) {
|
|
44
|
+
metaData.palette = palette;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function handleSimpleTransparency() {
|
|
48
|
+
metaData.alpha = true;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
let gamma;
|
|
52
|
+
function handleGamma(_gamma_) {
|
|
53
|
+
gamma = _gamma_;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
let inflateDataList = [];
|
|
57
|
+
function handleInflateData(inflatedData) {
|
|
58
|
+
inflateDataList.push(inflatedData);
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
let reader = new SyncReader(buffer);
|
|
62
|
+
|
|
63
|
+
let parser = new Parser(options, {
|
|
64
|
+
read: reader.read.bind(reader),
|
|
65
|
+
error: handleError,
|
|
66
|
+
metadata: handleMetaData,
|
|
67
|
+
gamma: handleGamma,
|
|
68
|
+
palette: handlePalette,
|
|
69
|
+
transColor: handleTransColor,
|
|
70
|
+
inflateData: handleInflateData,
|
|
71
|
+
simpleTransparency: handleSimpleTransparency,
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
parser.start();
|
|
75
|
+
reader.process();
|
|
76
|
+
|
|
77
|
+
if (err) {
|
|
78
|
+
throw err;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
//join together the inflate datas
|
|
82
|
+
let inflateData = Buffer.concat(inflateDataList);
|
|
83
|
+
inflateDataList.length = 0;
|
|
84
|
+
|
|
85
|
+
let inflatedData;
|
|
86
|
+
if (metaData.interlace) {
|
|
87
|
+
inflatedData = zlib.inflateSync(inflateData);
|
|
88
|
+
} else {
|
|
89
|
+
let rowSize =
|
|
90
|
+
((metaData.width * metaData.bpp * metaData.depth + 7) >> 3) + 1;
|
|
91
|
+
let imageSize = rowSize * metaData.height;
|
|
92
|
+
inflatedData = inflateSync(inflateData, {
|
|
93
|
+
chunkSize: imageSize,
|
|
94
|
+
maxLength: imageSize,
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
inflateData = null;
|
|
98
|
+
|
|
99
|
+
if (!inflatedData || !inflatedData.length) {
|
|
100
|
+
throw new Error("bad png - invalid inflate data response");
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
let unfilteredData = FilterSync.process(inflatedData, metaData);
|
|
104
|
+
inflateData = null;
|
|
105
|
+
|
|
106
|
+
let bitmapData = bitmapper.dataToBitMap(unfilteredData, metaData);
|
|
107
|
+
unfilteredData = null;
|
|
108
|
+
|
|
109
|
+
let normalisedBitmapData = formatNormaliser(
|
|
110
|
+
bitmapData,
|
|
111
|
+
metaData,
|
|
112
|
+
options.skipRescale
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
metaData.data = normalisedBitmapData;
|
|
116
|
+
metaData.gamma = gamma || 0;
|
|
117
|
+
|
|
118
|
+
return metaData;
|
|
119
|
+
};
|
|
120
|
+
|
|
121
|
+
export default parseSync;
|
|
@@ -0,0 +1,299 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Chunk-by-chunk PNG parser state machine: IHDR/PLTE/tRNS/gAMA/IDAT/IEND handlers + CRC check.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export
|
|
4
|
+
* (zlib/util/assert/buffer -> node: specifiers). See ./LICENSE.
|
|
5
|
+
* Algorithm body unchanged vs. upstream md5 246405eeea384bf713721b876aecb968.
|
|
6
|
+
* https://github.com/pngjs/pngjs
|
|
7
|
+
*/
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
import constants from './constants.js'
|
|
11
|
+
import CrcCalculator from './crc.js'
|
|
12
|
+
|
|
13
|
+
let Parser = function (options, dependencies) {
|
|
14
|
+
this._options = options;
|
|
15
|
+
options.checkCRC = options.checkCRC !== false;
|
|
16
|
+
|
|
17
|
+
this._hasIHDR = false;
|
|
18
|
+
this._hasIEND = false;
|
|
19
|
+
this._emittedHeadersFinished = false;
|
|
20
|
+
|
|
21
|
+
// input flags/metadata
|
|
22
|
+
this._palette = [];
|
|
23
|
+
this._colorType = 0;
|
|
24
|
+
|
|
25
|
+
this._chunks = {};
|
|
26
|
+
this._chunks[constants.TYPE_IHDR] = this._handleIHDR.bind(this);
|
|
27
|
+
this._chunks[constants.TYPE_IEND] = this._handleIEND.bind(this);
|
|
28
|
+
this._chunks[constants.TYPE_IDAT] = this._handleIDAT.bind(this);
|
|
29
|
+
this._chunks[constants.TYPE_PLTE] = this._handlePLTE.bind(this);
|
|
30
|
+
this._chunks[constants.TYPE_tRNS] = this._handleTRNS.bind(this);
|
|
31
|
+
this._chunks[constants.TYPE_gAMA] = this._handleGAMA.bind(this);
|
|
32
|
+
|
|
33
|
+
this.read = dependencies.read;
|
|
34
|
+
this.error = dependencies.error;
|
|
35
|
+
this.metadata = dependencies.metadata;
|
|
36
|
+
this.gamma = dependencies.gamma;
|
|
37
|
+
this.transColor = dependencies.transColor;
|
|
38
|
+
this.palette = dependencies.palette;
|
|
39
|
+
this.parsed = dependencies.parsed;
|
|
40
|
+
this.inflateData = dependencies.inflateData;
|
|
41
|
+
this.finished = dependencies.finished;
|
|
42
|
+
this.simpleTransparency = dependencies.simpleTransparency;
|
|
43
|
+
this.headersFinished = dependencies.headersFinished || function () {};
|
|
44
|
+
};
|
|
45
|
+
|
|
46
|
+
Parser.prototype.start = function () {
|
|
47
|
+
this.read(constants.PNG_SIGNATURE.length, this._parseSignature.bind(this));
|
|
48
|
+
};
|
|
49
|
+
|
|
50
|
+
Parser.prototype._parseSignature = function (data) {
|
|
51
|
+
let signature = constants.PNG_SIGNATURE;
|
|
52
|
+
|
|
53
|
+
for (let i = 0; i < signature.length; i++) {
|
|
54
|
+
if (data[i] !== signature[i]) {
|
|
55
|
+
this.error(new Error("Invalid file signature"));
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
this.read(8, this._parseChunkBegin.bind(this));
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
Parser.prototype._parseChunkBegin = function (data) {
|
|
63
|
+
// chunk content length
|
|
64
|
+
let length = data.readUInt32BE(0);
|
|
65
|
+
|
|
66
|
+
// chunk type
|
|
67
|
+
let type = data.readUInt32BE(4);
|
|
68
|
+
let name = "";
|
|
69
|
+
for (let i = 4; i < 8; i++) {
|
|
70
|
+
name += String.fromCharCode(data[i]);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
//console.log('chunk ', name, length);
|
|
74
|
+
|
|
75
|
+
// chunk flags
|
|
76
|
+
let ancillary = Boolean(data[4] & 0x20); // or critical
|
|
77
|
+
// priv = Boolean(data[5] & 0x20), // or public
|
|
78
|
+
// safeToCopy = Boolean(data[7] & 0x20); // or unsafe
|
|
79
|
+
|
|
80
|
+
if (!this._hasIHDR && type !== constants.TYPE_IHDR) {
|
|
81
|
+
this.error(new Error("Expected IHDR on beggining"));
|
|
82
|
+
return;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
this._crc = new CrcCalculator();
|
|
86
|
+
this._crc.write(Buffer.from(name));
|
|
87
|
+
|
|
88
|
+
if (this._chunks[type]) {
|
|
89
|
+
return this._chunks[type](length);
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (!ancillary) {
|
|
93
|
+
this.error(new Error("Unsupported critical chunk type " + name));
|
|
94
|
+
return;
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
this.read(length + 4, this._skipChunk.bind(this));
|
|
98
|
+
};
|
|
99
|
+
|
|
100
|
+
Parser.prototype._skipChunk = function (/*data*/) {
|
|
101
|
+
this.read(8, this._parseChunkBegin.bind(this));
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
Parser.prototype._handleChunkEnd = function () {
|
|
105
|
+
this.read(4, this._parseChunkEnd.bind(this));
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
Parser.prototype._parseChunkEnd = function (data) {
|
|
109
|
+
let fileCrc = data.readInt32BE(0);
|
|
110
|
+
let calcCrc = this._crc.crc32();
|
|
111
|
+
|
|
112
|
+
// check CRC
|
|
113
|
+
if (this._options.checkCRC && calcCrc !== fileCrc) {
|
|
114
|
+
this.error(new Error("Crc error - " + fileCrc + " - " + calcCrc));
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
if (!this._hasIEND) {
|
|
119
|
+
this.read(8, this._parseChunkBegin.bind(this));
|
|
120
|
+
}
|
|
121
|
+
};
|
|
122
|
+
|
|
123
|
+
Parser.prototype._handleIHDR = function (length) {
|
|
124
|
+
this.read(length, this._parseIHDR.bind(this));
|
|
125
|
+
};
|
|
126
|
+
Parser.prototype._parseIHDR = function (data) {
|
|
127
|
+
this._crc.write(data);
|
|
128
|
+
|
|
129
|
+
let width = data.readUInt32BE(0);
|
|
130
|
+
let height = data.readUInt32BE(4);
|
|
131
|
+
let depth = data[8];
|
|
132
|
+
let colorType = data[9]; // bits: 1 palette, 2 color, 4 alpha
|
|
133
|
+
let compr = data[10];
|
|
134
|
+
let filter = data[11];
|
|
135
|
+
let interlace = data[12];
|
|
136
|
+
|
|
137
|
+
// console.log(' width', width, 'height', height,
|
|
138
|
+
// 'depth', depth, 'colorType', colorType,
|
|
139
|
+
// 'compr', compr, 'filter', filter, 'interlace', interlace
|
|
140
|
+
// );
|
|
141
|
+
|
|
142
|
+
if (
|
|
143
|
+
depth !== 8 &&
|
|
144
|
+
depth !== 4 &&
|
|
145
|
+
depth !== 2 &&
|
|
146
|
+
depth !== 1 &&
|
|
147
|
+
depth !== 16
|
|
148
|
+
) {
|
|
149
|
+
this.error(new Error("Unsupported bit depth " + depth));
|
|
150
|
+
return;
|
|
151
|
+
}
|
|
152
|
+
if (!(colorType in constants.COLORTYPE_TO_BPP_MAP)) {
|
|
153
|
+
this.error(new Error("Unsupported color type"));
|
|
154
|
+
return;
|
|
155
|
+
}
|
|
156
|
+
if (compr !== 0) {
|
|
157
|
+
this.error(new Error("Unsupported compression method"));
|
|
158
|
+
return;
|
|
159
|
+
}
|
|
160
|
+
if (filter !== 0) {
|
|
161
|
+
this.error(new Error("Unsupported filter method"));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
if (interlace !== 0 && interlace !== 1) {
|
|
165
|
+
this.error(new Error("Unsupported interlace method"));
|
|
166
|
+
return;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
this._colorType = colorType;
|
|
170
|
+
|
|
171
|
+
let bpp = constants.COLORTYPE_TO_BPP_MAP[this._colorType];
|
|
172
|
+
|
|
173
|
+
this._hasIHDR = true;
|
|
174
|
+
|
|
175
|
+
this.metadata({
|
|
176
|
+
width: width,
|
|
177
|
+
height: height,
|
|
178
|
+
depth: depth,
|
|
179
|
+
interlace: Boolean(interlace),
|
|
180
|
+
palette: Boolean(colorType & constants.COLORTYPE_PALETTE),
|
|
181
|
+
color: Boolean(colorType & constants.COLORTYPE_COLOR),
|
|
182
|
+
alpha: Boolean(colorType & constants.COLORTYPE_ALPHA),
|
|
183
|
+
bpp: bpp,
|
|
184
|
+
colorType: colorType,
|
|
185
|
+
});
|
|
186
|
+
|
|
187
|
+
this._handleChunkEnd();
|
|
188
|
+
};
|
|
189
|
+
|
|
190
|
+
Parser.prototype._handlePLTE = function (length) {
|
|
191
|
+
this.read(length, this._parsePLTE.bind(this));
|
|
192
|
+
};
|
|
193
|
+
Parser.prototype._parsePLTE = function (data) {
|
|
194
|
+
this._crc.write(data);
|
|
195
|
+
|
|
196
|
+
let entries = Math.floor(data.length / 3);
|
|
197
|
+
// console.log('Palette:', entries);
|
|
198
|
+
|
|
199
|
+
for (let i = 0; i < entries; i++) {
|
|
200
|
+
this._palette.push([data[i * 3], data[i * 3 + 1], data[i * 3 + 2], 0xff]);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
this.palette(this._palette);
|
|
204
|
+
|
|
205
|
+
this._handleChunkEnd();
|
|
206
|
+
};
|
|
207
|
+
|
|
208
|
+
Parser.prototype._handleTRNS = function (length) {
|
|
209
|
+
this.simpleTransparency();
|
|
210
|
+
this.read(length, this._parseTRNS.bind(this));
|
|
211
|
+
};
|
|
212
|
+
Parser.prototype._parseTRNS = function (data) {
|
|
213
|
+
this._crc.write(data);
|
|
214
|
+
|
|
215
|
+
// palette
|
|
216
|
+
if (this._colorType === constants.COLORTYPE_PALETTE_COLOR) {
|
|
217
|
+
if (this._palette.length === 0) {
|
|
218
|
+
this.error(new Error("Transparency chunk must be after palette"));
|
|
219
|
+
return;
|
|
220
|
+
}
|
|
221
|
+
if (data.length > this._palette.length) {
|
|
222
|
+
this.error(new Error("More transparent colors than palette size"));
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
for (let i = 0; i < data.length; i++) {
|
|
226
|
+
this._palette[i][3] = data[i];
|
|
227
|
+
}
|
|
228
|
+
this.palette(this._palette);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
// for colorType 0 (grayscale) and 2 (rgb)
|
|
232
|
+
// there might be one gray/color defined as transparent
|
|
233
|
+
if (this._colorType === constants.COLORTYPE_GRAYSCALE) {
|
|
234
|
+
// grey, 2 bytes
|
|
235
|
+
this.transColor([data.readUInt16BE(0)]);
|
|
236
|
+
}
|
|
237
|
+
if (this._colorType === constants.COLORTYPE_COLOR) {
|
|
238
|
+
this.transColor([
|
|
239
|
+
data.readUInt16BE(0),
|
|
240
|
+
data.readUInt16BE(2),
|
|
241
|
+
data.readUInt16BE(4),
|
|
242
|
+
]);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
this._handleChunkEnd();
|
|
246
|
+
};
|
|
247
|
+
|
|
248
|
+
Parser.prototype._handleGAMA = function (length) {
|
|
249
|
+
this.read(length, this._parseGAMA.bind(this));
|
|
250
|
+
};
|
|
251
|
+
Parser.prototype._parseGAMA = function (data) {
|
|
252
|
+
this._crc.write(data);
|
|
253
|
+
this.gamma(data.readUInt32BE(0) / constants.GAMMA_DIVISION);
|
|
254
|
+
|
|
255
|
+
this._handleChunkEnd();
|
|
256
|
+
};
|
|
257
|
+
|
|
258
|
+
Parser.prototype._handleIDAT = function (length) {
|
|
259
|
+
if (!this._emittedHeadersFinished) {
|
|
260
|
+
this._emittedHeadersFinished = true;
|
|
261
|
+
this.headersFinished();
|
|
262
|
+
}
|
|
263
|
+
this.read(-length, this._parseIDAT.bind(this, length));
|
|
264
|
+
};
|
|
265
|
+
Parser.prototype._parseIDAT = function (length, data) {
|
|
266
|
+
this._crc.write(data);
|
|
267
|
+
|
|
268
|
+
if (
|
|
269
|
+
this._colorType === constants.COLORTYPE_PALETTE_COLOR &&
|
|
270
|
+
this._palette.length === 0
|
|
271
|
+
) {
|
|
272
|
+
throw new Error("Expected palette not found");
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
this.inflateData(data);
|
|
276
|
+
let leftOverLength = length - data.length;
|
|
277
|
+
|
|
278
|
+
if (leftOverLength > 0) {
|
|
279
|
+
this._handleIDAT(leftOverLength);
|
|
280
|
+
} else {
|
|
281
|
+
this._handleChunkEnd();
|
|
282
|
+
}
|
|
283
|
+
};
|
|
284
|
+
|
|
285
|
+
Parser.prototype._handleIEND = function (length) {
|
|
286
|
+
this.read(length, this._parseIEND.bind(this));
|
|
287
|
+
};
|
|
288
|
+
Parser.prototype._parseIEND = function (data) {
|
|
289
|
+
this._crc.write(data);
|
|
290
|
+
|
|
291
|
+
this._hasIEND = true;
|
|
292
|
+
this._handleChunkEnd();
|
|
293
|
+
|
|
294
|
+
if (this.finished) {
|
|
295
|
+
this.finished();
|
|
296
|
+
}
|
|
297
|
+
};
|
|
298
|
+
|
|
299
|
+
export default Parser;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Public sync API: `read(buffer, options)` / `write(png, options)` — mirrors `pngjs`'s PNG.sync.
|
|
3
|
+
* Vendored from `pngjs` 7.0.0 (MIT). Only require()/module.exports converted to import/export
|
|
4
|
+
* (zlib/util/assert/buffer -> node: specifiers). See ./LICENSE.
|
|
5
|
+
* Algorithm body unchanged vs. upstream md5 57db1eb23be674e473fa48a26b493ca7.
|
|
6
|
+
* https://github.com/pngjs/pngjs
|
|
7
|
+
*/
|
|
8
|
+
"use strict";
|
|
9
|
+
|
|
10
|
+
import parse from './parser-sync.js'
|
|
11
|
+
import pack from './packer-sync.js'
|
|
12
|
+
|
|
13
|
+
export const read = function (buffer, options) {
|
|
14
|
+
return parse(buffer, options || {});
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
export const write = function (png, options) {
|
|
18
|
+
return pack(png, options);
|
|
19
|
+
};
|