summaryrefslogtreecommitdiffstats
path: root/src/net/gcdc/asn1/uper/StringCoder.java
blob: d42238b876fc501f0527079de593086dc30f0749 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package net.gcdc.asn1.uper;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.math.BigInteger;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import logger.Logger;
import logger.LoggerFactory;

import net.gcdc.asn1.datatypes.Asn1Default;
import net.gcdc.asn1.datatypes.Asn1String;
import net.gcdc.asn1.datatypes.CharacterRestriction;
import net.gcdc.asn1.datatypes.DefaultAlphabet;
import net.gcdc.asn1.datatypes.FixedSize;
import net.gcdc.asn1.datatypes.RestrictedString;
import net.gcdc.asn1.datatypes.SizeRange;


class StringCoder implements Decoder, Encoder {

    private static final Logger LOGGER = LoggerFactory.getLogger("asnLogger");

    @Override public <T> boolean canEncode(T obj, Annotation[] extraAnnotations) {
        return obj instanceof String || obj instanceof Asn1String;
    }

    @Override public <T> void encode(BitBuffer bitbuffer, T obj, Annotation[] extraAnnotations) throws Asn1EncodingException {
    	String pos = String.format("Position: %d.%d", bitbuffer.position()/8 , bitbuffer.position() % 8);
        UperEncoder.logger.debug(String.format("%s: encode STRING %s of type %s", pos, obj, obj.getClass().getName()));
    	Class<?> type = obj.getClass();
        AnnotationStore annotations = new AnnotationStore(type.getAnnotations(), extraAnnotations);
        String string = (obj instanceof String) ? ((String) obj) : ((Asn1String) obj).value();
        RestrictedString restrictionAnnotation = annotations.getAnnotation(RestrictedString.class);
        if (restrictionAnnotation == null) {
            throw new UnsupportedOperationException("Unrestricted character strings are not supported yet. All annotations: " + Arrays.asList(type.getAnnotations()));
        }
        
        FixedSize fixedSize = annotations.getAnnotation(FixedSize.class);
        SizeRange sizeRange = annotations.getAnnotation(SizeRange.class);
        if (fixedSize != null && fixedSize.value() != string.length()) {
            throw new IllegalArgumentException(
                "Bad string length, expected " + fixedSize.value() + ", got " + string.length());
        }
        if (sizeRange != null
                && !sizeRange.hasExtensionMarker()
                && (string.length() < sizeRange.minValue() || sizeRange.maxValue() < string
                        .length())) { throw new IllegalArgumentException(
                "Bad string length, expected " + sizeRange.minValue() + ".."
                        + sizeRange.maxValue() + ", got " + string.length()); }
        if (restrictionAnnotation.value() == CharacterRestriction.UTF8String) {
            // UTF8 length
            BitBuffer stringbuffer = ByteBitBuffer.createInfinite();
            
            //char array replaced - begin
            byte[] stringasbytearray = string.getBytes(StandardCharsets.UTF_8);
            
            for (byte b: stringasbytearray){
            	UperEncoder.encodeConstrainedInt(stringbuffer, b & 0xff, 0, 255);
            }
            //-for (char c : string.toCharArray()) {
            //-    encodeChar(stringbuffer, c, restrictionAnnotation);
            //-}
            //char array replaced - end

            stringbuffer.flip();
            if (stringbuffer.limit() % 8 != 0) { 
            		throw new AssertionError("utf8 encoding resulted not in multiple of 8 bits"); 
            }
            int numOctets = (stringbuffer.limit() + 7) / 8;  // Actually +7 is not needed here,
                                                             // since we already checked with %8.
            int position1 = bitbuffer.position();
            UperEncoder.encodeLengthDeterminant(bitbuffer, numOctets);
            UperEncoder.logger.debug(String.format("UTF8String %s,  length %d octets, encoded as %s", string, numOctets, bitbuffer.toBooleanStringFromPosition(position1)));
            int position2 = bitbuffer.position();
            for (int i = 0; i < stringbuffer.limit(); i++) {
                bitbuffer.put(stringbuffer.get());
            }
            UperEncoder.logger.debug(String.format("UTF8String %s, encoded length %d octets, value bits: %s", string, numOctets, bitbuffer.toBooleanStringFromPosition(position2)));
            return;
        } else if (fixedSize != null) {
            if (fixedSize.value() != string.length()) { throw new IllegalArgumentException(
                    "String length does not match constraints"); }
            int position = bitbuffer.position();
            for (int i = 0; i < fixedSize.value(); i++) {
                encodeChar(bitbuffer, string.charAt(i), restrictionAnnotation);
            }
            UperEncoder.logger.debug(String.format("string encoded as <%s>", bitbuffer.toBooleanStringFromPosition(position)));
            return;
        } else if (sizeRange != null) {
            UperEncoder.logger.debug("string length");
            int position1 = bitbuffer.position();
            UperEncoder.encodeConstrainedInt(bitbuffer, string.length(), sizeRange.minValue(),sizeRange.maxValue(), sizeRange.hasExtensionMarker());
            int position2 = bitbuffer.position();
            UperEncoder.logger.debug("string content");
            for (int i = 0; i < string.length(); i++) {
                encodeChar(bitbuffer, string.charAt(i), restrictionAnnotation);
            }
            UperEncoder.logger.debug(String.format("STRING %s size %d: %s", obj.getClass().getName(), bitbuffer.toBooleanString(position1, position2 - position1),bitbuffer.toBooleanStringFromPosition(position2)));
            return;
        } else {
            int position1 = bitbuffer.position();
            UperEncoder.encodeLengthDeterminant(bitbuffer, string.length());
            int position2 = bitbuffer.position();
            for (int i = 0; i < string.length(); i++) {
                encodeChar(bitbuffer, string.charAt(i), restrictionAnnotation);
            }
            UperEncoder.logger.debug(String.format("STRING %s size %s: %s", obj.getClass().getName(), bitbuffer.toBooleanString(position1, position2 - position1),bitbuffer.toBooleanStringFromPosition(position2)));
            return;
        }
    }

