فهرست منبع

Update view.js

Lutz Roeder 2 سال پیش
والد
کامیت
47da9b88a5
4فایلهای تغییر یافته به همراه38 افزوده شده و 8 حذف شده
  1. 7 4
      source/darknet.js
  2. 17 1
      source/hdf5.js
  3. 9 0
      source/view.js
  4. 5 3
      source/zip.js

+ 7 - 4
source/darknet.js

@@ -1067,21 +1067,24 @@ darknet.Weights = class {
             const major = reader.int32();
             const minor = reader.int32();
             reader.int32(); // revision
+            reader.seek(0);
             const transpose = (major > 1000) || (minor > 1000);
             if (!transpose) {
-                reader.skip((major * 10 + minor) >= 2 ? 8 : 4);
-                return new darknet.Weights(reader);
+                const offset = 12 + ((major * 10 + minor) >= 2 ? 8 : 4);
+                return new darknet.Weights(reader, offset);
             }
-            reader.seek(0);
         }
         return null;
     }
 
-    constructor(reader) {
+    constructor(reader, offset) {
         this._reader = reader;
+        this._offset = offset;
     }
 
     read(size) {
+        this._reader.skip(this._offset);
+        this._offset = 0;
         return this._reader.read(size);
     }
 

+ 17 - 1
source/hdf5.js

@@ -139,6 +139,7 @@ hdf5.Group = class {
         if (!this._dataObjectHeader) {
             const reader = this._reader.at(this._entry.objectHeaderAddress);
             this._dataObjectHeader = new hdf5.DataObjectHeader(reader);
+            this._reader.seek(0);
         }
         if (!this._attributes) {
             this._attributes = new Map();
@@ -155,6 +156,7 @@ hdf5.Group = class {
             if (datatype && dataspace && dataLayout) {
                 this._value = new hdf5.Variable(this._reader, this._globalHeap, datatype, dataspace, dataLayout, filterPipeline);
             }
+            this._reader.seek(0);
         }
     }
 
@@ -487,6 +489,13 @@ hdf5.BinaryReader = class extends hdf5.Reader {
         return position;
     }
 
+    seek(position) {
+        this._position = position >= 0 ? position : this._length + position;
+        if (this._position > this._buffer.length) {
+            throw new Error(`Expected ${this._position - this._buffer.length} more bytes. The file might be corrupted. Unexpected end of file.`);
+        }
+    }
+
     skip(offset) {
         this._position += offset;
         if (this._offset + this._position > this._buffer.length) {
@@ -555,6 +564,13 @@ hdf5.StreamReader = class extends hdf5.Reader {
         return this._offset + this._position;
     }
 
+    seek(position) {
+        if (position > this._length) {
+            throw new Error(`Expected ${this._position - this._buffer.length} more bytes. The file might be corrupted. Unexpected end of file.`);
+        }
+        this._stream.seek(position);
+    }
+
     skip(offset) {
         this._position += offset;
         if (this._position > this._length) {
@@ -1035,7 +1051,7 @@ hdf5.Datatype = class {
             case 3: // string
                 return 'string';
             case 5: // opaque
-                return 'uint8[]';
+                return 'uint8';
             case 6: // compound
                 return 'compound';
             case 8: // enumerated

+ 9 - 0
source/view.js

@@ -5727,6 +5727,9 @@ view.ModelFactoryService = class {
             const factory = await this._require(module);
             /* eslint-enable no-await-in-loop */
             factory.match(context);
+            if (context.stream && context.stream.position !== 0) {
+                throw new view.Error('Invalid stream position.');
+            }
             if (context.type) {
                 try {
                     /* eslint-disable no-await-in-loop */
@@ -5745,6 +5748,9 @@ view.ModelFactoryService = class {
                     errors.push(error);
                 }
             }
+            if (context.stream && context.stream.position !== 0) {
+                throw new view.Error('Invalid stream position.');
+            }
         }
         if (errors.length > 0) {
             if (errors.length === 1) {
@@ -5782,6 +5788,9 @@ view.ModelFactoryService = class {
                         const factory = await this._require(module);
                         /* eslint-enable no-await-in-loop */
                         factory.match(context);
+                        if (context.stream && context.stream.position !== 0) {
+                            throw new view.Error('Invalid stream position.');
+                        }
                         delete context.target;
                         if (context.type) {
                             matches = matches.filter((match) => !factory.filter || factory.filter(context, match.type));

+ 5 - 3
source/zip.js

@@ -607,10 +607,12 @@ zip.InflaterStream = class {
     }
 
     seek(position) {
-        if (this._buffer === undefined) {
-            this._inflate();
+        if (position !== this._position) {
+            if (this._buffer === undefined) {
+                this._inflate();
+            }
+            this._position = position >= 0 ? position : this._length + position;
         }
-        this._position = position >= 0 ? position : this._length + position;
     }
 
     skip(offset) {