Prepare the way to decode Type1 charStrings
This commit is contained in:
parent
fe11af4b57
commit
3064305d91
232
PDFFont.js
232
PDFFont.js
@ -1,10 +1,39 @@
|
|||||||
|
|
||||||
|
var Type1Parser = function(aAsciiStream, aBinaryStream) {
|
||||||
|
var lexer = new Lexer(aAsciiStream);
|
||||||
|
|
||||||
var Type1Parser = function(aLexer) {
|
// Turn on this flag for additional debugging logs
|
||||||
var lexer = aLexer;
|
var debug = false;
|
||||||
|
|
||||||
|
var dump = function(aData) {
|
||||||
/*
|
if (debug)
|
||||||
|
log(aData);
|
||||||
|
};
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Decrypt a Sequence of Ciphertext Bytes to Produce the Original Sequence
|
||||||
|
* of Plaintext Bytes. The function took a key as a parameter which can be
|
||||||
|
* for decrypting the eexec block of for decoding charStrings.
|
||||||
|
*/
|
||||||
|
var kEexecEncryptionKey = 55665;
|
||||||
|
var kCharStringsEncryptionKey = 4330;
|
||||||
|
|
||||||
|
function decrypt(aStream, aKey, aDiscardNumber) {
|
||||||
|
var r = aKey, c1 = 52845, c2 = 22719;
|
||||||
|
|
||||||
|
var decryptedString = [];
|
||||||
|
var value = null;
|
||||||
|
|
||||||
|
var count = aStream.length;
|
||||||
|
for (var i = 0; i < count; i++) {
|
||||||
|
value = aStream.getByte();
|
||||||
|
decryptedString[i] = String.fromCharCode(value ^ (r >> 8));
|
||||||
|
r = ((value + r) * c1 + c2) & ((1 << 16) - 1);
|
||||||
|
}
|
||||||
|
return decryptedString.slice(aDiscardNumber);
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
* The operand stack holds arbitrary PostScript objects that are the operands
|
* The operand stack holds arbitrary PostScript objects that are the operands
|
||||||
* and results of PostScript operators being executed. The interpreter pushes
|
* and results of PostScript operators being executed. The interpreter pushes
|
||||||
* objects on the operand stack when it encounters them as literal data in a
|
* objects on the operand stack when it encounters them as literal data in a
|
||||||
@ -13,8 +42,28 @@ var Type1Parser = function(aLexer) {
|
|||||||
* operator returns one or more results, it does so by pushing them on the
|
* operator returns one or more results, it does so by pushing them on the
|
||||||
* operand stack.
|
* operand stack.
|
||||||
*/
|
*/
|
||||||
var operandStack = [];
|
var operandStack = {
|
||||||
|
__innerStack__: [],
|
||||||
|
|
||||||
|
push: function(aOperand) {
|
||||||
|
this.__innerStack__.push(aOperand);
|
||||||
|
},
|
||||||
|
|
||||||
|
pop: function() {
|
||||||
|
return this.__innerStack__.pop();
|
||||||
|
},
|
||||||
|
|
||||||
|
peek: function() {
|
||||||
|
return this.__innerStack__[this.__innerStack__.length - 1];
|
||||||
|
},
|
||||||
|
|
||||||
|
get length() {
|
||||||
|
return this.__innerStack__.length;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Flag indicating if the topmost operand of the operandStack is an array
|
||||||
|
var operandIsArray = false;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The dictionary stack holds only dictionary objects. The current set of
|
* The dictionary stack holds only dictionary objects. The current set of
|
||||||
@ -28,7 +77,6 @@ var Type1Parser = function(aLexer) {
|
|||||||
var systemDict = new Dict(),
|
var systemDict = new Dict(),
|
||||||
globalDict = new Dict(),
|
globalDict = new Dict(),
|
||||||
userDict = new Dict();
|
userDict = new Dict();
|
||||||
|
|
||||||
|
|
||||||
var dictionaryStack = {
|
var dictionaryStack = {
|
||||||
__innerStack__: [systemDict, globalDict],
|
__innerStack__: [systemDict, globalDict],
|
||||||
@ -46,10 +94,12 @@ var Type1Parser = function(aLexer) {
|
|||||||
|
|
||||||
peek: function() {
|
peek: function() {
|
||||||
return this.__innerStack__[this.__innerStack__.length - 1];
|
return this.__innerStack__[this.__innerStack__.length - 1];
|
||||||
|
},
|
||||||
|
|
||||||
|
get length() {
|
||||||
|
return this.__innerStack__.length;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
var currentDict = dictionaryStack.peek();
|
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* The execution stack holds executable objects (mainly procedures and files)
|
* The execution stack holds executable objects (mainly procedures and files)
|
||||||
@ -61,150 +111,134 @@ var Type1Parser = function(aLexer) {
|
|||||||
* object off the execution stack and resumes executing the suspended object
|
* object off the execution stack and resumes executing the suspended object
|
||||||
* beneath it.
|
* beneath it.
|
||||||
*/
|
*/
|
||||||
var isExecutionStack = false;
|
|
||||||
var executionStack = [];
|
var executionStack = [];
|
||||||
|
|
||||||
/* Stub to inhibit the logs */
|
|
||||||
|
|
||||||
this.getObj = function() {
|
this.getObj = function() {
|
||||||
var obj = lexer.getObj();
|
var obj = lexer.getObj();
|
||||||
if (isExecutionStack && !IsCmd(obj, "}") && !IsCmd(obj, "]")) {
|
|
||||||
executionStack.push(obj);
|
if (operandIsArray && !IsCmd(obj, "}") && !IsCmd(obj, "]")) {
|
||||||
|
operandStack.peek().push(obj);
|
||||||
|
this.getObj();
|
||||||
|
} else if (IsCmd(obj, "{") || IsCmd(obj, "[")) {
|
||||||
|
dump("Start Array: " + obj);
|
||||||
|
operandStack.push([]);
|
||||||
|
operandIsArray = true;
|
||||||
|
this.getObj();
|
||||||
|
} else if (IsCmd(obj, "}") || IsCmd(obj, "]")) {
|
||||||
|
dump("End Array: " + obj);
|
||||||
|
operandIsArray = false;
|
||||||
this.getObj();
|
this.getObj();
|
||||||
} else if (IsBool(obj) || IsInt(obj) || IsNum(obj) || IsString(obj)) {
|
} else if (IsBool(obj) || IsInt(obj) || IsNum(obj) || IsString(obj)) {
|
||||||
log("Value: " + obj);
|
dump("Value: " + obj);
|
||||||
operandStack.push(obj);
|
operandStack.push(obj);
|
||||||
this.getObj();
|
this.getObj();
|
||||||
} else if (IsCmd(obj, "dup") || IsCmd(obj, "readonly") ||
|
} else if (IsCmd(obj, "dup")) {
|
||||||
IsCmd(obj, "currentdict") || IsCmd(obj, "currentfile")) {
|
dump("Duplicate");
|
||||||
|
operandStack.push(operandStack.peek());
|
||||||
|
this.getObj();
|
||||||
|
} else if (IsCmd(obj, "currentdict")) {
|
||||||
|
dump("currentdict");
|
||||||
|
operandStack.push(dictionaryStack.peek());
|
||||||
|
this.getObj();
|
||||||
|
} else if (IsCmd(obj, "systemdict")) {
|
||||||
|
dump("systemdict");
|
||||||
|
operandStack.push(systemDict);
|
||||||
|
this.getObj();
|
||||||
|
} else if (IsCmd(obj, "readonly") || IsCmd(obj, "executeonly") ||
|
||||||
|
IsCmd(obj, "currentfile")) {
|
||||||
// Do nothing for the moment
|
// Do nothing for the moment
|
||||||
this.getObj();
|
this.getObj();
|
||||||
} else if (IsName(obj)) {
|
} else if (IsName(obj)) {
|
||||||
log("Name: " + obj.name);
|
dump("Name: " + obj.name);
|
||||||
operandStack.push(obj.name);
|
operandStack.push(obj.name);
|
||||||
this.getObj();
|
this.getObj();
|
||||||
} else if (IsCmd(obj, "dict")) {
|
} else if (IsCmd(obj, "dict")) {
|
||||||
log("Dict: " + obj);
|
dump("Dict: " + obj);
|
||||||
|
|
||||||
// XXX handling of dictionary is wrong here
|
|
||||||
var size = operandStack.pop();
|
var size = operandStack.pop();
|
||||||
var name = operandStack.pop();
|
var dict = new Dict(size);
|
||||||
if (!name) {
|
operandStack.push(dict);
|
||||||
log ("Creating the global dict");
|
|
||||||
currentDict = dictionaryStack.peek();
|
|
||||||
} else {
|
|
||||||
var dict = new Dict();
|
|
||||||
log("Assign name: " + name + " for the dictionary");
|
|
||||||
currentDict.set(name, dict);
|
|
||||||
dictionaryStack.push(dict);
|
|
||||||
}
|
|
||||||
|
|
||||||
this.getObj();
|
this.getObj();
|
||||||
} else if (IsCmd(obj, "begin")) {
|
} else if (IsCmd(obj, "begin")) {
|
||||||
log("begin a dictionary");
|
dump("begin a dictionary");
|
||||||
currentDict = dictionaryStack.peek();
|
dictionaryStack.push(operandStack.pop());
|
||||||
this.getObj();
|
this.getObj();
|
||||||
} else if (IsCmd(obj, "end")) {
|
} else if (IsCmd(obj, "end")) {
|
||||||
log("Ending a dictionary");
|
dump("Ending a dictionary");
|
||||||
dictionaryStack.pop();
|
dictionaryStack.pop();
|
||||||
currentDict = dictionaryStack.peek();
|
|
||||||
this.getObj();
|
this.getObj();
|
||||||
} else if (IsCmd(obj, "def")) {
|
} else if (IsCmd(obj, "def")) {
|
||||||
if (executionStack.length) {
|
var value = operandStack.pop();
|
||||||
var value = [];
|
|
||||||
while (executionStack.length)
|
|
||||||
value.push(executionStack.shift());
|
|
||||||
} else {
|
|
||||||
var value = operandStack.pop();
|
|
||||||
}
|
|
||||||
|
|
||||||
var key = operandStack.pop();
|
var key = operandStack.pop();
|
||||||
// XXX this happen because of a bad way to handle dictionary
|
dump("def: " + key + " = " + value);
|
||||||
if (key) {
|
dictionaryStack.peek().set(key, value);
|
||||||
log("def: " + key + " = " + value);
|
|
||||||
currentDict.set(key, value);
|
|
||||||
}
|
|
||||||
this.getObj();
|
|
||||||
} else if (IsCmd(obj, "{")) {
|
|
||||||
log("Start Proc: " + obj);
|
|
||||||
executionStack = [];
|
|
||||||
isExecutionStack = true;
|
|
||||||
this.getObj();
|
|
||||||
} else if (IsCmd(obj, "}")) {
|
|
||||||
log("End Proc: " + obj);
|
|
||||||
isExecutionStack = false;
|
|
||||||
this.getObj();
|
|
||||||
} else if (IsCmd(obj, "[")) {
|
|
||||||
isExecutionStack = true;
|
|
||||||
executionStack = [];
|
|
||||||
this.getObj();
|
|
||||||
log("Start array: " + obj);
|
|
||||||
} else if (IsCmd(obj, "]")) {
|
|
||||||
log("End array: " + obj);
|
|
||||||
isExecutionStack = false;
|
|
||||||
this.getObj();
|
this.getObj();
|
||||||
} else if (IsCmd(obj, "eexec")) {
|
} else if (IsCmd(obj, "eexec")) {
|
||||||
return; // end of the ASCII header
|
// All the first segment data has been read, decrypt the second segment
|
||||||
|
// and start interpreting it in order to decode it
|
||||||
|
var eexecString = decrypt(aBinaryStream, kEexecEncryptionKey, 4).join("");
|
||||||
|
lexer = new Lexer(new StringStream(eexecString));
|
||||||
|
|
||||||
|
this.getObj();
|
||||||
|
} else if (IsCmd(obj, "known")) {
|
||||||
|
dump("known");
|
||||||
|
var name = operandStack.pop();
|
||||||
|
var dict = operandStack.pop();
|
||||||
|
// returns dict.hasKey(name);
|
||||||
|
|
||||||
|
this.getObj();
|
||||||
|
} else if (IsCmd(obj, "RD")) {
|
||||||
|
dump("RD");
|
||||||
|
var size = operandStack.pop();
|
||||||
|
var key = operandStack.pop();
|
||||||
|
|
||||||
|
var stream = lexer.stream.makeSubStream(lexer.stream.pos, size);
|
||||||
|
var charString = decrypt(stream, kCharStringsEncryptionKey, 4).join("");
|
||||||
|
|
||||||
|
// XXX do we want to store that on the top dictionary or somewhere else
|
||||||
|
dictionaryStack.peek().set(key, new StringStream(charString));
|
||||||
|
log (new StringStream(charString));
|
||||||
|
this.getObj();
|
||||||
|
} else if (IsCmd(obj, "LenIV")) {
|
||||||
|
error("LenIV: argh! we need to modify the length of discard characters for charStrings");
|
||||||
} else {
|
} else {
|
||||||
log("Getting an unknow token, adding it to the stack just in case");
|
dump("Getting an unknow token, adding it to the stack just in case");
|
||||||
log(obj);
|
dump(obj);
|
||||||
operandStack.push(obj);
|
operandStack.push(obj);
|
||||||
this.getObj();
|
this.getObj();
|
||||||
}
|
}
|
||||||
return currentDict;
|
|
||||||
|
return operandStack.peek();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
var hack = false;
|
var hack = false;
|
||||||
|
|
||||||
var Type1Font = function(aFontName, aFontFile) {
|
var Type1Font = function(aFontName, aFontFile) {
|
||||||
// All type1 font program should begin with the comment %!
|
// All Type1 font program should begin with the comment %!
|
||||||
var validHeader = aFontFile.getByte() == 0x25 && aFontFile.getByte() == 0x21;
|
var validHeader = aFontFile.getByte() == 0x25 && aFontFile.getByte() == 0x21;
|
||||||
if (!validHeader)
|
if (!validHeader)
|
||||||
error("Invalid file header");
|
error("Invalid file header");
|
||||||
|
|
||||||
var programType = "PS-AdobeFont";
|
var programType = "PS-AdobeFont";
|
||||||
for (var i = 0; i< programType.length; i++)
|
for (var i = 0; i < programType.length; i++)
|
||||||
aFontFile.getChar();
|
aFontFile.getChar();
|
||||||
|
|
||||||
// Ignore the '-' separator
|
// Ignore the '-' separator
|
||||||
aFontFile.getChar();
|
aFontFile.getChar();
|
||||||
|
|
||||||
var version = parseFloat(aFontFile.getChar() + aFontFile.getChar() + aFontFile.getChar());
|
var version = parseFloat(aFontFile.getChar() + aFontFile.getChar() + aFontFile.getChar());
|
||||||
|
|
||||||
if (!hack) {
|
if (!hack) {
|
||||||
log(aFontName);
|
log(aFontName);
|
||||||
log("Version is: " + version);
|
log("Version is: " + version);
|
||||||
|
|
||||||
var ASCIIStream = aFontFile.makeSubStream(0, aFontFile.dict.get("Length1"), aFontFile.dict);
|
var ASCIIStream = aFontFile.makeSubStream(0, aFontFile.dict.get("Length1"), aFontFile.dict);
|
||||||
this.parser = new Type1Parser(new Lexer(ASCIIStream));
|
var binaryStream = aFontFile.makeSubStream(aFontFile.dict.get("Length1"), aFontFile.dict.get("Length2"), aFontFile.dict);
|
||||||
|
|
||||||
|
this.parser = new Type1Parser(ASCIIStream, binaryStream);
|
||||||
|
|
||||||
var fontDictionary = this.parser.getObj();
|
var fontDictionary = this.parser.getObj();
|
||||||
log(fontDictionary + "\t" +
|
log(fontDictionary + "\t" + "fontInfo: " + fontDictionary.get("FontInfo"));
|
||||||
"fontInfo: " + fontDictionary.get("FontInfo") + "\t" +
|
|
||||||
"charStrings: " + fontDictionary.get("charStrings"));
|
|
||||||
|
|
||||||
var binaryStream = aFontFile.makeSubStream(aFontFile.dict.get("Length1"), aFontFile.dict.get("Length2"), aFontFile.dict);
|
|
||||||
function decrypt(aBinaryStream, aKey) {
|
|
||||||
var c1 = 52845, c2 = 22719;
|
|
||||||
var R = aKey;
|
|
||||||
|
|
||||||
var streamSize = aBinaryStream.length;
|
|
||||||
var decryptedString = [];
|
|
||||||
|
|
||||||
var value = null;
|
|
||||||
for (var i = 0; i < streamSize; i++) {
|
|
||||||
value = aBinaryStream.getByte();
|
|
||||||
decryptedString[i] = String.fromCharCode(value ^ (R >> 8));
|
|
||||||
R = ((value + R) * c1 + c2) & ((1 << 16) - 1);
|
|
||||||
}
|
|
||||||
return decryptedString.slice(4);
|
|
||||||
}
|
|
||||||
|
|
||||||
var eexecString = decrypt(binaryStream, 55665).join("");
|
|
||||||
log(eexecString);
|
|
||||||
|
|
||||||
TODO("decrypt charStrings data with the key 4330");
|
|
||||||
hack = true;
|
hack = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user