    @Override public <T> boolean canDecode(Class<T> classOfT, Annotation[] extraAnnotations) {
        return String.class.isAssignableFrom(classOfT) || Asn1String.class.isAssignableFrom(classOfT);
    }

    @Override public <T> T decode(BitBuffer bitbuffer,
            Class<T> classOfT, Field field,
            Annotation[] extraAnnotations) {
        UperEncoder.logger.debug("decode String");
    	AnnotationStore annotations = new AnnotationStore(classOfT.getAnnotations(), extraAnnotations);
        RestrictedString restrictionAnnotation = annotations.getAnnotation(RestrictedString.class);
        if (restrictionAnnotation == null) { 
        	throw new UnsupportedOperationException(
                "Unrestricted character strings are not supported yet. All annotations: " + Arrays.asList(classOfT.getAnnotations())); 
        }
        if (restrictionAnnotation.value() == CharacterRestriction.UTF8String) {
            Long numOctets = UperEncoder.decodeLengthDeterminant(bitbuffer);
            List<Boolean> content = new ArrayList<Boolean>();
            for (int i = 0; i < numOctets * 8; i++) {
                content.add(bitbuffer.get());
            }
            byte[] contentBytes = UperEncoder.bytesFromCollection(content);
            UperEncoder.logger.debug(String.format("Content bytes (hex): %s", UperEncoder.hexStringFromBytes(contentBytes)));   
            String resultStr = StandardCharsets.UTF_8.decode(ByteBuffer.wrap(contentBytes)).toString();
            UperEncoder.logger.debug(String.format("Decoded as %s", resultStr));   
            T result = UperEncoder.instantiate(classOfT, resultStr);
            return result;
        } else {
            FixedSize fixedSize = annotations.getAnnotation(FixedSize.class);
            SizeRange sizeRange = annotations.getAnnotation(SizeRange.class);
            long numChars = (fixedSize != null) ? fixedSize.value() :
                    (sizeRange != null) ? UperEncoder.decodeConstrainedInt(bitbuffer,
                            UperEncoder.intRangeFromSizeRange(sizeRange)) :
                            UperEncoder.decodeLengthDeterminant(bitbuffer);
            UperEncoder.logger.debug(String.format("known-multiplier string, numchars: %d", numChars));
            StringBuilder stringBuilder = new StringBuilder((int) numChars);
            for (int c = 0; c < numChars; c++) {
                stringBuilder.append(decodeRestrictedChar(bitbuffer, restrictionAnnotation));
            }
            String resultStr = stringBuilder.toString();
            UperEncoder.logger.debug(String.format("Decoded as %s", resultStr));
            T result = UperEncoder.instantiate(classOfT, resultStr);
            return result;
        }
    }

