diff --git a/LICENSE b/LICENSE index 4fffea1..a9eca5b 100644 --- a/LICENSE +++ b/LICENSE @@ -1,6 +1,6 @@ MIT License -Copyright (c) 2018 Alen Turkovic +Copyright (c) 2020 Alen Turkovic Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal diff --git a/README.adoc b/README.adoc index afbce1a..3fd3ccc 100644 --- a/README.adoc +++ b/README.adoc @@ -1,9 +1,9 @@ = Asn annotation based parser -A Jackson inspired annotation based parser for Asn.1 data. Decode or encode your POJOs with annotations. +A Jackson inspired annotation based parser for Asn.1 BER data. +Decode or encode your POJOs with annotations. -Most of the Asn.1 parser I have seen online were either using their specific classes to decode data (i.e. AsnInteger instead of using regular java int or Integer) -or they came with a generator which converted Asn specifications into obscure code riddled with their internals. +Most of the Asn.1 parser I have seen online were either using their specific classes to decode data (i.e. AsnInteger instead of using regular java int or Integer) or they came with a generator which converted Asn specifications into obscure code riddled with their internals. This parser allows selective encoding/decoding, meaning that you don't have to read all Asn.1 fields into your data, but selectively choose which fields to decode. @@ -122,8 +122,8 @@ Add the following under your ``: ---- - com.github.alturkovic.asn-parser - asn-ber-parser + com.github.alturkovic + asn-parser [insert latest version here] diff --git a/asn-ber-parser/pom.xml b/asn-ber-parser/pom.xml deleted file mode 100644 index cbf94d9..0000000 --- a/asn-ber-parser/pom.xml +++ /dev/null @@ -1,44 +0,0 @@ - - - - 4.0.0 - - - com.github.alturkovic - asn-parser - 1.3.4 - - - asn-ber-parser - - - - org.projectlombok - lombok - true - - - - com.github.alturkovic - asn-parser-core - - - - junit - junit - test - - - pl.pragmatists - JUnitParams - test - - - org.assertj - assertj-core - test - - - \ No newline at end of file diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/decoder/BerDecoder.java b/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/decoder/BerDecoder.java deleted file mode 100644 index 69f0b17..0000000 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/decoder/BerDecoder.java +++ /dev/null @@ -1,195 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2018 Alen Turkovic - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.github.alturkovic.asn.ber.decoder; - -import com.github.alturkovic.asn.AsnAutoResolver; -import com.github.alturkovic.asn.AsnClassDescription; -import com.github.alturkovic.asn.annotation.AsnPostProcessMethod; -import com.github.alturkovic.asn.annotation.AsnStructure; -import com.github.alturkovic.asn.annotation.AsnTag; -import com.github.alturkovic.asn.ber.collection.MultiSet; -import com.github.alturkovic.asn.ber.tlv.BerData; -import com.github.alturkovic.asn.ber.tlv.TlvDataReader; -import com.github.alturkovic.asn.ber.util.BerUtils; -import com.github.alturkovic.asn.ber.util.HexUtils; -import com.github.alturkovic.asn.converter.AsnConverter; -import com.github.alturkovic.asn.decoder.AsnDecoder; -import com.github.alturkovic.asn.exception.AsnConfigurationException; -import com.github.alturkovic.asn.exception.AsnDecodeException; -import com.github.alturkovic.asn.exception.AsnException; -import com.github.alturkovic.asn.field.CollectionTaggedField; -import com.github.alturkovic.asn.field.PrimitiveTaggedField; -import com.github.alturkovic.asn.field.TaggedField; -import com.github.alturkovic.asn.field.accessor.FieldAccessor; -import com.github.alturkovic.asn.tag.Tag; -import com.github.alturkovic.asn.tag.TagFactory; -import java.io.ByteArrayInputStream; -import java.io.InputStream; -import java.lang.reflect.Method; -import java.util.ArrayList; -import java.util.Collection; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Set; -import lombok.Data; - -@Data -public class BerDecoder implements AsnDecoder { - private final TagFactory tagFactory; - private final AsnAutoResolver autoResolver; - private final FieldAccessor fieldAccessor; - private final TlvDataReader tlvDataReader; - private final Map, AsnClassDescription> classDescriptionCache; - private final Map>, AsnConverter> converterCache; - - @Override - public X decode(final Class clazz, final byte[] data) { - if (data == null) { - throw new AsnDecodeException("Cannot decode null data into: " + clazz.getSimpleName()); - } - - final AsnStructure clazzDeclaredAnnotation = clazz.getDeclaredAnnotation(AsnStructure.class); - if (clazzDeclaredAnnotation == null) { - throw new AsnDecodeException("Missing class AsnStructure annotation"); - } - - final AsnTag tag = clazzDeclaredAnnotation.value(); - final Tag fieldStructureTag = tagFactory.get(tag, true); - return decodeStructure(clazz, fieldStructureTag, data); - } - - private X decodeStructure(final Class clazz, final Tag structureTag, final byte[] data) { - try { - final AsnClassDescription asnClassDescription = classDescriptionCache.computeIfAbsent(clazz, (aClass) -> new AsnClassDescription(tagFactory, autoResolver, aClass)); - - final BerData tlvData = tlvDataReader.readNext(new ByteArrayInputStream(data)); - final Tag parsedMainTag = BerUtils.parseTag(tlvData.getTag()); - - if (!structureTag.equals(parsedMainTag)) { - throw new AsnDecodeException(String.format("Defined structure tag %s on %s does not match parser tag %s", structureTag, clazz, parsedMainTag)); - } - - final X instance = clazz.newInstance(); - final InputStream valueStream = new ByteArrayInputStream(tlvData.getValue()); - - final MultiSet tagCounter = new MultiSet<>(); - while (valueStream.available() > 0) { - //Read element by element - final BerData fieldTlvData = tlvDataReader.readNext(valueStream); - - final Tag parsedFieldTag = BerUtils.parseTag(fieldTlvData.getTag()); - final int index = tagCounter.count(parsedFieldTag); - tagCounter.add(parsedFieldTag); - - final TaggedField taggedField = asnClassDescription.findByTag(parsedFieldTag, index); - if (taggedField == null) { - continue; - } - - if (taggedField.isPrimitive()) { - //noinspection unchecked - final AsnConverter asnConverter = loadAsnConverterFromCache((Class>) ((PrimitiveTaggedField) taggedField).getConverter()); - - try { - fieldAccessor.setFieldValue(instance, taggedField.getField(), asnConverter.decode(fieldTlvData.getValue())); - } catch (final AsnException e) { - throw new AsnDecodeException(String.format("Cannot set value '%s' into field '%s'", HexUtils.encode(fieldTlvData.getValue()), taggedField.getField().getName()), e); - } - } else if (taggedField.isStructure()) { - fieldAccessor.setFieldValue(instance, taggedField.getField(), decodeStructure(taggedField.getField().getType(), taggedField.getTag(), fieldTlvData.toTlv())); - } else if (taggedField.isCollection()) { - final Class fieldClass = taggedField.getField().getType(); - - final Collection collection; - - if (fieldClass.isAssignableFrom(List.class)) { - collection = new ArrayList<>(); - } else if (fieldClass.isAssignableFrom(Set.class)) { - collection = new HashSet<>(); - } else { - throw new AsnDecodeException(String.format("Unsupported collection type: '%s'. Only List and Set supported!", fieldClass)); - } - - fieldAccessor.setFieldValue(instance, taggedField.getField(), collection); - - decodeCollection(collection, fieldTlvData.getValue(), (CollectionTaggedField) taggedField); - } else { - throw new AsnDecodeException("Unknown TaggedField type: " + taggedField); - } - } - - final AsnPostProcessMethod asnPostProcessMethod = clazz.getDeclaredAnnotation(AsnPostProcessMethod.class); - - if (asnPostProcessMethod != null) { - final Method declaredMethod = clazz.getDeclaredMethod(asnPostProcessMethod.value()); - if (!declaredMethod.isAccessible()) { - declaredMethod.setAccessible(true); - } - declaredMethod.invoke(instance); - } - - return instance; - } catch (final Exception e) { - throw new AsnDecodeException(String.format("Cannot decode '%s' into '%s' class for tag %s", HexUtils.encode(data), clazz.getName(), structureTag), e); - } - } - - private void decodeCollection(final Collection collection, final byte[] elementData, final CollectionTaggedField taggedField) { - try { - final InputStream stream = new ByteArrayInputStream(elementData); - while (stream.available() > 0) { - final BerData elementBerData = tlvDataReader.readNext(stream); - final Tag parsedElementTag = BerUtils.parseTag(elementBerData.getTag()); - if (taggedField.getElementTag().equals(parsedElementTag)) { - if (taggedField.isStructured()) { - final Object element = decodeStructure(taggedField.getType(), BerUtils.UNIVERSAL_TAG, elementBerData.toTlv()); - collection.add(element); - } else { - //noinspection unchecked - final AsnConverter asnConverter = loadAsnConverterFromCache((Class>) taggedField.getConverter()); - try { - collection.add(asnConverter.decode(elementBerData.getValue())); - } catch (final AsnException e) { - throw new AsnDecodeException(String.format("Cannot add primitive value '%s' to collection '%s'", HexUtils.encode(elementBerData.getValue()), taggedField.getField().getName()), e); - } - } - } - } - } catch (final Exception e) { - throw new AsnDecodeException(String.format("Cannot decode collection data '%s' into '%s' class", HexUtils.encode(elementData), taggedField.getType().getName()), e); - } - } - - private AsnConverter loadAsnConverterFromCache(final Class> asnConverterClass) { - return converterCache.computeIfAbsent(asnConverterClass, (aClass) -> { - try { - return aClass.newInstance(); - } catch (final InstantiationException | IllegalAccessException e) { - throw new AsnConfigurationException(String.format("Cannot create a new instance of converter %s", aClass), e); - } - }); - } -} diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/decoder/BerDecoderBuilder.java b/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/decoder/BerDecoderBuilder.java deleted file mode 100644 index f2c5db3..0000000 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/decoder/BerDecoderBuilder.java +++ /dev/null @@ -1,84 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2018 Alen Turkovic - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.github.alturkovic.asn.ber.decoder; - -import com.github.alturkovic.asn.AsnAutoResolver; -import com.github.alturkovic.asn.AsnClassDescription; -import com.github.alturkovic.asn.ber.BerAutoResolver; -import com.github.alturkovic.asn.ber.tag.BerTagFactory; -import com.github.alturkovic.asn.ber.tlv.BerDataReader; -import com.github.alturkovic.asn.ber.tlv.TlvDataReader; -import com.github.alturkovic.asn.converter.AsnConverter; -import com.github.alturkovic.asn.field.accessor.DirectFieldAccessor; -import com.github.alturkovic.asn.field.accessor.FieldAccessor; -import com.github.alturkovic.asn.tag.TagFactory; -import java.util.HashMap; -import java.util.Map; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -public class BerDecoderBuilder { - - private TagFactory tagFactory = new BerTagFactory(); - private AsnAutoResolver autoResolver = new BerAutoResolver(); - private FieldAccessor fieldAccessor = new DirectFieldAccessor(); - private TlvDataReader tlvDataReader = new BerDataReader(); - private Map, AsnClassDescription> classDescriptionCache = new HashMap<>(); - private Map>, AsnConverter> converterCache = new HashMap<>(); - - public BerDecoderBuilder tagFactory(final TagFactory tagFactory) { - this.tagFactory = tagFactory; - return this; - } - - public BerDecoderBuilder autoResolver(final AsnAutoResolver autoResolver) { - this.autoResolver = autoResolver; - return this; - } - - public BerDecoderBuilder fieldAccessor(final FieldAccessor fieldAccessor) { - this.fieldAccessor = fieldAccessor; - return this; - } - - public BerDecoderBuilder tlvDataReader(final TlvDataReader tlvDataReader) { - this.tlvDataReader = tlvDataReader; - return this; - } - - public BerDecoderBuilder classDescriptionCache(final Map, AsnClassDescription> classDescriptionCache) { - this.classDescriptionCache = classDescriptionCache; - return this; - } - - public BerDecoderBuilder converterCache(final Map>, AsnConverter> converterCache) { - this.converterCache = converterCache; - return this; - } - - public BerDecoder build() { - return new BerDecoder(tagFactory, autoResolver, fieldAccessor, tlvDataReader, classDescriptionCache, converterCache); - } -} diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/encoder/BerEncoderBuilder.java b/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/encoder/BerEncoderBuilder.java deleted file mode 100644 index a984019..0000000 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/encoder/BerEncoderBuilder.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * MIT License - * - * Copyright (c) 2018 Alen Turkovic - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in all - * copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE - * SOFTWARE. - */ - -package com.github.alturkovic.asn.ber.encoder; - -import com.github.alturkovic.asn.AsnAutoResolver; -import com.github.alturkovic.asn.AsnClassDescription; -import com.github.alturkovic.asn.ber.BerAutoResolver; -import com.github.alturkovic.asn.ber.tag.BerTagFactory; -import com.github.alturkovic.asn.converter.AsnConverter; -import com.github.alturkovic.asn.field.accessor.DirectFieldAccessor; -import com.github.alturkovic.asn.field.accessor.FieldAccessor; -import com.github.alturkovic.asn.tag.TagFactory; -import java.util.HashMap; -import java.util.Map; -import lombok.NoArgsConstructor; - -@NoArgsConstructor -public class BerEncoderBuilder { - - private TagFactory tagFactory = new BerTagFactory(); - private AsnAutoResolver autoResolver = new BerAutoResolver(); - private FieldAccessor fieldAccessor = new DirectFieldAccessor(); - private Map, AsnClassDescription> classDescriptionCache = new HashMap<>(); - private Map>, AsnConverter> converterCache = new HashMap<>(); - - public BerEncoderBuilder tagFactory(final TagFactory tagFactory) { - this.tagFactory = tagFactory; - return this; - } - - public BerEncoderBuilder autoResolver(final AsnAutoResolver autoResolver) { - this.autoResolver = autoResolver; - return this; - } - - public BerEncoderBuilder fieldAccessor(final FieldAccessor fieldAccessor) { - this.fieldAccessor = fieldAccessor; - return this; - } - - public BerEncoderBuilder classDescriptionCache(final Map, AsnClassDescription> classDescriptionCache) { - this.classDescriptionCache = classDescriptionCache; - return this; - } - - public BerEncoderBuilder converterCache(final Map>, AsnConverter> converterCache) { - this.converterCache = converterCache; - return this; - } - - public BerEncoder build() { - return new BerEncoder(tagFactory, autoResolver, fieldAccessor, classDescriptionCache, converterCache); - } -} diff --git a/asn-parser-core/pom.xml b/asn-parser-core/pom.xml deleted file mode 100644 index f976b79..0000000 --- a/asn-parser-core/pom.xml +++ /dev/null @@ -1,23 +0,0 @@ - - - - 4.0.0 - - - com.github.alturkovic - asn-parser - 1.3.4 - - - asn-parser-core - - - - org.projectlombok - lombok - true - - - \ No newline at end of file diff --git a/pom.xml b/pom.xml index 92f8c56..769aa35 100644 --- a/pom.xml +++ b/pom.xml @@ -6,9 +6,7 @@ com.github.alturkovic asn-parser - 1.3.4 - - pom + 1.4.0 asn-parser ASN.1 Decoding/Encoding @@ -37,62 +35,43 @@ UTF-8 UTF-8 - 1.8 + 11 - 1.16.16 + 1.18.12 4.12 1.0.5 3.8.0 - - asn-parser-core - asn-ber-parser - - - - - - org.projectlombok - lombok - ${lombok.version} - true - - - - - com.github.alturkovic - asn-parser-core - ${project.version} - - - com.github.alturkovic - asn-ber-parser - ${project.version} - + + + org.projectlombok + lombok + ${lombok.version} + true + - - - junit - junit - ${junit.version} - test - - - pl.pragmatists - JUnitParams - ${junit-params.version} - test - - - org.assertj - assertj-core - ${assertj-core.version} - test - - - + + + junit + junit + ${junit.version} + test + + + pl.pragmatists + JUnitParams + ${junit-params.version} + test + + + org.assertj + assertj-core + ${assertj-core.version} + test + + diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/AsnClassDescription.java b/src/main/java/com/github/alturkovic/asn/AsnClassDescription.java similarity index 57% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/AsnClassDescription.java rename to src/main/java/com/github/alturkovic/asn/AsnClassDescription.java index 7b14c45..3a94abe 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/AsnClassDescription.java +++ b/src/main/java/com/github/alturkovic/asn/AsnClassDescription.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2019 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,11 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn; import com.github.alturkovic.asn.annotation.AsnCollection; +import com.github.alturkovic.asn.annotation.AsnPolymorphic; import com.github.alturkovic.asn.annotation.AsnPrimitive; import com.github.alturkovic.asn.annotation.AsnStructure; import com.github.alturkovic.asn.annotation.AsnTag; @@ -35,7 +37,7 @@ import com.github.alturkovic.asn.field.StructureTaggedField; import com.github.alturkovic.asn.field.TaggedField; import com.github.alturkovic.asn.tag.Tag; -import com.github.alturkovic.asn.tag.TagFactory; +import com.github.alturkovic.asn.util.ClassUtils; import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collection; @@ -50,9 +52,10 @@ public class AsnClassDescription { private Map> multimap; private List classOrderedTaggedFields; + private Map> polymorphics; - public AsnClassDescription(final TagFactory tagFactory, final AsnAutoResolver asnAutoResolver, final Class clazz) { - init(clazz, tagFactory, asnAutoResolver); + public AsnClassDescription(final Class clazz) { + init(clazz); } // ensures that the order of class defined fields will be kept when encoding @@ -87,60 +90,76 @@ public TaggedField findByTag(final Tag tag, final int index) { return taggedFields.get(index); } - private void init(final Class clazz, final TagFactory tagFactory, final AsnAutoResolver asnAutoResolver) { + public Class findImplementationByTag(final Tag tag) { + if (polymorphics == null) { + return null; + } + + return polymorphics.get(tag); + } + + private void init(final Class clazz) { multimap = new HashMap<>(); + if (clazz.isInterface()) { + polymorphics = new HashMap<>(); + for (final var polymorphic : clazz.getDeclaredAnnotationsByType(AsnPolymorphic.class)) { + polymorphics.put(tag(polymorphic.value(), polymorphic.type(), ClassUtils.isPrimitiveOrWrapper(polymorphic.type())), polymorphic.type()); + } + } + int fieldPosition = 0; - for (final Field field : clazz.getDeclaredFields()) { - Tag tag = null; + for (final var field : clazz.getDeclaredFields()) { TaggedField taggedField = null; if (field.isAnnotationPresent(AsnPrimitive.class)) { - final AsnPrimitive primitiveTag = field.getAnnotation(AsnPrimitive.class); - final AsnTag asnTag = primitiveTag.value(); - - final Class> converter = getConverter(asnAutoResolver, primitiveTag.asnConverter(), field.getType()); - - tag = getTag(tagFactory, asnAutoResolver, asnTag, field.getType(), false); - taggedField = new PrimitiveTaggedField(fieldPosition, tag, field, converter); + taggedField = primitiveField(fieldPosition, field); } else if (field.isAnnotationPresent(AsnStructure.class)) { - final AsnStructure structureTag = field.getAnnotation(AsnStructure.class); - final AsnTag asnTag = structureTag.value(); - - tag = getTag(tagFactory, asnAutoResolver, asnTag, field.getType(), true); - taggedField = new StructureTaggedField(fieldPosition, tag, field); + taggedField = structureField(fieldPosition, field); } else if (field.isAnnotationPresent(AsnCollection.class)) { - final AsnCollection collectionTag = field.getAnnotation(AsnCollection.class); - final AsnTag asnTag = collectionTag.value(); - - final Class> converter = collectionTag.structured() ? null : getConverter(asnAutoResolver, collectionTag.asnConverter(), collectionTag.type()); - - tag = getTag(tagFactory, asnAutoResolver, asnTag, field.getType(), true); - final Tag elementTag = getTag(tagFactory, asnAutoResolver, collectionTag.elementTag(), collectionTag.type(), collectionTag.structured()); - - taggedField = new CollectionTaggedField(fieldPosition, tag, field, collectionTag.structured(), collectionTag.type(), elementTag, converter); + taggedField = collectionField(fieldPosition, field); } - if (tag != null && taggedField != null) { - final List listForTag = multimap.computeIfAbsent(tag, k -> new ArrayList<>()); - listForTag.add(taggedField); + if (taggedField != null) { + multimap.computeIfAbsent(taggedField.getTag(), k -> new ArrayList<>()).add(taggedField); } fieldPosition++; } } - private Tag getTag(final TagFactory tagFactory, final AsnAutoResolver asnAutoResolver, final AsnTag asnTag, final Class clazz, final boolean structured) { + private TaggedField primitiveField(final int fieldPosition, final Field field) { + final var primitiveTag = field.getAnnotation(AsnPrimitive.class); + final var asnTag = primitiveTag.value(); + final var converter = converter(primitiveTag.asnConverter(), field.getType()); + return new PrimitiveTaggedField(fieldPosition, tag(asnTag, field.getType(), false), field, converter); + } + + private TaggedField structureField(final int fieldPosition, final Field field) { + final var structureTag = field.getAnnotation(AsnStructure.class); + final var asnTag = structureTag.value(); + return new StructureTaggedField(fieldPosition, tag(asnTag, field.getType(), true), field); + } + + private TaggedField collectionField(final int fieldPosition, final Field field) { + final var collectionTag = field.getAnnotation(AsnCollection.class); + final var tag = tag(collectionTag.value(), field.getType(), true); + final var elementTag = tag(collectionTag.elementTag(), collectionTag.type(), collectionTag.structured()); + final var converter = collectionTag.structured() ? null : converter(collectionTag.asnConverter(), collectionTag.type()); + return new CollectionTaggedField(fieldPosition, tag, field, collectionTag.structured(), collectionTag.type(), elementTag, converter); + } + + private Tag tag(final AsnTag asnTag, final Class clazz, final boolean structured) { if (asnTag.value() == -1) { - return asnAutoResolver.getUniversalTag(clazz, structured); + return BerAutoResolver.getUniversalTag(clazz, structured); } - return tagFactory.get(asnTag, structured); + return new Tag(asnTag.value(), asnTag.type(), structured); } - private Class> getConverter(final AsnAutoResolver asnAutoResolver, final Class> converter, final Class clazz) { + private Class> converter(final Class> converter, final Class clazz) { if (converter.equals(AutoConverter.class)) { - return asnAutoResolver.getUniversalConverterClass(clazz); + return BerAutoResolver.getUniversalConverterClass(clazz); } return converter; diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/BerAutoResolver.java b/src/main/java/com/github/alturkovic/asn/BerAutoResolver.java similarity index 74% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/BerAutoResolver.java rename to src/main/java/com/github/alturkovic/asn/BerAutoResolver.java index 281a86a..feaf997 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/BerAutoResolver.java +++ b/src/main/java/com/github/alturkovic/asn/BerAutoResolver.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,26 +20,30 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber; +package com.github.alturkovic.asn; -import com.github.alturkovic.asn.AsnAutoResolver; -import com.github.alturkovic.asn.Type; -import com.github.alturkovic.asn.UniversalTags; -import com.github.alturkovic.asn.ber.converter.*; -import com.github.alturkovic.asn.ber.tag.BerTag; +import com.github.alturkovic.asn.converter.AsciiStringConverter; import com.github.alturkovic.asn.converter.AsnConverter; import com.github.alturkovic.asn.converter.AutoConverter; +import com.github.alturkovic.asn.converter.BooleanConverter; +import com.github.alturkovic.asn.converter.IntegerConverter; +import com.github.alturkovic.asn.converter.LongConverter; +import com.github.alturkovic.asn.converter.ShortConverter; +import com.github.alturkovic.asn.converter.Utf8StringConverter; import com.github.alturkovic.asn.exception.AsnConfigurationException; import com.github.alturkovic.asn.tag.Tag; -import java.util.Set; +import com.github.alturkovic.asn.tag.Type; +import com.github.alturkovic.asn.tag.UniversalTags; +import com.github.alturkovic.asn.util.ClassUtils; +import java.util.Collection; import lombok.AllArgsConstructor; -public class BerAutoResolver implements AsnAutoResolver { +public class BerAutoResolver { - @Override - public Class> getUniversalConverterClass(final Class c) { + public static Class> getUniversalConverterClass(final Class c) { if (c == null) { throw new AsnConfigurationException("Cannot get a converter for null class"); } @@ -49,10 +53,9 @@ public class BerAutoResolver implements AsnAutoResolver { return Utf8StringConverter.class; } - final Class clazz = supportedWrapperToPrimitive(c); + final var clazz = ClassUtils.supportedWrapperToPrimitive(c); if (clazz.isPrimitive() || clazz == byte[].class) { - final Mappings[] mappings = Mappings.values(); - for (final Mappings mapping : mappings) { + for (final var mapping : Mappings.values()) { if (mapping.clazz != null && mapping.clazz.equals(clazz)) { return mapping.converterClass; } @@ -62,23 +65,21 @@ public class BerAutoResolver implements AsnAutoResolver { throw new AsnConfigurationException("Cannot get a converter for: " + clazz.getName()); } - @Override - public Tag getUniversalTag(final Class c, final boolean constructed) { + public static Tag getUniversalTag(final Class c, final boolean constructed) { if (c == null) { return null; } if (c == String.class) { // String has multiple tags that can represent it, just hardcode this one - return new BerTag(Mappings.OCTET_STRING.value, Type.UNIVERSAL, constructed); + return new Tag(Mappings.OCTET_STRING.value, Type.UNIVERSAL, constructed); } Integer value = null; - final Class clazz = supportedWrapperToPrimitive(c); + final var clazz = ClassUtils.supportedWrapperToPrimitive(c); if (clazz.isPrimitive() || clazz == byte[].class) { - final Mappings[] mappings = Mappings.values(); - for (final Mappings mapping : mappings) { + for (final var mapping : Mappings.values()) { if (mapping.clazz != null && mapping.clazz.equals(clazz)) { value = mapping.value; break; @@ -87,30 +88,10 @@ public Tag getUniversalTag(final Class c, final boolean constructed) { } if (value == null) { - value = c.isAssignableFrom(Set.class) ? Mappings.SET.value : Mappings.SEQUENCE.value; - } - - return new BerTag(value, Type.UNIVERSAL, constructed); - } - - private Class supportedWrapperToPrimitive(final Class clazz) { - if (clazz == Integer.class) { - return int.class; - } - - if (clazz == Long.class) { - return long.class; - } - - if (clazz == Boolean.class) { - return boolean.class; - } - - if (clazz == Short.class) { - return short.class; + value = Collection.class.isAssignableFrom(c) ? Mappings.SET.value : Mappings.SEQUENCE.value; } - return clazz; + return new Tag(value, Type.UNIVERSAL, constructed); } @AllArgsConstructor diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnCollection.java b/src/main/java/com/github/alturkovic/asn/annotation/AsnCollection.java similarity index 98% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnCollection.java rename to src/main/java/com/github/alturkovic/asn/annotation/AsnCollection.java index a1092b3..dffa8b1 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnCollection.java +++ b/src/main/java/com/github/alturkovic/asn/annotation/AsnCollection.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.annotation; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/AsnAutoResolver.java b/src/main/java/com/github/alturkovic/asn/annotation/AsnPolymorphic.java similarity index 62% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/AsnAutoResolver.java rename to src/main/java/com/github/alturkovic/asn/annotation/AsnPolymorphic.java index d2c099e..d4860b4 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/AsnAutoResolver.java +++ b/src/main/java/com/github/alturkovic/asn/annotation/AsnPolymorphic.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,31 +20,31 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn; +package com.github.alturkovic.asn.annotation; -import com.github.alturkovic.asn.converter.AsnConverter; -import com.github.alturkovic.asn.tag.Tag; +import java.lang.annotation.ElementType; +import java.lang.annotation.Repeatable; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; /** - * Resolves encoding-specific universal details. + * Represents an ASN CHOICE implementation which should be described on the interface with each of it's implementations. */ -public interface AsnAutoResolver { +@Repeatable(AsnPolymorphics.class) +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface AsnPolymorphic { /** - * Resolves the universal converter for the given class. - * - * @param c class - * @return default converter + * Implementation tag. */ - Class> getUniversalConverterClass(Class c); + AsnTag value(); /** - * Resolves the universal tag for the given class. - * - * @param c class - * @param constructed indicates if the tag is structured or primitive - * @return universal tag + * Implementation type of the choice. */ - Tag getUniversalTag(Class c, boolean constructed); + Class type(); } diff --git a/src/main/java/com/github/alturkovic/asn/annotation/AsnPolymorphics.java b/src/main/java/com/github/alturkovic/asn/annotation/AsnPolymorphics.java new file mode 100644 index 0000000..878d7f2 --- /dev/null +++ b/src/main/java/com/github/alturkovic/asn/annotation/AsnPolymorphics.java @@ -0,0 +1,37 @@ +/* + * MIT License + * + * Copyright (c) 2020 Alen Turkovic + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package com.github.alturkovic.asn.annotation; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +@Retention(RetentionPolicy.RUNTIME) +@Target(ElementType.TYPE) +public @interface AsnPolymorphics { + AsnPolymorphic[] value(); +} diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnPostProcessMethod.java b/src/main/java/com/github/alturkovic/asn/annotation/AsnPostProcessMethod.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnPostProcessMethod.java rename to src/main/java/com/github/alturkovic/asn/annotation/AsnPostProcessMethod.java index e65d644..019cae3 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnPostProcessMethod.java +++ b/src/main/java/com/github/alturkovic/asn/annotation/AsnPostProcessMethod.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.annotation; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnPrimitive.java b/src/main/java/com/github/alturkovic/asn/annotation/AsnPrimitive.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnPrimitive.java rename to src/main/java/com/github/alturkovic/asn/annotation/AsnPrimitive.java index 18779e4..5bd68d4 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnPrimitive.java +++ b/src/main/java/com/github/alturkovic/asn/annotation/AsnPrimitive.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.annotation; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnStructure.java b/src/main/java/com/github/alturkovic/asn/annotation/AsnStructure.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnStructure.java rename to src/main/java/com/github/alturkovic/asn/annotation/AsnStructure.java index 8202999..c08f3cc 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnStructure.java +++ b/src/main/java/com/github/alturkovic/asn/annotation/AsnStructure.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.annotation; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnTag.java b/src/main/java/com/github/alturkovic/asn/annotation/AsnTag.java similarity index 95% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnTag.java rename to src/main/java/com/github/alturkovic/asn/annotation/AsnTag.java index 09273be..1739947 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/annotation/AsnTag.java +++ b/src/main/java/com/github/alturkovic/asn/annotation/AsnTag.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,11 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.annotation; -import com.github.alturkovic.asn.Type; +import com.github.alturkovic.asn.tag.Type; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/AsciiStringConverter.java b/src/main/java/com/github/alturkovic/asn/converter/AsciiStringConverter.java similarity index 91% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/AsciiStringConverter.java rename to src/main/java/com/github/alturkovic/asn/converter/AsciiStringConverter.java index 38ce52b..5c84322 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/AsciiStringConverter.java +++ b/src/main/java/com/github/alturkovic/asn/converter/AsciiStringConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,11 +20,11 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.converter.AsnConverter; import java.nio.charset.StandardCharsets; public class AsciiStringConverter implements AsnConverter { diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/converter/AsnConverter.java b/src/main/java/com/github/alturkovic/asn/converter/AsnConverter.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/converter/AsnConverter.java rename to src/main/java/com/github/alturkovic/asn/converter/AsnConverter.java index ba8c828..2f8dea5 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/converter/AsnConverter.java +++ b/src/main/java/com/github/alturkovic/asn/converter/AsnConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.converter; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/converter/AutoConverter.java b/src/main/java/com/github/alturkovic/asn/converter/AutoConverter.java similarity index 90% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/converter/AutoConverter.java rename to src/main/java/com/github/alturkovic/asn/converter/AutoConverter.java index 5af1032..a149f9c 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/converter/AutoConverter.java +++ b/src/main/java/com/github/alturkovic/asn/converter/AutoConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,12 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.converter; /** - * A no-op converter. + * A no-op converter used as a marker to indicate automatic type resolving for the conversion should be used. */ public class AutoConverter implements AsnConverter { diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/BooleanConverter.java b/src/main/java/com/github/alturkovic/asn/converter/BooleanConverter.java similarity index 90% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/BooleanConverter.java rename to src/main/java/com/github/alturkovic/asn/converter/BooleanConverter.java index 75950a2..94394aa 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/BooleanConverter.java +++ b/src/main/java/com/github/alturkovic/asn/converter/BooleanConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2019 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,13 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.util.HexUtils; -import com.github.alturkovic.asn.converter.AsnConverter; import com.github.alturkovic.asn.exception.AsnConvertException; +import com.github.alturkovic.asn.util.HexUtils; public class BooleanConverter implements AsnConverter { diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/DateConverter.java b/src/main/java/com/github/alturkovic/asn/converter/DateConverter.java similarity index 91% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/DateConverter.java rename to src/main/java/com/github/alturkovic/asn/converter/DateConverter.java index 1165464..b51aa64 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/DateConverter.java +++ b/src/main/java/com/github/alturkovic/asn/converter/DateConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,11 +20,11 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.converter.AsnConverter; import java.util.Date; public class DateConverter implements AsnConverter { diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/HexStringConverter.java b/src/main/java/com/github/alturkovic/asn/converter/HexStringConverter.java similarity index 89% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/HexStringConverter.java rename to src/main/java/com/github/alturkovic/asn/converter/HexStringConverter.java index 16bf132..275bb88 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/HexStringConverter.java +++ b/src/main/java/com/github/alturkovic/asn/converter/HexStringConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,13 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.util.HexUtils; -import com.github.alturkovic.asn.converter.AsnConverter; import com.github.alturkovic.asn.exception.AsnConvertException; +import com.github.alturkovic.asn.util.HexUtils; public class HexStringConverter implements AsnConverter { diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/IntegerConverter.java b/src/main/java/com/github/alturkovic/asn/converter/IntegerConverter.java similarity index 90% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/IntegerConverter.java rename to src/main/java/com/github/alturkovic/asn/converter/IntegerConverter.java index 21fb079..fadec7f 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/IntegerConverter.java +++ b/src/main/java/com/github/alturkovic/asn/converter/IntegerConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,13 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.util.HexUtils; -import com.github.alturkovic.asn.converter.AsnConverter; import com.github.alturkovic.asn.exception.AsnConvertException; +import com.github.alturkovic.asn.util.HexUtils; import java.math.BigInteger; public class IntegerConverter implements AsnConverter { diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/LongConverter.java b/src/main/java/com/github/alturkovic/asn/converter/LongConverter.java similarity index 90% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/LongConverter.java rename to src/main/java/com/github/alturkovic/asn/converter/LongConverter.java index 2495ffc..96efecc 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/LongConverter.java +++ b/src/main/java/com/github/alturkovic/asn/converter/LongConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,13 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.util.HexUtils; -import com.github.alturkovic.asn.converter.AsnConverter; import com.github.alturkovic.asn.exception.AsnConvertException; +import com.github.alturkovic.asn.util.HexUtils; import java.math.BigInteger; public class LongConverter implements AsnConverter { diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/ShortConverter.java b/src/main/java/com/github/alturkovic/asn/converter/ShortConverter.java similarity index 90% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/ShortConverter.java rename to src/main/java/com/github/alturkovic/asn/converter/ShortConverter.java index fd9fb05..88e33dc 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/ShortConverter.java +++ b/src/main/java/com/github/alturkovic/asn/converter/ShortConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,14 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.util.HexUtils; -import com.github.alturkovic.asn.converter.AsnConverter; import com.github.alturkovic.asn.exception.AsnConvertException; - +import com.github.alturkovic.asn.util.HexUtils; import java.math.BigInteger; public class ShortConverter implements AsnConverter { diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/Utf8StringConverter.java b/src/main/java/com/github/alturkovic/asn/converter/Utf8StringConverter.java similarity index 91% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/Utf8StringConverter.java rename to src/main/java/com/github/alturkovic/asn/converter/Utf8StringConverter.java index 5699316..338fc80 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/converter/Utf8StringConverter.java +++ b/src/main/java/com/github/alturkovic/asn/converter/Utf8StringConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,11 +20,11 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.converter.AsnConverter; import java.nio.charset.StandardCharsets; public class Utf8StringConverter implements AsnConverter { diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/decoder/AsnDecoder.java b/src/main/java/com/github/alturkovic/asn/decoder/AsnDecoder.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/decoder/AsnDecoder.java rename to src/main/java/com/github/alturkovic/asn/decoder/AsnDecoder.java index 7a4b43f..cf5f0c3 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/decoder/AsnDecoder.java +++ b/src/main/java/com/github/alturkovic/asn/decoder/AsnDecoder.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.decoder; diff --git a/src/main/java/com/github/alturkovic/asn/decoder/BerDecoder.java b/src/main/java/com/github/alturkovic/asn/decoder/BerDecoder.java new file mode 100644 index 0000000..685b337 --- /dev/null +++ b/src/main/java/com/github/alturkovic/asn/decoder/BerDecoder.java @@ -0,0 +1,236 @@ +/* + * MIT License + * + * Copyright (c) 2020 Alen Turkovic + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package com.github.alturkovic.asn.decoder; + +import com.github.alturkovic.asn.AsnClassDescription; +import com.github.alturkovic.asn.annotation.AsnPostProcessMethod; +import com.github.alturkovic.asn.converter.AsnConverter; +import com.github.alturkovic.asn.exception.AsnConfigurationException; +import com.github.alturkovic.asn.exception.AsnDecodeException; +import com.github.alturkovic.asn.exception.AsnException; +import com.github.alturkovic.asn.field.CollectionTaggedField; +import com.github.alturkovic.asn.field.PrimitiveTaggedField; +import com.github.alturkovic.asn.field.TaggedField; +import com.github.alturkovic.asn.field.accessor.DirectFieldAccessor; +import com.github.alturkovic.asn.field.accessor.FieldAccessor; +import com.github.alturkovic.asn.support.Counter; +import com.github.alturkovic.asn.tag.Tag; +import com.github.alturkovic.asn.tlv.BerData; +import com.github.alturkovic.asn.tlv.BerDataReader; +import com.github.alturkovic.asn.tlv.TlvDataReader; +import com.github.alturkovic.asn.util.BerUtils; +import com.github.alturkovic.asn.util.ClassUtils; +import com.github.alturkovic.asn.util.HexUtils; +import java.io.ByteArrayInputStream; +import java.lang.reflect.InvocationTargetException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.HashMap; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import lombok.AllArgsConstructor; +import lombok.Data; + +@Data +@AllArgsConstructor +public class BerDecoder implements AsnDecoder { + private final FieldAccessor fieldAccessor; + private final TlvDataReader tlvDataReader; + private final Map, AsnClassDescription> classDescriptionCache; + private final Map>, AsnConverter> converterCache; + + public BerDecoder() { + this(new BerDataReader()); + } + + public BerDecoder(final TlvDataReader tlvDataReader) { + this(tlvDataReader, new DirectFieldAccessor()); + } + + public BerDecoder(final TlvDataReader tlvDataReader, final FieldAccessor fieldAccessor) { + this(fieldAccessor, tlvDataReader, new HashMap<>(), new HashMap<>()); + } + + @Override + public X decode(final Class clazz, final byte[] data) { + if (data == null) { + throw new AsnDecodeException("Cannot decode null data into: " + clazz.getSimpleName()); + } + + return decodeStructure(clazz, data); + } + + private X decodeStructure(final Class clazz, final byte[] data) { + try { + final var asnClassDescription = loadAsnClassDescription(clazz); + final var tlvData = tlvDataReader.readNext(new ByteArrayInputStream(data)); + + if (clazz.isInterface()) { + return decodePolymorphic(asnClassDescription, tlvData.getValue()); + } + + final var valueStream = new ByteArrayInputStream(tlvData.getValue()); + final var instance = clazz.getDeclaredConstructor().newInstance(); + + final var tagCounter = new Counter(); + while (valueStream.available() > 0) { + //Read element by element + final var fieldTlvData = tlvDataReader.readNext(valueStream); + + final var taggedField = parseTaggedField(asnClassDescription, tagCounter, fieldTlvData); + if (taggedField == null) { + continue; + } + + if (taggedField.isPrimitive()) { + decodePrimitiveField(instance, fieldTlvData, taggedField); + } else if (taggedField.isStructure()) { + decodeStructureField(instance, fieldTlvData, taggedField); + } else if (taggedField.isCollection()) { + decodeCollectionField(instance, fieldTlvData, taggedField); + } else { + throw new AsnDecodeException("Unknown TaggedField type: " + taggedField); + } + } + + invokePostProcessMethod(clazz, instance); + + return instance; + } catch (final Exception e) { + throw new AsnDecodeException(String.format("Cannot decode '%s' into '%s' class", HexUtils.encode(data), clazz.getName()), e); + } + } + + @SuppressWarnings("unchecked") + private X decodePolymorphic(final AsnClassDescription asnClassDescription, final byte[] data) { + // read the next tlv which actually represents the nested choice implementation data + final var implementationData = tlvDataReader.readNext(new ByteArrayInputStream(data)); + final var tag = BerUtils.parseTag(implementationData.getTag()); + final var implementation = asnClassDescription.findImplementationByTag(tag); + + if (implementation == null) { + return null; + } + + if (ClassUtils.isPrimitiveOrWrapper(implementation)) { + final var converter = loadAsnConverterFromCache((Class>) implementation); + return (X) converter.decode(implementationData.getValue()); + } + + return (X) decodeStructure(implementation, implementationData.toTlv()); + } + + private void decodeCollection(final Collection collection, final byte[] elementData, final CollectionTaggedField taggedField) { + try { + final var stream = new ByteArrayInputStream(elementData); + while (stream.available() > 0) { + final var elementBerData = tlvDataReader.readNext(stream); + final var parsedElementTag = BerUtils.parseTag(elementBerData.getTag()); + + if (taggedField.getType().isInterface()) { + final var asnClassDescription = loadAsnClassDescription(taggedField.getType()); + collection.add(decodePolymorphic(asnClassDescription, elementBerData.toTlv())); + } else if (taggedField.getElementTag().equals(parsedElementTag)) { + if (taggedField.isStructured()) { + collection.add(decodeStructure(taggedField.getType(), elementBerData.toTlv())); + } else { + //noinspection unchecked + final var asnConverter = loadAsnConverterFromCache((Class>) taggedField.getConverter()); + collection.add(asnConverter.decode(elementBerData.getValue())); + } + } + } + } catch (final Exception e) { + throw new AsnDecodeException(String.format("Cannot decode collection data '%s' into '%s' class", HexUtils.encode(elementData), taggedField.getType().getName()), e); + } + } + + private void decodePrimitiveField(final X instance, final BerData fieldTlvData, final TaggedField taggedField) { + //noinspection unchecked + final var asnConverter = loadAsnConverterFromCache((Class>) ((PrimitiveTaggedField) taggedField).getConverter()); + + try { + fieldAccessor.setFieldValue(instance, taggedField.getField(), asnConverter.decode(fieldTlvData.getValue())); + } catch (final AsnException e) { + throw new AsnDecodeException(String.format("Cannot set value '%s' into field '%s'", HexUtils.encode(fieldTlvData.getValue()), taggedField.getField().getName()), e); + } + } + + private void decodeStructureField(final X instance, final BerData fieldTlvData, final TaggedField taggedField) { + fieldAccessor.setFieldValue(instance, taggedField.getField(), decodeStructure(taggedField.getField().getType(), fieldTlvData.toTlv())); + } + + private void decodeCollectionField(final X instance, final BerData fieldTlvData, final TaggedField taggedField) { + final var fieldClass = taggedField.getField().getType(); + + final Collection collection; + + if (fieldClass.isAssignableFrom(List.class)) { + collection = new ArrayList<>(); + } else if (fieldClass.isAssignableFrom(Set.class)) { + collection = new HashSet<>(); + } else { + throw new AsnDecodeException(String.format("Unsupported collection type: '%s'. Only List and Set supported!", fieldClass)); + } + + fieldAccessor.setFieldValue(instance, taggedField.getField(), collection); + decodeCollection(collection, fieldTlvData.getValue(), (CollectionTaggedField) taggedField); + } + + private TaggedField parseTaggedField(final AsnClassDescription asnClassDescription, final Counter tagCounter, final BerData fieldTlvData) { + final var parsedFieldTag = BerUtils.parseTag(fieldTlvData.getTag()); + final var index = tagCounter.count(parsedFieldTag); + return asnClassDescription.findByTag(parsedFieldTag, index); + } + + private AsnClassDescription loadAsnClassDescription(final Class clazz) { + return classDescriptionCache.computeIfAbsent(clazz, AsnClassDescription::new); + } + + private AsnConverter loadAsnConverterFromCache(final Class> asnConverterClass) { + return converterCache.computeIfAbsent(asnConverterClass, c -> { + try { + return c.getDeclaredConstructor().newInstance(); + } catch (final InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new AsnConfigurationException(String.format("Cannot create a new instance of converter %s", c), e); + } + }); + } + + private void invokePostProcessMethod(final Class clazz, final X instance) throws NoSuchMethodException, IllegalAccessException, InvocationTargetException { + final var asnPostProcessMethod = clazz.getDeclaredAnnotation(AsnPostProcessMethod.class); + + if (asnPostProcessMethod != null) { + final var declaredMethod = clazz.getDeclaredMethod(asnPostProcessMethod.value()); + if (!declaredMethod.canAccess(instance)) { + declaredMethod.setAccessible(true); + } + declaredMethod.invoke(instance); + } + } +} diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/encoder/AsnEncoder.java b/src/main/java/com/github/alturkovic/asn/encoder/AsnEncoder.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/encoder/AsnEncoder.java rename to src/main/java/com/github/alturkovic/asn/encoder/AsnEncoder.java index 82f8880..b83a26e 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/encoder/AsnEncoder.java +++ b/src/main/java/com/github/alturkovic/asn/encoder/AsnEncoder.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.encoder; diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/encoder/BerEncoder.java b/src/main/java/com/github/alturkovic/asn/encoder/BerEncoder.java similarity index 67% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/encoder/BerEncoder.java rename to src/main/java/com/github/alturkovic/asn/encoder/BerEncoder.java index e943818..bdb6070 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/encoder/BerEncoder.java +++ b/src/main/java/com/github/alturkovic/asn/encoder/BerEncoder.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2019 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,64 +20,70 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.encoder; +package com.github.alturkovic.asn.encoder; -import com.github.alturkovic.asn.AsnAutoResolver; import com.github.alturkovic.asn.AsnClassDescription; import com.github.alturkovic.asn.annotation.AsnStructure; -import com.github.alturkovic.asn.annotation.AsnTag; -import com.github.alturkovic.asn.ber.tag.BerTag; -import com.github.alturkovic.asn.ber.util.BerUtils; -import com.github.alturkovic.asn.ber.util.HexUtils; import com.github.alturkovic.asn.converter.AsnConverter; -import com.github.alturkovic.asn.encoder.AsnEncoder; import com.github.alturkovic.asn.exception.AsnConfigurationException; import com.github.alturkovic.asn.exception.AsnEncodeException; import com.github.alturkovic.asn.field.CollectionTaggedField; import com.github.alturkovic.asn.field.PrimitiveTaggedField; -import com.github.alturkovic.asn.field.TaggedField; +import com.github.alturkovic.asn.field.accessor.DirectFieldAccessor; import com.github.alturkovic.asn.field.accessor.FieldAccessor; import com.github.alturkovic.asn.tag.Tag; -import com.github.alturkovic.asn.tag.TagFactory; +import com.github.alturkovic.asn.util.BerUtils; +import com.github.alturkovic.asn.util.HexUtils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; +import java.lang.reflect.InvocationTargetException; import java.util.Collection; +import java.util.HashMap; import java.util.Map; +import lombok.AllArgsConstructor; import lombok.Data; @Data +@AllArgsConstructor public class BerEncoder implements AsnEncoder { - private final TagFactory tagFactory; - private final AsnAutoResolver autoResolver; private final FieldAccessor fieldAccessor; private final Map, AsnClassDescription> classDescriptionCache; private final Map>, AsnConverter> converterCache; + public BerEncoder() { + this(new DirectFieldAccessor()); + } + + public BerEncoder(final FieldAccessor fieldAccessor) { + this(fieldAccessor, new HashMap<>(), new HashMap<>()); + } + @Override public byte[] encode(final Object object) { - final AsnStructure clazzDeclaredAnnotation = object.getClass().getDeclaredAnnotation(AsnStructure.class); + final var clazzDeclaredAnnotation = object.getClass().getDeclaredAnnotation(AsnStructure.class); if (clazzDeclaredAnnotation == null) { throw new AsnEncodeException("Missing class AsnStructure annotation"); } - final AsnTag tag = clazzDeclaredAnnotation.value(); - final Tag fieldStructureTag = tagFactory.get(tag, true); + final var tag = clazzDeclaredAnnotation.value(); + final var fieldStructureTag = new Tag(tag.value(), tag.type(), true); return encodeStructure(object, fieldStructureTag); } private byte[] encodeStructure(final Object object, final Tag structureTag) { final BerStructureBuilder berStructureBuilder; try { - final Class clazz = object.getClass(); - final AsnClassDescription asnClassDescription = classDescriptionCache.computeIfAbsent(clazz, (aClass) -> new AsnClassDescription(tagFactory, autoResolver, aClass)); + final var clazz = object.getClass(); + final var asnClassDescription = classDescriptionCache.computeIfAbsent(clazz, AsnClassDescription::new); - berStructureBuilder = new BerStructureBuilder((BerTag) structureTag); + berStructureBuilder = new BerStructureBuilder(structureTag); - for (final TaggedField taggedField : asnClassDescription.getClassDeclaredOrderedTaggedFields()) { + for (final var taggedField : asnClassDescription.getClassDeclaredOrderedTaggedFields()) { final byte[] encoded; if (taggedField.isPrimitive()) { encoded = encodePrimitive(object, (PrimitiveTaggedField) taggedField); @@ -86,7 +92,7 @@ private byte[] encodeStructure(final Object object, final Tag structureTag) { continue; } } else if (taggedField.isStructure()) { - final Object fieldValue = fieldAccessor.getFieldValue(object, taggedField.getField()); + final var fieldValue = fieldAccessor.getFieldValue(object, taggedField.getField()); if (fieldValue == null) { continue; @@ -94,7 +100,7 @@ private byte[] encodeStructure(final Object object, final Tag structureTag) { encoded = encodeStructure(fieldValue, taggedField.getTag()); } else if (taggedField.isCollection()) { - final CollectionTaggedField collectionTaggedField = (CollectionTaggedField) taggedField; + final var collectionTaggedField = (CollectionTaggedField) taggedField; final Collection collection = fieldAccessor.getFieldValue(object, taggedField.getField()); @@ -102,16 +108,15 @@ private byte[] encodeStructure(final Object object, final Tag structureTag) { continue; } - final BerStructureBuilder collectionBuilder = new BerStructureBuilder((BerTag) collectionTaggedField.getTag()); + final var collectionBuilder = new BerStructureBuilder(collectionTaggedField.getTag()); if (collectionTaggedField.isStructured()) { collection.forEach(e -> collectionBuilder.addValue(encodeStructure(e, collectionTaggedField.getElementTag()))); } else { collection.forEach(e -> { //noinspection unchecked - final AsnConverter asnConverter = loadAsnConverterFromCache((Class>) collectionTaggedField.getConverter()); - final byte[] encodedFieldValue = asnConverter.encode(e); - collectionBuilder.addValue(encodePrimitive(collectionTaggedField.getElementTag(), encodedFieldValue)); + final var asnConverter = loadAsnConverterFromCache((Class>) collectionTaggedField.getConverter()); + collectionBuilder.addValue(encodePrimitive(collectionTaggedField.getElementTag(), asnConverter.encode(e))); }); } @@ -132,8 +137,8 @@ private byte[] encodeStructure(final Object object, final Tag structureTag) { private byte[] encodePrimitive(final Object object, final PrimitiveTaggedField taggedField) { try { //noinspection unchecked - final AsnConverter asnConverter = loadAsnConverterFromCache((Class>) taggedField.getConverter()); - final byte[] encodedFieldValue = asnConverter.encode(fieldAccessor.getFieldValue(object, taggedField.getField())); + final var asnConverter = loadAsnConverterFromCache((Class>) taggedField.getConverter()); + final var encodedFieldValue = asnConverter.encode(fieldAccessor.getFieldValue(object, taggedField.getField())); if (encodedFieldValue == null) { return null; @@ -147,9 +152,9 @@ private byte[] encodePrimitive(final Object object, final PrimitiveTaggedField t private byte[] encodePrimitive(final Tag tag, final byte[] value) { try { - final ByteArrayOutputStream result = new ByteArrayOutputStream(); + final var result = new ByteArrayOutputStream(); - result.write(BerUtils.convert((BerTag) tag)); + result.write(BerUtils.convert(tag)); result.write(BerUtils.encodeLength(value.length)); result.write(value); @@ -160,11 +165,11 @@ private byte[] encodePrimitive(final Tag tag, final byte[] value) { } private AsnConverter loadAsnConverterFromCache(final Class> asnConverterClass) { - return converterCache.computeIfAbsent(asnConverterClass, (aClass) -> { + return converterCache.computeIfAbsent(asnConverterClass, c -> { try { - return aClass.newInstance(); - } catch (final InstantiationException | IllegalAccessException e) { - throw new AsnConfigurationException(String.format("Cannot create a new instance of converter %s", aClass), e); + return c.getDeclaredConstructor().newInstance(); + } catch (final InstantiationException | IllegalAccessException | NoSuchMethodException | InvocationTargetException e) { + throw new AsnConfigurationException(String.format("Cannot create a new instance of converter %s", c), e); } }); } diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/encoder/BerStructureBuilder.java b/src/main/java/com/github/alturkovic/asn/encoder/BerStructureBuilder.java similarity index 87% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/encoder/BerStructureBuilder.java rename to src/main/java/com/github/alturkovic/asn/encoder/BerStructureBuilder.java index 83cc0eb..e57111c 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/encoder/BerStructureBuilder.java +++ b/src/main/java/com/github/alturkovic/asn/encoder/BerStructureBuilder.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,12 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.encoder; +package com.github.alturkovic.asn.encoder; -import com.github.alturkovic.asn.ber.tag.BerTag; -import com.github.alturkovic.asn.ber.util.BerUtils; +import com.github.alturkovic.asn.tag.Tag; +import com.github.alturkovic.asn.util.BerUtils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; @@ -36,7 +37,7 @@ public class BerStructureBuilder { private final ByteArrayOutputStream result = new ByteArrayOutputStream(); private final List values = new ArrayList<>(); - public BerStructureBuilder(final BerTag tag) { + public BerStructureBuilder(final Tag tag) { try { result.write(BerUtils.convert(tag)); } catch (final IOException e) { @@ -51,7 +52,7 @@ public void addValue(final byte[] value) { public byte[] build() { try { result.write(BerUtils.encodeLength(values.stream().mapToInt(b -> b.length).sum())); - for (final byte[] value : values) { + for (final var value : values) { result.write(value); } return result.toByteArray(); diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/tag/Tag.java b/src/main/java/com/github/alturkovic/asn/exception/AsnAccessException.java similarity index 75% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/tag/Tag.java rename to src/main/java/com/github/alturkovic/asn/exception/AsnAccessException.java index e059721..44fa705 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/tag/Tag.java +++ b/src/main/java/com/github/alturkovic/asn/exception/AsnAccessException.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,12 +20,22 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.tag; +package com.github.alturkovic.asn.exception; -/** - * A marker interface for Asn tags. - */ -public interface Tag extends Comparable { +public class AsnAccessException extends AsnException { + + public AsnAccessException(final String msg) { + super(msg); + } + + public AsnAccessException(final Exception e) { + super(e); + } + + public AsnAccessException(final String msg, final Exception e) { + super(msg, e); + } } diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnConfigurationException.java b/src/main/java/com/github/alturkovic/asn/exception/AsnConfigurationException.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnConfigurationException.java rename to src/main/java/com/github/alturkovic/asn/exception/AsnConfigurationException.java index 14eaaaa..5eef3bf 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnConfigurationException.java +++ b/src/main/java/com/github/alturkovic/asn/exception/AsnConfigurationException.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.exception; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnConvertException.java b/src/main/java/com/github/alturkovic/asn/exception/AsnConvertException.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnConvertException.java rename to src/main/java/com/github/alturkovic/asn/exception/AsnConvertException.java index 2f49584..6068a81 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnConvertException.java +++ b/src/main/java/com/github/alturkovic/asn/exception/AsnConvertException.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.exception; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnDecodeException.java b/src/main/java/com/github/alturkovic/asn/exception/AsnDecodeException.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnDecodeException.java rename to src/main/java/com/github/alturkovic/asn/exception/AsnDecodeException.java index ee914f6..a6ddb28 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnDecodeException.java +++ b/src/main/java/com/github/alturkovic/asn/exception/AsnDecodeException.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.exception; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnEncodeException.java b/src/main/java/com/github/alturkovic/asn/exception/AsnEncodeException.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnEncodeException.java rename to src/main/java/com/github/alturkovic/asn/exception/AsnEncodeException.java index fddc84a..a2bc57b 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnEncodeException.java +++ b/src/main/java/com/github/alturkovic/asn/exception/AsnEncodeException.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.exception; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnException.java b/src/main/java/com/github/alturkovic/asn/exception/AsnException.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnException.java rename to src/main/java/com/github/alturkovic/asn/exception/AsnException.java index 00aa352..c1f1e9d 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnException.java +++ b/src/main/java/com/github/alturkovic/asn/exception/AsnException.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.exception; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnParseException.java b/src/main/java/com/github/alturkovic/asn/exception/AsnParseException.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnParseException.java rename to src/main/java/com/github/alturkovic/asn/exception/AsnParseException.java index 73694ad..33dfa48 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnParseException.java +++ b/src/main/java/com/github/alturkovic/asn/exception/AsnParseException.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.exception; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnReadException.java b/src/main/java/com/github/alturkovic/asn/exception/AsnReadException.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnReadException.java rename to src/main/java/com/github/alturkovic/asn/exception/AsnReadException.java index 574ffda..28afe0a 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/exception/AsnReadException.java +++ b/src/main/java/com/github/alturkovic/asn/exception/AsnReadException.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.exception; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/CollectionTaggedField.java b/src/main/java/com/github/alturkovic/asn/field/CollectionTaggedField.java similarity index 96% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/field/CollectionTaggedField.java rename to src/main/java/com/github/alturkovic/asn/field/CollectionTaggedField.java index f59b828..9eaff7d 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/CollectionTaggedField.java +++ b/src/main/java/com/github/alturkovic/asn/field/CollectionTaggedField.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.field; @@ -27,11 +28,11 @@ import com.github.alturkovic.asn.converter.AsnConverter; import com.github.alturkovic.asn.tag.Tag; import java.lang.reflect.Field; -import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.ToString; -@Data +@Getter @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) public class CollectionTaggedField extends TaggedField { diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/PrimitiveTaggedField.java b/src/main/java/com/github/alturkovic/asn/field/PrimitiveTaggedField.java similarity index 96% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/field/PrimitiveTaggedField.java rename to src/main/java/com/github/alturkovic/asn/field/PrimitiveTaggedField.java index 37b17d4..3b92156 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/PrimitiveTaggedField.java +++ b/src/main/java/com/github/alturkovic/asn/field/PrimitiveTaggedField.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.field; @@ -27,11 +28,11 @@ import com.github.alturkovic.asn.converter.AsnConverter; import com.github.alturkovic.asn.tag.Tag; import java.lang.reflect.Field; -import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.ToString; -@Data +@Getter @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) public class PrimitiveTaggedField extends TaggedField { diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/StructureTaggedField.java b/src/main/java/com/github/alturkovic/asn/field/StructureTaggedField.java similarity index 95% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/field/StructureTaggedField.java rename to src/main/java/com/github/alturkovic/asn/field/StructureTaggedField.java index a832061..dc2c7ef 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/StructureTaggedField.java +++ b/src/main/java/com/github/alturkovic/asn/field/StructureTaggedField.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,17 +20,18 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.field; import com.github.alturkovic.asn.tag.Tag; import java.lang.reflect.Field; -import lombok.Data; import lombok.EqualsAndHashCode; +import lombok.Getter; import lombok.ToString; -@Data +@Getter @EqualsAndHashCode(callSuper = true) @ToString(callSuper = true) public class StructureTaggedField extends TaggedField { diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/TaggedField.java b/src/main/java/com/github/alturkovic/asn/field/TaggedField.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/field/TaggedField.java rename to src/main/java/com/github/alturkovic/asn/field/TaggedField.java index 70e02ac..4187a17 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/TaggedField.java +++ b/src/main/java/com/github/alturkovic/asn/field/TaggedField.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.field; diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/accessor/DirectFieldAccessor.java b/src/main/java/com/github/alturkovic/asn/field/accessor/DirectFieldAccessor.java similarity index 87% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/field/accessor/DirectFieldAccessor.java rename to src/main/java/com/github/alturkovic/asn/field/accessor/DirectFieldAccessor.java index 11d606a..4ccdc26 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/accessor/DirectFieldAccessor.java +++ b/src/main/java/com/github/alturkovic/asn/field/accessor/DirectFieldAccessor.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,30 +20,32 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.field.accessor; +import com.github.alturkovic.asn.exception.AsnAccessException; import java.lang.reflect.Field; public class DirectFieldAccessor implements FieldAccessor { @Override public void setFieldValue(final Object instance, final Field field, final Object value) { - if (!field.isAccessible()) { + if (!field.canAccess(instance)) { field.setAccessible(true); } try { field.set(instance, value); } catch (final IllegalAccessException e) { - throw new RuntimeException(e); + throw new AsnAccessException(e); } } @Override public T getFieldValue(final Object instance, final Field field) { - if (!field.isAccessible()) { + if (!field.canAccess(instance)) { field.setAccessible(true); } @@ -51,7 +53,7 @@ public T getFieldValue(final Object instance, final Field field) { //noinspection unchecked return (T) field.get(instance); } catch (final IllegalAccessException e) { - throw new RuntimeException(e); + throw new AsnAccessException(e); } } } \ No newline at end of file diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/accessor/FieldAccessor.java b/src/main/java/com/github/alturkovic/asn/field/accessor/FieldAccessor.java similarity index 97% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/field/accessor/FieldAccessor.java rename to src/main/java/com/github/alturkovic/asn/field/accessor/FieldAccessor.java index 75930f9..7aa009b 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/field/accessor/FieldAccessor.java +++ b/src/main/java/com/github/alturkovic/asn/field/accessor/FieldAccessor.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,6 +20,7 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ package com.github.alturkovic.asn.field.accessor; diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/collection/MultiSet.java b/src/main/java/com/github/alturkovic/asn/support/Counter.java similarity index 76% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/collection/MultiSet.java rename to src/main/java/com/github/alturkovic/asn/support/Counter.java index 2e15769..4ed8907 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/collection/MultiSet.java +++ b/src/main/java/com/github/alturkovic/asn/support/Counter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.collection; +package com.github.alturkovic.asn.support; import java.util.HashMap; import java.util.Map; @@ -35,10 +36,10 @@ * @param element type */ @AllArgsConstructor -public class MultiSet { - private final Map counter; +public class Counter { + private final Map internalCounterMap; - public MultiSet() { + public Counter() { this(new HashMap<>()); } @@ -49,16 +50,7 @@ public MultiSet() { * @return count */ public int count(final T key) { - final AtomicInteger count = counter.get(key); - return count == null ? 0 : count.get(); - } - - /** - * Increments the counter for the given key. - * - * @param key key - */ - public void add(final T key) { - counter.computeIfAbsent(key, aKey -> new AtomicInteger()).incrementAndGet(); + return internalCounterMap.computeIfAbsent(key, k -> new AtomicInteger()) + .getAndIncrement(); } } diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tag/BerTag.java b/src/main/java/com/github/alturkovic/asn/tag/Tag.java similarity index 72% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tag/BerTag.java rename to src/main/java/com/github/alturkovic/asn/tag/Tag.java index a469bcb..1c77891 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tag/BerTag.java +++ b/src/main/java/com/github/alturkovic/asn/tag/Tag.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,12 +20,11 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.tag; +package com.github.alturkovic.asn.tag; -import com.github.alturkovic.asn.Type; -import com.github.alturkovic.asn.tag.Tag; import lombok.Data; import lombok.EqualsAndHashCode; import lombok.ToString; @@ -33,12 +32,12 @@ @Data @ToString @EqualsAndHashCode(of = {"value", "type"}) -public class BerTag implements Tag { +public class Tag implements Comparable { private final int value; private final Type type; private final boolean constructed; - public BerTag(final int value, final Type type, final boolean constructed) { + public Tag(final int value, final Type type, final boolean constructed) { this.value = value; this.type = type; this.constructed = constructed; @@ -46,18 +45,13 @@ public BerTag(final int value, final Type type, final boolean constructed) { @Override // order by type, then by value - public int compareTo(final Tag o) { - if (!(o instanceof BerTag)) { - // should not happen, if it does, just mix the two, doesn't matter much - return 0; - } - - final int typeComparison = type.compareTo(((BerTag) o).type); + public int compareTo(final Tag tag) { + final var typeComparison = type.compareTo(tag.type); if (typeComparison != 0) { return typeComparison; } - return Integer.compare(value, ((BerTag) o).value); + return Integer.compare(value, tag.value); } } \ No newline at end of file diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/Type.java b/src/main/java/com/github/alturkovic/asn/tag/Type.java similarity index 68% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/Type.java rename to src/main/java/com/github/alturkovic/asn/tag/Type.java index 590a1d2..8828adf 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/Type.java +++ b/src/main/java/com/github/alturkovic/asn/tag/Type.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,34 +20,28 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn; +package com.github.alturkovic.asn.tag; + +import lombok.AllArgsConstructor; /** * Tag type. */ +@AllArgsConstructor public enum Type { - UNIVERSAL(0, 'U'), APPLICATION(1, 'A'), CONTEXT(2, 'C'), PRIVATE(3, 'P'); + UNIVERSAL(0), APPLICATION(1), CONTEXT(2), PRIVATE(3); private final int code; - private final char character; - - Type(final int code, final char character) { - this.code = code; - this.character = character; - } public int getCode() { return code; } - public char getCharacter() { - return character; - } - public static Type fromCode(final int code) { - for (final Type type : Type.values()) { + for (final var type : Type.values()) { if (type.getCode() == code) { return type; } @@ -55,14 +49,4 @@ public static Type fromCode(final int code) { throw new IllegalArgumentException("Unknown Type code: " + code); } - - public static Type fromCharacter(final char character) { - for (final Type type : Type.values()) { - if (type.getCharacter() == character) { - return type; - } - } - - throw new IllegalArgumentException("Unknown Type character: " + character); - } } \ No newline at end of file diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/UniversalTags.java b/src/main/java/com/github/alturkovic/asn/tag/UniversalTags.java similarity index 96% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/UniversalTags.java rename to src/main/java/com/github/alturkovic/asn/tag/UniversalTags.java index 55084eb..a811e24 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/UniversalTags.java +++ b/src/main/java/com/github/alturkovic/asn/tag/UniversalTags.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn; +package com.github.alturkovic.asn.tag; import lombok.AccessLevel; import lombok.NoArgsConstructor; diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/AbstractInputStreamReader.java b/src/main/java/com/github/alturkovic/asn/tlv/AbstractInputStreamReader.java similarity index 88% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/AbstractInputStreamReader.java rename to src/main/java/com/github/alturkovic/asn/tlv/AbstractInputStreamReader.java index 7910b1c..fa815a3 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/AbstractInputStreamReader.java +++ b/src/main/java/com/github/alturkovic/asn/tlv/AbstractInputStreamReader.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.tlv; +package com.github.alturkovic.asn.tlv; import com.github.alturkovic.asn.exception.AsnReadException; import java.io.DataInputStream; @@ -33,7 +34,7 @@ public abstract class AbstractInputStreamReader { protected int readByte(final InputStream inputStream) { try { - final int bite = inputStream.read(); + final var bite = inputStream.read(); checkClosure(bite); return bite; } catch (final IOException e) { @@ -48,9 +49,8 @@ protected void checkClosure(final int bite) throws IOException { } protected byte[] readBytes(final InputStream inputStream, final int bytesToRead) { - final byte[] result = new byte[bytesToRead]; - - final DataInputStream dis = new DataInputStream(inputStream); + final var result = new byte[bytesToRead]; + final var dis = new DataInputStream(inputStream); try { dis.readFully(result); } catch (final IOException e) { diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerData.java b/src/main/java/com/github/alturkovic/asn/tlv/BerData.java similarity index 87% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerData.java rename to src/main/java/com/github/alturkovic/asn/tlv/BerData.java index 5a0f1bc..df27bed 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerData.java +++ b/src/main/java/com/github/alturkovic/asn/tlv/BerData.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,12 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.tlv; +package com.github.alturkovic.asn.tlv; -import com.github.alturkovic.asn.ber.util.HexUtils; import com.github.alturkovic.asn.exception.AsnDecodeException; +import com.github.alturkovic.asn.util.HexUtils; import java.io.ByteArrayOutputStream; import java.io.IOException; import lombok.Data; @@ -42,12 +43,8 @@ public BerData(final byte[] tag, final byte[] length, final byte[] value) { this.value = value; } - public boolean isValuePresent() { - return value != null && value.length > 0; - } - public byte[] toTlv() { - final ByteArrayOutputStream out = new ByteArrayOutputStream(); + final var out = new ByteArrayOutputStream(); try { out.write(tag); diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerDataExtractReader.java b/src/main/java/com/github/alturkovic/asn/tlv/BerDataExtractReader.java similarity index 82% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerDataExtractReader.java rename to src/main/java/com/github/alturkovic/asn/tlv/BerDataExtractReader.java index 2d445fe..cad40ed 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerDataExtractReader.java +++ b/src/main/java/com/github/alturkovic/asn/tlv/BerDataExtractReader.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,13 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.tlv; +package com.github.alturkovic.asn.tlv; -import com.github.alturkovic.asn.ber.tag.BerTag; -import com.github.alturkovic.asn.ber.util.BerUtils; import com.github.alturkovic.asn.tag.Tag; +import com.github.alturkovic.asn.util.BerUtils; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.List; @@ -36,12 +36,12 @@ @Data @AllArgsConstructor public class BerDataExtractReader implements TlvDataReader { - private final List tags; + private final List tags; private final BerTagReader tagReader; private final BerLengthReader lengthReader; private final BerValueReader valueReader; - public BerDataExtractReader(final List tags) { + public BerDataExtractReader(final List tags) { this.tags = tags; this.tagReader = new BerTagReader(); this.lengthReader = new BerLengthReader(); @@ -54,21 +54,20 @@ public BerData readNext(final InputStream inputStream) { byte[] length; byte[] value; - int depth = 0; - InputStream stream = inputStream; - final int tagSize = tags.size(); + var depth = 0; + var stream = inputStream; do { tag = tagReader.read(stream); length = lengthReader.read(stream); value = valueReader.read(stream, BerUtils.parseLength(length)); - final Tag parsedTag = BerUtils.parseTag(tag); + final var parsedTag = BerUtils.parseTag(tag); if (parsedTag.equals(tags.get(depth))) { depth++; stream = new ByteArrayInputStream(value); } - } while (depth < tagSize); + } while (depth < tags.size()); return new BerData(tag, length, value); } diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerDataReader.java b/src/main/java/com/github/alturkovic/asn/tlv/BerDataReader.java similarity index 83% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerDataReader.java rename to src/main/java/com/github/alturkovic/asn/tlv/BerDataReader.java index bc4205e..c554113 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerDataReader.java +++ b/src/main/java/com/github/alturkovic/asn/tlv/BerDataReader.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,11 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.tlv; +package com.github.alturkovic.asn.tlv; -import com.github.alturkovic.asn.ber.util.BerUtils; +import com.github.alturkovic.asn.util.BerUtils; import java.io.InputStream; import lombok.AllArgsConstructor; import lombok.Data; @@ -44,9 +45,9 @@ public BerDataReader() { @Override public BerData readNext(final InputStream inputStream) { - final byte[] tag = tagReader.read(inputStream); - final byte[] length = lengthReader.read(inputStream); - final byte[] value = valueReader.read(inputStream, BerUtils.parseLength(length)); + final var tag = tagReader.read(inputStream); + final var length = lengthReader.read(inputStream); + final var value = valueReader.read(inputStream, BerUtils.parseLength(length)); return new BerData(tag, length, value); } } diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerLengthReader.java b/src/main/java/com/github/alturkovic/asn/tlv/BerLengthReader.java similarity index 81% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerLengthReader.java rename to src/main/java/com/github/alturkovic/asn/tlv/BerLengthReader.java index 26ccdb0..16ce217 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerLengthReader.java +++ b/src/main/java/com/github/alturkovic/asn/tlv/BerLengthReader.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,11 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.tlv; +package com.github.alturkovic.asn.tlv; -import com.github.alturkovic.asn.ber.util.BerBitMask; +import com.github.alturkovic.asn.util.BerBitMask; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; @@ -33,16 +34,15 @@ public class BerLengthReader extends AbstractInputStreamReader { public byte[] read(final InputStream inputStream) { - final int firstByte = readByte(inputStream); - - final ByteArrayOutputStream out = new ByteArrayOutputStream(); + final var firstByte = readByte(inputStream); + final var out = new ByteArrayOutputStream(); out.write(firstByte); // if first byte has MSB set to 1 // then bits 7-1 describe number of octets that represent length if (((byte) firstByte & BerBitMask.MOST_SIGNIFICANT_BIT) == BerBitMask.MOST_SIGNIFICANT_BIT) { - final int lengthOctetsRequired = (byte) firstByte & BerBitMask.NON_LEADING_BITS; - final byte[] lengthOctets = readBytes(inputStream, lengthOctetsRequired); + final var lengthOctetsRequired = (byte) firstByte & BerBitMask.NON_LEADING_BITS; + final var lengthOctets = readBytes(inputStream, lengthOctetsRequired); try { out.write(lengthOctets); diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerTagReader.java b/src/main/java/com/github/alturkovic/asn/tlv/BerTagReader.java similarity index 87% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerTagReader.java rename to src/main/java/com/github/alturkovic/asn/tlv/BerTagReader.java index bfacda0..83b271c 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerTagReader.java +++ b/src/main/java/com/github/alturkovic/asn/tlv/BerTagReader.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,20 +20,20 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.tlv; +package com.github.alturkovic.asn.tlv; -import com.github.alturkovic.asn.ber.util.BerBitMask; +import com.github.alturkovic.asn.util.BerBitMask; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class BerTagReader extends AbstractInputStreamReader { public byte[] read(final InputStream inputStream) { - final int firstByte = readByte(inputStream); - - final ByteArrayOutputStream out = new ByteArrayOutputStream(); + final var firstByte = readByte(inputStream); + final var out = new ByteArrayOutputStream(); out.write(firstByte); // if first byte has bits 5-1 set to 1 diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerValueReader.java b/src/main/java/com/github/alturkovic/asn/tlv/BerValueReader.java similarity index 90% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerValueReader.java rename to src/main/java/com/github/alturkovic/asn/tlv/BerValueReader.java index 9d46c24..346671f 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/BerValueReader.java +++ b/src/main/java/com/github/alturkovic/asn/tlv/BerValueReader.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,17 +20,19 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.tlv; +package com.github.alturkovic.asn.tlv; import java.io.InputStream; public class BerValueReader extends AbstractInputStreamReader { + private static final byte[] EMPTY = new byte[0]; public byte[] read(final InputStream inputStream, final int bytesToRead) { if (bytesToRead == 0) { - return new byte[0]; + return EMPTY; } return readBytes(inputStream, bytesToRead); diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/TlvDataReader.java b/src/main/java/com/github/alturkovic/asn/tlv/TlvDataReader.java similarity index 94% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/TlvDataReader.java rename to src/main/java/com/github/alturkovic/asn/tlv/TlvDataReader.java index 2303ee9..25fe87e 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tlv/TlvDataReader.java +++ b/src/main/java/com/github/alturkovic/asn/tlv/TlvDataReader.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.tlv; +package com.github.alturkovic.asn.tlv; import java.io.InputStream; diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/ArrayUtils.java b/src/main/java/com/github/alturkovic/asn/util/ArrayUtils.java similarity index 92% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/ArrayUtils.java rename to src/main/java/com/github/alturkovic/asn/util/ArrayUtils.java index 4678e20..3868136 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/ArrayUtils.java +++ b/src/main/java/com/github/alturkovic/asn/util/ArrayUtils.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.util; +package com.github.alturkovic.asn.util; import lombok.AccessLevel; import lombok.NoArgsConstructor; @@ -35,8 +36,8 @@ public static void reverse(final byte[] array) { return; } - int i = 0; - int j = array.length - 1; + var i = 0; + var j = array.length - 1; byte tmp; while (j > i) { tmp = array[j]; diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/BerBitMask.java b/src/main/java/com/github/alturkovic/asn/util/BerBitMask.java similarity index 94% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/BerBitMask.java rename to src/main/java/com/github/alturkovic/asn/util/BerBitMask.java index adea6c4..ff7a9fa 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/BerBitMask.java +++ b/src/main/java/com/github/alturkovic/asn/util/BerBitMask.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.util; +package com.github.alturkovic.asn.util; import lombok.AccessLevel; import lombok.NoArgsConstructor; diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/BerUtils.java b/src/main/java/com/github/alturkovic/asn/util/BerUtils.java similarity index 81% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/BerUtils.java rename to src/main/java/com/github/alturkovic/asn/util/BerUtils.java index 1844bce..9bbc109 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/BerUtils.java +++ b/src/main/java/com/github/alturkovic/asn/util/BerUtils.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,14 +20,15 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.util; +package com.github.alturkovic.asn.util; -import com.github.alturkovic.asn.Type; -import com.github.alturkovic.asn.UniversalTags; -import com.github.alturkovic.asn.ber.tag.BerTag; import com.github.alturkovic.asn.exception.AsnParseException; +import com.github.alturkovic.asn.tag.Tag; +import com.github.alturkovic.asn.tag.Type; +import com.github.alturkovic.asn.tag.UniversalTags; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.UncheckedIOException; @@ -37,17 +38,16 @@ @NoArgsConstructor(access = AccessLevel.PRIVATE) public final class BerUtils { - public static final BerTag UNIVERSAL_TAG = new BerTag(UniversalTags.SEQUENCE, Type.UNIVERSAL, true); + public static final Tag UNIVERSAL_TAG = new Tag(UniversalTags.SEQUENCE, Type.UNIVERSAL, true); - public static BerTag parseTag(final byte[] b) { + public static Tag parseTag(final byte[] b) { if (b == null || b.length == 0) { throw new AsnParseException("Null"); } - final int type = (b[0] & BerBitMask.CLASS_BITS) >> 6; - - final boolean isConstructed = (b[0] & BerBitMask.CONSTRUCTED_BIT) == BerBitMask.CONSTRUCTED_BIT; - int value = 0; + final var type = (b[0] & BerBitMask.CLASS_BITS) >> 6; + final var isConstructed = (b[0] & BerBitMask.CONSTRUCTED_BIT) == BerBitMask.CONSTRUCTED_BIT; + var value = 0; if (b.length == 1) { if ((b[0] & BerBitMask.TAG_VALUE_BITS) == BerBitMask.TAG_VALUE_BITS) { @@ -74,11 +74,11 @@ public static BerTag parseTag(final byte[] b) { } } - return new BerTag(value, Type.fromCode(type), isConstructed); + return new Tag(value, Type.fromCode(type), isConstructed); } public static int parseLength(final byte[] b) { - final int length = b.length; + final var length = b.length; if (length == 1) { if ((b[0] & BerBitMask.MOST_SIGNIFICANT_BIT) == BerBitMask.MOST_SIGNIFICANT_BIT) { @@ -106,7 +106,7 @@ public static byte[] encodeLength(final int length) { return new byte[]{(byte) length}; } - String hex = Integer.toHexString(length); + var hex = Integer.toHexString(length); // good enough... if (hex.length() % 2 != 0) { @@ -122,30 +122,30 @@ public static byte[] encodeLength(final int length) { // result should be the actual HEX representation of the length with the first byte // being the length of the HEX, first byte is used to tell how many bytes to read - final byte[] result = new byte[raw.length + 1]; + final var result = new byte[raw.length + 1]; result[0] = (byte) (raw.length | BerBitMask.MOST_SIGNIFICANT_BIT); System.arraycopy(raw, 0, result, 1, raw.length); return result; } - public static byte[] convert(final BerTag tag) { - final ByteArrayOutputStream result = new ByteArrayOutputStream(); - final byte descriptionByte = (byte) (tag.getType().getCode() << 6 | ((tag.isConstructed() ? 1 : 0) << 5)); + public static byte[] convert(final Tag tag) { + final var result = new ByteArrayOutputStream(); + final var descriptionByte = (byte) (tag.getType().getCode() << 6 | ((tag.isConstructed() ? 1 : 0) << 5)); if (tag.getValue() < 31) { result.write(descriptionByte | tag.getValue()); } else { result.write(descriptionByte | BerBitMask.TAG_VALUE_BITS); - final ByteArrayOutputStream valueByteStream = new ByteArrayOutputStream(); - int tempValue = tag.getValue(); + final var valueByteStream = new ByteArrayOutputStream(); + var tempValue = tag.getValue(); valueByteStream.write(tempValue & BerBitMask.NON_LEADING_BITS); while ((tempValue >>= 7) > 0) { valueByteStream.write((tempValue & BerBitMask.NON_LEADING_BITS) | BerBitMask.MOST_SIGNIFICANT_BIT); } - final byte[] valueBytes = valueByteStream.toByteArray(); + final var valueBytes = valueByteStream.toByteArray(); ArrayUtils.reverse(valueBytes); try { diff --git a/src/main/java/com/github/alturkovic/asn/util/ClassUtils.java b/src/main/java/com/github/alturkovic/asn/util/ClassUtils.java new file mode 100644 index 0000000..5d6d685 --- /dev/null +++ b/src/main/java/com/github/alturkovic/asn/util/ClassUtils.java @@ -0,0 +1,57 @@ +/* + * MIT License + * + * Copyright (c) 2020 Alen Turkovic + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package com.github.alturkovic.asn.util; + +import lombok.AccessLevel; +import lombok.NoArgsConstructor; + +@NoArgsConstructor(access = AccessLevel.PRIVATE) +public final class ClassUtils { + + public static boolean isPrimitiveOrWrapper(final Class clazz) { + return supportedWrapperToPrimitive(clazz).isPrimitive(); + } + + public static Class supportedWrapperToPrimitive(final Class clazz) { + if (clazz == Integer.class) { + return int.class; + } + + if (clazz == Long.class) { + return long.class; + } + + if (clazz == Boolean.class) { + return boolean.class; + } + + if (clazz == Short.class) { + return short.class; + } + + return clazz; + } +} diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/HexUtils.java b/src/main/java/com/github/alturkovic/asn/util/HexUtils.java similarity index 86% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/HexUtils.java rename to src/main/java/com/github/alturkovic/asn/util/HexUtils.java index 511a408..15f3edf 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/util/HexUtils.java +++ b/src/main/java/com/github/alturkovic/asn/util/HexUtils.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2019 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.util; +package com.github.alturkovic.asn.util; import com.github.alturkovic.asn.exception.AsnParseException; import lombok.AccessLevel; @@ -34,8 +35,8 @@ public final class HexUtils { private static final char[] DIGITS = "0123456789ABCDEF".toCharArray(); public static String encode(final byte[] data) { - final int l = data.length; - final char[] out = new char[l << 1]; + final var l = data.length; + final var out = new char[l << 1]; // two characters form the hex value. for (int i = 0, j = 0; i < l; i++) { out[j++] = DIGITS[(0xF0 & data[i]) >>> 4]; @@ -45,19 +46,18 @@ public static String encode(final byte[] data) { } public static byte[] decode(final String hex) { - final char[] data = hex.toCharArray(); - - final int len = data.length; + final var data = hex.toCharArray(); + final var len = data.length; if ((len & 0x01) != 0) { throw new AsnParseException("Odd number of characters."); } - final byte[] out = new byte[len >> 1]; + final var out = new byte[len >> 1]; // two characters form the hex value. for (int i = 0, j = 0; j < len; i++) { - int f = toDigit(data[j], j) << 4; + var f = toDigit(data[j], j) << 4; j++; f = f | toDigit(data[j], j); j++; @@ -68,7 +68,7 @@ public static byte[] decode(final String hex) { } private static int toDigit(final char ch, final int index) { - final int digit = Character.digit(ch, 16); + final var digit = Character.digit(ch, 16); if (digit == -1) { throw new AsnParseException("Illegal hexadecimal character " + ch + " at index " + index); } diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/AsciiStringConverterTest.java b/src/test/java/com/github/alturkovic/asn/converter/AsciiStringConverterTest.java similarity index 93% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/AsciiStringConverterTest.java rename to src/test/java/com/github/alturkovic/asn/converter/AsciiStringConverterTest.java index 6d64804..83eacc2 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/AsciiStringConverterTest.java +++ b/src/test/java/com/github/alturkovic/asn/converter/AsciiStringConverterTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,12 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.converter.AsnConverter; +import com.github.alturkovic.asn.params.HexParam; import junitparams.JUnitParamsRunner; import junitparams.Parameters; import junitparams.naming.TestCaseName; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/BooleanConverterTest.java b/src/test/java/com/github/alturkovic/asn/converter/BooleanConverterTest.java similarity index 92% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/BooleanConverterTest.java rename to src/test/java/com/github/alturkovic/asn/converter/BooleanConverterTest.java index 0fdde35..1a8a17a 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/BooleanConverterTest.java +++ b/src/test/java/com/github/alturkovic/asn/converter/BooleanConverterTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,14 +20,14 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.ber.util.HexUtils; -import com.github.alturkovic.asn.converter.AsnConverter; import com.github.alturkovic.asn.exception.AsnConvertException; +import com.github.alturkovic.asn.params.HexParam; +import com.github.alturkovic.asn.util.HexUtils; import junitparams.JUnitParamsRunner; import junitparams.Parameters; import org.junit.Test; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/DateConverterTest.java b/src/test/java/com/github/alturkovic/asn/converter/DateConverterTest.java similarity index 90% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/DateConverterTest.java rename to src/test/java/com/github/alturkovic/asn/converter/DateConverterTest.java index 766dfd9..6e305c0 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/DateConverterTest.java +++ b/src/test/java/com/github/alturkovic/asn/converter/DateConverterTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,13 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.params.DateParam; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.converter.AsnConverter; +import com.github.alturkovic.asn.params.DateParam; +import com.github.alturkovic.asn.params.HexParam; import java.util.Date; import junitparams.JUnitParamsRunner; import junitparams.Parameters; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/HexStringConverterTest.java b/src/test/java/com/github/alturkovic/asn/converter/HexStringConverterTest.java similarity index 92% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/HexStringConverterTest.java rename to src/test/java/com/github/alturkovic/asn/converter/HexStringConverterTest.java index a3811c4..3a57315 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/HexStringConverterTest.java +++ b/src/test/java/com/github/alturkovic/asn/converter/HexStringConverterTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,12 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.converter.AsnConverter; +import com.github.alturkovic.asn.params.HexParam; import junitparams.JUnitParamsRunner; import junitparams.Parameters; import junitparams.naming.TestCaseName; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/IntegerConverterTest.java b/src/test/java/com/github/alturkovic/asn/converter/IntegerConverterTest.java similarity index 94% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/IntegerConverterTest.java rename to src/test/java/com/github/alturkovic/asn/converter/IntegerConverterTest.java index e97b7ef..fd4b8a7 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/IntegerConverterTest.java +++ b/src/test/java/com/github/alturkovic/asn/converter/IntegerConverterTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,13 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.converter.AsnConverter; import com.github.alturkovic.asn.exception.AsnConvertException; +import com.github.alturkovic.asn.params.HexParam; import junitparams.JUnitParamsRunner; import junitparams.Parameters; import junitparams.naming.TestCaseName; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/LongConverterTest.java b/src/test/java/com/github/alturkovic/asn/converter/LongConverterTest.java similarity index 94% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/LongConverterTest.java rename to src/test/java/com/github/alturkovic/asn/converter/LongConverterTest.java index e16f51a..1398950 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/LongConverterTest.java +++ b/src/test/java/com/github/alturkovic/asn/converter/LongConverterTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,12 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.converter.AsnConverter; +import com.github.alturkovic.asn.params.HexParam; import junitparams.JUnitParamsRunner; import junitparams.Parameters; import junitparams.naming.TestCaseName; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/Utf8StringConverterTest.java b/src/test/java/com/github/alturkovic/asn/converter/Utf8StringConverterTest.java similarity index 93% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/Utf8StringConverterTest.java rename to src/test/java/com/github/alturkovic/asn/converter/Utf8StringConverterTest.java index 14c5e93..7368ca5 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/converter/Utf8StringConverterTest.java +++ b/src/test/java/com/github/alturkovic/asn/converter/Utf8StringConverterTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,12 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.converter; +package com.github.alturkovic.asn.converter; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.converter.AsnConverter; +import com.github.alturkovic.asn.params.HexParam; import junitparams.JUnitParamsRunner; import junitparams.Parameters; import junitparams.naming.TestCaseName; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/decoder/BerDecoderTest.java b/src/test/java/com/github/alturkovic/asn/decoder/BerDecoderTest.java similarity index 53% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/decoder/BerDecoderTest.java rename to src/test/java/com/github/alturkovic/asn/decoder/BerDecoderTest.java index f616213..af70918 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/decoder/BerDecoderTest.java +++ b/src/test/java/com/github/alturkovic/asn/decoder/BerDecoderTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,15 +20,19 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.decoder; +package com.github.alturkovic.asn.decoder; -import com.github.alturkovic.asn.ber.model.Address; -import com.github.alturkovic.asn.ber.model.MultipleAddressWrapper; -import com.github.alturkovic.asn.ber.model.Person; -import com.github.alturkovic.asn.ber.util.HexUtils; -import com.github.alturkovic.asn.decoder.AsnDecoder; +import com.github.alturkovic.asn.model.Address; +import com.github.alturkovic.asn.model.EventA; +import com.github.alturkovic.asn.model.EventB; +import com.github.alturkovic.asn.model.EventListWrapper; +import com.github.alturkovic.asn.model.EventWrapper; +import com.github.alturkovic.asn.model.MultipleAddressWrapper; +import com.github.alturkovic.asn.model.Person; +import com.github.alturkovic.asn.util.HexUtils; import java.util.HashSet; import org.junit.Test; @@ -37,11 +41,11 @@ public class BerDecoderTest { - private final AsnDecoder decoder = new BerDecoderBuilder().build(); + private final AsnDecoder decoder = new BerDecoder(); @Test public void shouldDecodePersonExample() { - final byte[] ber = HexUtils.decode("F03C0101FF020118311085063859980690038506385998069002A11F300D040546697273740201018201FF300E04065365636F6E64020102820100830128"); + final byte[] ber = HexUtils.decode("F03C0101FF020118311004063859980690030406385998069002A11FA20D040546697273740201018201FFA20E04065365636F6E64020102820100830128"); final Person decoded = decoder.decode(Person.class, ber); assertThat(decoded).isEqualTo(Person.builder() @@ -61,4 +65,33 @@ public void shouldDecodeMultipleAddressStingsAndDiscardTheExtraOne() { assertThat(decoded.getAddressOne()).isEqualTo(new Address("adr1", 1, true)); assertThat(decoded.getAddressTwo()).isEqualTo(new Address("adr2", 2, false)); } + + @Test + public void shouldDecodePolymorphicType() { + final byte[] encodedA = HexUtils.decode("300D810161A208A106810101820102"); + final var wrapperA = decoder.decode(EventWrapper.class, encodedA); + assertThat(wrapperA.getId()).isEqualTo("a"); + assertThat(wrapperA.getEvent()).isInstanceOf(EventA.class); + assertThat(wrapperA.getEvent().getValue()).isEqualTo(1); + assertThat(((EventA) wrapperA.getEvent()).getNumber()).isEqualTo(2); + + final byte[] encodedB = HexUtils.decode("300D810162A208A2068101FF820102"); + final var wrapperB = decoder.decode(EventWrapper.class, encodedB); + assertThat(wrapperB.getId()).isEqualTo("b"); + assertThat(wrapperB.getEvent()).isInstanceOf(EventB.class); + assertThat(wrapperB.getEvent().getValue()).isEqualTo(2); + assertThat(((EventB) wrapperB.getEvent()).isEnabled()).isTrue(); + } + + @Test + public void shouldDecodePolymorphicCollection() { + final byte[] encoded = HexUtils.decode("301D810101A218A106810101820102A106810103820104A2068101FF820102"); + final var wrapper = decoder.decode(EventListWrapper.class, encoded); + assertThat(wrapper.getId()).isEqualTo(1); + assertThat(wrapper.getEvents()).containsExactlyInAnyOrder( + new EventA(1, 2), + new EventA(3, 4), + new EventB(true, 2) + ); + } } \ No newline at end of file diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/encoder/BerEncoderTest.java b/src/test/java/com/github/alturkovic/asn/encoder/BerEncoderTest.java similarity index 78% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/encoder/BerEncoderTest.java rename to src/test/java/com/github/alturkovic/asn/encoder/BerEncoderTest.java index 7634b3f..5cf9e5b 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/encoder/BerEncoderTest.java +++ b/src/test/java/com/github/alturkovic/asn/encoder/BerEncoderTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,14 +20,14 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.encoder; +package com.github.alturkovic.asn.encoder; -import com.github.alturkovic.asn.ber.model.Address; -import com.github.alturkovic.asn.ber.model.Person; -import com.github.alturkovic.asn.ber.util.HexUtils; -import com.github.alturkovic.asn.encoder.AsnEncoder; +import com.github.alturkovic.asn.model.Address; +import com.github.alturkovic.asn.model.Person; +import com.github.alturkovic.asn.util.HexUtils; import java.util.HashSet; import org.junit.Test; @@ -36,7 +36,7 @@ public class BerEncoderTest { - private final AsnEncoder encoder = new BerEncoderBuilder().build(); + private final AsnEncoder encoder = new BerEncoder(); @Test public void shouldEncodePersonExample() { @@ -49,7 +49,7 @@ public void shouldEncodePersonExample() { .addresses(asList(new Address("First", 1, true), new Address("Second", 2, false))) .build()); - final String expected = "F03C0101FF020118311085063859980690038506385998069002A11F300D040546697273740201018201FF300E04065365636F6E64020102820100830128"; + final String expected = "F03C0101FF020118311004063859980690030406385998069002A11FA20D040546697273740201018201FFA20E04065365636F6E64020102820100830128"; assertThat(HexUtils.decode(expected)).isEqualTo(encodedHex); } } \ No newline at end of file diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/model/Address.java b/src/test/java/com/github/alturkovic/asn/model/Address.java similarity index 94% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/model/Address.java rename to src/test/java/com/github/alturkovic/asn/model/Address.java index 0f542d8..27d51ee 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/model/Address.java +++ b/src/test/java/com/github/alturkovic/asn/model/Address.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.model; +package com.github.alturkovic.asn.model; import com.github.alturkovic.asn.annotation.AsnPrimitive; import com.github.alturkovic.asn.annotation.AsnTag; diff --git a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tag/BerTagFactory.java b/src/test/java/com/github/alturkovic/asn/model/Event.java similarity index 77% rename from asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tag/BerTagFactory.java rename to src/test/java/com/github/alturkovic/asn/model/Event.java index 9be5da4..8aea4db 100644 --- a/asn-ber-parser/src/main/java/com/github/alturkovic/asn/ber/tag/BerTagFactory.java +++ b/src/test/java/com/github/alturkovic/asn/model/Event.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,16 +20,16 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.tag; +package com.github.alturkovic.asn.model; +import com.github.alturkovic.asn.annotation.AsnPolymorphic; import com.github.alturkovic.asn.annotation.AsnTag; -import com.github.alturkovic.asn.tag.TagFactory; -public class BerTagFactory implements TagFactory { - @Override - public BerTag get(final AsnTag asnTag, final boolean structured) { - return new BerTag(asnTag.value(), asnTag.type(), structured); - } -} +@AsnPolymorphic(value = @AsnTag(1), type = EventA.class) +@AsnPolymorphic(value = @AsnTag(2), type = EventB.class) +public interface Event { + int getValue(); +} \ No newline at end of file diff --git a/asn-parser-core/src/main/java/com/github/alturkovic/asn/tag/TagFactory.java b/src/test/java/com/github/alturkovic/asn/model/EventA.java similarity index 73% rename from asn-parser-core/src/main/java/com/github/alturkovic/asn/tag/TagFactory.java rename to src/test/java/com/github/alturkovic/asn/model/EventA.java index 5273604..4993a59 100644 --- a/asn-parser-core/src/main/java/com/github/alturkovic/asn/tag/TagFactory.java +++ b/src/test/java/com/github/alturkovic/asn/model/EventA.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,22 +20,25 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.tag; +package com.github.alturkovic.asn.model; +import com.github.alturkovic.asn.annotation.AsnPrimitive; import com.github.alturkovic.asn.annotation.AsnTag; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; -/** - * A factory which produces Tags based on AsnTag annotations. - */ -public interface TagFactory { - /** - * Tag represented by asnTag annotation. - * - * @param asnTag annotation describing the tag - * @param structured indicates if the tag is structured or primitive - * @return tag - */ - Tag get(AsnTag asnTag, boolean structured); -} +@Data +@NoArgsConstructor +@AllArgsConstructor +public class EventA implements Event { + + @AsnPrimitive(@AsnTag(1)) + private int value; + + @AsnPrimitive(@AsnTag(2)) + private int number; +} \ No newline at end of file diff --git a/src/test/java/com/github/alturkovic/asn/model/EventB.java b/src/test/java/com/github/alturkovic/asn/model/EventB.java new file mode 100644 index 0000000..ed7a532 --- /dev/null +++ b/src/test/java/com/github/alturkovic/asn/model/EventB.java @@ -0,0 +1,44 @@ +/* + * MIT License + * + * Copyright (c) 2020 Alen Turkovic + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package com.github.alturkovic.asn.model; + +import com.github.alturkovic.asn.annotation.AsnPrimitive; +import com.github.alturkovic.asn.annotation.AsnTag; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +public class EventB implements Event { + + @AsnPrimitive(@AsnTag(1)) + private boolean enabled; + + @AsnPrimitive(@AsnTag(2)) + private int value; +} \ No newline at end of file diff --git a/src/test/java/com/github/alturkovic/asn/model/EventListWrapper.java b/src/test/java/com/github/alturkovic/asn/model/EventListWrapper.java new file mode 100644 index 0000000..d8c853e --- /dev/null +++ b/src/test/java/com/github/alturkovic/asn/model/EventListWrapper.java @@ -0,0 +1,49 @@ +/* + * MIT License + * + * Copyright (c) 2020 Alen Turkovic + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package com.github.alturkovic.asn.model; + +import com.github.alturkovic.asn.annotation.AsnCollection; +import com.github.alturkovic.asn.annotation.AsnPrimitive; +import com.github.alturkovic.asn.annotation.AsnStructure; +import com.github.alturkovic.asn.annotation.AsnTag; +import com.github.alturkovic.asn.tag.Type; +import java.util.List; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@AsnStructure(@AsnTag(value = 16, type = Type.UNIVERSAL)) +public class EventListWrapper { + + @AsnPrimitive(@AsnTag(1)) + private int id; + + @AsnCollection(value = @AsnTag(2), type = Event.class) + private List events; +} diff --git a/src/test/java/com/github/alturkovic/asn/model/EventWrapper.java b/src/test/java/com/github/alturkovic/asn/model/EventWrapper.java new file mode 100644 index 0000000..b3d0830 --- /dev/null +++ b/src/test/java/com/github/alturkovic/asn/model/EventWrapper.java @@ -0,0 +1,47 @@ +/* + * MIT License + * + * Copyright (c) 2020 Alen Turkovic + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + * + */ + +package com.github.alturkovic.asn.model; + +import com.github.alturkovic.asn.annotation.AsnPrimitive; +import com.github.alturkovic.asn.annotation.AsnStructure; +import com.github.alturkovic.asn.annotation.AsnTag; +import com.github.alturkovic.asn.tag.Type; +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.NoArgsConstructor; + +@Data +@NoArgsConstructor +@AllArgsConstructor +@AsnStructure(@AsnTag(value = 16, type = Type.UNIVERSAL)) +public class EventWrapper { + + @AsnPrimitive(@AsnTag(1)) + private String id; + + @AsnStructure(@AsnTag(2)) + private Event event; +} diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/model/MultipleAddressWrapper.java b/src/test/java/com/github/alturkovic/asn/model/MultipleAddressWrapper.java similarity index 92% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/model/MultipleAddressWrapper.java rename to src/test/java/com/github/alturkovic/asn/model/MultipleAddressWrapper.java index 5e78d38..7153b9c 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/model/MultipleAddressWrapper.java +++ b/src/test/java/com/github/alturkovic/asn/model/MultipleAddressWrapper.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,13 +20,14 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.model; +package com.github.alturkovic.asn.model; -import com.github.alturkovic.asn.Type; import com.github.alturkovic.asn.annotation.AsnStructure; import com.github.alturkovic.asn.annotation.AsnTag; +import com.github.alturkovic.asn.tag.Type; import lombok.Data; @Data diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/model/Person.java b/src/test/java/com/github/alturkovic/asn/model/Person.java similarity index 85% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/model/Person.java rename to src/test/java/com/github/alturkovic/asn/model/Person.java index 6a1e2bb..4a4724c 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/model/Person.java +++ b/src/test/java/com/github/alturkovic/asn/model/Person.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,17 +20,18 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.model; +package com.github.alturkovic.asn.model; -import com.github.alturkovic.asn.Type; import com.github.alturkovic.asn.annotation.AsnCollection; import com.github.alturkovic.asn.annotation.AsnPostProcessMethod; import com.github.alturkovic.asn.annotation.AsnPrimitive; import com.github.alturkovic.asn.annotation.AsnStructure; import com.github.alturkovic.asn.annotation.AsnTag; -import com.github.alturkovic.asn.ber.converter.HexStringConverter; +import com.github.alturkovic.asn.converter.HexStringConverter; +import com.github.alturkovic.asn.tag.Type; import java.util.List; import java.util.Set; import lombok.AllArgsConstructor; @@ -52,10 +53,10 @@ public class Person { @AsnPrimitive private Integer age; - @AsnCollection(elementTag = @AsnTag(5), structured = false, asnConverter = HexStringConverter.class, type = String.class) + @AsnCollection(structured = false, asnConverter = HexStringConverter.class, type = String.class) private Set phones; - @AsnCollection(value = @AsnTag(1), type = Address.class) + @AsnCollection(value = @AsnTag(1), elementTag = @AsnTag(2), type = Address.class) private List
addresses; @AsnPrimitive(@AsnTag(3)) diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/DateParam.java b/src/test/java/com/github/alturkovic/asn/params/DateParam.java similarity index 94% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/DateParam.java rename to src/test/java/com/github/alturkovic/asn/params/DateParam.java index 465977e..cb86d37 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/DateParam.java +++ b/src/test/java/com/github/alturkovic/asn/params/DateParam.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.params; +package com.github.alturkovic.asn.params; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/DateParamConverter.java b/src/test/java/com/github/alturkovic/asn/params/DateParamConverter.java similarity index 95% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/DateParamConverter.java rename to src/test/java/com/github/alturkovic/asn/params/DateParamConverter.java index 3f01f34..6ffcdca 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/DateParamConverter.java +++ b/src/test/java/com/github/alturkovic/asn/params/DateParamConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.params; +package com.github.alturkovic.asn.params; import java.text.DateFormat; import java.text.ParseException; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/HexParam.java b/src/test/java/com/github/alturkovic/asn/params/HexParam.java similarity index 94% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/HexParam.java rename to src/test/java/com/github/alturkovic/asn/params/HexParam.java index 6726ec6..bfe56b7 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/HexParam.java +++ b/src/test/java/com/github/alturkovic/asn/params/HexParam.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,9 +20,10 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.params; +package com.github.alturkovic.asn.params; import java.lang.annotation.ElementType; import java.lang.annotation.Retention; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/HexParamConverter.java b/src/test/java/com/github/alturkovic/asn/params/HexParamConverter.java similarity index 92% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/HexParamConverter.java rename to src/test/java/com/github/alturkovic/asn/params/HexParamConverter.java index 9459670..05f1c04 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/params/HexParamConverter.java +++ b/src/test/java/com/github/alturkovic/asn/params/HexParamConverter.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,11 +20,12 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.params; +package com.github.alturkovic.asn.params; -import com.github.alturkovic.asn.ber.util.HexUtils; +import com.github.alturkovic.asn.util.HexUtils; import junitparams.converters.ConversionFailedException; import junitparams.converters.Converter; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/reader/BerDataExtractReaderTest.java b/src/test/java/com/github/alturkovic/asn/reader/BerDataExtractReaderTest.java similarity index 76% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/reader/BerDataExtractReaderTest.java rename to src/test/java/com/github/alturkovic/asn/reader/BerDataExtractReaderTest.java index 975e33d..3c201ad 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/reader/BerDataExtractReaderTest.java +++ b/src/test/java/com/github/alturkovic/asn/reader/BerDataExtractReaderTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,16 +20,17 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.reader; +package com.github.alturkovic.asn.reader; -import com.github.alturkovic.asn.Type; -import com.github.alturkovic.asn.UniversalTags; -import com.github.alturkovic.asn.ber.tag.BerTag; -import com.github.alturkovic.asn.ber.tlv.BerData; -import com.github.alturkovic.asn.ber.tlv.BerDataExtractReader; -import com.github.alturkovic.asn.ber.util.HexUtils; +import com.github.alturkovic.asn.tag.Tag; +import com.github.alturkovic.asn.tag.Type; +import com.github.alturkovic.asn.tag.UniversalTags; +import com.github.alturkovic.asn.tlv.BerData; +import com.github.alturkovic.asn.tlv.BerDataExtractReader; +import com.github.alturkovic.asn.util.HexUtils; import java.io.ByteArrayInputStream; import java.util.Arrays; import org.junit.Test; @@ -41,8 +42,8 @@ public class BerDataExtractReaderTest { @Test public void shouldExtract() { final BerDataExtractReader berDataExtractReader = new BerDataExtractReader(Arrays.asList( - new BerTag(1, Type.CONTEXT, true), - new BerTag(UniversalTags.INTEGER, Type.UNIVERSAL, false) + new Tag(1, Type.CONTEXT, true), + new Tag(UniversalTags.INTEGER, Type.UNIVERSAL, false) )); final BerData rawData = berDataExtractReader.readNext(new ByteArrayInputStream(HexUtils.decode("A103020103"))); diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/reader/BerDataReaderTest.java b/src/test/java/com/github/alturkovic/asn/reader/BerDataReaderTest.java similarity index 90% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/reader/BerDataReaderTest.java rename to src/test/java/com/github/alturkovic/asn/reader/BerDataReaderTest.java index 31f8edd..cb96566 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/reader/BerDataReaderTest.java +++ b/src/test/java/com/github/alturkovic/asn/reader/BerDataReaderTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,13 +20,14 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.reader; +package com.github.alturkovic.asn.reader; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.ber.tlv.BerData; -import com.github.alturkovic.asn.ber.tlv.BerDataReader; +import com.github.alturkovic.asn.params.HexParam; +import com.github.alturkovic.asn.tlv.BerData; +import com.github.alturkovic.asn.tlv.BerDataReader; import java.io.ByteArrayInputStream; import junitparams.JUnitParamsRunner; import junitparams.Parameters; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/utils/BerUtilsLengthEncodeTest.java b/src/test/java/com/github/alturkovic/asn/utils/BerUtilsLengthEncodeTest.java similarity index 89% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/utils/BerUtilsLengthEncodeTest.java rename to src/test/java/com/github/alturkovic/asn/utils/BerUtilsLengthEncodeTest.java index f5fde03..471d90c 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/utils/BerUtilsLengthEncodeTest.java +++ b/src/test/java/com/github/alturkovic/asn/utils/BerUtilsLengthEncodeTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,12 +20,13 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.utils; +package com.github.alturkovic.asn.utils; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.ber.util.BerUtils; +import com.github.alturkovic.asn.params.HexParam; +import com.github.alturkovic.asn.util.BerUtils; import junitparams.JUnitParamsRunner; import junitparams.Parameters; import org.junit.Test; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/utils/BerUtilsLengthParseTest.java b/src/test/java/com/github/alturkovic/asn/utils/BerUtilsLengthParseTest.java similarity index 93% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/utils/BerUtilsLengthParseTest.java rename to src/test/java/com/github/alturkovic/asn/utils/BerUtilsLengthParseTest.java index 4fe5860..e195c97 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/utils/BerUtilsLengthParseTest.java +++ b/src/test/java/com/github/alturkovic/asn/utils/BerUtilsLengthParseTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,13 +20,14 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.utils; +package com.github.alturkovic.asn.utils; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.ber.util.BerUtils; import com.github.alturkovic.asn.exception.AsnParseException; +import com.github.alturkovic.asn.params.HexParam; +import com.github.alturkovic.asn.util.BerUtils; import junitparams.JUnitParamsRunner; import junitparams.Parameters; import junitparams.naming.TestCaseName; diff --git a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/utils/BerUtilsTagParseTest.java b/src/test/java/com/github/alturkovic/asn/utils/BerUtilsTagParseTest.java similarity index 78% rename from asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/utils/BerUtilsTagParseTest.java rename to src/test/java/com/github/alturkovic/asn/utils/BerUtilsTagParseTest.java index 095696e..f25a925 100644 --- a/asn-ber-parser/src/test/java/com/github/alturkovic/asn/ber/utils/BerUtilsTagParseTest.java +++ b/src/test/java/com/github/alturkovic/asn/utils/BerUtilsTagParseTest.java @@ -1,7 +1,7 @@ /* * MIT License * - * Copyright (c) 2018 Alen Turkovic + * Copyright (c) 2020 Alen Turkovic * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -20,15 +20,16 @@ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. + * */ -package com.github.alturkovic.asn.ber.utils; +package com.github.alturkovic.asn.utils; -import com.github.alturkovic.asn.Type; -import com.github.alturkovic.asn.ber.params.HexParam; -import com.github.alturkovic.asn.ber.tag.BerTag; -import com.github.alturkovic.asn.ber.util.BerUtils; import com.github.alturkovic.asn.exception.AsnParseException; +import com.github.alturkovic.asn.params.HexParam; +import com.github.alturkovic.asn.tag.Tag; +import com.github.alturkovic.asn.tag.Type; +import com.github.alturkovic.asn.util.BerUtils; import junitparams.JUnitParamsRunner; import junitparams.Parameters; import junitparams.naming.TestCaseName; @@ -56,15 +57,15 @@ public class BerUtilsTagParseTest { }) @TestCaseName("[{index}] encode: ({0}, {1}, {2})") public void shouldEncode(final int value, final Type type, final boolean constructed, @HexParam final byte[] expected) throws Exception { - final BerTag tag = new BerTag(value, type, constructed); + final Tag tag = new Tag(value, type, constructed); assertThat(BerUtils.convert(tag)).isEqualTo(expected); } @Test @Parameters @TestCaseName("[{index}] parse: ({0})") - public void shouldParse(@HexParam final byte[] given, final BerTag expected) throws Exception { - final BerTag parsed = BerUtils.parseTag(given); + public void shouldParse(@HexParam final byte[] given, final Tag expected) throws Exception { + final Tag parsed = BerUtils.parseTag(given); assertThat(parsed.getType()).isEqualTo(expected.getType()); assertThat(parsed.getValue()).isEqualTo(expected.getValue()); assertThat(parsed.isConstructed()).isEqualTo(expected.isConstructed()); @@ -102,15 +103,15 @@ public void shouldFailBecauseOneOfValueBytesMSBIsNotOne(@HexParam final byte[] d @SuppressWarnings("unused") // used by shouldParse method @Parameters private Object parametersForShouldParse() { return new Object[][]{ - {"80", new BerTag(0, Type.CONTEXT, false)}, - {"7F25", new BerTag(37, Type.APPLICATION, true)}, - {"BF25", new BerTag(37, Type.CONTEXT, true)}, - {"9F25", new BerTag(37, Type.CONTEXT, false)}, - {"BF8104", new BerTag(132, Type.CONTEXT, true)}, - {"7f8163", new BerTag(227, Type.APPLICATION, true)}, - {"9f8333", new BerTag(435, Type.CONTEXT, false)}, - {"9f808003", new BerTag(3, Type.CONTEXT, false)}, - {"9f818333", new BerTag(16819, Type.CONTEXT, false)} + {"80", new Tag(0, Type.CONTEXT, false)}, + {"7F25", new Tag(37, Type.APPLICATION, true)}, + {"BF25", new Tag(37, Type.CONTEXT, true)}, + {"9F25", new Tag(37, Type.CONTEXT, false)}, + {"BF8104", new Tag(132, Type.CONTEXT, true)}, + {"7f8163", new Tag(227, Type.APPLICATION, true)}, + {"9f8333", new Tag(435, Type.CONTEXT, false)}, + {"9f808003", new Tag(3, Type.CONTEXT, false)}, + {"9f818333", new Tag(16819, Type.CONTEXT, false)} }; } } \ No newline at end of file