summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/apkzlib/zip/utils/LittleEndianUtils.java
blob: c257d39c53f9be0b8ccfd3eee8f9a662509ba58c (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
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
/*
 * Copyright (C) 2015 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 com.android.apkzlib.zip.utils;

import com.google.common.base.Preconditions;
import com.google.common.base.Verify;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.annotation.Nonnull;

/**
 * Utilities to read and write 16 and 32 bit integers with support for little-endian
 * encoding, as used in zip files. Zip files actually use unsigned data types. We use Java's native
 * (signed) data types but will use long (64 bit) to ensure we can fit the whole range.
 */
public class LittleEndianUtils {
    /**
     * Utility class, no constructor.
     */
    private LittleEndianUtils() {
    }

    /**
     * Reads 4 bytes in little-endian format and converts them into a 32-bit value.
     *
     * @param bytes from where should the bytes be read; the first 4 bytes of the source will be
     * read
     * @return the 32-bit value
     * @throws IOException failed to read the value
     */
    public static long readUnsigned4Le(@Nonnull ByteBuffer bytes) throws IOException {
        Preconditions.checkNotNull(bytes, "bytes == null");

        if (bytes.remaining() < 4) {
            throw new EOFException("Not enough data: 4 bytes expected, " + bytes.remaining()
                    + " available.");
        }

        byte b0 = bytes.get();
        byte b1 = bytes.get();
        byte b2 = bytes.get();
        byte b3 = bytes.get();
        long r = (b0 & 0xff) | ((b1 & 0xff) << 8) | ((b2 & 0xff) << 16) | ((b3 & 0xffL) << 24);
        Verify.verify(r >= 0);
        Verify.verify(r <= 0x00000000ffffffffL);
        return r;
    }

    /**
     * Reads 2 bytes in little-endian format and converts them into a 16-bit value.
     *
     * @param bytes from where should the bytes be read; the first 2 bytes of the source will be
     * read
     * @return the 16-bit value
     * @throws IOException failed to read the value
     */
    public static int readUnsigned2Le(@Nonnull ByteBuffer bytes) throws IOException {
        Preconditions.checkNotNull(bytes, "bytes == null");

        if (bytes.remaining() < 2) {
            throw new EOFException(
                    "Not enough data: 2 bytes expected, "
                            + bytes.remaining()
                            + " available.");
        }

        byte b0 = bytes.get();
        byte b1 = bytes.get();
        int r = (b0 & 0xff) | ((b1 & 0xff) << 8);

        Verify.verify(r >= 0);
        Verify.verify(r <= 0x0000ffff);
        return r;
    }

    /**
     * Writes 4 bytes in little-endian format, converting them from a 32-bit value.
     *
     * @param output the output stream where the bytes will be written
     * @param value the 32-bit value to convert
     * @throws IOException failed to write the value data
     */
    public static void writeUnsigned4Le(@Nonnull ByteBuffer output, long value)
            throws IOException {
        Preconditions.checkNotNull(output, "output == null");
        Preconditions.checkArgument(value >= 0, "value (%s) < 0", value);
        Preconditions.checkArgument(
                value <= 0x00000000ffffffffL,
                "value (%s) > 0x00000000ffffffffL",
                value);

        output.put((byte) (value & 0xff));
        output.put((byte) ((value >> 8) & 0xff));
        output.put((byte) ((value >> 16) & 0xff));
        output.put((byte) ((value >> 24) & 0xff));
    }

    /**
     * Writes 2 bytes in little-endian format, converting them from a 16-bit value.
     *
     * @param output the output stream where the bytes will be written
     * @param value the 16-bit value to convert
     * @throws IOException failed to write the value data
     */
    public static void writeUnsigned2Le(@Nonnull ByteBuffer output, int value)
            throws IOException {
        Preconditions.checkNotNull(output, "output == null");
        Preconditions.checkArgument(value >= 0, "value (%s) < 0", value);
        Preconditions.checkArgument(value <= 0x0000ffff, "value (%s) > 0x0000ffff", value);

        output.put((byte) (value & 0xff));
        output.put((byte) ((value >> 8) & 0xff));
    }
}