blob: 1e1e58e9ac275452943cc97224e5dd1169a78f6f [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
Matthias Andreas Benkard91dbd742022-10-17 19:38:56 +02007import static java.nio.charset.StandardCharsets.US_ASCII;
8
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +01009import eu.mulk.jgvariant.core.Decoder;
10import eu.mulk.jgvariant.core.Variant;
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010011import java.nio.ByteBuffer;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010012import java.nio.ByteOrder;
Matthias Andreas Benkard329168c2021-12-28 01:20:05 +010013import java.util.Map;
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010014
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 Benkard05114642021-12-29 21:51:29 +010019 *
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 Benkard4e8423d2021-12-19 22:56:09 +010023 */
24public record SignedDelta(
Matthias Andreas Benkard05114642021-12-29 21:51:29 +010025 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 Benkard4e8423d2021-12-19 22:56:09 +010029
30 private static final Decoder<SignedDelta> DECODER =
31 Decoder.ofStructure(
32 SignedDelta.class,
33 Decoder.ofLong().withByteOrder(ByteOrder.BIG_ENDIAN),
Matthias Andreas Benkardaa11d822023-12-10 09:20:48 +010034 Decoder.ofByteArray().map(SignedDelta::decodeSuperblock, SignedDelta::encodeSuperblock),
Matthias Andreas Benkard91dbd742022-10-17 19:38:56 +020035 Decoder.ofDictionary(Decoder.ofString(US_ASCII), Decoder.ofVariant()));
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010036
Matthias Andreas Benkardaa11d822023-12-10 09:20:48 +010037 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 Benkard05114642021-12-29 21:51:29 +010046 }
47
48 /**
49 * Acquires a {@link Decoder} for the enclosing type.
50 *
51 * @return a possibly shared {@link Decoder}.
52 */
Matthias Andreas Benkard4e8423d2021-12-19 22:56:09 +010053 public static Decoder<SignedDelta> decoder() {
54 return DECODER;
55 }
56}