Add new APNG renderer, just for internal users for now.

This commit is contained in:
Greyson Parrelli
2024-02-02 10:08:08 -05:00
committed by Cody Henthorne
parent 34d87cf6e1
commit c3f9e5d972
151 changed files with 2425 additions and 13 deletions

View File

@@ -0,0 +1,17 @@
/*
* Copyright 2024 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.signal.core.util
import java.nio.ByteBuffer
import java.nio.ByteOrder
fun ByteArray.toUInt(): UInt {
return ByteBuffer.wrap(this).order(ByteOrder.BIG_ENDIAN).int.toUInt()
}
fun ByteArray.toUShort(): UShort {
return ByteBuffer.wrap(this).order(ByteOrder.BIG_ENDIAN).getShort().toUShort()
}

View File

@@ -10,6 +10,7 @@ import java.io.ByteArrayOutputStream
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
import kotlin.jvm.Throws
import kotlin.math.min
/**
@@ -107,6 +108,13 @@ fun InputStream.readLength(): Long {
return count
}
/**
* Reads a 32-bit unsigned integer from the stream.
*/
fun InputStream.readUInt(): UInt {
return this.readNBytesOrThrow(4).toUInt()
}
/**
* Reads the contents of the stream and discards them.
*/

View File

@@ -6,6 +6,8 @@
package org.signal.core.util
import java.io.OutputStream
import java.nio.ByteBuffer
import java.nio.ByteOrder
/**
* Writes a 32-bit variable-length integer to the stream.
@@ -30,3 +32,11 @@ fun OutputStream.writeVarInt32(value: Int) {
}
}
}
/**
* Writes a 32-bit unsigned integer to the stream.
*/
fun OutputStream.writeUInt(value: UInt) {
// Note that casting to an int here is fine, because at the end of the day, we're just writing 4 bytes to the stream
this.write(ByteBuffer.allocate(4).order(ByteOrder.BIG_ENDIAN).putInt(value.toInt()).array())
}

View File

@@ -0,0 +1,34 @@
/*
* Copyright 2024 Signal Messenger, LLC
* SPDX-License-Identifier: AGPL-3.0-only
*/
package org.signal.core.util.stream
import java.io.FilterOutputStream
import java.io.OutputStream
import java.util.zip.CRC32
/**
* A simple pass-through stream that calculates a CRC32 as data is written to the target [OutputStream].
*/
class Crc32OutputStream(private val wrapped: OutputStream) : FilterOutputStream(wrapped) {
private val crc32 = CRC32()
val currentCrc32: Long
get() = crc32.value
override fun write(byte: Int) {
wrapped.write(byte)
crc32.update(byte)
}
override fun write(data: ByteArray) {
write(data, 0, data.size)
}
override fun write(data: ByteArray, offset: Int, length: Int) {
wrapped.write(data, offset, length)
crc32.update(data, offset, length)
}
}