簡體   English   中英

js DataView 從 BigInt 偏移讀取

[英]js DataView read from BigInt offset

我正在編寫代碼以在瀏覽器中讀取 gimp XCF 文件,並且從 XCF 版本 11 及更高版本開始,為了 > 4GB XCF 文件,“POINTER”數據類型是 64 位而不是 32 位。

我的問題是我不認為 DataView 能夠用它的 get* 函數處理 BigInt 偏移量。 從數據視圖中讀取 BigInt 是的,在 BigInt 偏移處讀取沒有。

有沒有其他人遇到過這種情況,如果有,您是如何處理的?

我的代碼片段:

word(signed: boolean = true): number {
  let result = signed ?
  this.view.getInt32(this.offset, false) :
  this.view.getUint32(this.offset, false);

  this.offset += 4;
  return result;
}

word64(signed: boolean = true): BigInt {
  let result = signed ?
  this.view.getBigInt64(this.offset, false) :
  this.view.getBigUint64(this.offset, false);

  this.offset += 8;
  return result;
}

pointer(xcfVersion: number): number | BigInt {
  if (xcfVersion < 11) {
    return this.word(false);
  } else {
    return this.word64(false);
  }
}

//....

let nv = 11; //XCF version 11

let ptr: BigInt|number = 0;
let layerPointers = new Array<BigInt|number>();

while ( (ptr = this.pointer(nv)) != 0) {
  console.log("layer pointer", ptr);
  layerPointers.push(ptr);
  if (ptr > this.view.byteLength) {
    console.log("gtr than allowed");
  }
  
  //Now I want to read 'this.view' at 'ptr' offset
  this.view.getUint32 (ptr);
  
  //yields: Uncaught TypeError: can't convert BigInt to number
}

解決方案是將 64 位指針轉換為數字:

pointer(xcfVersion: number): number {
  if (xcfVersion < 11) {
    return this.word(false);
  } else {
    return Number(this.word64(false));
  }
}

當然,這不會涵蓋完整的 64 位范圍,它只能安全到2**53 但這很好,因為ArrayBuffer被指定為最大大小為2**53 ,因此任何可以創建的 ArrayBuffer 都可以通過這種方式完全索引。

(在您認為這是一個限制之前,請花點時間考慮一下這個數字有多大,以及 XCF 文件可能達到這個大小需要多長時間。)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM