blob: e1fcd537b79b1228f9638faf7ab9213450f68dbc [file] [log] [blame]
Matthias Andreas Benkardb5d657a2022-02-03 21:14:30 +01001// SPDX-FileCopyrightText: © 2021 Matthias Andreas Benkard <code@mail.matthias.benkard.de>
2//
3// SPDX-License-Identifier: LGPL-3.0-or-later
4
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +01005package eu.mulk.jgvariant.ostree;
6
7import eu.mulk.jgvariant.core.Decoder;
8import eu.mulk.jgvariant.core.Variant;
Matthias Andreas Benkard05114642021-12-29 21:51:29 +01009import java.nio.ByteBuffer;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010010import java.nio.ByteOrder;
11import java.nio.charset.StandardCharsets;
Matthias Andreas Benkard329168c2021-12-28 01:20:05 +010012import java.util.Map;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010013
14/**
15 * A {@link DeltaSuperblock} signed with some sort of key.
16 *
17 * <p>Reference: {@code ostree-repo-static-delta-private.h#OSTREE_STATIC_DELTA_SIGNED_FORMAT}
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010018 *
19 * @param magicNumber the value {@link #MAGIC}.
20 * @param superblock the {@link DeltaSuperblock}.
21 * @param signatures a list of signatures, indexed by type.
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010022 */
23public record SignedDelta(
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010024 long magicNumber, DeltaSuperblock superblock, Map<String, Variant> signatures) {
25
26 /** The value of {@link #magicNumber()}. */
27 public static final long MAGIC = 0x4F53_5453_474E_4454L;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010028
29 private static final Decoder<SignedDelta> DECODER =
30 Decoder.ofStructure(
31 SignedDelta.class,
32 Decoder.ofLong().withByteOrder(ByteOrder.BIG_ENDIAN),
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010033 ByteString.decoder().map(SignedDelta::decodeSuperblock),
Matthias Andreas Benkard329168c2021-12-28 01:20:05 +010034 Decoder.ofDictionary(Decoder.ofString(StandardCharsets.US_ASCII), Decoder.ofVariant()));
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010035
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010036 private static DeltaSuperblock decodeSuperblock(ByteString byteString) {
37 return DeltaSuperblock.decoder().decode(ByteBuffer.wrap(byteString.bytes()));
38 }
39
40 /**
41 * Acquires a {@link Decoder} for the enclosing type.
42 *
43 * @return a possibly shared {@link Decoder}.
44 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010045 public static Decoder<SignedDelta> decoder() {
46 return DECODER;
47 }
48}