Matthias Andreas Benkard | b5d657a | 2022-02-03 21:14:30 +0100 | [diff] [blame] | 1 | // SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de> |
| 2 | // |
| 3 | // SPDX-License-Identifier: LGPL-3.0-or-later |
| 4 | |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 5 | package eu.mulk.jgvariant.ostree; |
| 6 | |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 7 | import static java.nio.charset.StandardCharsets.US_ASCII; |
| 8 | |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 9 | import eu.mulk.jgvariant.core.Decoder; |
| 10 | import eu.mulk.jgvariant.core.Variant; |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 11 | import java.nio.ByteBuffer; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 12 | import java.nio.ByteOrder; |
Matthias Andreas Benkard | 329168c | 2021-12-28 01:20:05 +0100 | [diff] [blame] | 13 | import java.util.Map; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 14 | |
| 15 | /** |
| 16 | * A {@link DeltaSuperblock} signed with some sort of key. |
| 17 | * |
| 18 | * <p>Reference: {@code ostree-repo-static-delta-private.h#OSTREE_STATIC_DELTA_SIGNED_FORMAT} |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 19 | * |
| 20 | * @param magicNumber the value {@link #MAGIC}. |
| 21 | * @param superblock the {@link DeltaSuperblock}. |
| 22 | * @param signatures a list of signatures, indexed by type. |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 23 | */ |
| 24 | public record SignedDelta( |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 25 | long magicNumber, DeltaSuperblock superblock, Map<String, Variant> signatures) { |
| 26 | |
| 27 | /** The value of {@link #magicNumber()}. */ |
| 28 | public static final long MAGIC = 0x4F53_5453_474E_4454L; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 29 | |
| 30 | private static final Decoder<SignedDelta> DECODER = |
| 31 | Decoder.ofStructure( |
| 32 | SignedDelta.class, |
| 33 | Decoder.ofLong().withByteOrder(ByteOrder.BIG_ENDIAN), |
Matthias Andreas Benkard | aa11d82 | 2023-12-10 09:20:48 +0100 | [diff] [blame] | 34 | Decoder.ofByteArray().map(SignedDelta::decodeSuperblock, SignedDelta::encodeSuperblock), |
Matthias Andreas Benkard | 91dbd74 | 2022-10-17 19:38:56 +0200 | [diff] [blame] | 35 | Decoder.ofDictionary(Decoder.ofString(US_ASCII), Decoder.ofVariant())); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 36 | |
Matthias Andreas Benkard | aa11d82 | 2023-12-10 09:20:48 +0100 | [diff] [blame] | 37 | private static DeltaSuperblock decodeSuperblock(byte[] bytes) { |
| 38 | return DeltaSuperblock.decoder().decode(ByteBuffer.wrap(bytes)); |
| 39 | } |
| 40 | |
| 41 | private static byte[] encodeSuperblock(DeltaSuperblock deltaSuperblock) { |
| 42 | var byteBuffer = DeltaSuperblock.decoder().encode(deltaSuperblock); |
| 43 | byte[] bytes = new byte[byteBuffer.remaining()]; |
| 44 | byteBuffer.get(bytes); |
| 45 | return bytes; |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 46 | } |
| 47 | |
| 48 | /** |
| 49 | * Acquires a {@link Decoder} for the enclosing type. |
| 50 | * |
| 51 | * @return a possibly shared {@link Decoder}. |
| 52 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 53 | public static Decoder<SignedDelta> decoder() { |
| 54 | return DECODER; |
| 55 | } |
| 56 | } |