blob: 0077cb403a54b4b28d88442bcdd2bc927b8113b4 [file] [log] [blame]
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +01001package eu.mulk.jgvariant.ostree;
2
3import eu.mulk.jgvariant.core.Decoder;
4import eu.mulk.jgvariant.core.Variant;
Matthias Andreas Benkard05114642021-12-29 21:51:29 +01005import java.nio.ByteBuffer;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +01006import java.nio.ByteOrder;
7import java.nio.charset.StandardCharsets;
Matthias Andreas Benkard329168c2021-12-28 01:20:05 +01008import java.util.Map;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +01009
10/**
11 * A {@link DeltaSuperblock} signed with some sort of key.
12 *
13 * <p>Reference: {@code ostree-repo-static-delta-private.h#OSTREE_STATIC_DELTA_SIGNED_FORMAT}
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010014 *
15 * @param magicNumber the value {@link #MAGIC}.
16 * @param superblock the {@link DeltaSuperblock}.
17 * @param signatures a list of signatures, indexed by type.
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010018 */
19public record SignedDelta(
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010020 long magicNumber, DeltaSuperblock superblock, Map<String, Variant> signatures) {
21
22 /** The value of {@link #magicNumber()}. */
23 public static final long MAGIC = 0x4F53_5453_474E_4454L;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010024
25 private static final Decoder<SignedDelta> DECODER =
26 Decoder.ofStructure(
27 SignedDelta.class,
28 Decoder.ofLong().withByteOrder(ByteOrder.BIG_ENDIAN),
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010029 ByteString.decoder().map(SignedDelta::decodeSuperblock),
Matthias Andreas Benkard329168c2021-12-28 01:20:05 +010030 Decoder.ofDictionary(Decoder.ofString(StandardCharsets.US_ASCII), Decoder.ofVariant()));
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010031
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010032 private static DeltaSuperblock decodeSuperblock(ByteString byteString) {
33 return DeltaSuperblock.decoder().decode(ByteBuffer.wrap(byteString.bytes()));
34 }
35
36 /**
37 * Acquires a {@link Decoder} for the enclosing type.
38 *
39 * @return a possibly shared {@link Decoder}.
40 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010041 public static Decoder<SignedDelta> decoder() {
42 return DECODER;
43 }
44}