| Tim Windelschmidt | 6d33a43 | 2025-02-04 14:34:25 +0100 | [diff] [blame] | 1 | // Copyright The Monogon Project Authors. |
| Lorenz Brun | 378a445 | 2021-01-26 13:47:41 +0100 | [diff] [blame] | 2 | // SPDX-License-Identifier: Apache-2.0 |
| Lorenz Brun | 378a445 | 2021-01-26 13:47:41 +0100 | [diff] [blame] | 3 | |
| 4 | package erofs |
| 5 | |
| 6 | // This file contains compression-related functions. |
| 7 | // TODO(lorenz): Fully implement compression. These are currently unused. |
| 8 | |
| 9 | import "encoding/binary" |
| 10 | |
| Serge Bazanski | 216fe7b | 2021-05-21 18:36:16 +0200 | [diff] [blame] | 11 | // 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 Brun | 378a445 | 2021-01-26 13:47:41 +0100 | [diff] [blame] | 13 | type 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. |
| 21 | func 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. |
| 30 | func 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 | } |