Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-usd

Source of release 0.9.2

0.9.2 / src / usdc / reader / sections.lucb

## The bootstrap, the table of contents and the structural sections
## (CrateFile::_ReadStructuralSections): tokens (NUL-separated, compressed
## from 0.4.0), strings (token indexes), fields, field sets and specs
## (integer-compressed from 0.4.0, fixed records before).

## A table of contents entry: the section's name, start and size.
struct Section:
    var name: const u8[]
    var start: u64
    var size: u64

extend Crate:
    func read_structure() -> !:
        if self.data.length < 88: error(errors.invalid, "not a crate file: too short for its bootstrap")
        if (str)self.data[..<8] != "PXR-USDC": error(errors.invalid, "not a crate file: no PXR-USDC bootstrap")
        let major = self.data[8]
        self.minor = self.data[9]
        self.patch = self.data[10]
        if major != 0 or self.minor > newest_minor:
            error(errors.unsupported, "a crate version newer than this reader (0.15.0)")
        let toc = try self.u64_at(16)
        if toc >= (u64)self.data.length: error(errors.invalid, "a crate file truncated before its table of contents")
        let count = try self.u64_at(toc)
        if count > 64: error(errors.invalid, "a crate table of contents with too many sections")
        var sections: Section[64] = ---
        for index in 0..<(usize)count:
            let at = toc + 8 + (u64)index * 32
            let name = try self.bytes_at(at, 16)
            var length: usize = 0
            while length < 16 and name[length] != 0: length += 1
            sections[index] = Section(name = name[..<length], start = try self.u64_at(at + 16), size = try self.u64_at(at + 24))
        let all = sections[..<(usize)count]
        if let tokens = find_section(all, "TOKENS"): try self.read_tokens(tokens.start)
        if let strings = find_section(all, "STRINGS"): try self.read_strings(strings.start)
        if let fields = find_section(all, "FIELDS"): try self.read_fields(fields.start)
        if let field_sets = find_section(all, "FIELDSETS"): try self.read_field_sets(field_sets.start)
        if let paths = find_section(all, "PATHS"): try self.read_paths(paths.start)
        if let specs = find_section(all, "SPECS"): try self.read_specs(specs.start)

    func read_tokens(start: u64) -> !:
        let count = try self.u64_at(start)
        var chars: const u8[] = try self.bytes_at(start, 0)
        var owned: u8[]
        defer:
            if owned.length > 0: free(owned) in memory.heap
        if self.since(4):
            let size = try self.u64_at(start + 8)
            let packed = try self.u64_at(start + 16)
            if size > 1 << 32: error(errors.invalid, "a crate token section too large")
            owned = try new u8[(usize)size] --- in memory.heap
            let used = try compression.decompress(try self.bytes_at(start + 24, packed), owned)
            if (u64)used != size: error(errors.invalid, "a crate token section of the wrong size")
            chars = owned
        else:
            let size = try self.u64_at(start + 8)
            chars = try self.bytes_at(start + 16, size)
        if count > (u64)chars.length + 1: error(errors.invalid, "a crate claims more tokens than its bytes hold")
        try self.tokens.reserve((usize)count)
        var at: usize = 0
        for _ in 0..<(usize)count:
            var end = at
            while end < chars.length and chars[end] != 0: end += 1
            if end >= chars.length and chars.length > 0 and at >= chars.length:
                error(errors.invalid, "a crate token section ends early")
            try self.tokens.push(try self.names.token((str)chars[at..<end]))
            at = end + 1

    func read_strings(start: u64) -> !:
        let count = try self.u64_at(start)
        let indexes = try self.bytes_at(start + 8, count * 4)
        try self.strings.reserve((usize)count)
        for index in 0..<(usize)count:
            try self.strings.push(try self.token((u64)memory.read[u32]((void*)&indexes[index * 4])))

    func read_fields(start: u64) -> !:
        let count = try self.u64_at(start)
        if not self.since(4):
            let records = try self.bytes_at(start + 8, count * 16)
            for index in 0..<(usize)count:
                try self.field_tokens.push(try self.token((u64)memory.read[u32]((void*)&records[index * 16 + 4])))
                try self.field_reps.push(memory.read[u64]((void*)&records[index * 16 + 8]))
            return
        if count > (u64)self.data.length: error(errors.invalid, "a crate claims more fields than it can hold")
        let tokens = try new u8[(usize)count * 4] --- in memory.heap
        defer free(tokens) in memory.heap
        var at = start + 8
        at = try self.compressed_ints(at, (usize)count, 4, tokens)
        let reps_size = try self.u64_at(at)
        let reps = try new u8[(usize)count * 8] --- in memory.heap
        defer free(reps) in memory.heap
        if count > 0:
            let used = try compression.decompress(try self.bytes_at(at + 8, reps_size), reps)
            if used != (usize)count * 8: error(errors.invalid, "crate field reps of the wrong size")
        for index in 0..<(usize)count:
            try self.field_tokens.push(try self.token((u64)memory.read[u32]((void*)&tokens[index * 4])))
            try self.field_reps.push(memory.read[u64]((void*)&reps[index * 8]))

    func read_field_sets(start: u64) -> !:
        let count = try self.u64_at(start)
        if count > (u64)self.data.length: error(errors.invalid, "a crate claims more field set entries than it can hold")
        try self.field_sets.extend((usize)count, 0)
        let out = u8[]((u8*)self.field_sets.items.data, (usize)count * 4)
        if self.since(4):
            _ = try self.compressed_ints(start + 8, (usize)count, 4, out)
        else:
            memory.copy(out, try self.bytes_at(start + 8, count * 4), (usize)count * 4)
        if count > 0 and self.field_sets.items[(usize)count - 1] != 0xFFFFFFFF:
            error(errors.invalid, "crate field sets must end with a terminator")

    func read_specs(start: u64) -> !:
        let count = try self.u64_at(start)
        if count > (u64)self.data.length: error(errors.invalid, "a crate claims more specs than it can hold")
        if self.since(4):
            let columns = try new u8[(usize)count * 12] --- in memory.heap
            defer free(columns) in memory.heap
            var at = start + 8
            at = try self.compressed_ints(at, (usize)count, 4, columns[..<(usize)count * 4])
            at = try self.compressed_ints(at, (usize)count, 4, columns[(usize)count * 4..<(usize)count * 8])
            at = try self.compressed_ints(at, (usize)count, 4, columns[(usize)count * 8..])
            for index in 0..<(usize)count:
                try self.specs.push(CrateSpec(
                    path = memory.read[u32]((void*)&columns[index * 4]),
                    field_set = memory.read[u32]((void*)&columns[((usize)count + index) * 4]),
                    kind = memory.read[u32]((void*)&columns[((usize)count * 2 + index) * 4])))
            return
        let size: u64 = 16 if not self.since(1) else 12
        let skip: u64 = 4 if not self.since(1) else 0
        let records = try self.bytes_at(start + 8, count * size)
        for index in 0..<(usize)count:
            let at = (usize)((u64)index * size + skip)
            try self.specs.push(CrateSpec(path = memory.read[u32]((void*)&records[at]), field_set = memory.read[u32]((void*)&records[at + 4]), kind = memory.read[u32]((void*)&records[at + 8])))

    ## `count` integer-compressed ints of `width` bytes at `at` (a u64 size,
    ## then the compressed bytes) into `out`; the offset after them.
    func compressed_ints(at: u64, count: usize, width: usize, out: u8[]) -> u64!:
        let size = try self.u64_at(at)
        if count > 0: try compression.decompress_integers(try self.bytes_at(at + 8, size), count, width, out)
        return at + 8 + size

func find_section(sections: const Section[], name: str) -> Section?:
    for section in sections:
        if (str)section.name == name: return section
    return none