• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2012, Google Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are
7  * met:
8  *
9  *     * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *     * Redistributions in binary form must reproduce the above
12  * copyright notice, this list of conditions and the following disclaimer
13  * in the documentation and/or other materials provided with the
14  * distribution.
15  *     * Neither the name of Google Inc. nor the names of its
16  * contributors may be used to endorse or promote products derived from
17  * this software without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
22  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
23  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 package org.jf.dexlib2.dexbacked;
33 
34 import org.jf.util.ExceptionWithContext;
35 
36 import javax.annotation.Nonnull;
37 
38 public class BaseDexBuffer {
39     @Nonnull /* package private */ final byte[] buf;
40     /* package private */ final int baseOffset;
41 
BaseDexBuffer(@onnull byte[] buf)42     public BaseDexBuffer(@Nonnull byte[] buf) {
43         this(buf, 0);
44     }
BaseDexBuffer(@onnull byte[] buf, int offset)45     public BaseDexBuffer(@Nonnull byte[] buf, int offset) {
46         this.buf = buf;
47         this.baseOffset = offset;
48     }
49 
readSmallUint(int offset)50     public int readSmallUint(int offset) {
51         byte[] buf = this.buf;
52         offset += baseOffset;
53         int result = (buf[offset] & 0xff) |
54                 ((buf[offset+1] & 0xff) << 8) |
55                 ((buf[offset+2] & 0xff) << 16) |
56                 ((buf[offset+3]) << 24);
57         if (result < 0) {
58             throw new ExceptionWithContext("Encountered small uint that is out of range at offset 0x%x", offset);
59         }
60         return result;
61     }
62 
readOptionalUint(int offset)63     public int readOptionalUint(int offset) {
64         byte[] buf = this.buf;
65         offset += baseOffset;
66         int result = (buf[offset] & 0xff) |
67                 ((buf[offset+1] & 0xff) << 8) |
68                 ((buf[offset+2] & 0xff) << 16) |
69                 ((buf[offset+3]) << 24);
70         if (result < -1) {
71             throw new ExceptionWithContext("Encountered optional uint that is out of range at offset 0x%x", offset);
72         }
73         return result;
74     }
75 
readUshort(int offset)76     public int readUshort(int offset) {
77         byte[] buf = this.buf;
78         offset += baseOffset;
79         return (buf[offset] & 0xff) |
80                 ((buf[offset+1] & 0xff) << 8);
81     }
82 
readUbyte(int offset)83     public int readUbyte(int offset) {
84         return buf[offset + baseOffset] & 0xff;
85     }
86 
readLong(int offset)87     public long readLong(int offset) {
88         byte[] buf = this.buf;
89         offset += baseOffset;
90         return (buf[offset] & 0xff) |
91                 ((buf[offset+1] & 0xff) << 8) |
92                 ((buf[offset+2] & 0xff) << 16) |
93                 ((buf[offset+3] & 0xffL) << 24) |
94                 ((buf[offset+4] & 0xffL) << 32) |
95                 ((buf[offset+5] & 0xffL) << 40) |
96                 ((buf[offset+6] & 0xffL) << 48) |
97                 (((long)buf[offset+7]) << 56);
98     }
99 
readLongAsSmallUint(int offset)100     public int readLongAsSmallUint(int offset) {
101         byte[] buf = this.buf;
102         offset += baseOffset;
103         long result = (buf[offset] & 0xff) |
104                 ((buf[offset+1] & 0xff) << 8) |
105                 ((buf[offset+2] & 0xff) << 16) |
106                 ((buf[offset+3] & 0xffL) << 24) |
107                 ((buf[offset+4] & 0xffL) << 32) |
108                 ((buf[offset+5] & 0xffL) << 40) |
109                 ((buf[offset+6] & 0xffL) << 48) |
110                 (((long)buf[offset+7]) << 56);
111         if (result < 0 || result > Integer.MAX_VALUE) {
112             throw new ExceptionWithContext("Encountered out-of-range ulong at offset 0x%x", offset);
113         }
114         return (int)result;
115     }
116 
readInt(int offset)117     public int readInt(int offset) {
118         byte[] buf = this.buf;
119         offset += baseOffset;
120         return (buf[offset] & 0xff) |
121                 ((buf[offset+1] & 0xff) << 8) |
122                 ((buf[offset+2] & 0xff) << 16) |
123                 (buf[offset+3] << 24);
124     }
125 
readShort(int offset)126     public int readShort(int offset) {
127         byte[] buf = this.buf;
128         offset += baseOffset;
129         return (buf[offset] & 0xff) |
130                 (buf[offset+1] << 8);
131     }
132 
readByte(int offset)133     public int readByte(int offset) {
134         return buf[baseOffset + offset];
135     }
136 
137     @Nonnull
readerAt(int offset)138     public BaseDexReader readerAt(int offset) {
139         return new BaseDexReader<BaseDexBuffer>(this, offset);
140     }
141 
142     @Nonnull
getBuf()143     protected byte[] getBuf() {
144         return buf;
145     }
146 
getBaseOffset()147     protected int getBaseOffset() {
148         return baseOffset;
149     }
150 }
151