diff options
author | CGantert345 <57003061+CGantert345@users.noreply.github.com> | 2021-11-25 16:39:28 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-25 16:39:28 +0100 |
commit | c9f27f2de9b3fa713d10cb2f8014aad4e8af3070 (patch) | |
tree | ca00d8bae19bfcb5d7d55b388b568136ea62b57d /src/main/java/org/uic/barcode/asn1/uper | |
parent | Merge pull request #34 from UnionInternationalCheminsdeFer/1.2.7 (diff) | |
parent | test and bug fix on countermark and viaStation (diff) | |
download | UIC-barcode-c9f27f2de9b3fa713d10cb2f8014aad4e8af3070.tar UIC-barcode-c9f27f2de9b3fa713d10cb2f8014aad4e8af3070.tar.gz UIC-barcode-c9f27f2de9b3fa713d10cb2f8014aad4e8af3070.tar.bz2 UIC-barcode-c9f27f2de9b3fa713d10cb2f8014aad4e8af3070.tar.lz UIC-barcode-c9f27f2de9b3fa713d10cb2f8014aad4e8af3070.tar.xz UIC-barcode-c9f27f2de9b3fa713d10cb2f8014aad4e8af3070.tar.zst UIC-barcode-c9f27f2de9b3fa713d10cb2f8014aad4e8af3070.zip |
Diffstat (limited to '')
3 files changed, 29 insertions, 131 deletions
diff --git a/src/main/java/org/uic/barcode/asn1/uper/BigIntCoder.java b/src/main/java/org/uic/barcode/asn1/uper/BigIntCoder.java deleted file mode 100644 index 94e4b05..0000000 --- a/src/main/java/org/uic/barcode/asn1/uper/BigIntCoder.java +++ /dev/null @@ -1,97 +0,0 @@ -package org.uic.barcode.asn1.uper; - -import java.lang.annotation.Annotation; -import java.lang.reflect.Field; -import java.math.BigInteger; - -import org.uic.barcode.asn1.datatypes.Asn1BigInteger; -import org.uic.barcode.asn1.datatypes.Asn1Default; -import org.uic.barcode.asn1.datatypes.IntRange; - -class BigIntCoder implements Encoder, Decoder { - - @Override public <T> boolean canDecode(Class<T> classOfT, Annotation[] extraAnnotations) { - return Asn1BigInteger.class.isAssignableFrom(classOfT); - } - - @Override public <T> T decode(BitBuffer bitbuffer, - Class<T> classOfT, Field f, - Annotation[] extraAnnotations) { - AnnotationStore annotations = new AnnotationStore(classOfT.getAnnotations(), - extraAnnotations); - - String pos = String.format("%d.%d", bitbuffer.position()/8 , bitbuffer.position() % 8); - UperEncoder.logger.debug(String.format("Position %s BIG INT",pos)); - IntRange intRange = annotations.getAnnotation(IntRange.class); - if (intRange != null && intRange.maxValue() > 0) { - throw new UnsupportedOperationException("Big int with upper range is not supported yet"); - } - - int lengthInOctets = (int) UperEncoder.decodeLengthDeterminant(bitbuffer); - BitBuffer valueBits = ByteBitBuffer.allocate(lengthInOctets * 8); - for (int i = 0; i < lengthInOctets * 8; i++) { - valueBits.put(bitbuffer.get()); - } - valueBits.flip(); - BigInteger resultValue = new BigInteger(+1, valueBits.array()); - UperEncoder.logger.debug(String.format("big int Decoded as %s", resultValue)); - - - //CG support for int range - if (intRange != null){ - resultValue.add(BigInteger.valueOf(intRange.minValue())); - } - - - return UperEncoder.instantiate(classOfT, resultValue); - } - - @Override public <T> boolean canEncode(T obj, Annotation[] extraAnnotations) { - return obj instanceof Asn1BigInteger; - } - - @Override public <T> void encode(BitBuffer bitbuffer, T obj, Annotation[] extraAnnotations) throws Asn1EncodingException { - Class<?> type = obj.getClass(); - AnnotationStore annotations = new AnnotationStore(type.getAnnotations(), extraAnnotations); - IntRange range = annotations.getAnnotation(IntRange.class); - - //CG implementation with lower range limit added - BigInteger bint = ((Asn1BigInteger) obj).toBigInteger(); - if (range != null) { - throw new UnsupportedOperationException("Asn1 BigInteger with range is not supported"); - } - byte[] array = bint.toByteArray(); - int lengthInOctets = array.length; - int position1 = bitbuffer.position(); - try { - UperEncoder.encodeLengthDeterminant(bitbuffer, lengthInOctets); - } catch (Asn1EncodingException e) { - throw new Asn1EncodingException(" length determinant of " + type.getName(), e); - } - int position2 = bitbuffer.position(); - for (byte b : array) { - bitbuffer.putByte(b); - } - UperEncoder.logger.debug(String.format("Big Int(%s): len %s, val %s", obj, - bitbuffer.toBooleanString(position1, position2 - position1), - bitbuffer.toBooleanStringFromPosition(position2))); - return; - } - - @Override - public <T> T getDefault(Class<T> classOfT, Annotation[] extraAnnotations) { - AnnotationStore annotations = new AnnotationStore(classOfT.getAnnotations(), extraAnnotations); - Asn1Default defaultAnnotation = annotations.getAnnotation(Asn1Default.class); - if (defaultAnnotation == null) return null; - //check whether the class has a constructor for numeric types - String valueString = defaultAnnotation.value(); - long value = Long.parseLong(valueString); - UperEncoder.logger.debug(String.format("Default INTEGER: %d",value )); - - @SuppressWarnings("unchecked") - T t = (T) new Asn1BigInteger(value); - return t; - - } - -}
\ No newline at end of file diff --git a/src/main/java/org/uic/barcode/asn1/uper/BitStringCoder.java b/src/main/java/org/uic/barcode/asn1/uper/BitStringCoder.java index ba1692c..19aac9b 100644 --- a/src/main/java/org/uic/barcode/asn1/uper/BitStringCoder.java +++ b/src/main/java/org/uic/barcode/asn1/uper/BitStringCoder.java @@ -4,12 +4,12 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Field; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; +import java.util.List; import org.uic.barcode.asn1.datatypes.Asn1VarSizeBitstring; import org.uic.barcode.asn1.datatypes.Bitstring; import org.uic.barcode.asn1.datatypes.FixedSize; import org.uic.barcode.asn1.datatypes.SizeRange; -import org.uic.barcode.asn1.uper.UperEncoder.Asn1ContainerFieldSorter; class BitStringCoder implements Decoder, Encoder { @@ -29,18 +29,24 @@ class BitStringCoder implements Decoder, Encoder { throw new UnsupportedOperationException( "Bitstring with extensions is not implemented yet"); } - FixedSize size = type.getAnnotation(FixedSize.class); + FixedSize size = annotations.getAnnotation(FixedSize.class); int position = bitbuffer.position(); if (size != null) { - Asn1ContainerFieldSorter sorter = new Asn1ContainerFieldSorter(type); - if (sorter.ordinaryFields.size() != size.value()) { throw new AssertionError( + if (!List.class.isAssignableFrom(type)) { + throw new AssertionError("Field should be a list of booleans!"); + } + + List<Boolean> list = (List<Boolean>)obj; + if (list.size() != size.value()) { + throw new AssertionError( "Declared size (" + size.value() + - ") and number of fields (" + sorter.ordinaryFields.size() + - ") do not match!"); } - for (Field f : sorter.ordinaryFields) { + ") and number of fields (" + list.size() + + ") do not match!"); + } + for (Boolean f : list) { try { - bitbuffer.put(f.getBoolean(obj)); - } catch (IllegalArgumentException | IllegalAccessException e) { + bitbuffer.put(f); + } catch (IllegalArgumentException e) { throw new IllegalArgumentException("can't encode" + obj, e); } } @@ -100,26 +106,28 @@ class BitStringCoder implements Decoder, Encoder { if (fixedSize == null) { throw new UnsupportedOperationException( "bitstrings of non-fixed size that do not extend Asn1VarSizeBitstring are not supported yet"); } - Asn1ContainerFieldSorter sorter = new Asn1ContainerFieldSorter(classOfT); - if (fixedSize.value() != sorter.ordinaryFields.size()) { throw new IllegalArgumentException( - "Fixed size annotation " + fixedSize.value() - + " does not match the number of fields " - + sorter.ordinaryFields.size() + " in " + classOfT.getName()); } if (UperEncoder.hasExtensionMarker(annotations)) { boolean extensionPresent = bitbuffer.get(); if (extensionPresent) { throw new UnsupportedOperationException( "extensions in fixed-size bitlist are not supported yet"); } } T result = UperEncoder.instantiate(classOfT); - for (Field f : sorter.ordinaryFields) { - boolean value = bitbuffer.get(); - UperEncoder.logger.debug(String.format("Field %s set to %s", f.getName(), value)); + + Method addBooleanMethod; + try { + addBooleanMethod = classOfT.getDeclaredMethod("add", Object.class); + addBooleanMethod.setAccessible(true); + } catch (SecurityException | NoSuchMethodException e) { + throw new AssertionError("Can't find/access add " + e); + } + + for (int i = 0; i < fixedSize.value(); i++) { try { - f.set(result, value); - } catch (IllegalArgumentException | IllegalAccessException e) { - throw new IllegalArgumentException("can't decode " + classOfT, e); + addBooleanMethod.invoke(result, bitbuffer.get()); + } catch (IllegalArgumentException | InvocationTargetException | IllegalAccessException e) { + throw new IllegalArgumentException("Can't invoke add", e); } - } + } return result; } else { UperEncoder.logger.debug("Bitlist(var-size)"); diff --git a/src/main/java/org/uic/barcode/asn1/uper/UperEncoder.java b/src/main/java/org/uic/barcode/asn1/uper/UperEncoder.java index a5ef5c5..bba64e2 100644 --- a/src/main/java/org/uic/barcode/asn1/uper/UperEncoder.java +++ b/src/main/java/org/uic/barcode/asn1/uper/UperEncoder.java @@ -162,7 +162,6 @@ public final class UperEncoder { static { encoders.add(new IntCoder()); - //encoders.add(new BigIntCoder()); encoders.add(new ByteCoder()); encoders.add(new BooleanCoder()); encoders.add(new SequenceCoder()); @@ -173,7 +172,6 @@ public final class UperEncoder { encoders.add(new StringCoder()); decoders.add(new IntCoder()); - //decoders.add(new BigIntCoder()); decoders.add(new ByteCoder()); decoders.add(new BooleanCoder()); decoders.add(new SequenceCoder()); @@ -691,17 +689,6 @@ public final class UperEncoder { return sb.toString(); } - public static byte[] bytesFromBinaryString(String s) { - int len = s.length(); - byte[] result = new byte[(len + Byte.SIZE - 1) / Byte.SIZE]; - char c; - for (int i = 0; i < len; i++) - if ((c = s.charAt(i)) == '1') result[i / Byte.SIZE] = (byte) (result[i / Byte.SIZE] | (0x80 >>> (i % Byte.SIZE))); - else if (c != '0') - throw new IllegalArgumentException(); - return result; - } - private static BitBuffer bitBufferFromBinaryString(String s) { ByteBitBuffer result = ByteBitBuffer.allocate(s.length()); for (int i = 0; i < s.length(); i++) { |