From ae867e3a9c98dd55d8dbb250771b8ef891b88dcb Mon Sep 17 00:00:00 2001 From: Saebekassebil Date: Sat, 9 Jul 2011 00:30:46 +0200 Subject: [PATCH] Using map for hex conversion and left-shift for secure division --- pdf.js | 41 ++++++++++++++++++++++++++++++++++------- 1 file changed, 34 insertions(+), 7 deletions(-) diff --git a/pdf.js b/pdf.js index 563b9ca3f..69a25f051 100644 --- a/pdf.js +++ b/pdf.js @@ -912,28 +912,55 @@ var AsciiHexStream = (function() { DecodeStream.call(this); } + var hexvalueMap = { + 9: -1, // \t + 32: -1, // space + 48: 0, + 49: 1, + 50: 2, + 51: 3, + 52: 4, + 53: 5, + 54: 6, + 55: 7, + 56: 8, + 57: 9, + 65: 10, + 66: 11, + 67: 12, + 68: 13, + 69: 14, + 70: 15, + 97: 10, + 98: 11, + 99: 12, + 100: 13, + 101: 14, + 102: 15 + }; + constructor.prototype = Object.create(DecodeStream.prototype); constructor.prototype.readBlock = function() { var gtCode = '>'.charCodeAt(0), bytes = this.str.getBytes(), c, n, decodeLength, buffer, bufferLength, i, length; - decodeLength = (bytes.length + 1) / 2; + decodeLength = (bytes.length + 1) >> 1; buffer = this.ensureBuffer(this.bufferLength + decodeLength); bufferLength = this.bufferLength; for(i = 0, length = bytes.length; i < length; i++) { - c = String.fromCharCode(bytes[i]); - while (Lexer.isSpace(c) && (i+1) < length) { - c = String.fromCharCode(bytes[++i]); + c = hexvalueMap[bytes[i]]; + while (c == -1 && (i+1) < length) { + c = hexvalueMap[bytes[++i]]; } if((i+1) < length && (bytes[i+1] !== gtCode)) { - n = String.fromCharCode(bytes[++i]); - buffer[bufferLength++] = Number('0x'+c+n); + n = hexvalueMap[bytes[++i]]; + buffer[bufferLength++] = c*16+n; } else { if(bytes[i] !== gtCode) { // EOD marker at an odd number, behave as if a 0 followed the last digit. - buffer[bufferLength++] = Number('0x'+c+'0'); + buffer[bufferLength++] = c*16; } } }