• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /*
18  * Functions for interpreting LEB128 (little endian base 128) values
19  */
20 
21 #include "Leb128.h"
22 
23 /*
24  * Reads an unsigned LEB128 value, updating the given pointer to point
25  * just past the end of the read value and also indicating whether the
26  * value was syntactically valid. The only syntactically *invalid*
27  * values are ones that are five bytes long where the final byte has
28  * any but the low-order four bits set. Additionally, if the limit is
29  * passed as non-NULL and bytes would need to be read past the limit,
30  * then the read is considered invalid.
31  */
readAndVerifyUnsignedLeb128(const u1 ** pStream,const u1 * limit,bool * okay)32 int readAndVerifyUnsignedLeb128(const u1** pStream, const u1* limit,
33         bool* okay) {
34     const u1* ptr = *pStream;
35     int result = readUnsignedLeb128(pStream);
36 
37     if (((limit != NULL) && (*pStream > limit))
38             || (((*pStream - ptr) == 5) && (ptr[4] > 0x0f))) {
39         *okay = false;
40     }
41 
42     return result;
43 }
44 
45 /*
46  * Reads a signed LEB128 value, updating the given pointer to point
47  * just past the end of the read value and also indicating whether the
48  * value was syntactically valid. The only syntactically *invalid*
49  * values are ones that are five bytes long where the final byte has
50  * any but the low-order four bits set. Additionally, if the limit is
51  * passed as non-NULL and bytes would need to be read past the limit,
52  * then the read is considered invalid.
53  */
readAndVerifySignedLeb128(const u1 ** pStream,const u1 * limit,bool * okay)54 int readAndVerifySignedLeb128(const u1** pStream, const u1* limit,
55         bool* okay) {
56     const u1* ptr = *pStream;
57     int result = readSignedLeb128(pStream);
58 
59     if (((limit != NULL) && (*pStream > limit))
60             || (((*pStream - ptr) == 5) && (ptr[4] > 0x0f))) {
61         *okay = false;
62     }
63 
64     return result;
65 }
66