github.com/m3db/m3@v1.5.1-0.20231129193456-75a402aa583b/src/dbnode/encoding/proto/buffer_encode.go (about) 1 // Copyright (c) 2019 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 // This file is mostly copy-pasta from: https://github.com/jhump/protoreflect/blob/master/dynamic/codec.go 22 // since the jhump/protoreflect library does not expose the `codedBuffer` type. 23 24 package proto 25 26 func (cb *buffer) encodeTagAndWireType(tag int32, wireType int8) { 27 v := uint64((int64(tag) << 3) | int64(wireType)) 28 cb.encodeVarint(v) 29 } 30 31 // EncodeVarint writes a varint-encoded integer to the Buffer. 32 // This is the format for the 33 // int32, int64, uint32, uint64, bool, and enum 34 // protocol buffer types. 35 func (cb *buffer) encodeVarint(x uint64) { 36 for x >= 1<<7 { 37 cb.buf = append(cb.buf, uint8(x&0x7f|0x80)) 38 x >>= 7 39 } 40 cb.buf = append(cb.buf, uint8(x)) 41 } 42 43 // EncodeFixed64 writes a 64-bit integer to the Buffer. 44 // This is the format for the 45 // fixed64, sfixed64, and double protocol buffer types. 46 func (cb *buffer) encodeFixed64(x uint64) { 47 cb.buf = append(cb.buf, 48 uint8(x), 49 uint8(x>>8), 50 uint8(x>>16), 51 uint8(x>>24), 52 uint8(x>>32), 53 uint8(x>>40), 54 uint8(x>>48), 55 uint8(x>>56)) 56 } 57 58 // EncodeFixed32 writes a 32-bit integer to the Buffer. 59 // This is the format for the 60 // fixed32, sfixed32, and float protocol buffer types. 61 func (cb *buffer) encodeFixed32(x uint32) { 62 cb.buf = append(cb.buf, 63 uint8(x), 64 uint8(x>>8), 65 uint8(x>>16), 66 uint8(x>>24)) 67 } 68 69 // EncodeRawBytes writes a count-delimited byte buffer to the Buffer. 70 // This is the format used for the bytes protocol buffer 71 // type and for embedded messages. 72 func (cb *buffer) encodeRawBytes(b []byte) { 73 cb.encodeVarint(uint64(len(b))) 74 cb.buf = append(cb.buf, b...) 75 } 76 77 func (cb *buffer) append(b []byte) { 78 cb.buf = append(cb.buf, b...) 79 } 80 81 func encodeZigZag32(v int32) uint64 { 82 return uint64((uint32(v) << 1) ^ uint32((v >> 31))) 83 } 84 85 func encodeZigZag64(v int64) uint64 { 86 return (uint64(v) << 1) ^ uint64(v>>63) 87 }