Skip to content

Commit 235577f

Browse files
authored
Merge pull request #114 from filip26/feat/uvarint-write
2.3.0
2 parents f8545c5 + e81a053 commit 235577f

4 files changed

Lines changed: 32 additions & 35 deletions

File tree

README.md

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ encoding and decoding of self-describing data formats.
2424
[![Maven Central](https://img.shields.io/maven-central/v/com.apicatalog/copper-multicodec.svg?label=Maven%20Central)](https://search.maven.org/search?q=g:com.apicatalog%20AND%20a:copper-multicodec)
2525
[![License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](https://opensource.org/licenses/Apache-2.0)
2626

27-
## Features
27+
## Features
2828

2929
- Static codec registry with predefined codecs for efficient access
3030
- Direct static access to codecs without runtime lookups
@@ -56,8 +56,8 @@ byte[] decoded = decoder.decode(encoded);
5656

5757
/* or check if encoding is supported */
5858
byte[] decoded = decoder.getCodec(encoded)
59-
.map(codec -> codec.decode(encoded))
60-
.orElseThrow(() -> new IllegalArgumentException("Unsupported codec."));
59+
.map(codec -> codec.decode(encoded))
60+
.orElseThrow(() -> new IllegalArgumentException("Unsupported codec."));
6161

6262
/* or directly when only one codec is supported */
6363
byte[] decoded = KeyCodec.P521_PUBLIC_KEY.decode(encoded);
@@ -106,16 +106,16 @@ byte[] decoded = MultihashCodec.SHA2_384.decode(encoded);
106106
107107
/* check if byte array is encoded with multihash codec */
108108
if (MultihashCodec.SHA2_384.isEncoded(encoded)) {
109+
var digestLength = MultihashCodec.SHA2_384.digestLength(encoded);
109110
...
110111
}
111112

112-
/* get registry initialized with all multihash codecs */
113-
var registry = MulticodecRegistry.getInstance(Tag.Multihash);
113+
/* determine digest index */
114+
var index = decoder.getCodec(encoded)
115+
.map(Multihash.class::cast)
116+
.mapToInt(codec -> encoded.length - codec.digestLength(encoded))
117+
.orElseThrow(() -> new IllegalArgumentException("Unsupported multihash."));
114118

115-
/* encode an input as multihash */
116-
byte[] encoded = registry.getCodec(code)
117-
.map(codec -> codec.encode(input))
118-
.orElseThrow(() -> new IllegalArgumentException("Unsupported multihash."));
119119
```
120120

121121
## Installation
@@ -128,13 +128,13 @@ To include Copper Multicodec in your project, add the following dependency to yo
128128
<dependency>
129129
<groupId>com.apicatalog</groupId>
130130
<artifactId>copper-multicodec</artifactId>
131-
<version>2.2.0</version>
131+
<version>2.3.0</version>
132132
</dependency>
133133
```
134134

135-
## ld-cli
135+
## 🛠️ LD-CLI
136136

137-
[ld-cli](https://github.com/filip26/ld-cli) is a command-line utility for
137+
[LD-CLI](https://github.com/filip26/ld-cli) is a command-line utility for
138138
working with multiformats including multibase, multicodec, and multihash,
139139
as well as JSON-LD and related specifications.
140140

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<modelVersion>4.0.0</modelVersion>
66
<groupId>com.apicatalog</groupId>
77
<artifactId>copper-multicodec</artifactId>
8-
<version>2.3.0-SNAPSHOT</version>
8+
<version>2.3.0</version>
99
<packaging>jar</packaging>
1010
<url>https://github.com/filip26/copper-multicodec</url>
1111
<scm>

src/main/java/com/apicatalog/multihash/Multihash.java

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -214,31 +214,19 @@ public byte[] decode(byte[] encoded, int index, int length) {
214214
throw new IllegalArgumentException(
215215
"The requested decode length (" + length + ") is greater than the available bytes (" + (encoded.length - index) + ").");
216216
}
217+
218+
long digestLength = digestLength(encoded, index, length);
219+
int varintDigestLength = UVarInt.byteLength(digestLength);
217220

218-
if (length < (codeVarint.length + 2)) {
219-
throw new IllegalArgumentException(
220-
"The value to decode must be a non-empty byte array with a minimum length of "
221-
+ (codeVarint.length + 2) + " bytes, but the actual length is " + length + " bytes.");
222-
}
223-
224-
if (!IntStream.range(0, codeVarint.length).allMatch(i -> codeVarint[i] == encoded[i + index])) {
225-
throw new IllegalArgumentException(
226-
"The provided value is not encoded with this multihash: " + toString() + ".");
227-
}
228-
229-
// Get digest size
230-
long size = UVarInt.decode(encoded, index + codeVarint.length);
231-
int sizeVarintLength = UVarInt.byteLength(size);
232-
233-
if (size != (length - codeVarint.length - sizeVarintLength)) {
221+
if (digestLength != (length - codeVarint.length - varintDigestLength)) {
234222
throw new IllegalArgumentException(
235-
"Digest size mismatch: declared size is " + size + " bytes, but the actual digest size is " +
236-
(length - codeVarint.length - sizeVarintLength) + " bytes.");
223+
"Digest size mismatch: declared size is " + digestLength + " bytes, but the actual digest size is " +
224+
(length - codeVarint.length - varintDigestLength) + " bytes.");
237225
}
238226

239227
// Extract digest
240228
return Arrays.copyOfRange(encoded,
241-
index + codeVarint.length + sizeVarintLength,
229+
index + codeVarint.length + varintDigestLength,
242230
length + index);
243231
}
244232

@@ -261,7 +249,7 @@ public byte[] decode(byte[] encoded, int index, int length) {
261249
* with this multihash's code
262250
*/
263251
public long digestLength(byte[] encoded) {
264-
return digestLength(encoded, 0);
252+
return digestLength(encoded, 0, encoded.length);
265253
}
266254

267255
/**
@@ -288,12 +276,16 @@ public long digestLength(byte[] encoded) {
288276
* available range
289277
*/
290278
public long digestLength(byte[] encoded, int index) {
279+
return digestLength(encoded, index, encoded.length - index);
280+
}
281+
282+
protected long digestLength(byte[] encoded, int index, int length) {
291283
Objects.requireNonNull(encoded);
292284

293-
if (encoded.length < (codeVarint.length + 2)) {
285+
if (length < (codeVarint.length + 2)) {
294286
throw new IllegalArgumentException(
295287
"The value to decode must be a non-empty byte array with a minimum length of "
296-
+ (codeVarint.length + 2) + " bytes, but the actual length is " + encoded.length + " bytes.");
288+
+ (codeVarint.length + 2) + " bytes, but the actual length is " + length + " bytes.");
297289
}
298290

299291
if (!IntStream.range(0, codeVarint.length).allMatch(i -> codeVarint[i] == encoded[i + index])) {

src/test/java/com/apicatalog/multihash/MultihashTest.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.apicatalog.multicodec.Multicodec.Tag;
1515
import com.apicatalog.multicodec.MulticodecDecoder;
1616
import com.apicatalog.multicodec.codec.MultihashCodec;
17+
import com.apicatalog.uvarint.UVarInt;
1718

1819
class MultihashTest {
1920

@@ -66,6 +67,8 @@ void testDecodeFromIndex(byte[] input, Multihash codec, byte[] expected) {
6667

6768
System.arraycopy(input, 0, array, index, input.length);
6869
assertArrayEquals(expected, codec.decode(array, index));
70+
assertEquals(expected.length, codec.digestLength(array, index));
71+
assertEquals(codec.length() + UVarInt.byteLength(expected.length), input.length - codec.digestLength(array, index));
6972
}
7073

7174
@ParameterizedTest(name = "{index}")
@@ -80,6 +83,8 @@ void testDecodeRange(byte[] input, Multihash codec, byte[] expected) {
8083

8184
System.arraycopy(input, 0, array, index, input.length);
8285
assertArrayEquals(expected, codec.decode(array, index, input.length));
86+
assertEquals(expected.length, codec.digestLength(array, index));
87+
assertEquals(codec.length() + UVarInt.byteLength(expected.length), input.length - codec.digestLength(array, index));
8388
}
8489

8590
@ParameterizedTest(name = "{index}")

0 commit comments

Comments
 (0)