Luce packages
LuceLuce BaseLuciaOS

dymokomi/luce-usd

Source of release 0.9.2

0.9.2 / src / sniff.lucb

## Which USD serialization a file holds, from its first bytes, as OpenUSD's
## file formats recognize them: the crate's "PXR-USDC" bootstrap
## (sdf/crateFile.cpp), the text format's "#usda " cookie
## (sdf/textFileFormat.cpp) and a zip local file header for usdz packages
## (sdf/zipFile.cpp). A `.usd` file is either usda or usdc.
import errors

pub enum Format as u8:
    usda = 0
    usdc = 1
    usdz = 2

## Bytes `detect` needs to decide.
pub let prefix_bytes: usize = 8

## The format whose signature `data` starts with.
pub func detect(data: const u8[]) -> Format!:
    if starts_with(data, "PXR-USDC"):
        return Format.usdc
    if starts_with(data, "#usda "):
        return Format.usda
    if data.length >= 4 and data[0] == 0x50 and data[1] == 0x4b and data[2] == 0x03 and data[3] == 0x04:
        return Format.usdz
    error(errors.invalid, "not a USD file: no usda, usdc or usdz signature")

## The format's name, as the extension of a file holding only that format.
pub func extension(kind: Format) -> str:
    match kind:
        .usdc: return "usdc"
        .usdz: return "usdz"
        _: return "usda"

func starts_with(data: const u8[], text: str) -> bool:
    if data.length < text.length:
        return false
    for at in 0..<text.length:
        if data[at] != text.bytes[at]:
            return false
    return true