summaryrefslogtreecommitdiff
path: root/src/main/java/com/android/apkzlib/zip/ExtraField.java
blob: 90c6faef9679bcd9307142045573e9f812ed2645 (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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
 * 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.zip;

import com.android.apkzlib.zip.utils.LittleEndianUtils;
import com.google.common.base.Preconditions;
import com.google.common.collect.ImmutableList;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;

/**
 * Contains an extra field.
 *
 * <p>According to the zip specification, the extra field is composed of a sequence of fields.
 * This class provides a way to access, parse and modify that information.
 *
 * <p>The zip specification calls fields to the fields inside the extra field. Because this
 * terminology is confusing, we use <i>segment</i> to refer to a part of the extra field. Each
 * segment is represented by an instance of {@link Segment} and contains a header ID and data.
 *
 * <p>Each instance of {@link ExtraField} is immutable. The extra field of a particular entry can
 * be changed by creating a new instanceof {@link ExtraField} and pass it to
 * {@link StoredEntry#setLocalExtra(ExtraField)}.
 *
 * <p>Instances of {@link ExtraField} can be created directly from the list of segments in it
 * or from the raw byte data. If created from the raw byte data, the data will only be parsed
 * on demand. So, if neither {@link #getSegments()} nor {@link #getSingleSegment(int)} is
 * invoked, the extra field will not be parsed. This guarantees low performance impact of the
 * using the extra field unless its contents are needed.
 */
public class ExtraField {

    /**
     * Header ID for field with zip alignment.
     */
    static final int ALIGNMENT_ZIP_EXTRA_DATA_FIELD_HEADER_ID = 0xd935;

    /**
     * The field's raw data, if it is known. Either this variable or {@link #segments} must be
     * non-{@code null}.
     */
    @Nullable
    private final byte[] rawData;

    /**
     * The list of field's segments. Will be populated if the extra field is created based on a
     * list of segments; will also be populated after parsing if the extra field is created based
     * on the raw bytes.
     */
    @Nullable
    private ImmutableList<Segment> segments;

    /**
     * Creates an extra field based on existing raw data.
     *
     * @param rawData the raw data; will not be parsed unless needed
     */
    public ExtraField(@Nonnull byte[] rawData) {
        this.rawData = rawData;
        segments = null;
    }

    /**
     * Creates a new extra field with no segments.
     */
    public ExtraField() {
        rawData = null;
        segments = ImmutableList.of();
    }

    /**
     * Creates a new extra field with the given segments.
     *
     * @param segments the segments
     */
    public ExtraField(@Nonnull ImmutableList<Segment> segments) {
        rawData = null;
        this.segments = segments;
    }

    /**
     * Obtains all segments in the extra field.
     *
     * @return all segments
     * @throws IOException failed to parse the extra field
     */
    public ImmutableList<Segment> getSegments() throws IOException {
        if (segments == null) {
            parseSegments();
        }

        Preconditions.checkNotNull(segments);
        return segments;
    }

    /**
     * Obtains the only segment with the provided header ID.
     *
     * @param headerId the header ID
     * @return the segment found or {@code null} if no segment contains the provided header ID
     * @throws IOException there is more than one header with the provided header ID
     */
    @Nullable
    public Segment getSingleSegment(int headerId) throws IOException {
        List<Segment> found =
                getSegments().stream()
                        .filter(s -> s.getHeaderId() == headerId)
                        .collect(Collectors.toList());
        if (found.isEmpty()) {
            return null;
        } else if (found.size() == 1) {
            return found.get(0);
        } else {
            throw new IOException(found.size() + " segments with header ID " + headerId + "found");
        }
    }

    /**
     * Parses the raw data and generates all segments in {@link #segments}.
     *
     * @throws IOException failed to parse the data
     */
    private void parseSegments() throws IOException {
        Preconditions.checkNotNull(rawData);
        Preconditions.checkState(segments == null);

        List<Segment> segments = new ArrayList<>();
        ByteBuffer buffer = ByteBuffer.wrap(rawData);

        while (buffer.remaining() > 0) {
            int headerId = LittleEndianUtils.readUnsigned2Le(buffer);
            int dataSize = LittleEndianUtils.readUnsigned2Le(buffer);
            if (dataSize < 0) {
                throw new IOException(
                        "Invalid data size for extra field segment with header ID "
                                + headerId
                                + ": "
                                + dataSize);
            }

            byte[] data = new byte[dataSize];
            buffer.get(data);

            SegmentFactory factory = identifySegmentFactory(headerId);
            Segment seg = factory.make(headerId, data);
            segments.add(seg);
        }

        this.segments = ImmutableList.copyOf(segments);
    }

    /**
     * Obtains the size of the extra field.
     *
     * @return the size
     */
    public int size() {
        if (rawData != null) {
            return rawData.length;
        } else {
            Preconditions.checkNotNull(segments);
            int sz = 0;
            for (Segment s : segments) {
                sz += s.size();
            }

            return sz;
        }
    }

    /**
     * Writes the extra field to the given output buffer.
     *
     * @param out the output buffer to write the field; exactly {@link #size()} bytes will be
     * written
     * @throws IOException failed to write the extra fields
     */
    public void write(@Nonnull ByteBuffer out) throws IOException {
        if (rawData != null) {
            out.put(rawData);
        } else {
            Preconditions.checkNotNull(segments);
            for (Segment s : segments) {
                s.write(out);
            }
        }
    }

    /**
     * Identifies the factory to create the segment with the provided header ID.
     *
     * @param headerId the header ID
     * @return the segmnet factory that creates segments with the given header
     */
    @Nonnull
    private static SegmentFactory identifySegmentFactory(int headerId) {
        if (headerId == ALIGNMENT_ZIP_EXTRA_DATA_FIELD_HEADER_ID) {
            return AlignmentSegment::new;
        }

        return RawDataSegment::new;
    }

    /**
     * Field inside the extra field. A segment contains a header ID and data. Specific types of
     * segments implement this interface.
     */
    public interface Segment {

        /**
         * Obtains the segment's header ID.
         *
         * @return the segment's header ID
         */
        int getHeaderId();

        /**
         * Obtains the size of the segment including the header ID.
         *
         * @return the number of bytes needed to write the segment
         */
        int size();

        /**
         * Writes the segment to a buffer.
         *
         * @param out the buffer where to write the segment to; exactly {@link #size()} bytes will
         * be written
         * @throws IOException failed to write segment data
         */
        void write(@Nonnull ByteBuffer out) throws IOException;
    }

    /**
     * Factory that creates a segment.
     */
    @FunctionalInterface
    interface SegmentFactory {

        /**
         * Creates a new segment.
         *
         * @param headerId the header ID
         * @param data the segment's data
         * @return the created segment
         * @throws IOException failed to create the segment from the data
         */
        @Nonnull
        Segment make(int headerId, @Nonnull byte[] data) throws IOException;
    }

    /**
     * Segment of raw data: this class represents a general segment containing an array of bytes
     * as data.
     */
    public static class RawDataSegment implements Segment {

        /**
         * Header ID.
         */
        private final int headerId;

        /**
         * Data in the segment.
         */
        @Nonnull
        private final byte[] data;

        /**
         * Creates a new raw data segment.
         *
         * @param headerId the header ID
         * @param data the segment data
         */
        RawDataSegment(int headerId, @Nonnull byte[] data) {
            this.headerId = headerId;
            this.data = data;
        }

        @Override
        public int getHeaderId() {
            return headerId;
        }

        @Override
        public void write(@Nonnull ByteBuffer out) throws IOException {
            LittleEndianUtils.writeUnsigned2Le(out, headerId);
            LittleEndianUtils.writeUnsigned2Le(out, data.length);
            out.put(data);
        }

        @Override
        public int size() {
            return 4 + data.length;
        }
    }

    /**
     * Segment with information on an alignment: this segment contains information on how an entry
     * should be aligned and contains zero-filled data to force alignment.
     *
     * <p>An alignment segment contains the header ID, the size of the data, the alignment value
     * and zero bytes to pad
     */
    public static class AlignmentSegment implements Segment {

        /**
         * Minimum size for an alignment segment.
         */
        public static final int MINIMUM_SIZE = 6;

        /**
         * The alignment value.
         */
        private int alignment;

        /**
         * How many bytes of padding are in this segment?
         */
        private int padding;

        /**
         * Creates a new alignment segment.
         *
         * @param alignment the alignment value
         * @param totalSize how many bytes should this segment take?
         */
        public AlignmentSegment(int alignment, int totalSize) {
            Preconditions.checkArgument(alignment > 0, "alignment <= 0");
            Preconditions.checkArgument(totalSize >= MINIMUM_SIZE, "totalSize < MINIMUM_SIZE");

            /*
             * We have 6 bytes of fixed data: header ID (2 bytes), data size (2 bytes), alignment
             * value (2 bytes).
             */
            this.alignment = alignment;
            padding = totalSize - MINIMUM_SIZE;
        }

        /**
         * Creates a new alignment segment from extra data.
         *
         * @param headerId the header ID
         * @param data the segment data
         * @throws IOException failed to create the segment from the data
         */
        public AlignmentSegment(int headerId, @Nonnull byte[] data) throws IOException {
            Preconditions.checkArgument(headerId == ALIGNMENT_ZIP_EXTRA_DATA_FIELD_HEADER_ID);

            ByteBuffer dataBuffer = ByteBuffer.wrap(data);
            alignment = LittleEndianUtils.readUnsigned2Le(dataBuffer);
            if (alignment <= 0) {
                throw new IOException("Invalid alignment in alignment field: " + alignment);
            }

            padding = data.length - 2;
        }

        @Override
        public void write(@Nonnull ByteBuffer out) throws IOException {
            LittleEndianUtils.writeUnsigned2Le(out, ALIGNMENT_ZIP_EXTRA_DATA_FIELD_HEADER_ID);
            LittleEndianUtils.writeUnsigned2Le(out, padding + 2);
            LittleEndianUtils.writeUnsigned2Le(out, alignment);
            out.put(new byte[padding]);
        }

        @Override
        public int size() {
            return padding + 6;
        }

        @Override
        public int getHeaderId() {
            return ALIGNMENT_ZIP_EXTRA_DATA_FIELD_HEADER_ID;
        }
    }
}