summaryrefslogtreecommitdiffstats
path: root/tools/dexfuzz/src/dexfuzz/rawdex/EncodedValue.java
blob: fdf133f816841da512fed819e41fd99f1584a367 (plain)
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
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package dexfuzz.rawdex;

import java.io.IOException;

public class EncodedValue implements RawDexObject {
  public byte valueArg;
  public byte valueType;
  public byte[] value;
  public EncodedArray encodedArray;
  public EncodedAnnotation encodedAnnotation;

  private static final byte VALUE_BYTE = 0x00;
  private static final byte VALUE_ARRAY = 0x1c;
  private static final byte VALUE_ANNOTATION = 0x1d;
  private static final byte VALUE_NULL = 0x1e;
  private static final byte VALUE_BOOLEAN = 0x1f;

  @Override
  public void read(DexRandomAccessFile file) throws IOException {
    int valueArgAndType = file.readUnsignedByte();

    // Get lower 5 bits.
    valueType = (byte) (valueArgAndType & 0x1f);
    // Get upper 3 bits.
    valueArg = (byte) ((valueArgAndType & 0xe0) >> 5);

    int size = 0;

    switch (valueType) {
      case VALUE_BYTE:
        size = 1;
        break;
      case VALUE_ARRAY:
        (encodedArray = new EncodedArray()).read(file);
        size = 0; // So we don't read into value.
        break;
      case VALUE_ANNOTATION:
        (encodedAnnotation = new EncodedAnnotation()).read(file);
        size = 0; // So we don't read into value.
        break;
      case VALUE_NULL:
      case VALUE_BOOLEAN:
        // No value
        size = 0;
        break;
      default:
        // All others encode value_arg as (size - 1), so...
        size = valueArg + 1;
        break;
    }

    if (size != 0) {
      value = new byte[size];
      for (int i = 0; i < size; i++) {
        value[i] = file.readByte();
      }
    }
  }

  @Override
  public void write(DexRandomAccessFile file) throws IOException {
    int valueArgAndType = ((valueType) | (valueArg << 5));
    file.writeByte(valueArgAndType);

    if (encodedArray != null) {
      encodedArray.write(file);
    } else if (encodedAnnotation != null) {
      encodedAnnotation.write(file);
    } else if (value != null) {
      file.write(value);
    }
  }

  @Override
  public void incrementIndex(IndexUpdateKind kind, int insertedIdx) {
    if (encodedArray != null) {
      encodedArray.incrementIndex(kind, insertedIdx);
    } else if (encodedAnnotation != null) {
      encodedAnnotation.incrementIndex(kind, insertedIdx);
    }
  }
}