/usr/lib/nodejs/gettext-parser/lib/mocompiler.js is in node-gettext-parser 1.2.2-1.
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 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 | 'use strict';
var encoding = require('encoding');
var sharedFuncs = require('./shared');
/**
* Exposes general compiler function. Takes a translation
* object as a parameter and returns binary MO object
*
* @param {Object} table Translation object
* @return {Buffer} Compiled binary MO object
*/
module.exports = function (table) {
var compiler = new Compiler(table);
return compiler.compile();
};
/**
* Creates a MO compiler object.
*
* @constructor
* @param {Object} table Translation table as defined in the README
*/
function Compiler (table) {
this._table = table || {};
this._table.headers = this._table.headers || {};
this._table.translations = this._table.translations || {};
this._translations = [];
this._writeFunc = 'writeUInt32LE';
this._handleCharset();
}
/**
* Magic bytes for the generated binary data
*/
Compiler.prototype.MAGIC = 0x950412de;
/**
* Handles header values, replaces or adds (if needed) a charset property
*/
Compiler.prototype._handleCharset = function () {
var parts = (this._table.headers['content-type'] || 'text/plain').split(';');
var contentType = parts.shift();
var charset = sharedFuncs.formatCharset(this._table.charset);
var params = [];
params = parts.map(function (part) {
var parts = part.split('=');
var key = parts.shift().trim();
var value = parts.join('=');
if (key.toLowerCase() === 'charset') {
if (!charset) {
charset = sharedFuncs.formatCharset(value.trim() || 'utf-8');
}
return 'charset=' + charset;
}
return part;
});
if (!charset) {
charset = this._table.charset || 'utf-8';
params.push('charset=' + charset);
}
this._table.charset = charset;
this._table.headers['content-type'] = contentType + '; ' + params.join('; ');
this._charset = charset;
};
/**
* Generates an array of translation strings
* in the form of [{msgid:... , msgstr:...}]
*
* @return {Array} Translation strings array
*/
Compiler.prototype._generateList = function () {
var list = [];
list.push({
msgid: new Buffer(0),
msgstr: encoding.convert(sharedFuncs.generateHeader(this._table.headers), this._charset)
});
Object.keys(this._table.translations).forEach(function (msgctxt) {
if (typeof this._table.translations[msgctxt] !== 'object') {
return;
}
Object.keys(this._table.translations[msgctxt]).forEach(function (msgid) {
if (typeof this._table.translations[msgctxt][msgid] !== 'object') {
return;
}
if (msgctxt === '' && msgid === '') {
return;
}
var msgidPlural = this._table.translations[msgctxt][msgid].msgid_plural;
var key = msgid;
var value;
if (msgctxt) {
key = msgctxt + '\u0004' + key;
}
if (msgidPlural) {
key += '\u0000' + msgidPlural;
}
value = [].concat(this._table.translations[msgctxt][msgid].msgstr || []).join('\u0000');
list.push({
msgid: encoding.convert(key, this._charset),
msgstr: encoding.convert(value, this._charset)
});
}.bind(this));
}.bind(this));
return list;
};
/**
* Calculate buffer size for the final binary object
*
* @param {Array} list An array of translation strings from _generateList
* @return {Object} Size data of {msgid, msgstr, total}
*/
Compiler.prototype._calculateSize = function (list) {
var msgidLength = 0;
var msgstrLength = 0;
var totalLength = 0;
list.forEach(function (translation) {
msgidLength += translation.msgid.length + 1; // + extra 0x00
msgstrLength += translation.msgstr.length + 1; // + extra 0x00
});
totalLength = 4 + // magic number
4 + // revision
4 + // string count
4 + // original string table offset
4 + // translation string table offset
4 + // hash table size
4 + // hash table offset
(4 + 4) * list.length + // original string table
(4 + 4) * list.length + // translations string table
msgidLength + // originals
msgstrLength; // translations
return {
msgid: msgidLength,
msgstr: msgstrLength,
total: totalLength
};
};
/**
* Generates the binary MO object from the translation list
*
* @param {Array} list translation list
* @param {Object} size Byte size information
* @return {Buffer} Compiled MO object
*/
Compiler.prototype._build = function (list, size) {
var returnBuffer = new Buffer(size.total);
var curPosition = 0;
var i;
var len;
// magic
returnBuffer[this._writeFunc](this.MAGIC, 0);
// revision
returnBuffer[this._writeFunc](0, 4);
// string count
returnBuffer[this._writeFunc](list.length, 8);
// original string table offset
returnBuffer[this._writeFunc](28, 12);
// translation string table offset
returnBuffer[this._writeFunc](28 + (4 + 4) * list.length, 16);
// hash table size
returnBuffer[this._writeFunc](0, 20);
// hash table offset
returnBuffer[this._writeFunc](28 + (4 + 4) * list.length * 2, 24);
// build originals table
curPosition = 28 + 2 * (4 + 4) * list.length;
for (i = 0, len = list.length; i < len; i++) {
list[i].msgid.copy(returnBuffer, curPosition);
returnBuffer[this._writeFunc](list[i].msgid.length, 28 + i * 8);
returnBuffer[this._writeFunc](curPosition, 28 + i * 8 + 4);
returnBuffer[curPosition + list[i].msgid.length] = 0x00;
curPosition += list[i].msgid.length + 1;
}
// build translations table
for (i = 0, len = list.length; i < len; i++) {
list[i].msgstr.copy(returnBuffer, curPosition);
returnBuffer[this._writeFunc](list[i].msgstr.length, 28 + (4 + 4) * list.length + i * 8);
returnBuffer[this._writeFunc](curPosition, 28 + (4 + 4) * list.length + i * 8 + 4);
returnBuffer[curPosition + list[i].msgstr.length] = 0x00;
curPosition += list[i].msgstr.length + 1;
}
return returnBuffer;
};
/**
* Compiles translation object into a binary MO object
*
* @return {Buffer} Compiled MO object
*/
Compiler.prototype.compile = function () {
var list = this._generateList();
var size = this._calculateSize(list);
// sort by msgid
list.sort(function (a, b) {
if (a.msgid > b.msgid) {
return 1;
}
if (a.msgid < b.msgid) {
return -1;
}
return 0;
});
return this._build(list, size);
};
|