usd-gltf 0.1.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/usd/usdz.js CHANGED
@@ -3,6 +3,7 @@
3
3
 
4
4
  import { crc32 } from '../util/crc32.js';
5
5
  import { inflate } from '../image/inflate.js';
6
+ import { Budget } from '../limits.js';
6
7
 
7
8
  const LOCAL = 0x04034b50;
8
9
  const CENTRAL = 0x02014b50;
@@ -12,9 +13,10 @@ const ALIGN = 64;
12
13
  /**
13
14
  * Lists the files in a zip, in archive order.
14
15
  * @param {Uint8Array} bytes
16
+ * @param {{ budget?: import('../limits.js').Budget }} [options]
15
17
  * @returns {Map<string, Uint8Array>}
16
18
  */
17
- export function readZip( bytes ) {
19
+ export function readZip( bytes, { budget = new Budget() } = {} ) {
18
20
 
19
21
  const view = new DataView( bytes.buffer, bytes.byteOffset, bytes.byteLength );
20
22
  const files = new Map();
@@ -33,6 +35,7 @@ export function readZip( bytes ) {
33
35
 
34
36
  const method = view.getUint16( offset + 10, true );
35
37
  const compressedSize = view.getUint32( offset + 20, true );
38
+ const size = view.getUint32( offset + 24, true );
36
39
  const nameLength = view.getUint16( offset + 28, true );
37
40
  const extraLength = view.getUint16( offset + 30, true );
38
41
  const commentLength = view.getUint16( offset + 32, true );
@@ -40,12 +43,13 @@ export function readZip( bytes ) {
40
43
  const name = decoder.decode( bytes.subarray( offset + 46, offset + 46 + nameLength ) );
41
44
 
42
45
  const dataStart = localOffset + 30 + view.getUint16( localOffset + 26, true ) + view.getUint16( localOffset + 28, true );
46
+ if ( dataStart + compressedSize > bytes.length ) throw new Error( 'usd-gltf: truncated zip archive' );
43
47
  const data = bytes.subarray( dataStart, dataStart + compressedSize );
44
48
 
45
49
  if ( ! name.endsWith( '/' ) ) {
46
50
 
47
51
  if ( method === 0 ) files.set( name, data );
48
- else if ( method === 8 ) files.set( name, inflate( data ) );
52
+ else if ( method === 8 ) { budget.decode( size, 'zip' ); files.set( name, inflate( data, size, size ) ); }
49
53
  else throw new Error( `usd-gltf: unsupported zip compression ${ method } for ${ name }` );
50
54
 
51
55
  }
@@ -61,9 +65,10 @@ export function readZip( bytes ) {
61
65
  /**
62
66
  * Writes a USDZ package. The first entry must be the root layer.
63
67
  * @param {Array<{ name: string, data: Uint8Array }>} entries
68
+ * @param {{ budget?: import('../limits.js').Budget }} [options]
64
69
  * @returns {Uint8Array}
65
70
  */
66
- export function writeUSDZ( entries ) {
71
+ export function writeUSDZ( entries, { budget = new Budget() } = {} ) {
67
72
 
68
73
  const encoder = new TextEncoder();
69
74
  const locals = [];
@@ -127,6 +132,7 @@ export function writeUSDZ( entries ) {
127
132
  ev.setUint32( 12, centralSize, true );
128
133
  ev.setUint32( 16, offset, true );
129
134
 
135
+ budget.output( offset + centralSize + 22, 'usdz' );
130
136
  const out = new Uint8Array( offset + centralSize + 22 );
131
137
  let p = 0;
132
138
  for ( const part of [ ...locals, ...centrals, end ] ) {