Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Negative values for ASN1 grammar #16

Open
jayanam opened this issue Dec 23, 2024 · 0 comments
Open

Negative values for ASN1 grammar #16

jayanam opened this issue Dec 23, 2024 · 0 comments

Comments

@jayanam
Copy link

jayanam commented Dec 23, 2024

I use this simple ASN1:

CAM DEFINITIONS ::= BEGIN
Latitude ::= INTEGER (-900000000..900000001)
Longitude ::= INTEGER (-1800000000..1800000001)

CAM ::= SEQUENCE {
    latitude Latitude,
    longitude Longitude
}

END

When I encode and decode 2 long values, I get negative values in the decoded array.

Here is the code I used:

    try {

        // R.raw.cam is the CAM ASN1
        InputStream asn1Stream = getResources().openRawResource(R.raw.cam);

        // Initialize ASN.1 Translator
        // ByteArrayInputStream schemaStream = new ByteArrayInputStream(camASN1Schema.getBytes());
        ASN1Translator translator = new ASN1Translator(
                new PERTranslatorFactory(true),  // true for aligned PER
                Collections.singletonList(asn1Stream)
        );

        System.out.println("ASN1 Translator successfully created!");

        // Sample values for testing
        long latitude = 48825214;
        long longitude = 9097241;

        // ASN.1 JSON structure
        String camMessageAsJSON = "{ \"latitude\": " + latitude + ", \"longitude\": " + longitude + " }";

        InputStream inputStream = new ByteArrayInputStream(camMessageAsJSON.getBytes());

        JSONFormatReader reader = new JSONFormatReader(inputStream, "CAM");

        BitArray bitArray = new BitArray();

        translator.encode("CAM", bitArray, reader);

        byte[] encodedMessage = bitArray.getBinaryArray();

        // Create an InputStream from the byte array
        InputStream inputStream2 = new ByteArrayInputStream(encodedMessage);

        // Create a JSONFormatWriter
        JSONFormatWriter formatWriter = new JSONFormatWriter();

        // Decode the message
        translator.decode("CAM", inputStream2, formatWriter);

        // Get the decoded data as a JsonNode
        JsonNode decodedMessageJson = formatWriter.getJsonNode();

        // Extract latitude and longitude
        latitude = decodedMessageJson.get("latitude").asInt();
        longitude = decodedMessageJson.get("longitude").asInt();

        // Convert latitude and longitude to decimal degrees
        double latitudeDeg = latitude / 10000000.0;
        double longitudeDeg = longitude / 10000000.0;

    } catch (Exception e) {
        e.printStackTrace();
        System.err.println("Error while encoding or decoding the CAM message: " + e.getMessage());
        if (e instanceof NullPointerException) {
            // Log further information to debug
            System.err.println("NullPointerException occurred: " + e.getStackTrace());
        }
    }

If I use the boundaries (0..900000001) and (0..1800000001) the encoding and decoding works, but the original ANS1 grammar uses the boundaries I stated above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant