Complete batch 20261016-C-embedded-2: 45 verified GLM/Pi samples, independent regrades and archives; preserve difficulty misses and Salty blockage.
153 lines
3.6 KiB
C
153 lines
3.6 KiB
C
#include "image.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
uint16_t codec_u16(const uint8_t *p)
|
|
{
|
|
uint16_t low = p[0];
|
|
uint16_t high = p[1];
|
|
return (uint16_t)(low | (uint16_t)(high << 8));
|
|
}
|
|
|
|
uint32_t codec_u32(const uint8_t *p)
|
|
{
|
|
uint32_t value = 0;
|
|
for (unsigned index = 0; index < 4; ++index) {
|
|
value |= (uint32_t)p[index] << (index * 8);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
void codec_put16(uint8_t *p, uint16_t value)
|
|
{
|
|
p[0] = (uint8_t)value;
|
|
p[1] = (uint8_t)(value >> 8);
|
|
}
|
|
|
|
void codec_put32(uint8_t *p, uint32_t value)
|
|
{
|
|
for (unsigned index = 0; index < 4; ++index) {
|
|
p[index] = (uint8_t)(value >> (index * 8));
|
|
}
|
|
}
|
|
|
|
static uint32_t crc_byte(uint32_t crc, uint8_t value)
|
|
{
|
|
crc ^= value;
|
|
for (unsigned bit = 0; bit < 8; ++bit) {
|
|
uint32_t mask = 0u - (crc & 1u);
|
|
crc = (crc >> 1) ^ (0xedb88320u & mask);
|
|
}
|
|
return crc;
|
|
}
|
|
|
|
uint32_t codec_crc32(const uint8_t *bytes, size_t count)
|
|
{
|
|
uint32_t crc = 0xffffffffu;
|
|
for (size_t index = 0; index < count; ++index) {
|
|
crc = crc_byte(crc, bytes[index]);
|
|
}
|
|
return crc ^ 0xffffffffu;
|
|
}
|
|
|
|
uint32_t codec_header_crc(const uint8_t *bytes, size_t count)
|
|
{
|
|
uint32_t crc = 0xffffffffu;
|
|
for (size_t index = 0; index < count; ++index) {
|
|
uint8_t value = bytes[index];
|
|
if (index >= 28 && index < 32) {
|
|
value = 0;
|
|
}
|
|
crc = crc_byte(crc, value);
|
|
}
|
|
return crc ^ 0xffffffffu;
|
|
}
|
|
|
|
int codec_magic(const uint8_t *bytes, size_t count)
|
|
{
|
|
static const uint8_t magic[] = {'F', 'W', 'I', '2'};
|
|
if (count < sizeof(magic)) {
|
|
return 0;
|
|
}
|
|
return memcmp(bytes, magic, sizeof(magic)) == 0;
|
|
}
|
|
|
|
size_t codec_magic_suffix(const uint8_t *bytes, size_t count)
|
|
{
|
|
static const uint8_t magic[] = {'F', 'W', 'I', '2'};
|
|
size_t keep = count < 3 ? count : 3;
|
|
while (keep > 0) {
|
|
if (memcmp(bytes + count - keep, magic, keep) == 0) {
|
|
return keep;
|
|
}
|
|
--keep;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
int codec_read_bytes(uint8_t *bytes, size_t count)
|
|
{
|
|
for (size_t index = 0; index < count; ++index) {
|
|
unsigned value;
|
|
if (scanf("%u", &value) != 1 || value > 255) {
|
|
return 0;
|
|
}
|
|
bytes[index] = (uint8_t)value;
|
|
}
|
|
return 1;
|
|
}
|
|
|
|
void codec_print_bytes(const uint8_t *bytes, size_t count)
|
|
{
|
|
if (count == 0) {
|
|
putchar('-');
|
|
return;
|
|
}
|
|
for (size_t index = 0; index < count; ++index) {
|
|
printf("%02x", bytes[index]);
|
|
}
|
|
}
|
|
|
|
int codec_hex_digit(unsigned char value)
|
|
{
|
|
if (value >= '0' && value <= '9') {
|
|
return value - '0';
|
|
}
|
|
if (value >= 'a' && value <= 'f') {
|
|
return value - 'a' + 10;
|
|
}
|
|
if (value >= 'A' && value <= 'F') {
|
|
return value - 'A' + 10;
|
|
}
|
|
return -1;
|
|
}
|
|
|
|
int codec_decode_hex(const char *text, uint8_t *bytes, size_t cap,
|
|
size_t *written)
|
|
{
|
|
size_t length = strlen(text);
|
|
if ((length & 1u) != 0 || length / 2 > cap) {
|
|
return 0;
|
|
}
|
|
for (size_t index = 0; index < length / 2; ++index) {
|
|
int high = codec_hex_digit((unsigned char)text[index * 2]);
|
|
int low = codec_hex_digit((unsigned char)text[index * 2 + 1]);
|
|
if (high < 0 || low < 0) {
|
|
return 0;
|
|
}
|
|
bytes[index] = (uint8_t)((high << 4) | low);
|
|
}
|
|
*written = length / 2;
|
|
return 1;
|
|
}
|
|
|
|
void codec_encode_hex(const uint8_t *bytes, size_t count, char *text)
|
|
{
|
|
static const char digits[] = "0123456789abcdef";
|
|
for (size_t index = 0; index < count; ++index) {
|
|
text[index * 2] = digits[bytes[index] >> 4];
|
|
text[index * 2 + 1] = digits[bytes[index] & 15u];
|
|
}
|
|
text[count * 2] = 0;
|
|
}
|