Browse Source

Added RTF extractor

n1474335 6 năm trước cách đây
mục cha
commit
19b7957523
1 tập tin đã thay đổi với 42 bổ sung2 xóa
  1. 42 2
      src/core/lib/FileSignatures.mjs

+ 42 - 2
src/core/lib/FileSignatures.mjs

@@ -572,7 +572,7 @@ export const FILE_SIGNATURES = {
                 3: 0x74,
                 4: 0x66
             },
-            extractor: null
+            extractor: extractRTF
         },
         {
             name: "Microsoft Office documents/OLE2",
@@ -1307,7 +1307,7 @@ export function extractFLV(bytes, offset) {
     stream.moveForwardsBy(headerSize - 9);
 
     let tagSize = -11; // Fake size of previous tag header
-    while (stream.position < stream.length) {
+    while (stream.hasMore()) {
         const prevTagSize = stream.readInt(4, "be");
         const tagType = stream.readInt(1, "be");
 
@@ -1332,3 +1332,43 @@ export function extractFLV(bytes, offset) {
 
     return stream.carve();
 }
+
+
+/**
+ * RTF extractor.
+ *
+ * @param {Uint8Array} bytes
+ * @param {number} offset
+ * @returns {Uint8Array}
+ */
+export function extractRTF(bytes, offset) {
+    const stream = new Stream(bytes.slice(offset));
+
+    let openTags = 0;
+
+    if (stream.readInt(1, "be") !== 0x7b) { // {
+        throw new Error("Not a valid RTF file");
+    } else {
+        openTags++;
+    }
+
+    while (openTags > 0 && stream.hasMore()) {
+        switch (stream.readInt(1, "be")) {
+            case 0x7b: // {
+                openTags++;
+                break;
+            case 0x7d: // }
+                openTags--;
+                break;
+            case 0x5c: // \
+                // Consume any more escapes and then skip over the next character
+                stream.consumeIf(0x5c);
+                stream.position++;
+                break;
+            default:
+                break;
+        }
+    }
+
+    return stream.carve();
+}