    private static void encodeChar(BitBuffer bitbuffer, char c, RestrictedString restriction) throws Asn1EncodingException {
        UperEncoder.logger.debug(String.format("char %s", c));
        switch (restriction.value()) {
            case IA5String:
                if (restriction.alphabet() != DefaultAlphabet.class) {
                    throw new UnsupportedOperationException("alphabet for IA5String is not supported yet.");
                }
                UperEncoder.encodeConstrainedInt(
                        bitbuffer,
                        StandardCharsets.US_ASCII.encode(CharBuffer.wrap(new char[] { c })).get() & 0xff,
                        0,
                        127);
                return;
            case UTF8String:
                if (restriction.alphabet() != DefaultAlphabet.class) {
                    throw new UnsupportedOperationException("alphabet for UTF8 is not supported yet.");
                }
                ByteBuffer buffer = StandardCharsets.UTF_8.encode(CharBuffer.wrap(new char[] { c }));
                for (int i = 0; i < buffer.limit(); i++) {
                    UperEncoder.encodeConstrainedInt(bitbuffer, buffer.get() & 0xff, 0, 255);
                }
                return;
            case VisibleString:
            case ISO646String:
                if (restriction.alphabet() != DefaultAlphabet.class) {
                    char[] chars;
                    try {
                        chars = UperEncoder.instantiate(restriction.alphabet()).chars().toCharArray();
                    } catch (IllegalArgumentException e) {
                        LOGGER.info("Uninstantinatable alphabet ", e);
                        throw new IllegalArgumentException("Uninstantinatable alphabet" + restriction.alphabet().getName());
                    }
                    if (BigInteger.valueOf(chars.length - 1).bitLength() < BigInteger.valueOf(126)
                            .bitLength()) {
                        Arrays.sort(chars);
                        String strAlphabet = new String(chars);
                        int index = strAlphabet.indexOf(c);
                        if (index < 0) { throw new IllegalArgumentException("can't find character " + c + " in alphabet " + strAlphabet); }
                        UperEncoder.encodeConstrainedInt(
                                bitbuffer,
                                index,
                                0,
                                chars.length - 1);
                        return;
                    } else {
                        UperEncoder.encodeConstrainedInt(
                                bitbuffer,
                                StandardCharsets.US_ASCII.encode(CharBuffer.wrap(new char[] { c }))
                                        .get() & 0xff,
                                0,
                                126);
                        return;
                    }
                } else {
                    UperEncoder.encodeConstrainedInt(
                            bitbuffer,
                            StandardCharsets.US_ASCII.encode(CharBuffer.wrap(new char[] { c }))
                                    .get() & 0xff,
                            0,
                            126);
                    return;
                }
            default:
                throw new UnsupportedOperationException("String type " + restriction
                        + " is not supported yet");
        }
    }

    private static String decodeRestrictedChar(BitBuffer bitqueue,
            RestrictedString restrictionAnnotation) {
        switch (restrictionAnnotation.value()) {
            case IA5String: {
                if (restrictionAnnotation.alphabet() != DefaultAlphabet.class) {
                    throw new UnsupportedOperationException(
                        "alphabet for IA5String is not supported yet.");
                }
                byte charByte = (byte) UperEncoder.decodeConstrainedInt(bitqueue, UperEncoder.newRange(0, 127, false));
                byte[] bytes = new byte[] { charByte };
                String result = StandardCharsets.US_ASCII.decode(ByteBuffer.wrap(bytes)).toString();
                if (result.length() != 1) { 
                	throw new AssertionError("decoded more than one char (" + result + ")"); 
                }
                return result;
            }
            case VisibleString:
            case ISO646String: {
                if (restrictionAnnotation.alphabet() != DefaultAlphabet.class) {
                    char[] chars;
                    try {
                        chars = UperEncoder.instantiate(restrictionAnnotation.alphabet()).chars().toCharArray();
                    } catch (IllegalArgumentException e) {
                        LOGGER.info("Uninstantinatable alphabet ", e);
                        throw new IllegalArgumentException("Uninstantinatable alphabet " + restrictionAnnotation.alphabet().getName());
                    }
                    if (BigInteger.valueOf(chars.length - 1).bitLength() < BigInteger.valueOf(126)
                            .bitLength()) {
                        Arrays.sort(chars);
                        int index = (byte) UperEncoder.decodeConstrainedInt(bitqueue, UperEncoder.newRange(0, chars.length - 1, false));
                        String strAlphabet = new String(chars);
                        char c = strAlphabet.charAt(index);
                        String result = new String("" + c);
                        return result;
                    } else {  // Encode normally
                        byte charByte = (byte) UperEncoder.decodeConstrainedInt(bitqueue, UperEncoder.newRange(0, 126, false));
                        byte[] bytes = new byte[] { charByte };
                        String result = StandardCharsets.US_ASCII.decode(ByteBuffer.wrap(bytes)).toString();
                        if (result.length() != 1) { throw new AssertionError(
                                "decoded more than one char (" + result + ")");
                        }
                        return result;
                    }
                } else {  // Encode normally
                    byte charByte = (byte) UperEncoder.decodeConstrainedInt(bitqueue, UperEncoder.newRange(0, 126, false));
                    byte[] bytes = new byte[] { charByte };
                    String result = StandardCharsets.US_ASCII.decode(ByteBuffer.wrap(bytes)).toString();
                    if (result.length() != 1) {
                        throw new AssertionError("decoded more than one char (" + result + ")");
                    }
                    return result;
                }
            }
            default:
                throw new UnsupportedOperationException("String type " + restrictionAnnotation + " is not supported yet");

        }
    }

	@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;
		T result = UperEncoder.instantiate(classOfT, defaultAnnotation.value());
		return result;
	}

}