github.com/ethersphere/bee/v2@v2.2.0/pkg/file/redundancy/span.go (about) 1 // Copyright 2023 The Swarm 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 redundancy 6 7 import ( 8 "github.com/ethersphere/bee/v2/pkg/swarm" 9 ) 10 11 // EncodeLevel encodes used redundancy level for uploading into span keeping the real byte count for the chunk. 12 // assumes span is LittleEndian 13 func EncodeLevel(span []byte, level Level) { 14 // set parity in the most signifact byte 15 span[swarm.SpanSize-1] = uint8(level) | 1<<7 // p + 128 16 } 17 18 // DecodeSpan decodes the used redundancy level from span keeping the real byte count for the chunk. 19 // assumes span is LittleEndian 20 func DecodeSpan(span []byte) (Level, []byte) { 21 spanCopy := make([]byte, swarm.SpanSize) 22 copy(spanCopy, span) 23 if !IsLevelEncoded(spanCopy) { 24 return 0, spanCopy 25 } 26 pByte := spanCopy[swarm.SpanSize-1] 27 return Level(pByte & ((1 << 7) - 1)), append(spanCopy[:swarm.SpanSize-1], 0) 28 } 29 30 // IsLevelEncoded checks whether the redundancy level is encoded in the span 31 // assumes span is LittleEndian 32 func IsLevelEncoded(span []byte) bool { 33 return span[swarm.SpanSize-1] > 128 34 }