Public
Edited
Apr 23
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
/// This is fun!
/// https://docs.oracle.com/javase/specs/jvms/se6/html/ClassFile.doc.html
java_class_protocol = combine([
{ protocol: exact(new Uint8Array([0xca, 0xfe, 0xba, 0xbe])) },
{ name: "minor_version", protocol: bytes.uint16 },
{ name: "major_version", protocol: bytes.uint16 },
{ name: "constant_pool_count", protocol: bytes.uint16 },
{ name: "rest", protocol: uint8array }
])
Insert cell
display_java = async (file) => {
let x = 0xcafebabe;

let { rest, ...data } = consume(
java_class_protocol,
new Uint8Array(await file.arrayBuffer())
);

return htl.html`<pre style=${{ padding: "16px" }}>${JSON.stringify(
data,
null,
2
)}</pre>`;
}
Insert cell
Insert cell
Insert cell
example_zip = FileAttachment("import-sample-single-version.zip")
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
END_OF_DIRECTORY_SIGNATURE = new TextEncoder().encode("PK\x05\x06")
Insert cell
expected_end_of_directory = new Uint8Array(
await zip_blob.slice(-22).arrayBuffer()
)
Insert cell
Insert cell
Insert cell
Insert cell
end_of_central_directory_protocol = combine([
{ protocol: exact(string.encode("PK\x05\x06")) },
{
name: "disk",
protocol: bytes.uint16le
},
{
name: "start_disk",
protocol: bytes.uint16le
},
{
name: "records_on_disk",
protocol: bytes.uint16le
},
{
name: "records",
protocol: bytes.uint16le
},
{
name: "size_of_central_directory",
protocol: bytes.uint32le
},
{
name: "offset_of_central_directory",
protocol: bytes.uint32le
},
{
name: "comment",
protocol: with_length(bytes.uint16le, string)
}
])
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
central_directory_protocol = {
let base_protocol = combine([
{ protocol: exact(string.encode("PK\x01\x02")) },
{ name: "version_made_by", protocol: bytes.uint16le },
{ name: "version_required", protocol: bytes.uint16le },
// { name: "raw_flags", protocol: bytes.uint16le },
{
name: "flags_1",
protocol: flags([
"encrypted",
"compression_1",
"compression_2",
"crc32_at_the_end",
null,
"is_compressed_patched_data",
"strong_encryption",
null
])
},
{
name: "flags_2",
protocol: flags([null, null, null, "use_utf8", null, null, null, null])
},
{
name: "compression",
protocol: switch_protocol(
bytes.uint16le,
new Map([
[0, "uncompressed"],
[8, "deflate"]
])
)
},
{ name: "modification_time", protocol: bytes.uint16le },
{ name: "modification_date", protocol: bytes.uint16le },
{ name: "crc_32", protocol: bytes.uint32le },
{ name: "compressed_size", protocol: bytes.uint32le },
{ name: "uncompressed_size", protocol: bytes.uint32le },
{ name: "name_length", protocol: bytes.uint16le },
{ name: "extra_field_length", protocol: bytes.uint16le },
{ name: "comment_length", protocol: bytes.uint16le },
{ name: "start_disk_number", protocol: bytes.uint16le },
{ name: "internal_attributes", protocol: bytes.uint16le },
{ name: "external_attributes", protocol: bytes.uint32le },
{ name: "offset", protocol: bytes.uint32le }
]);

return just_repeat({
decode: (buffer) => {
/// First we parse the data that has a set length,
/// that contains the information about the length of the following attributes
let [
{
name_length,
compression,
flags_1,
flags_2,
extra_field_length,
comment_length,
modification_time,
modification_date,
...data
},
base_offset
] = base_protocol.decode(buffer);

///
let [{ file_name, extra_field, comment }, rest_offset] = combine([
{
name: "file_name",
protocol: with_exact_length(name_length, string)
},
{
name: "extra_field",
protocol: with_exact_length(
extra_field_length,
just_repeat(
combine([
{
name: "header",
protocol: bytes.uint16le
},
{
name: "value",
protocol: with_length(bytes.uint16le, uint8array)
}
])
)
)
},
{
name: "comment",
protocol: with_exact_length(comment_length, string)
}
]).decode(buffer.subarray(base_offset));

return [
{
...data,
compression: compression,
last_modified: parseDosDateTime(modification_date, modification_time),
file_name_length: name_length,
file_name: file_name,
extra_field_length: extra_field_length,
extra_field: extra_field,
comment_length: comment_length,
comment: comment,
flags: new Set([...flags_1, ...flags_2])
},
base_offset + rest_offset
];
}
});
}
Insert cell
Insert cell
Insert cell
local_file_header_protocol = combine([
{ protocol: exact(string.encode("PK\x03\x04")) },
{ name: "version_required", protocol: bytes.uint16le },
{ name: "flags", protocol: bytes.uint16le },
{
name: "compression",
protocol: switch_protocol(
bytes.uint16le,
new Map([
[0, "uncompressed"],
[8, "deflate"]
])
)
},
{ name: "modification_time", protocol: bytes.uint16le },
{ name: "modification_date", protocol: bytes.uint16le },
{ name: "crc_32", protocol: bytes.uint32le },
{ name: "compressed_size", protocol: bytes.uint32le },
{ name: "uncompressed_size", protocol: bytes.uint32le },
{ name: "name_length", protocol: bytes.uint16le },
{ name: "extra_field_length", protocol: bytes.uint16le }
])
Insert cell
selected_file
Insert cell
selected_file_entry_size = selected_file.offset +
30 +
selected_file.file_name_length +
selected_file.extra_field_length
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
gzip_header_protocol = combine([
/// ID
{ protocol: exact(new Uint8Array([0x1f, 0x8b])) },
/// Compression: 8 = DEFLATE
{ protocol: exact(bytes.uint8.encode(8)) },
/// Flags - Sets some extra flags that allow for extra data.
/// ....... If we set it to 0 no extra data is expected, which is great!
{ protocol: exact(bytes.uint8.encode(0)) },
/// MTIME - Modification time in Unix format
/// IGNORED BY DECOMPRESSIONSTREAM
{ name: "modification_time", protocol: bytes.uint32le },
/// XFL - 0x00000010 is slowest compression, 0x00000100 is fastest
/// IGNORED BY DECOMPRESSIONSTREAM
{ name: "extra_flags", protocol: bytes.uint8 },
/// OS 255 = UNKNOWN
{ name: "OS", protocol: bytes.uint8 }
])
Insert cell
gzip_trailer_protocol = combine([
{ name: "crc32", protocol: bytes.uint32le },
{ name: "uncompressed_size", protocol: bytes.uint32le }
])
Insert cell
Insert cell
result_file = new LazyFile(
{
byteLength: selected_file.uncompressed_size,
stream(start, end) {
// ... read the file contents from somewhere and return a ReadableStream
let data = zip_blob
.slice(
local_file_header[1],
local_file_header[1] + selected_file.compressed_size
)
.stream();

if (selected_file.compression === "deflate") {
if (STRICT_CRC) {
return concat_streams([
ReadableStreamFrom([gzip_header_protocol.encode({})]),
data,
ReadableStreamFrom([
gzip_trailer_protocol.encode({
crc32: selected_file.crc_32,
uncompressed_size: selected_file.uncompressed_size
})
])
])
.pipeThrough(new DecompressionStream("gzip"))
.pipeThrough(new ByteSliceStream(start, end));
} else {
return data
.pipeThrough(new DecompressionStream("deflate-raw"))
.pipeThrough(new ByteSliceStream(start, end));
}
} else {
return data.pipeThrough(new ByteSliceStream(start, end));
}
}
},
selected_file.file_name,
{ lastModified: selected_file.last_modified }
// { type: Mime.lookup(selected_file.file_name) ?? null }
)
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more