Beispiel #1
0
 // Decodes and consumes a variable-length integer consisting of "length" bytes
 // in little-endian format.  Returns false if not enough bytes are available.
 //
 // REQUIRES: T is an unsigned integer type.
 // REQUIRES: 2 <= sizeof(T) <= 8
 // REQUIRES: 0 <= length <= sizeof(T)
 public static bool DecodeUintWithLength(int length, Decoder decoder, out UInt64 result)
 {
     result = default;
     if (decoder.Avail() < length)
     {
         return(false);
     }
     result = EncodedUintVector_UInt64.GetUintWithLength(decoder.Buffer, decoder.Offset, length);
     decoder.Skip(length);
     return(true);
 }
    // Initializes the EncodedS2PointVector.
    //
    // REQUIRES: The Decoder data buffer must outlive this object.
    public bool Init(Decoder decoder)
    {
        if (decoder.Avail() < 1)
        {
            return(false);
        }

        // Peek at the format but don't advance the decoder; the format-specific
        // Init functions will do that.
        format_ = (Format)(decoder.Get8() & kEncodingFormatMask);
        return(format_ switch
        {
            Format.UNCOMPRESSED => InitUncompressedFormat(decoder),
            Format.CELL_IDS => InitCellIdsFormat(decoder),
            _ => false,
        });
    // Initializes the EncodedS2CellIdVector.
    //
    // REQUIRES: The Decoder data buffer must outlive this object.
    public bool Init(Decoder decoder)
    {
        // All encodings have at least 2 bytes (one for our header and one for the
        // EncodedUintVector header), so this is safe.
        if (decoder.Avail() < 2)
        {
            return(false);
        }

        // Invert the encoding of (shift_code, base_len) described above.
        int code_plus_len = decoder.Get8();
        int shift_code    = code_plus_len >> 3;

        if (shift_code == 31)
        {
            shift_code = 29 + decoder.Get8();
            if (shift_code > 56)
            {
                return(false);                  // Valid range 0..56
            }
        }

        // Decode the "base_len" most-significant bytes of "base".
        int base_len = code_plus_len & 7;

        if (!EncodedUintVector.DecodeUintWithLength(base_len, decoder, out base_))
        {
            return(false);
        }
        base_ <<= 64 - 8 * Math.Max(1, base_len);

        // Invert the encoding of "shift_code" described above.
        if (shift_code >= 29)
        {
            shift_ = (byte)(2 * (shift_code - 29) + 1);
            base_ |= 1UL << (shift_ - 1);
        }
        else
        {
            shift_ = (byte)(2 * shift_code);
        }
        return(deltas_.Init(decoder));
    }
Beispiel #4
0
    // Initializes the EncodedUintVector.  Returns false on errors, leaving the
    // vector in an unspecified state.
    //
    // REQUIRES: The Decoder data buffer must outlive this object.
    public bool Init(Decoder decoder)
    {
        if (!decoder.TryGetVarUInt64(out var size_len))
        {
            return(false);
        }
        Count = (int)(size_len / sizeof(UInt16));  // Optimized into bit shift.
        len_  = (Byte)((size_len & ((UInt16)(sizeof(UInt16) - 1))) + 1);
        if (Count > int.MaxValue / sizeof(UInt16))
        {
            return(false);
        }
        int bytes = Count * len_;

        if (decoder.Avail() < bytes)
        {
            return(false);
        }
        data_ = decoder.Buffer;
        decoder.Skip(bytes);
        return(true);
    }