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 | |
| 7 | import eu.mulk.jgvariant.core.Decoder; |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 8 | import java.nio.ByteBuffer; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 9 | |
| 10 | /** |
| 11 | * A wrapper for {@link ByteString} that refers to a content-addressed object in an OSTree |
| 12 | * repository. |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 13 | * |
| 14 | * @param byteString the bytes that make up this {@link Checksum}. |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 15 | */ |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 16 | public record Checksum(ByteString byteString) { |
| 17 | |
| 18 | private static final int SIZE = 32; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 19 | |
Matthias Andreas Benkard | aa11d82 | 2023-12-10 09:20:48 +0100 | [diff] [blame] | 20 | private static final Decoder<Checksum> DECODER = |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame^] | 21 | ByteString.decoder().map(Checksum::new, Checksum::optimizedByteString); |
| 22 | |
| 23 | private static final ByteString NULL_BYTESTRING = new ByteString(new byte[0]); |
| 24 | |
| 25 | private static final Checksum ZERO = new Checksum(new ByteString(new byte[SIZE])); |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 26 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 27 | public Checksum { |
Matthias Andreas Benkard | f96d0e3 | 2021-12-29 21:53:50 +0100 | [diff] [blame] | 28 | if (byteString.size() == 0) { |
| 29 | byteString = zero().byteString; |
| 30 | } |
| 31 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 32 | if (byteString.size() != SIZE) { |
| 33 | throw new IllegalArgumentException( |
| 34 | "attempted to construct Checksum of length %d (expected: %d)" |
| 35 | .formatted(byteString.size(), SIZE)); |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * A decoder for a {@code byte[]} that wraps the result in a {@link Checksum}. |
| 41 | * |
| 42 | * @return a possibly shared {@link Decoder}. |
| 43 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 44 | public static Decoder<Checksum> decoder() { |
| 45 | return DECODER; |
| 46 | } |
| 47 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 48 | /** |
| 49 | * Returns an empty checksum. |
| 50 | * |
| 51 | * @return a checksum whose bits are all zero. |
| 52 | */ |
| 53 | public static Checksum zero() { |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame^] | 54 | return ZERO; |
| 55 | } |
| 56 | |
| 57 | public ByteString optimizedByteString() { |
| 58 | return isEmpty() ? NULL_BYTESTRING : byteString; |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 61 | /** |
| 62 | * Checks whether the checksum contains only zero bits. |
| 63 | * |
| 64 | * @return {@code true} if the byte string is equal to {@link #zero()}, {@code false} otherwise. |
| 65 | */ |
| 66 | public boolean isEmpty() { |
Matthias Andreas Benkard | c442ebe | 2023-12-10 17:58:38 +0100 | [diff] [blame^] | 67 | return equals(ZERO); |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Converts the contained byte array into a hex string. |
| 72 | * |
| 73 | * <p>Useful for printing. |
| 74 | * |
| 75 | * @return a hex string representation of the bytes making up this checksum. |
| 76 | */ |
| 77 | public String hex() { |
| 78 | return byteString.hex(); |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Parses a hex string into a {@link Checksum}. |
| 83 | * |
| 84 | * @param hex a hex string. |
| 85 | * @return a {@link Checksum} corresponding to the given hex string. |
| 86 | */ |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 87 | public static Checksum ofHex(String hex) { |
| 88 | return new Checksum(ByteString.ofHex(hex)); |
| 89 | } |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 90 | |
| 91 | /** |
Matthias Andreas Benkard | c7aa2b6 | 2022-01-23 18:10:03 +0100 | [diff] [blame] | 92 | * Converts the contained byte array into modified Base64 (with {@code "/"} replaced with {@code |
| 93 | * "-"}). |
| 94 | * |
| 95 | * <p>Modified Base64 is Base64 with {@code "/"} replaced with {@code "_"}. It is used to address |
| 96 | * static deltas in an OSTree repository. |
| 97 | * |
| 98 | * <p>Useful for printing. |
| 99 | * |
| 100 | * @return a modified Base64 representation of the bytes making up this checksum. |
| 101 | */ |
| 102 | public String modifiedBase64() { |
| 103 | return byteString.modifiedBase64(); |
| 104 | } |
| 105 | |
| 106 | /** |
| 107 | * Parses a modified Base64 string into a {@link Checksum}. |
| 108 | * |
| 109 | * <p>Modified Base64 is Base64 with {@code "/"} replaced with {@code "_"}. It is used to address |
| 110 | * static deltas in an OSTree repository. |
| 111 | * |
| 112 | * @param mbase64 a hex string. |
| 113 | * @return a {@link Checksum} corresponding to the given modified Base64 string. |
| 114 | */ |
| 115 | public static Checksum ofModifiedBase64(String mbase64) { |
| 116 | return new Checksum(ByteString.ofModifiedBase64(mbase64)); |
| 117 | } |
| 118 | |
| 119 | /** |
Matthias Andreas Benkard | 0511464 | 2021-12-29 21:51:29 +0100 | [diff] [blame] | 120 | * Reads a Checksum for a {@link ByteBuffer}. |
| 121 | * |
| 122 | * @param byteBuffer the byte buffer to read from. |
| 123 | * @return a checksum. |
| 124 | */ |
| 125 | public static Checksum readFrom(ByteBuffer byteBuffer) { |
| 126 | var bytes = new byte[SIZE]; |
| 127 | byteBuffer.get(bytes); |
| 128 | return new Checksum(new ByteString(bytes)); |
| 129 | } |
Matthias Andreas Benkard | 4e8423d | 2021-12-19 22:56:09 +0100 | [diff] [blame] | 130 | } |