Skip to content

Commit d8bb0b3

Browse files
authored
FFI: fetch callbacks: send correct allocation size (#19)
* FFI: fetch callbacks: send correct allocation size in case of insufficient allocated size in order to retry in Rust code
1 parent e295457 commit d8bb0b3

14 files changed

Lines changed: 711 additions & 129 deletions

File tree

.gitlab-ci.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ build:
1717
services:
1818
- name: gitlab.cosmian.com:5000/core/kms:2.2.0_ci
1919
alias: kms_ci
20+
- redis:latest
2021
script:
2122
- mvn package
2223
artifacts:

CHANGELOG.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,16 @@
22

33
All notable changes to this project will be documented in this file.
44

5+
---
6+
## [0.10.1]
7+
### Added
8+
- Add Findex callbacks implementations for indexes compaction usage
9+
### Changed
10+
- Test Findex with Redis callbacks
11+
### Fixed
12+
- FFI: fetch callbacks: send correct allocation size in case of insufficient allocated size in order to retry in Rust code
13+
### Removed
14+
515
---
616
## [0.10.0]
717
### Added

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ This library free software and is available on Maven Central
3131
<dependency>
3232
<groupId>com.cosmian</groupId>
3333
<artifactId>cosmian_java_lib</artifactId>
34-
<version>0.9.0</version>
34+
<version>0.10.1</version>
3535
</dependency>
3636
```
3737

@@ -54,6 +54,7 @@ KMS Server | Java Lib | GPSW lib | CoverCrypt lib | Findex
5454
2.1.0 | 0.7.7 | 1.1.1 | 4.0.0 | 0.2.3
5555
2.2.0 | 0.8.0 | 2.0.1 | 6.0.1 | 0.4.1
5656
2.2.0 | 0.9.0 | 2.0.1 | 6.0.1 | 0.5.0
57+
2.2.0 | 0.10.1 | 2.0.1 | 6.0.1 | 0.6.1
5758

5859
## Update native libraries
5960

docker-compose.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,8 @@ services:
2323
- "9998:9998"
2424
depends_on:
2525
- db
26+
27+
redis:
28+
image: redis:latest
29+
ports:
30+
- "6379:6379"

pom.xml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
<modelVersion>4.0.0</modelVersion>
44
<groupId>com.cosmian</groupId>
55
<artifactId>cosmian_java_lib</artifactId>
6-
<version>0.10.0</version>
6+
<version>0.10.1</version>
77

88
<name>cosmian_java_lib</name>
99
<description>The Cosmian Java Lib provides local encryption/decyption and access to the Cosmian public platform APIs</description>
@@ -100,6 +100,12 @@
100100
<groupId>org.xerial</groupId>
101101
<artifactId>sqlite-jdbc</artifactId>
102102
<version>3.8.7</version>
103+
</dependency>
104+
<dependency>
105+
<groupId>redis.clients</groupId>
106+
<artifactId>jedis</artifactId>
107+
<version>4.1.1</version>
108+
<type>jar</type>
103109
<scope>test</scope>
104110
</dependency>
105111

src/main/java/com/cosmian/jna/findex/Callbacks/FetchChain.java

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55

66
import com.cosmian.jna.FfiException;
7+
import com.cosmian.jna.findex.Ffi;
78
import com.cosmian.jna.findex.FfiWrapper.FetchChainCallback;
89
import com.cosmian.jna.findex.FfiWrapper.FetchChainInterface;
910
import com.cosmian.jna.findex.Leb128Serializer;
@@ -33,23 +34,14 @@ public int apply(Pointer output, IntByReference outputSize, Pointer uidsPointer,
3334
List<byte[]> chainTableUids = Leb128Serializer.deserializeList(uids);
3435

3536
//
36-
// Select uid and value in EntryTable
37+
// Select uid and value in ChainTable
3738
//
38-
HashMap<byte[], byte[]> values = this.fetch.fetch(chainTableUids);
39+
HashMap<byte[], byte[]> uidsAndValues = this.fetch.fetch(chainTableUids);
3940

4041
//
4142
// Serialize results
4243
//
43-
if (values.size() > 0) {
44-
byte[] valuesBytes = Leb128Serializer.serializeHashMap(values);
45-
46-
output.write(0, valuesBytes, 0, valuesBytes.length);
47-
outputSize.setValue(valuesBytes.length);
48-
} else {
49-
outputSize.setValue(0);
50-
}
51-
52-
return 0;
44+
return Ffi.writeOutputPointerAndSize(uidsAndValues, output, outputSize);
5345
}
5446

5547
}

src/main/java/com/cosmian/jna/findex/Callbacks/FetchEntry.java

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import java.util.List;
55

66
import com.cosmian.jna.FfiException;
7+
import com.cosmian.jna.findex.Ffi;
78
import com.cosmian.jna.findex.FfiWrapper.FetchEntryCallback;
89
import com.cosmian.jna.findex.FfiWrapper.FetchEntryInterface;
910
import com.cosmian.jna.findex.Leb128Serializer;
@@ -39,15 +40,7 @@ public int apply(Pointer output, IntByReference outputSize, Pointer uidsPointer,
3940
//
4041
// Serialize results
4142
//
42-
if (uidsAndValues.size() > 0) {
43-
byte[] uidsAndValuesBytes = Leb128Serializer.serializeHashMap(uidsAndValues);
44-
output.write(0, uidsAndValuesBytes, 0, uidsAndValuesBytes.length);
45-
outputSize.setValue(uidsAndValuesBytes.length);
46-
} else {
47-
outputSize.setValue(0);
48-
}
49-
50-
return 0;
43+
return Ffi.writeOutputPointerAndSize(uidsAndValues, output, outputSize);
5144
}
5245

5346
}

src/main/java/com/cosmian/jna/findex/Ffi.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,22 @@ public static void set_error(String error_msg) throws FfiException {
6565
unwrap(Ffi.INSTANCE.set_error(error_msg));
6666
}
6767

68+
public static int writeOutputPointerAndSize(HashMap<byte[], byte[]> uidsAndValues, Pointer output,
69+
IntByReference outputSize) {
70+
if (uidsAndValues.size() > 0) {
71+
byte[] uidsAndValuesBytes = Leb128Serializer.serializeHashMap(uidsAndValues);
72+
if (outputSize.getValue() < uidsAndValuesBytes.length) {
73+
outputSize.setValue(uidsAndValuesBytes.length);
74+
return 1;
75+
}
76+
outputSize.setValue(uidsAndValuesBytes.length);
77+
output.write(0, uidsAndValuesBytes, 0, uidsAndValuesBytes.length);
78+
} else {
79+
outputSize.setValue(0);
80+
}
81+
return 0;
82+
}
83+
6884
public static void upsert(MasterKeys masterKeys, byte[] label, HashMap<IndexedValue, Word[]> indexedValuesAndWords,
6985
FetchEntryCallback fetchEntry, UpsertEntryCallback upsertEntry, UpsertChainCallback upsertChain)
7086
throws FfiException, CosmianException {

0 commit comments

Comments
 (0)