/usr/lib/nodejs/iconv-lite/generation/utils.js is in node-iconv-lite 0.4.13-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 | var request = require('request'),
fs = require('fs'),
path = require('path'),
errTo = require('errto');
// Common utilities used in scripts.
exports.getFile = function(url, cb) {
var fullpath = path.join(__dirname, "source-data", path.basename(url));
fs.readFile(fullpath, "utf8", function(err, text) {
if (!err) return cb(null, text);
if (err.code != "ENOENT") return cb(err);
request(url, errTo(cb, function(res, text) {
fs.writeFile(fullpath, text, errTo(cb, function() {
cb(null, text);
}));
}));
});
}
// Returns array of arrays.
exports.parseText = function(text, splitChar) {
return text.split("\n").map(function(line) {
return line.split("#")[0].trim();
}).filter(Boolean).map(function(line) {
return line.split(splitChar || /\s+/).map(function(s) {return s.trim()}).filter(Boolean);
});
}
// Convert array of character codes to string. Character codes can be > 0xFFFF,
// so we emit surrogates when needed. Also, some character codes are actually
// sequences (arrays) - we emit them prepended with U+0FFF-(length-2).
// U+0FFF was chosen because it's small and unassigned, as well as 32 chars before it
function arrToStr(arr) {
var s = '';
for (var i = 0; i < arr.length; i++)
if (Array.isArray(arr[i])) {
if (arr[i].length == 1)
s += arrToStr(arr[i]);
else if (arr[i].length > 1)
s += String.fromCharCode(0xFFF - (arr[i].length-2))
+ arrToStr(arr[i]);
} else if (arr[i] > 0xFFFF) {
// Surrogates
s += String.fromCharCode(0xD800 + Math.floor((arr[i] - 0x10000) / 0x400))
+ String.fromCharCode(0xDC00 + (arr[i] - 0x10000) % 0x400);
} else {
// Basic characters.
s += String.fromCharCode(arr[i]);
}
return s;
}
// Input: map <dbcs num> -> <unicode num>
// Resulting format: Array of chunks, each chunk is:
// [0] = address of start of the chunk, hex string.
// <str> - characters of the chunk.
// <num> - increasing sequence of the length num, starting with prev character.
exports.generateTable = function(dbcs, maxBytes) {
var minSeqLen = 4;
var table = [], range, block, seqLen;
var max = 1 << ((maxBytes || 2) * 8);
for (var i = 0x0000; i < max; i++)
if (dbcs[i] !== undefined) {
if (dbcs[i-1] === undefined) { // Range started.
range = [i.toString(16)]; // Range[0] is starting address.
block = []; // Current block of character codes.
seqLen = 0; // Increasing sequence length at the end of the block.
}
else if (typeof dbcs[i-1] === 'number' && // We have arrays as elements of dbcs - check against it.
typeof dbcs[i] === 'number' &&
dbcs[i-1] + 1 === dbcs[i]) { // Increasing sequence continues - track its length.
seqLen++;
}
else { // Increasing sequence ended (or not started at all).
if (seqLen >= minSeqLen) {
// Seq is long enough: write prev segment and its length.
range.push(arrToStr(block.slice(0, -seqLen)), seqLen);
block = [];
}
seqLen = 0;
}
block.push(dbcs[i]);
} else if (range) { // Range finished, write last segments.
if (seqLen >= minSeqLen)
range.push(arrToStr(block.slice(0, -seqLen)), seqLen);
else
range.push(arrToStr(block));
table.push(range);
range = null;
}
return table;
}
exports.writeTable = function(name, table) {
this.writeFile(name, "[\n" + table.map(function(a) {return JSON.stringify(a);}).join(",\n") + "\n]\n");
}
exports.writeFile = function(name, body) {
fs.writeFileSync(path.join(__dirname, "../encodings/tables", name + ".json"), body);
}
|