blob: 5880c6a2d99bc482b207eecda427e28c0f2e50ef [file] [log] [blame]
Victor Hsieh3271d042017-10-24 15:46:32 -07001/*
2 * Copyright (C) 2017 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
17package android.util.apk;
18
19import java.io.IOException;
20import java.io.RandomAccessFile;
21import java.nio.ByteBuffer;
22import java.nio.ByteOrder;
Victor Hsiehdef64f22017-11-08 10:25:11 -080023import java.security.DigestException;
Victor Hsieh3271d042017-10-24 15:46:32 -070024import java.security.MessageDigest;
25import java.security.NoSuchAlgorithmException;
26import java.util.ArrayList;
27
28/**
29 * ApkVerityBuilder builds the APK verity tree and the verity header, which will be used by the
30 * kernel to verity the APK content on access.
31 *
32 * <p>Unlike a regular Merkle tree, APK verity tree does not cover the content fully. Due to
33 * the existing APK format, it has to skip APK Signing Block and also has some special treatment for
34 * the "Central Directory offset" field of ZIP End of Central Directory.
35 *
36 * @hide
37 */
38abstract class ApkVerityBuilder {
39 private ApkVerityBuilder() {}
40
41 private static final int CHUNK_SIZE_BYTES = 4096; // Typical Linux block size
42 private static final int DIGEST_SIZE_BYTES = 32; // SHA-256 size
43 private static final int FSVERITY_HEADER_SIZE_BYTES = 64;
44 private static final int ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE = 4;
45 private static final int ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET = 16;
46 private static final String JCA_DIGEST_ALGORITHM = "SHA-256";
47 private static final byte[] DEFAULT_SALT = new byte[8];
48
49 static class ApkVerityResult {
50 public final ByteBuffer fsverityData;
51 public final byte[] rootHash;
52
53 ApkVerityResult(ByteBuffer fsverityData, byte[] rootHash) {
54 this.fsverityData = fsverityData;
55 this.rootHash = rootHash;
56 }
57 }
58
59 /**
60 * Generates fsverity metadata and the Merkle tree into the {@link ByteBuffer} created by the
61 * {@link ByteBufferFactory}. The bytes layout in the buffer will be used by the kernel and is
62 * ready to be appended to the target file to set up fsverity. For fsverity to work, this data
63 * must be placed at the next page boundary, and the caller must add additional padding in that
64 * case.
65 *
66 * @return ApkVerityResult containing the fsverity data and the root hash of the Merkle tree.
67 */
68 static ApkVerityResult generateApkVerity(RandomAccessFile apk,
69 SignatureInfo signatureInfo, ByteBufferFactory bufferFactory)
Victor Hsiehdef64f22017-11-08 10:25:11 -080070 throws IOException, SecurityException, NoSuchAlgorithmException, DigestException {
Victor Hsieh3051d782018-01-12 08:28:03 -080071 long signingBlockSize =
72 signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset;
73 long dataSize = apk.length() - signingBlockSize - ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE;
74 int[] levelOffset = calculateVerityLevelOffset(dataSize);
75
76 ByteBuffer output = bufferFactory.create(
77 CHUNK_SIZE_BYTES + // fsverity header + extensions + padding
78 levelOffset[levelOffset.length - 1]); // Merkle tree size
79 output.order(ByteOrder.LITTLE_ENDIAN);
80
81 ByteBuffer header = slice(output, 0, FSVERITY_HEADER_SIZE_BYTES);
82 ByteBuffer extensions = slice(output, FSVERITY_HEADER_SIZE_BYTES, CHUNK_SIZE_BYTES);
83 ByteBuffer tree = slice(output, CHUNK_SIZE_BYTES, output.limit());
84 byte[] apkDigestBytes = new byte[DIGEST_SIZE_BYTES];
85 ByteBuffer apkDigest = ByteBuffer.wrap(apkDigestBytes);
86 apkDigest.order(ByteOrder.LITTLE_ENDIAN);
87
88 calculateFsveritySignatureInternal(apk, signatureInfo, tree, apkDigest, header, extensions);
89
90 output.rewind();
91 return new ApkVerityResult(output, apkDigestBytes);
92 }
93
94 /**
95 * Calculates the fsverity root hash for integrity measurement. This needs to be consistent to
96 * what kernel returns.
97 */
98 static byte[] generateFsverityRootHash(RandomAccessFile apk, ByteBuffer apkDigest,
99 SignatureInfo signatureInfo)
100 throws NoSuchAlgorithmException, DigestException, IOException {
101 ByteBuffer verityBlock = ByteBuffer.allocate(CHUNK_SIZE_BYTES)
102 .order(ByteOrder.LITTLE_ENDIAN);
103 ByteBuffer header = slice(verityBlock, 0, FSVERITY_HEADER_SIZE_BYTES);
104 ByteBuffer extensions = slice(verityBlock, FSVERITY_HEADER_SIZE_BYTES, CHUNK_SIZE_BYTES);
105
106 calculateFsveritySignatureInternal(apk, signatureInfo, null, null, header, extensions);
107
108 MessageDigest md = MessageDigest.getInstance(JCA_DIGEST_ALGORITHM);
109 md.update(DEFAULT_SALT);
110 md.update(verityBlock);
111 md.update(apkDigest);
112 return md.digest();
113 }
114
115 private static void calculateFsveritySignatureInternal(
116 RandomAccessFile apk, SignatureInfo signatureInfo, ByteBuffer treeOutput,
117 ByteBuffer rootHashOutput, ByteBuffer headerOutput, ByteBuffer extensionsOutput)
118 throws IOException, NoSuchAlgorithmException, DigestException {
Victor Hsieh3271d042017-10-24 15:46:32 -0700119 assertSigningBlockAlignedAndHasFullPages(signatureInfo);
120
121 long signingBlockSize =
122 signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset;
123 long dataSize = apk.length() - signingBlockSize - ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE;
124 int[] levelOffset = calculateVerityLevelOffset(dataSize);
Victor Hsieh3271d042017-10-24 15:46:32 -0700125
Victor Hsieh3051d782018-01-12 08:28:03 -0800126 if (treeOutput != null) {
127 byte[] apkRootHash = generateApkVerityTree(apk, signatureInfo, DEFAULT_SALT,
128 levelOffset, treeOutput);
129 if (rootHashOutput != null) {
130 rootHashOutput.put(apkRootHash);
131 }
132 }
Victor Hsieh3271d042017-10-24 15:46:32 -0700133
Victor Hsieh3051d782018-01-12 08:28:03 -0800134 if (headerOutput != null) {
135 generateFsverityHeader(headerOutput, apk.length(), levelOffset.length - 1,
136 DEFAULT_SALT);
137 }
Victor Hsieh3271d042017-10-24 15:46:32 -0700138
Victor Hsieh3051d782018-01-12 08:28:03 -0800139 if (extensionsOutput != null) {
140 generateFsverityExtensions(extensionsOutput, signatureInfo.apkSigningBlockOffset,
141 signingBlockSize, signatureInfo.eocdOffset);
142 }
Victor Hsieh3271d042017-10-24 15:46:32 -0700143 }
144
145 /**
146 * A helper class to consume and digest data by block continuously, and write into a buffer.
147 */
148 private static class BufferedDigester implements DataDigester {
149 /** Amount of the data to digest in each cycle before writting out the digest. */
150 private static final int BUFFER_SIZE = CHUNK_SIZE_BYTES;
151
152 /**
153 * Amount of data the {@link MessageDigest} has consumed since the last reset. This must be
154 * always less than BUFFER_SIZE since {@link MessageDigest} is reset whenever it has
155 * consumed BUFFER_SIZE of data.
156 */
157 private int mBytesDigestedSinceReset;
158
159 /** The final output {@link ByteBuffer} to write the digest to sequentially. */
160 private final ByteBuffer mOutput;
161
162 private final MessageDigest mMd;
Victor Hsiehdef64f22017-11-08 10:25:11 -0800163 private final byte[] mDigestBuffer = new byte[DIGEST_SIZE_BYTES];
Victor Hsieh3271d042017-10-24 15:46:32 -0700164 private final byte[] mSalt;
165
166 private BufferedDigester(byte[] salt, ByteBuffer output) throws NoSuchAlgorithmException {
167 mSalt = salt;
168 mOutput = output.slice();
169 mMd = MessageDigest.getInstance(JCA_DIGEST_ALGORITHM);
170 mMd.update(mSalt);
171 mBytesDigestedSinceReset = 0;
172 }
173
174 /**
175 * Consumes and digests data up to BUFFER_SIZE (may continue from the previous remaining),
176 * then writes the final digest to the output buffer. Repeat until all data are consumed.
177 * If the last consumption is not enough for BUFFER_SIZE, the state will stay and future
178 * consumption will continuous from there.
179 */
180 @Override
Victor Hsiehdef64f22017-11-08 10:25:11 -0800181 public void consume(ByteBuffer buffer) throws DigestException {
Victor Hsieh3271d042017-10-24 15:46:32 -0700182 int offset = buffer.position();
183 int remaining = buffer.remaining();
184 while (remaining > 0) {
185 int allowance = (int) Math.min(remaining, BUFFER_SIZE - mBytesDigestedSinceReset);
Victor Hsiehdef64f22017-11-08 10:25:11 -0800186 // Optimization: set the buffer limit to avoid allocating a new ByteBuffer object.
187 buffer.limit(buffer.position() + allowance);
188 mMd.update(buffer);
Victor Hsieh3271d042017-10-24 15:46:32 -0700189 offset += allowance;
190 remaining -= allowance;
191 mBytesDigestedSinceReset += allowance;
192
193 if (mBytesDigestedSinceReset == BUFFER_SIZE) {
Victor Hsiehdef64f22017-11-08 10:25:11 -0800194 mMd.digest(mDigestBuffer, 0, mDigestBuffer.length);
195 mOutput.put(mDigestBuffer);
196 // After digest, MessageDigest resets automatically, so no need to reset again.
Victor Hsieh3271d042017-10-24 15:46:32 -0700197 mMd.update(mSalt);
198 mBytesDigestedSinceReset = 0;
199 }
200 }
201 }
202
203 /** Finish the current digestion if any. */
204 @Override
Victor Hsiehdef64f22017-11-08 10:25:11 -0800205 public void finish() throws DigestException {
Victor Hsieh3271d042017-10-24 15:46:32 -0700206 if (mBytesDigestedSinceReset == 0) {
207 return;
208 }
Victor Hsiehdef64f22017-11-08 10:25:11 -0800209 mMd.digest(mDigestBuffer, 0, mDigestBuffer.length);
210 mOutput.put(mDigestBuffer);
Victor Hsieh3271d042017-10-24 15:46:32 -0700211 }
212
213 private void fillUpLastOutputChunk() {
Victor Hsiehb62a64e2018-01-19 12:46:23 -0800214 int lastBlockSize = (int) (mOutput.position() % BUFFER_SIZE);
215 if (lastBlockSize == 0) {
Victor Hsieh3271d042017-10-24 15:46:32 -0700216 return;
217 }
Victor Hsiehb62a64e2018-01-19 12:46:23 -0800218 mOutput.put(ByteBuffer.allocate(BUFFER_SIZE - lastBlockSize));
Victor Hsieh3271d042017-10-24 15:46:32 -0700219 }
220 }
221
222 /**
223 * Digest the source by chunk in the given range. If the last chunk is not a full chunk,
224 * digest the remaining.
225 */
226 private static void consumeByChunk(DataDigester digester, DataSource source, int chunkSize)
Victor Hsiehdef64f22017-11-08 10:25:11 -0800227 throws IOException, DigestException {
Victor Hsieh3271d042017-10-24 15:46:32 -0700228 long inputRemaining = source.size();
229 long inputOffset = 0;
230 while (inputRemaining > 0) {
231 int size = (int) Math.min(inputRemaining, chunkSize);
232 source.feedIntoDataDigester(digester, inputOffset, size);
233 inputOffset += size;
234 inputRemaining -= size;
235 }
236 }
237
238 // Rationale: 1) 1 MB should fit in memory space on all devices. 2) It is not too granular
239 // thus the syscall overhead is not too big.
240 private static final int MMAP_REGION_SIZE_BYTES = 1024 * 1024;
241
242 private static void generateApkVerityDigestAtLeafLevel(RandomAccessFile apk,
243 SignatureInfo signatureInfo, byte[] salt, ByteBuffer output)
Victor Hsiehdef64f22017-11-08 10:25:11 -0800244 throws IOException, NoSuchAlgorithmException, DigestException {
Victor Hsieh3271d042017-10-24 15:46:32 -0700245 BufferedDigester digester = new BufferedDigester(salt, output);
246
247 // 1. Digest from the beginning of the file, until APK Signing Block is reached.
248 consumeByChunk(digester,
249 new MemoryMappedFileDataSource(apk.getFD(), 0, signatureInfo.apkSigningBlockOffset),
250 MMAP_REGION_SIZE_BYTES);
251
252 // 2. Skip APK Signing Block and continue digesting, until the Central Directory offset
253 // field in EoCD is reached.
254 long eocdCdOffsetFieldPosition =
255 signatureInfo.eocdOffset + ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_OFFSET;
256 consumeByChunk(digester,
257 new MemoryMappedFileDataSource(apk.getFD(), signatureInfo.centralDirOffset,
258 eocdCdOffsetFieldPosition - signatureInfo.centralDirOffset),
259 MMAP_REGION_SIZE_BYTES);
260
Victor Hsieh3051d782018-01-12 08:28:03 -0800261 // 3. Consume offset of Signing Block as an alternative EoCD.
Victor Hsieh3271d042017-10-24 15:46:32 -0700262 ByteBuffer alternativeCentralDirOffset = ByteBuffer.allocate(
263 ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE).order(ByteOrder.LITTLE_ENDIAN);
264 alternativeCentralDirOffset.putInt(Math.toIntExact(signatureInfo.apkSigningBlockOffset));
265 alternativeCentralDirOffset.flip();
266 digester.consume(alternativeCentralDirOffset);
267
268 // 4. Read from end of the Central Directory offset field in EoCD to the end of the file.
269 long offsetAfterEocdCdOffsetField =
270 eocdCdOffsetFieldPosition + ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE;
271 consumeByChunk(digester,
272 new MemoryMappedFileDataSource(apk.getFD(), offsetAfterEocdCdOffsetField,
273 apk.length() - offsetAfterEocdCdOffsetField),
274 MMAP_REGION_SIZE_BYTES);
275 digester.finish();
276
277 // 5. Fill up the rest of buffer with 0s.
278 digester.fillUpLastOutputChunk();
279 }
280
281 private static byte[] generateApkVerityTree(RandomAccessFile apk, SignatureInfo signatureInfo,
282 byte[] salt, int[] levelOffset, ByteBuffer output)
Victor Hsiehdef64f22017-11-08 10:25:11 -0800283 throws IOException, NoSuchAlgorithmException, DigestException {
Victor Hsieh3271d042017-10-24 15:46:32 -0700284 // 1. Digest the apk to generate the leaf level hashes.
285 generateApkVerityDigestAtLeafLevel(apk, signatureInfo, salt, slice(output,
286 levelOffset[levelOffset.length - 2], levelOffset[levelOffset.length - 1]));
287
288 // 2. Digest the lower level hashes bottom up.
289 for (int level = levelOffset.length - 3; level >= 0; level--) {
290 ByteBuffer inputBuffer = slice(output, levelOffset[level + 1], levelOffset[level + 2]);
291 ByteBuffer outputBuffer = slice(output, levelOffset[level], levelOffset[level + 1]);
292
293 DataSource source = new ByteBufferDataSource(inputBuffer);
294 BufferedDigester digester = new BufferedDigester(salt, outputBuffer);
295 consumeByChunk(digester, source, CHUNK_SIZE_BYTES);
296 digester.finish();
297
298 digester.fillUpLastOutputChunk();
299 }
300
301 // 3. Digest the first block (i.e. first level) to generate the root hash.
302 byte[] rootHash = new byte[DIGEST_SIZE_BYTES];
303 BufferedDigester digester = new BufferedDigester(salt, ByteBuffer.wrap(rootHash));
304 digester.consume(slice(output, 0, CHUNK_SIZE_BYTES));
Victor Hsieh4385fe42017-11-08 10:44:20 -0800305 digester.finish();
Victor Hsieh3271d042017-10-24 15:46:32 -0700306 return rootHash;
307 }
308
Victor Hsieh3051d782018-01-12 08:28:03 -0800309 private static ByteBuffer generateFsverityHeader(ByteBuffer buffer, long fileSize, int depth,
310 byte[] salt) {
Victor Hsieh3271d042017-10-24 15:46:32 -0700311 if (salt.length != 8) {
312 throw new IllegalArgumentException("salt is not 8 bytes long");
313 }
314
Victor Hsieh3051d782018-01-12 08:28:03 -0800315 // TODO(b/30972906): update the reference when there is a better one in public.
Victor Hsieh3271d042017-10-24 15:46:32 -0700316 buffer.put("TrueBrew".getBytes()); // magic
Victor Hsieh3051d782018-01-12 08:28:03 -0800317
Victor Hsieh3271d042017-10-24 15:46:32 -0700318 buffer.put((byte) 1); // major version
319 buffer.put((byte) 0); // minor version
Victor Hsieh3051d782018-01-12 08:28:03 -0800320 buffer.put((byte) 12); // log2(block-size): log2(4096)
321 buffer.put((byte) 7); // log2(leaves-per-node): log2(4096 / 32)
Victor Hsieh3271d042017-10-24 15:46:32 -0700322
Victor Hsieh3051d782018-01-12 08:28:03 -0800323 buffer.putShort((short) 1); // meta algorithm, SHA256_MODE == 1
324 buffer.putShort((short) 1); // data algorithm, SHA256_MODE == 1
325
326 buffer.putInt(0x1); // flags, 0x1: has extension
327 buffer.putInt(0); // reserved
328
329 buffer.putLong(fileSize); // original file size
330
331 buffer.put((byte) 0); // auth block offset, disabled here
332 buffer.put(salt); // salt (8 bytes)
333 // skip(buffer, 22); // reserved
Victor Hsieh3271d042017-10-24 15:46:32 -0700334
335 buffer.rewind();
336 return buffer;
337 }
338
Victor Hsieh3051d782018-01-12 08:28:03 -0800339 private static ByteBuffer generateFsverityExtensions(ByteBuffer buffer, long signingBlockOffset,
340 long signingBlockSize, long eocdOffset) {
341 // Snapshot of the FSVerity structs (subject to change once upstreamed).
342 //
343 // struct fsverity_header_extension {
344 // u8 extension_count;
345 // u8 reserved[7];
346 // };
347 //
348 // struct fsverity_extension {
349 // __le16 length;
350 // u8 type;
351 // u8 reserved[5];
352 // };
353 //
354 // struct fsverity_extension_elide {
355 // __le64 offset;
356 // __le64 length;
357 // }
358 //
359 // struct fsverity_extension_patch {
360 // __le64 offset;
361 // u8 length;
362 // u8 reserved[7];
363 // u8 databytes[];
364 // };
365
366 // struct fsverity_header_extension
367 buffer.put((byte) 2); // extension count
368 skip(buffer, 3); // reserved
369
370 final int kSizeOfFsverityExtensionHeader = 8;
371
372 {
373 // struct fsverity_extension #1
374 final int kSizeOfFsverityElidedExtension = 16;
375
376 buffer.putShort((short) // total size of extension, padded to 64-bit alignment
377 (kSizeOfFsverityExtensionHeader + kSizeOfFsverityElidedExtension));
378 buffer.put((byte) 0); // ID of elide extension
379 skip(buffer, 5); // reserved
380
381 // struct fsverity_extension_elide
382 buffer.putLong(signingBlockOffset);
383 buffer.putLong(signingBlockSize);
384 }
385
386 {
387 // struct fsverity_extension #2
388 final int kSizeOfFsverityPatchExtension =
389 8 + // offset size
390 1 + // size of length from offset (up to 255)
391 7 + // reserved
392 ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE;
393 final int kPadding = (int) divideRoundup(kSizeOfFsverityPatchExtension % 8, 8);
394
395 buffer.putShort((short) // total size of extension, padded to 64-bit alignment
396 (kSizeOfFsverityExtensionHeader + kSizeOfFsverityPatchExtension + kPadding));
397 buffer.put((byte) 1); // ID of patch extension
398 skip(buffer, 5); // reserved
399
400 // struct fsverity_extension_patch
401 buffer.putLong(eocdOffset); // offset
402 buffer.put((byte) ZIP_EOCD_CENTRAL_DIR_OFFSET_FIELD_SIZE); // length
403 skip(buffer, 7); // reserved
404 buffer.putInt(Math.toIntExact(signingBlockOffset)); // databytes
405
406 // There are extra kPadding bytes of 0s here, included in the total size field of the
407 // extension header. The output ByteBuffer is assumed to be initialized to 0.
408 }
409
410 buffer.rewind();
411 return buffer;
Victor Hsieh3271d042017-10-24 15:46:32 -0700412 }
413
414 /**
415 * Returns an array of summed area table of level size in the verity tree. In other words, the
416 * returned array is offset of each level in the verity tree file format, plus an additional
417 * offset of the next non-existing level (i.e. end of the last level + 1). Thus the array size
418 * is level + 1. Thus, the returned array is guarantee to have at least 2 elements.
419 */
420 private static int[] calculateVerityLevelOffset(long fileSize) {
421 ArrayList<Long> levelSize = new ArrayList<>();
422 while (true) {
423 long levelDigestSize = divideRoundup(fileSize, CHUNK_SIZE_BYTES) * DIGEST_SIZE_BYTES;
424 long chunksSize = CHUNK_SIZE_BYTES * divideRoundup(levelDigestSize, CHUNK_SIZE_BYTES);
425 levelSize.add(chunksSize);
426 if (levelDigestSize <= CHUNK_SIZE_BYTES) {
427 break;
428 }
429 fileSize = levelDigestSize;
430 }
431
432 // Reverse and convert to summed area table.
433 int[] levelOffset = new int[levelSize.size() + 1];
434 levelOffset[0] = 0;
435 for (int i = 0; i < levelSize.size(); i++) {
436 // We don't support verity tree if it is larger then Integer.MAX_VALUE.
437 levelOffset[i + 1] = levelOffset[i]
438 + Math.toIntExact(levelSize.get(levelSize.size() - i - 1));
439 }
440 return levelOffset;
441 }
442
443 private static void assertSigningBlockAlignedAndHasFullPages(SignatureInfo signatureInfo) {
444 if (signatureInfo.apkSigningBlockOffset % CHUNK_SIZE_BYTES != 0) {
445 throw new IllegalArgumentException(
446 "APK Signing Block does not start at the page boundary: "
447 + signatureInfo.apkSigningBlockOffset);
448 }
449
450 if ((signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset)
451 % CHUNK_SIZE_BYTES != 0) {
452 throw new IllegalArgumentException(
453 "Size of APK Signing Block is not a multiple of 4096: "
454 + (signatureInfo.centralDirOffset - signatureInfo.apkSigningBlockOffset));
455 }
456 }
457
458 /** Returns a slice of the buffer which shares content with the provided buffer. */
459 private static ByteBuffer slice(ByteBuffer buffer, int begin, int end) {
460 ByteBuffer b = buffer.duplicate();
461 b.position(0); // to ensure position <= limit invariant.
462 b.limit(end);
463 b.position(begin);
464 return b.slice();
465 }
466
Victor Hsieh3051d782018-01-12 08:28:03 -0800467 /** Skip the {@code ByteBuffer} position by {@code bytes}. */
468 private static void skip(ByteBuffer buffer, int bytes) {
469 buffer.position(buffer.position() + bytes);
470 }
471
Victor Hsieh3271d042017-10-24 15:46:32 -0700472 /** Divides a number and round up to the closest integer. */
473 private static long divideRoundup(long dividend, long divisor) {
474 return (dividend + divisor - 1) / divisor;
475 }
476}