2019-06-09 07:24:20 +00:00
|
|
|
package cid
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
import (
|
|
|
|
"github.com/multiformats/go-varint"
|
|
|
|
)
|
|
|
|
|
2022-08-19 16:34:07 +00:00
|
|
|
// Version of varint function that works with a string rather than
|
2019-06-09 07:24:20 +00:00
|
|
|
// []byte to avoid unnecessary allocation
|
|
|
|
|
|
|
|
// Copyright 2011 The Go Authors. All rights reserved.
|
|
|
|
// Use of this source code is governed by a BSD-style
|
|
|
|
// license as given at https://golang.org/LICENSE
|
|
|
|
|
|
|
|
// uvarint decodes a uint64 from buf and returns that value and the
|
2022-08-19 16:34:07 +00:00
|
|
|
// number of bytes read (> 0). If an error occurred, then 0 is
|
|
|
|
// returned for both the value and the number of bytes read, and an
|
|
|
|
// error is returned.
|
2021-06-16 20:19:45 +00:00
|
|
|
func uvarint(buf string) (uint64, int, error) {
|
2019-06-09 07:24:20 +00:00
|
|
|
var x uint64
|
|
|
|
var s uint
|
2022-08-19 16:34:07 +00:00
|
|
|
// we have a binary string so we can't use a range loop
|
2019-06-09 07:24:20 +00:00
|
|
|
for i := 0; i < len(buf); i++ {
|
|
|
|
b := buf[i]
|
|
|
|
if b < 0x80 {
|
|
|
|
if i > 9 || i == 9 && b > 1 {
|
2021-06-16 20:19:45 +00:00
|
|
|
return 0, 0, varint.ErrOverflow
|
2022-08-19 16:34:07 +00:00
|
|
|
}
|
|
|
|
if b == 0 && i > 0 {
|
2021-06-16 20:19:45 +00:00
|
|
|
return 0, 0, varint.ErrNotMinimal
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
return x | uint64(b)<<s, i + 1, nil
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|
|
|
|
x |= uint64(b&0x7f) << s
|
|
|
|
s += 7
|
|
|
|
}
|
2021-06-16 20:19:45 +00:00
|
|
|
return 0, 0, varint.ErrUnderflow
|
2019-06-09 07:24:20 +00:00
|
|
|
}
|