2018-04-16 01:48:19 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2018-10-18 12:10:29 +01:00
|
|
|
package encryption
|
2018-04-16 01:48:19 +01:00
|
|
|
|
2018-10-03 09:55:42 +01:00
|
|
|
// incrementBytes takes a byte slice buf and treats it like a little-endian
|
2018-04-16 01:48:19 +01:00
|
|
|
// encoded unsigned integer. it adds amount to it (which must be nonnegative)
|
|
|
|
// in place. if rollover happens (the most significant bytes don't fit
|
|
|
|
// anymore), truncated is true.
|
2018-10-03 09:55:42 +01:00
|
|
|
func incrementBytes(buf []byte, amount int64) (truncated bool, err error) {
|
2018-04-16 01:48:19 +01:00
|
|
|
if amount < 0 {
|
|
|
|
return false, Error.New("amount was negative")
|
|
|
|
}
|
|
|
|
|
2018-10-03 09:55:42 +01:00
|
|
|
idx := 0
|
|
|
|
for amount > 0 && idx < len(buf) {
|
|
|
|
var inc, prev byte
|
|
|
|
inc, amount = byte(amount), amount>>8
|
2018-04-16 01:48:19 +01:00
|
|
|
|
2018-10-03 09:55:42 +01:00
|
|
|
prev = buf[idx]
|
|
|
|
buf[idx] += inc
|
|
|
|
if buf[idx] < prev {
|
|
|
|
amount++
|
|
|
|
}
|
2018-04-16 01:48:19 +01:00
|
|
|
|
2018-10-03 09:55:42 +01:00
|
|
|
idx++
|
|
|
|
}
|
2018-04-16 01:48:19 +01:00
|
|
|
|
2018-10-03 09:55:42 +01:00
|
|
|
return amount != 0, nil
|
2018-04-16 01:48:19 +01:00
|
|
|
}
|