blob: 984f597147ef07db4bd644182180c8d1c407be78 [file] [log] [blame]
Tim Windelschmidt6d33a432025-02-04 14:34:25 +01001// Copyright The Monogon Project Authors.
Lorenz Brun378a4452021-01-26 13:47:41 +01002// SPDX-License-Identifier: Apache-2.0
Lorenz Brun378a4452021-01-26 13:47:41 +01003
4package erofs
5
6// This file contains compression-related functions.
7// TODO(lorenz): Fully implement compression. These are currently unused.
8
9import "encoding/binary"
10
Serge Bazanski216fe7b2021-05-21 18:36:16 +020011// mapHeader is a legacy but still-used advisory structure at the start of a
12// compressed VLE block. It contains constant values as annotated.
Lorenz Brun378a4452021-01-26 13:47:41 +010013type mapHeader struct {
14 Reserved uint32 // 0
15 Advise uint16 // 1
16 AlgorithmType uint8 // 0
17 ClusterBits uint8 // 0
18}
19
20// encodeSmallVLEBlock encodes two VLE extents into a 8 byte block.
21func encodeSmallVLEBlock(vals [2]uint16, blkaddr uint32) [8]byte {
22 var out [8]byte
23 binary.LittleEndian.PutUint16(out[0:2], vals[0])
24 binary.LittleEndian.PutUint16(out[2:4], vals[1])
25 binary.LittleEndian.PutUint32(out[4:8], blkaddr)
26 return out
27}
28
29// encodeBigVLEBlock encodes 16 VLE extents into a 32 byte block.
30func encodeBigVLEBlock(vals [16]uint16, blkaddr uint32) [32]byte {
31 var out [32]byte
32 for i, val := range vals {
33 if val > 1<<14 {
34 panic("value is bigger than 14 bits, cannot encode")
35 }
36 // Writes packed 14 bit unsigned integers
37 pos := i * 14
38 bitStartPos := pos % 8
39 byteStartPos := pos / 8
40 out[byteStartPos] = out[byteStartPos]&((1<<bitStartPos)-1) | uint8(val<<bitStartPos)
41 out[byteStartPos+1] = uint8(val >> (8 - bitStartPos))
42 out[byteStartPos+2] = uint8(val >> (16 - bitStartPos))
43 }
44 binary.LittleEndian.PutUint32(out[28:32], blkaddr)
45 return out
46}