-
Notifications
You must be signed in to change notification settings - Fork 199
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: merge 4.1 dev into master #2818
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,10 @@ | ||
// loaders.gl, MIT license | ||
// Copyright (c) vis.gl contributors | ||
|
||
import {FileProvider, compareArrayBuffers} from '@loaders.gl/loader-utils'; | ||
import {FileProvider, compareArrayBuffers, concatenateArrayBuffers} from '@loaders.gl/loader-utils'; | ||
import {parseEoCDRecord} from './end-of-central-directory'; | ||
import {ZipSignature} from './search-from-the-end'; | ||
import {createZip64Info, NUMBER_SETTERS} from './zip64-info-generation'; | ||
|
||
/** | ||
* zip central directory file header info | ||
|
@@ -188,3 +189,167 @@ const findExpectedData = (zip64data: Zip64Data): {length: number; name: string}[ | |
|
||
return zip64dataList; | ||
}; | ||
|
||
/** info that can be placed into cd header */ | ||
type GenerateCDOptions = { | ||
/** CRC-32 of uncompressed data */ | ||
crc32: number; | ||
/** File name */ | ||
fileName: string; | ||
/** File size */ | ||
length: number; | ||
/** Relative offset of local file header */ | ||
offset: number; | ||
}; | ||
|
||
/** | ||
* generates cd header for the file | ||
* @param options info that can be placed into cd header | ||
* @returns buffer with header | ||
*/ | ||
export function generateCDHeader(options: GenerateCDOptions): ArrayBuffer { | ||
const optionsToUse = { | ||
...options, | ||
fnlength: options.fileName.length, | ||
extraLength: 0 | ||
}; | ||
|
||
let zip64header: ArrayBuffer = new ArrayBuffer(0); | ||
|
||
const optionsToZip64: any = {}; | ||
if (optionsToUse.offset >= 0xffffffff) { | ||
optionsToZip64.offset = optionsToUse.offset; | ||
optionsToUse.offset = 0xffffffff; | ||
} | ||
if (optionsToUse.length >= 0xffffffff) { | ||
optionsToZip64.size = optionsToUse.length; | ||
optionsToUse.length = 0xffffffff; | ||
} | ||
|
||
if (Object.keys(optionsToZip64).length) { | ||
zip64header = createZip64Info(optionsToZip64); | ||
optionsToUse.extraLength = zip64header.byteLength; | ||
} | ||
const header = new DataView(new ArrayBuffer(46)); | ||
|
||
for (const field of ZIP_HEADER_FIELDS) { | ||
NUMBER_SETTERS[field.size]( | ||
header, | ||
field.offset, | ||
optionsToUse[field.name ?? ''] ?? field.default ?? 0 | ||
); | ||
} | ||
|
||
const encodedName = new TextEncoder().encode(optionsToUse.fileName); | ||
|
||
const resHeader = concatenateArrayBuffers(header.buffer, encodedName, zip64header); | ||
|
||
return resHeader; | ||
} | ||
|
||
/** Fields map */ | ||
const ZIP_HEADER_FIELDS = [ | ||
{ | ||
offset: 0, | ||
size: 4, | ||
description: 'Central directory file header signature = 0x02014b50', | ||
default: new DataView(signature.buffer).getUint32(0, true) | ||
}, | ||
{ | ||
offset: 4, | ||
size: 2, | ||
description: 'Version made by', | ||
default: 45 | ||
}, | ||
{ | ||
offset: 6, | ||
size: 2, | ||
description: 'Version needed to extract (minimum)', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do we actually use all these strings or are they just nice to have? They do add to the size of the generated code. |
||
default: 45 | ||
}, | ||
{ | ||
offset: 8, | ||
size: 2, | ||
description: 'General purpose bit flag', | ||
default: 0 | ||
}, | ||
{ | ||
offset: 10, | ||
size: 2, | ||
description: 'Compression method', | ||
default: 0 | ||
}, | ||
{ | ||
offset: 12, | ||
size: 2, | ||
description: 'File last modification time', | ||
default: 0 | ||
}, | ||
{ | ||
offset: 14, | ||
size: 2, | ||
description: 'File last modification date', | ||
default: 0 | ||
}, | ||
{ | ||
offset: 16, | ||
size: 4, | ||
description: 'CRC-32 of uncompressed data', | ||
name: 'crc32' | ||
}, | ||
{ | ||
offset: 20, | ||
size: 4, | ||
description: 'Compressed size (or 0xffffffff for ZIP64)', | ||
name: 'length' | ||
}, | ||
{ | ||
offset: 24, | ||
size: 4, | ||
description: 'Uncompressed size (or 0xffffffff for ZIP64)', | ||
name: 'length' | ||
}, | ||
{ | ||
offset: 28, | ||
size: 2, | ||
description: 'File name length (n)', | ||
name: 'fnlength' | ||
}, | ||
{ | ||
offset: 30, | ||
size: 2, | ||
description: 'Extra field length (m)', | ||
default: 0, | ||
name: 'extraLength' | ||
}, | ||
{ | ||
offset: 32, | ||
size: 2, | ||
description: 'File comment length (k)', | ||
default: 0 | ||
}, | ||
{ | ||
offset: 34, | ||
size: 2, | ||
description: 'Disk number where file starts (or 0xffff for ZIP64)', | ||
default: 0 | ||
}, | ||
{ | ||
offset: 36, | ||
size: 2, | ||
description: 'Internal file attributes', | ||
default: 0 | ||
}, | ||
{ | ||
offset: 38, | ||
size: 4, | ||
description: 'External file attributes', | ||
default: 0 | ||
}, | ||
{ | ||
offset: 42, | ||
size: 4, | ||
description: 'Relative offset of local file header ', | ||
name: 'offset' | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import {concatenateArrayBuffers} from '@loaders.gl/loader-utils'; | ||
|
||
export const signature = new Uint8Array([0x01, 0x00]); | ||
|
||
/** info that can be placed into zip64 field, doc: https://en.wikipedia.org/wiki/ZIP_(file_format)#ZIP64 */ | ||
type Zip64Options = { | ||
/** Original uncompressed file size and Size of compressed data */ | ||
size?: number; | ||
/** Offset of local header record */ | ||
offset?: number; | ||
}; | ||
|
||
/** | ||
* creates zip64 extra field | ||
* @param options info that can be placed into zip64 field | ||
* @returns buffer with field | ||
*/ | ||
export function createZip64Info(options: Zip64Options): ArrayBuffer { | ||
const optionsToUse = { | ||
...options, | ||
zip64Length: (options.offset ? 1 : 0) * 8 + (options.size ? 1 : 0) * 16 | ||
}; | ||
|
||
const arraysToConcat: ArrayBuffer[] = []; | ||
|
||
for (const field of ZIP64_FIELDS) { | ||
if (!optionsToUse[field.name ?? ''] && !field.default) { | ||
continue; | ||
} | ||
const newValue = new DataView(new ArrayBuffer(field.size)); | ||
NUMBER_SETTERS[field.size](newValue, 0, optionsToUse[field.name ?? ''] ?? field.default); | ||
arraysToConcat.push(newValue.buffer); | ||
} | ||
|
||
return concatenateArrayBuffers(...arraysToConcat); | ||
} | ||
|
||
/** | ||
* Function to write values into buffer | ||
* @param header buffer where to write a value | ||
* @param offset offset of the writing start | ||
* @param value value to be written | ||
*/ | ||
type NumberSetter = (header: DataView, offset: number, value: number) => void; | ||
|
||
/** functions to write values into buffer according to the bytes amount */ | ||
export const NUMBER_SETTERS: {[key: number]: NumberSetter} = { | ||
2: (header, offset, value) => { | ||
header.setUint16(offset, value, true); | ||
}, | ||
4: (header, offset, value) => { | ||
header.setUint32(offset, value, true); | ||
}, | ||
8: (header, offset, value) => { | ||
header.setBigUint64(offset, BigInt(value), true); | ||
} | ||
}; | ||
|
||
/** zip64 info fields description, we need it as a pattern to build a zip64 info */ | ||
const ZIP64_FIELDS = [ | ||
{ | ||
size: 2, | ||
description: 'Header ID 0x0001', | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto |
||
default: new DataView(signature.buffer).getUint16(0, true) | ||
}, | ||
{ | ||
size: 2, | ||
description: 'Size of the extra field chunk (8, 16, 24 or 28)', | ||
name: 'zip64Length' | ||
}, | ||
{ | ||
size: 8, | ||
description: 'Original uncompressed file size', | ||
name: 'size' | ||
}, | ||
{ | ||
size: 8, | ||
description: 'Size of compressed data', | ||
name: 'size' | ||
}, | ||
{ | ||
size: 8, | ||
description: 'Offset of local header record', | ||
name: 'offset' | ||
} | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we export is as cdSignature so that we don't need to rename it?
For constants we often use
CAPITAL_CASE
i.e.CD_SIGNATURE