All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cosmian.jna.findex.ffi.FFiUtils Maven / Gradle / Ivy

package com.cosmian.jna.findex.ffi;

import java.util.Map;
import java.util.Set;

import com.cosmian.jna.findex.serde.Leb128Serializable;
import com.cosmian.jna.findex.serde.Leb128Writer;
import com.cosmian.utils.CloudproofException;
import com.sun.jna.Pointer;
import com.sun.jna.ptr.IntByReference;

public class FFiUtils {

    /**
     * Serialize a map to a memory location specified by the Pointer; set its actual size in the pointed int.
     *
     * @param  the map key type. Must be {@link Leb128Serializable}
     * @param  the map value type. Must be {@link Leb128Serializable}
     * @param map the map to serialize and export
     * @param output the output Pointer
     * @param outputSize the output byte size
     * @return 0 on success, 1 if the pre-allocated memory is too small. The outputSized contains the required size to
     *         hold the map.
     * @throws CloudproofException if the pointer cannot be constructed
     */
    public static  int mapToOutputPointer(Map map,
                                                                                                      Pointer output,
                                                                                                      IntByReference outputSize)
        throws CloudproofException {
        byte[] uidsAndValuesBytes = Leb128Writer.serializeMap(map);
        if (outputSize.getValue() < uidsAndValuesBytes.length) {
            outputSize.setValue(uidsAndValuesBytes.length);
            return 1;
        }
        outputSize.setValue(uidsAndValuesBytes.length);
        output.write(0, uidsAndValuesBytes, 0, uidsAndValuesBytes.length);
        return 0;
    }

    public static  int setToOutputPointer(Set set,
                                                                        Pointer output,
                                                                        IntByReference outputSize)
        throws CloudproofException {
        byte[] uidsAndValuesBytes = Leb128Writer.serializeCollection(set);
        if (outputSize.getValue() < uidsAndValuesBytes.length) {
            outputSize.setValue(uidsAndValuesBytes.length);
            return 1;
        }
        outputSize.setValue(uidsAndValuesBytes.length);
        output.write(0, uidsAndValuesBytes, 0, uidsAndValuesBytes.length);
        return 0;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy