summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/apkzlib/sign/ZFileDataSource.java
blob: 049cf3533ea5a617a840638954ceea5796c3a340 (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
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
/*
 * Copyright (C) 2016 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.sign;

import com.android.apksig.util.DataSink;
import com.android.apksig.util.DataSource;
import com.android.apkzlib.zip.ZFile;
import com.google.common.base.Preconditions;
import java.io.EOFException;
import java.io.IOException;
import java.nio.ByteBuffer;
import javax.annotation.Nonnull;

/**
 * {@link DataSource} backed by contents of {@link ZFile}.
 */
class ZFileDataSource implements DataSource {

    private static final int MAX_READ_CHUNK_SIZE = 65536;

    @Nonnull
    private final ZFile file;

    /**
     * Offset (in bytes) relative to the start of file where the region visible in this data source
     * starts.
     */
    private final long offset;

    /**
     * Size (in bytes) of the file region visible in this data source or {@code -1} if the whole
     * file is visible in this data source and thus its size may change if the file's size changes.
     */
    private final long size;

    /**
     * Constructs a new {@code ZFileDataSource} based on the data contained in the file. Changes to
     * the contents of the file, including the size of the file, will be visible in this data
     * source.
     */
    public ZFileDataSource(@Nonnull ZFile file) {
        this.file = file;
        offset = 0;
        size = -1;
    }

    /**
     * Constructs a new {@code ZFileDataSource} based on the data contained in the specified region
     * of the provided file. Changes to the contents of this region of the file will be visible in
     * this data source.
     */
    public ZFileDataSource(@Nonnull ZFile file, long offset, long size) {
        Preconditions.checkArgument(offset >= 0, "offset < 0");
        Preconditions.checkArgument(size >= 0, "size < 0");
        this.file = file;
        this.offset = offset;
        this.size = size;
    }

    @Override
    public long size() {
        if (size == -1) {
            // Data source size is the current size of the file
            try {
                return file.directSize();
            } catch (IOException e) {
                return 0;
            }
        } else {
            // Data source size is fixed
            return size;
        }
    }

    @Override
    public DataSource slice(long offset, long size) {
        long sourceSize = size();
        checkChunkValid(offset, size, sourceSize);
        if ((offset == 0) && (size == sourceSize)) {
            return this;
        }

        return new ZFileDataSource(file, this.offset + offset, size);
    }

    @Override
    public void feed(long offset, long size, @Nonnull DataSink sink) throws IOException {
        long sourceSize = size();
        checkChunkValid(offset, size, sourceSize);
        if (size == 0) {
            return;
        }

        long chunkOffsetInFile = this.offset + offset;
        long remaining = size;
        byte[] buf = new byte[(int) Math.min(remaining, MAX_READ_CHUNK_SIZE)];
        while (remaining > 0) {
            int chunkSize = (int) Math.min(remaining, buf.length);
            int readSize = file.directRead(chunkOffsetInFile, buf, 0, chunkSize);
            if (readSize == -1) {
                throw new EOFException("Premature EOF");
            }
            if (readSize > 0) {
                sink.consume(buf, 0, readSize);
                chunkOffsetInFile += readSize;
                remaining -= readSize;
            }
        }
    }

    @Override
    public void copyTo(long offset, int size, @Nonnull ByteBuffer dest) throws IOException {
        long sourceSize = size();
        checkChunkValid(offset, size, sourceSize);
        if (size == 0) {
            return;
        }

        int prevLimit = dest.limit();
        try {
            file.directFullyRead(this.offset + offset, dest);
        } finally {
            dest.limit(prevLimit);
        }
    }

    @Override
    public ByteBuffer getByteBuffer(long offset, int size) throws IOException {
        ByteBuffer result = ByteBuffer.allocate(size);
        copyTo(offset, size, result);
        result.flip();
        return result;
    }

    private static void checkChunkValid(long offset, long size, long sourceSize) {
        Preconditions.checkArgument(offset >= 0, "offset < 0");
        Preconditions.checkArgument(size >= 0, "size < 0");
        Preconditions.checkArgument(offset <= sourceSize, "offset > sourceSize");
        long endOffset = offset + size;
        Preconditions.checkArgument(offset <= endOffset, "offset > endOffset");
        Preconditions.checkArgument(endOffset <= sourceSize, "endOffset > sourceSize");
    }
}