go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/common/data/cmpbin/binary_tools.go (about) 1 // Copyright 2015 The LUCI Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package cmpbin 16 17 import ( 18 "bytes" 19 ) 20 21 // ConcatBytes is a convenience invocation of bytes.Join(itms, nil) 22 func ConcatBytes(itms ...[]byte) []byte { 23 return bytes.Join(itms, nil) 24 } 25 26 // InvertBytes simply inverts all the bytes in bs. 27 func InvertBytes(bs []byte) []byte { 28 if len(bs) == 0 { 29 return nil 30 } 31 ret := make([]byte, len(bs)) 32 for i, b := range bs { 33 ret[i] = 0xFF ^ b 34 } 35 return ret 36 } 37 38 // IncrementBytes attempts to increment a copy of bstr as if adding 1 to an integer. 39 // 40 // If it overflows, the returned []byte will be all 0's, and the overflow bool 41 // will be true. 42 func IncrementBytes(bstr []byte) ([]byte, bool) { 43 ret := ConcatBytes(bstr) 44 for i := len(ret) - 1; i >= 0; i-- { 45 if ret[i] == 0xFF { 46 ret[i] = 0 47 } else { 48 ret[i]++ 49 return ret, false 50 } 51 } 52 return ret, true 53 }