Add a new BaseStream.getString(...) method to replace manual bytesToString(BaseStream.getBytes(...)) calls

Given that the `bytesToString(BaseStream.getBytes(...))` pattern is somewhat common throughout the `src/core/` code, it cannot hurt to add a new `BaseStream`-method which handles that case internally.
This commit is contained in:
Jonas Jenwald 2021-05-01 12:11:09 +02:00
parent f6f335173d
commit 3624f9eac7
6 changed files with 33 additions and 32 deletions

View File

@ -13,7 +13,7 @@
* limitations under the License. * limitations under the License.
*/ */
import { shadow, unreachable } from "../shared/util.js"; import { bytesToString, shadow, unreachable } from "../shared/util.js";
class BaseStream { class BaseStream {
constructor() { constructor() {
@ -79,6 +79,10 @@ class BaseStream {
unreachable("Abstract method `getByteRange` called"); unreachable("Abstract method `getByteRange` called");
} }
getString(length) {
return bytesToString(this.getBytes(length, /* forceClamped = */ false));
}
skip(n) { skip(n) {
this.pos += n || 1; this.pos += n || 1;
} }

View File

@ -13,23 +13,6 @@
* limitations under the License. * limitations under the License.
*/ */
import {
bytesToString,
createPromiseCapability,
createValidAbsoluteUrl,
DocumentActionEventType,
FormatError,
info,
isBool,
isNum,
isString,
objectSize,
PermissionFlag,
shadow,
stringToPDFString,
stringToUTF8String,
warn,
} from "../shared/util.js";
import { import {
clearPrimitiveCaches, clearPrimitiveCaches,
Dict, Dict,
@ -46,6 +29,22 @@ import {
MissingDataException, MissingDataException,
toRomanNumerals, toRomanNumerals,
} from "./core_utils.js"; } from "./core_utils.js";
import {
createPromiseCapability,
createValidAbsoluteUrl,
DocumentActionEventType,
FormatError,
info,
isBool,
isNum,
isString,
objectSize,
PermissionFlag,
shadow,
stringToPDFString,
stringToUTF8String,
warn,
} from "../shared/util.js";
import { NameTree, NumberTree } from "./name_number_tree.js"; import { NameTree, NumberTree } from "./name_number_tree.js";
import { ColorSpace } from "./colorspace.js"; import { ColorSpace } from "./colorspace.js";
import { FileSpec } from "./file_spec.js"; import { FileSpec } from "./file_spec.js";
@ -136,7 +135,7 @@ class Catalog {
// charsets, let's just hope that the author of the PDF was reasonable // charsets, let's just hope that the author of the PDF was reasonable
// enough to stick with the XML default charset, which is UTF-8. // enough to stick with the XML default charset, which is UTF-8.
try { try {
const data = stringToUTF8String(bytesToString(stream.getBytes())); const data = stringToUTF8String(stream.getString());
if (data) { if (data) {
metadata = new MetadataParser(data).serializable; metadata = new MetadataParser(data).serializable;
} }
@ -906,7 +905,7 @@ class Catalog {
let js = jsDict.get("JS"); let js = jsDict.get("JS");
if (isStream(js)) { if (isStream(js)) {
js = bytesToString(js.getBytes()); js = js.getString();
} else if (typeof js !== "string") { } else if (typeof js !== "string") {
return; return;
} }
@ -1344,7 +1343,7 @@ class Catalog {
let js; let js;
if (isStream(jsAction)) { if (isStream(jsAction)) {
js = bytesToString(jsAction.getBytes()); js = jsAction.getString();
} else if (isString(jsAction)) { } else if (isString(jsAction)) {
js = jsAction; js = jsAction;
} }

View File

@ -16,7 +16,6 @@
import { import {
assert, assert,
BaseException, BaseException,
bytesToString,
objectSize, objectSize,
stringToPDFString, stringToPDFString,
warn, warn,
@ -269,7 +268,7 @@ function _collectJS(entry, xref, list, parents) {
const js = entry.get("JS"); const js = entry.get("JS");
let code; let code;
if (isStream(js)) { if (isStream(js)) {
code = bytesToString(js.getBytes()); code = js.getString();
} else { } else {
code = js; code = js;
} }

View File

@ -15,7 +15,6 @@
import { import {
assert, assert,
bytesToString,
FormatError, FormatError,
info, info,
InvalidPDFException, InvalidPDFException,
@ -811,7 +810,7 @@ class PDFDocument {
}; };
if (isStream(xfa) && !xfa.isEmpty) { if (isStream(xfa) && !xfa.isEmpty) {
try { try {
entries["xdp:xdp"] = stringToUTF8String(bytesToString(xfa.getBytes())); entries["xdp:xdp"] = stringToUTF8String(xfa.getString());
return entries; return entries;
} catch (_) { } catch (_) {
warn("XFA - Invalid utf-8 string."); warn("XFA - Invalid utf-8 string.");
@ -841,7 +840,7 @@ class PDFDocument {
continue; continue;
} }
try { try {
entries[name] = stringToUTF8String(bytesToString(data.getBytes())); entries[name] = stringToUTF8String(data.getString());
} catch (_) { } catch (_) {
warn("XFA - Invalid utf-8 string."); warn("XFA - Invalid utf-8 string.");
return null; return null;

View File

@ -1486,7 +1486,7 @@ var Font = (function FontClosure() {
} }
function readTableEntry(file) { function readTableEntry(file) {
var tag = bytesToString(file.getBytes(4)); var tag = file.getString(4);
var checksum = file.getInt32() >>> 0; var checksum = file.getInt32() >>> 0;
var offset = file.getInt32() >>> 0; var offset = file.getInt32() >>> 0;
@ -1516,7 +1516,7 @@ var Font = (function FontClosure() {
function readOpenTypeHeader(ttf) { function readOpenTypeHeader(ttf) {
return { return {
version: bytesToString(ttf.getBytes(4)), version: ttf.getString(4),
numTables: ttf.getUint16(), numTables: ttf.getUint16(),
searchRange: ttf.getUint16(), searchRange: ttf.getUint16(),
entrySelector: ttf.getUint16(), entrySelector: ttf.getUint16(),
@ -1525,7 +1525,7 @@ var Font = (function FontClosure() {
} }
function readTrueTypeCollectionHeader(ttc) { function readTrueTypeCollectionHeader(ttc) {
const ttcTag = bytesToString(ttc.getBytes(4)); const ttcTag = ttc.getString(4);
assert(ttcTag === "ttcf", "Must be a TrueType Collection font."); assert(ttcTag === "ttcf", "Must be a TrueType Collection font.");
const majorVersion = ttc.getUint16(); const majorVersion = ttc.getUint16();
@ -2344,7 +2344,7 @@ var Font = (function FontClosure() {
} }
names[1][nameIndex] = str; names[1][nameIndex] = str;
} else { } else {
names[0][nameIndex] = bytesToString(font.getBytes(record.length)); names[0][nameIndex] = font.getString(record.length);
} }
} }
return names; return names;

View File

@ -31,7 +31,7 @@ function writeDict(dict, buffer, transform) {
function writeStream(stream, buffer, transform) { function writeStream(stream, buffer, transform) {
writeDict(stream.dict, buffer, transform); writeDict(stream.dict, buffer, transform);
buffer.push(" stream\n"); buffer.push(" stream\n");
let string = bytesToString(stream.getBytes()); let string = stream.getString();
if (transform !== null) { if (transform !== null) {
string = transform.encryptString(string); string = transform.encryptString(string);
} }
@ -129,7 +129,7 @@ function updateXFA(datasetsRef, newRefs, xref) {
return; return;
} }
const datasets = xref.fetchIfRef(datasetsRef); const datasets = xref.fetchIfRef(datasetsRef);
const str = bytesToString(datasets.getBytes()); const str = datasets.getString();
const xml = new SimpleXMLParser({ hasAttributes: true }).parseFromString(str); const xml = new SimpleXMLParser({ hasAttributes: true }).parseFromString(str);
for (const { xfa } of newRefs) { for (const { xfa } of newRefs) {