pax_global_header00006660000000000000000000000064143375114450014521gustar00rootroot0000000000000052 comment=8c8129accb08fc978bb276c81d568c41eafdd48f go-varint-0.0.7/000077500000000000000000000000001433751144500134335ustar00rootroot00000000000000go-varint-0.0.7/.github/000077500000000000000000000000001433751144500147735ustar00rootroot00000000000000go-varint-0.0.7/.github/workflows/000077500000000000000000000000001433751144500170305ustar00rootroot00000000000000go-varint-0.0.7/.github/workflows/automerge.yml000066400000000000000000000003761433751144500215510ustar00rootroot00000000000000# File managed by web3-bot. DO NOT EDIT. # See https://github.com/protocol/.github/ for details. name: Automerge on: [ pull_request ] jobs: automerge: uses: protocol/.github/.github/workflows/automerge.yml@master with: job: 'automerge' go-varint-0.0.7/.github/workflows/go-check.yml000066400000000000000000000052251433751144500212370ustar00rootroot00000000000000# File managed by web3-bot. DO NOT EDIT. # See https://github.com/protocol/.github/ for details. on: [push, pull_request] name: Go Checks jobs: unit: runs-on: ubuntu-latest name: All env: RUNGOGENERATE: false steps: - uses: actions/checkout@v3 with: submodules: recursive - uses: actions/setup-go@v3 with: go-version: "1.19.x" - name: Run repo-specific setup uses: ./.github/actions/go-check-setup if: hashFiles('./.github/actions/go-check-setup') != '' - name: Read config if: hashFiles('./.github/workflows/go-check-config.json') != '' run: | if jq -re .gogenerate ./.github/workflows/go-check-config.json; then echo "RUNGOGENERATE=true" >> $GITHUB_ENV fi - name: Install staticcheck run: go install honnef.co/go/tools/cmd/staticcheck@376210a89477dedbe6fdc4484b233998650d7b3c # 2022.1.3 (v0.3.3) - name: Check that go.mod is tidy uses: protocol/multiple-go-modules@v1.2 with: run: | go mod tidy if [[ -n $(git ls-files --other --exclude-standard --directory -- go.sum) ]]; then echo "go.sum was added by go mod tidy" exit 1 fi git diff --exit-code -- go.sum go.mod - name: gofmt if: ${{ success() || failure() }} # run this step even if the previous one failed run: | out=$(gofmt -s -l .) if [[ -n "$out" ]]; then echo $out | awk '{print "::error file=" $0 ",line=0,col=0::File is not gofmt-ed."}' exit 1 fi - name: go vet if: ${{ success() || failure() }} # run this step even if the previous one failed uses: protocol/multiple-go-modules@v1.2 with: run: go vet ./... - name: staticcheck if: ${{ success() || failure() }} # run this step even if the previous one failed uses: protocol/multiple-go-modules@v1.2 with: run: | set -o pipefail staticcheck ./... | sed -e 's@\(.*\)\.go@./\1.go@g' - name: go generate uses: protocol/multiple-go-modules@v1.2 if: (success() || failure()) && env.RUNGOGENERATE == 'true' with: run: | git clean -fd # make sure there aren't untracked files / directories go generate ./... # check if go generate modified or added any files if ! $(git add . && git diff-index HEAD --exit-code --quiet); then echo "go generated caused changes to the repository:" git status --short exit 1 fi go-varint-0.0.7/.github/workflows/go-test.yml000066400000000000000000000051161433751144500211400ustar00rootroot00000000000000# File managed by web3-bot. DO NOT EDIT. # See https://github.com/protocol/.github/ for details. on: [push, pull_request] name: Go Test jobs: unit: strategy: fail-fast: false matrix: os: [ "ubuntu", "windows", "macos" ] go: [ "1.18.x", "1.19.x" ] env: COVERAGES: "" runs-on: ${{ format('{0}-latest', matrix.os) }} name: ${{ matrix.os }} (go ${{ matrix.go }}) steps: - uses: actions/checkout@v3 with: submodules: recursive - uses: actions/setup-go@v3 with: go-version: ${{ matrix.go }} - name: Go information run: | go version go env - name: Use msys2 on windows if: ${{ matrix.os == 'windows' }} shell: bash # The executable for msys2 is also called bash.cmd # https://github.com/actions/virtual-environments/blob/main/images/win/Windows2019-Readme.md#shells # If we prepend its location to the PATH # subsequent 'shell: bash' steps will use msys2 instead of gitbash run: echo "C:/msys64/usr/bin" >> $GITHUB_PATH - name: Run repo-specific setup uses: ./.github/actions/go-test-setup if: hashFiles('./.github/actions/go-test-setup') != '' - name: Run tests uses: protocol/multiple-go-modules@v1.2 with: # Use -coverpkg=./..., so that we include cross-package coverage. # If package ./A imports ./B, and ./A's tests also cover ./B, # this means ./B's coverage will be significantly higher than 0%. run: go test -v -shuffle=on -coverprofile=module-coverage.txt -coverpkg=./... ./... - name: Run tests (32 bit) if: ${{ matrix.os != 'macos' }} # can't run 32 bit tests on OSX. uses: protocol/multiple-go-modules@v1.2 env: GOARCH: 386 with: run: | export "PATH=${{ env.PATH_386 }}:$PATH" go test -v -shuffle=on ./... - name: Run tests with race detector if: ${{ matrix.os == 'ubuntu' }} # speed things up. Windows and OSX VMs are slow uses: protocol/multiple-go-modules@v1.2 with: run: go test -v -race ./... - name: Collect coverage files shell: bash run: echo "COVERAGES=$(find . -type f -name 'module-coverage.txt' | tr -s '\n' ',' | sed 's/,$//')" >> $GITHUB_ENV - name: Upload coverage to Codecov uses: codecov/codecov-action@81cd2dc8148241f03f5839d295e000b8f761e378 # v3.1.0 with: files: '${{ env.COVERAGES }}' env_vars: OS=${{ matrix.os }}, GO=${{ matrix.go }} go-varint-0.0.7/.github/workflows/release-check.yml000066400000000000000000000004101433751144500222410ustar00rootroot00000000000000# File managed by web3-bot. DO NOT EDIT. # See https://github.com/protocol/.github/ for details. name: Release Checker on: pull_request: paths: [ 'version.json' ] jobs: release-check: uses: protocol/.github/.github/workflows/release-check.yml@master go-varint-0.0.7/.github/workflows/releaser.yml000066400000000000000000000003571433751144500213620ustar00rootroot00000000000000# File managed by web3-bot. DO NOT EDIT. # See https://github.com/protocol/.github/ for details. name: Releaser on: push: paths: [ 'version.json' ] jobs: releaser: uses: protocol/.github/.github/workflows/releaser.yml@master go-varint-0.0.7/.github/workflows/tagpush.yml000066400000000000000000000003551433751144500212310ustar00rootroot00000000000000# File managed by web3-bot. DO NOT EDIT. # See https://github.com/protocol/.github/ for details. name: Tag Push Checker on: push: tags: - v* jobs: releaser: uses: protocol/.github/.github/workflows/tagpush.yml@master go-varint-0.0.7/LICENSE000066400000000000000000000020701433751144500144370ustar00rootroot00000000000000The MIT License (MIT) Copyright (c) 2019 Protocol Labs Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. go-varint-0.0.7/README.md000066400000000000000000000035021433751144500147120ustar00rootroot00000000000000# go-varint [![](https://img.shields.io/badge/made%20by-Protocol%20Labs-blue.svg?style=flat-square)](https://protocol.ai) [![](https://img.shields.io/badge/project-multiformats-blue.svg?style=flat-square)](https://github.com/multiformats/multiformats) [![](https://img.shields.io/badge/freenode-%23ipfs-blue.svg?style=flat-square)](https://webchat.freenode.net/?channels=%23ipfs) [![](https://img.shields.io/badge/readme%20style-standard-brightgreen.svg?style=flat-square)](https://github.com/RichardLitt/standard-readme) [![GoDoc](https://godoc.org/github.com/multiformats/go-varint?status.svg)](https://godoc.org/github.com/multiformats/go-varint) [![Travis CI](https://img.shields.io/travis/multiformats/go-varint.svg?style=flat-square&branch=master)](https://travis-ci.org/multiformats/go-varint) [![codecov.io](https://img.shields.io/codecov/c/github/multiformats/go-varint.svg?style=flat-square&branch=master)](https://codecov.io/github/multiformats/go-varint?branch=master) > Varint helpers that enforce minimal encoding. ## Table of Contents - [Install](#install) - [Contribute](#contribute) - [License](#license) ## Install ```sh go get github.com/multiformats/go-varint ``` ## Contribute Contributions welcome. Please check out [the issues](https://github.com/multiformats/go-multiaddr/issues). Check out our [contributing document](https://github.com/multiformats/multiformats/blob/master/contributing.md) for more information on how we work, and about contributing in general. Please be aware that all interactions related to multiformats are subject to the IPFS [Code of Conduct](https://github.com/ipfs/community/blob/master/code-of-conduct.md). Small note: If editing the README, please conform to the [standard-readme](https://github.com/RichardLitt/standard-readme) specification. ## License [MIT](LICENSE) © 2019 Protocol Labs go-varint-0.0.7/codecov.yml000066400000000000000000000000301433751144500155710ustar00rootroot00000000000000ignore: - "multiaddr" go-varint-0.0.7/go.mod000066400000000000000000000000621433751144500145370ustar00rootroot00000000000000module github.com/multiformats/go-varint go 1.18 go-varint-0.0.7/varint.go000066400000000000000000000063171433751144500152740ustar00rootroot00000000000000package varint import ( "encoding/binary" "errors" "io" "math/bits" ) var ( ErrOverflow = errors.New("varints larger than uint63 not supported") ErrUnderflow = errors.New("varints malformed, could not reach the end") ErrNotMinimal = errors.New("varint not minimally encoded") ) const ( // MaxLenUvarint63 is the maximum number of bytes representing an uvarint in // this encoding, supporting a maximum value of 2^63 (uint63), aka // MaxValueUvarint63. MaxLenUvarint63 = 9 // MaxValueUvarint63 is the maximum encodable uint63 value. MaxValueUvarint63 = (1 << 63) - 1 ) // UvarintSize returns the size (in bytes) of `num` encoded as a unsigned varint. // // This may return a size greater than MaxUvarintLen63, which would be an // illegal value, and would be rejected by readers. func UvarintSize(num uint64) int { bits := bits.Len64(num) q, r := bits/7, bits%7 size := q if r > 0 || size == 0 { size++ } return size } // ToUvarint converts an unsigned integer to a varint-encoded []byte func ToUvarint(num uint64) []byte { buf := make([]byte, UvarintSize(num)) n := binary.PutUvarint(buf, uint64(num)) return buf[:n] } // FromUvarint reads an unsigned varint from the beginning of buf, returns the // varint, and the number of bytes read. func FromUvarint(buf []byte) (uint64, int, error) { // Modified from the go standard library. Copyright the Go Authors and // released under the BSD License. var x uint64 var s uint for i, b := range buf { if (i == 8 && b >= 0x80) || i >= MaxLenUvarint63 { // this is the 9th and last byte we're willing to read, but it // signals there's more (1 in MSB). // or this is the >= 10th byte, and for some reason we're still here. return 0, 0, ErrOverflow } if b < 0x80 { if b == 0 && s > 0 { return 0, 0, ErrNotMinimal } return x | uint64(b)<= 0x80) || s >= (7*MaxLenUvarint63) { // this is the 9th and last byte we're willing to read, but it // signals there's more (1 in MSB). // or this is the >= 10th byte, and for some reason we're still here. return 0, ErrOverflow } if b < 0x80 { if b == 0 && s > 0 { return 0, ErrNotMinimal } return x | uint64(b)<