Pseudo-code to read 255UInt16

As requested in https://www.w3.org/Fonts/WG/track/actions/191, C-like
pseudo-code to read a UIntBase128 follows:

bool ReadUIntBase128( data, *result )

    UInt32 accum = 0;

    for (i = 0; i < 5; i++) {
        UInt8 data_byte = data.getNextUInt8();

        // No leading 0's
        if (i == 0 && data_byte = 0x80) return false;

        // If any of top 7 bits are set then << 7 would overflow
        if (accum & 0xFE000000) return false;

        *accum = (accum << 7) | (data_byte & 0x7F);

        // Spin until most significant bit of data byte is false
        if ((data_byte & 0x80) == 0) {
            *result = accum;
            return true;
        }
    }

    // UIntBase128 sequence exceeds 5 bytes
    return false;
}

(pseudo-code may suspiciously resemble actual code in
https://github.com/google/woff2/blob/master/src/variable_length.cc)

I wasn't sure how much of the error checking to keep. I noticed the
Read255UShort
pseudo-code doesn't include things like checking if we actually have a next
byte and attempted to match.

Cheers, Rod S.

Received on Wednesday, 9 December 2015 22:55:52 UTC