github.com/linuxboot/fiano@v1.2.0/pkg/intel/metadata/fit/check/bounds.go (about) 1 // Copyright 2017-2021 the LinuxBoot Authors. All rights reserved 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package check 6 7 import ( 8 "github.com/hashicorp/go-multierror" 9 ) 10 11 func bounds(length uint, startIdx, endIdx int) error { 12 var result *multierror.Error 13 if startIdx < 0 { 14 result = multierror.Append(result, &ErrStartLessThanZero{StartIdx: startIdx}) 15 } 16 if endIdx < startIdx { 17 result = multierror.Append(result, &ErrEndLessThanStart{StartIdx: startIdx, EndIdx: endIdx}) 18 } 19 if endIdx >= 0 && uint(endIdx) > length { 20 result = multierror.Append(result, &ErrEndGreaterThanLength{Length: length, EndIdx: endIdx}) 21 } 22 23 return result.ErrorOrNil() 24 } 25 26 // BytesRange checks if starting index `startIdx`, ending index `endIdx` and 27 // len(b) passes sanity checks: 28 // * 0 <= startIdx 29 // * startIdx <= endIdx 30 // * endIdx < len(b) 31 func BytesRange(length uint, startIdx, endIdx int) error { 32 return bounds(length, startIdx, endIdx) 33 }