avhuff.cpp, cdrom.cpp, msdib.cpp, unzip.cpp, vbiparse.cpp: Use multibyte.h functions

This commit is contained in:
AJR 2023-09-17 16:17:07 -04:00
parent 245282f4b2
commit 7aca06fb13
5 changed files with 71 additions and 124 deletions

View File

@ -58,11 +58,11 @@
***************************************************************************/
#include <cassert>
#include "avhuff.h"
#include "huffman.h"
#include "chd.h"
#include "huffman.h"
#include "multibyte.h"
#include <cmath>
#include <cstdlib>
@ -211,20 +211,17 @@ avhuff_error avhuff_encoder::encode_data(const uint8_t *source, uint8_t *dest, u
// extract info from the header
uint32_t metasize = source[4];
uint32_t channels = source[5];
uint32_t samples = (source[6] << 8) + source[7];
uint32_t width = (source[8] << 8) + source[9];
uint32_t height = (source[10] << 8) + source[11];
uint32_t samples = get_u16be(&source[6]);
uint32_t width = get_u16be(&source[8]);
uint32_t height = get_u16be(&source[10]);
source += 12;
// write the basics to the new header
dest[0] = metasize;
dest[1] = channels;
dest[2] = samples >> 8;
dest[3] = samples;
dest[4] = width >> 8;
dest[5] = width;
dest[6] = height >> 8;
dest[7] = height;
put_u16be(&dest[2], samples);
put_u16be(&dest[4], width);
put_u16be(&dest[6], height);
// starting offsets
uint32_t dstoffs = 10 + 2 * channels;
@ -247,11 +244,11 @@ avhuff_error avhuff_encoder::encode_data(const uint8_t *source, uint8_t *dest, u
return err;
// advance the pointers past the data
uint16_t treesize = (dest[8] << 8) + dest[9];
uint16_t treesize = get_u16be(&dest[8]);
if (treesize != 0xffff)
dstoffs += treesize;
for (int chnum = 0; chnum < channels; chnum++)
dstoffs += (dest[10 + 2 * chnum] << 8) + dest[11 + 2 * chnum];
dstoffs += get_u16be(&dest[10 + 2 * chnum]);
}
else
{
@ -299,10 +296,10 @@ uint32_t avhuff_encoder::raw_data_size(const uint8_t *data)
size = 12 + data[4];
// add in channels * samples
size += 2 * data[5] * ((data[6] << 8) + data[7]);
size += 2 * data[5] * get_u16be(&data[6]);
// add in 2 * width * height
size += 2 * ((data[8] << 8) + data[9]) * (((data[10] << 8) + data[11]) & 0x7fff);
size += 2 * get_u16be(&data[8]) * (get_u16be(&data[10]) & 0x7fff);
}
return size;
}
@ -344,12 +341,12 @@ avhuff_error avhuff_encoder::assemble_data(std::vector<uint8_t> &buffer, bitmap_
*dest++ = 'v';
*dest++ = metadatasize;
*dest++ = channels;
*dest++ = numsamples >> 8;
*dest++ = numsamples & 0xff;
*dest++ = bitmap.width() >> 8;
*dest++ = bitmap.width() & 0xff;
*dest++ = bitmap.height() >> 8;
*dest++ = bitmap.height() & 0xff;
put_u16be(dest, numsamples);
dest += 2;
put_u16be(dest, bitmap.width());
dest += 2;
put_u16be(dest, bitmap.height());
dest += 2;
// copy the metadata
if (metadatasize > 0)
@ -360,8 +357,8 @@ avhuff_error avhuff_encoder::assemble_data(std::vector<uint8_t> &buffer, bitmap_
for (uint8_t curchan = 0; curchan < channels; curchan++)
for (uint32_t cursamp = 0; cursamp < numsamples; cursamp++)
{
*dest++ = samples[curchan][cursamp] >> 8;
*dest++ = samples[curchan][cursamp] & 0xff;
put_u16be(dest, samples[curchan][cursamp]);
dest += 2;
}
// copy the video data
@ -370,8 +367,8 @@ avhuff_error avhuff_encoder::assemble_data(std::vector<uint8_t> &buffer, bitmap_
uint16_t *src = &bitmap.pix(y);
for (int32_t x = 0; x < bitmap.width(); x++)
{
*dest++ = src[x] >> 8;
*dest++ = src[x] & 0xff;
put_u16be(dest, src[x]);
dest += 2;
}
}
return AVHERR_NONE;
@ -417,8 +414,7 @@ avhuff_error avhuff_encoder::encode_audio(const uint8_t *source, int channels, i
// set the size for this channel
uint32_t cursize = m_flac_encoder.finish();
sizes[chnum * 2 + 2] = cursize >> 8;
sizes[chnum * 2 + 3] = cursize;
put_u16be(&sizes[chnum * 2 + 2], cursize);
dest += cursize;
}
@ -437,7 +433,7 @@ avhuff_error avhuff_encoder::encode_audio(const uint8_t *source, int channels, i
int16_t prevsample = 0;
for (int sampnum = 0; sampnum < samples; sampnum++)
{
int16_t newsample = (source[0] << 8) | source[1];
int16_t newsample = get_s16be(source);
source += 2;
int16_t delta = newsample - prevsample;
@ -467,8 +463,7 @@ avhuff_error avhuff_encoder::encode_audio(const uint8_t *source, int channels, i
// note the size of the two trees
uint32_t huffsize = bitbuf.flush();
sizes[0] = huffsize >> 8;
sizes[1] = huffsize;
put_u16be(&sizes[0], huffsize);
// iterate over channels
uint32_t totalsize = huffsize;
@ -488,8 +483,7 @@ avhuff_error avhuff_encoder::encode_audio(const uint8_t *source, int channels, i
totalsize += cursize;
if (totalsize >= channels * samples * 2)
break;
sizes[chnum * 2 + 2] = cursize >> 8;
sizes[chnum * 2 + 3] = cursize;
put_u16be(&sizes[chnum * 2 + 2], cursize);
}
// if we ran out of room, throw it all away and just store raw
@ -499,10 +493,7 @@ avhuff_error avhuff_encoder::encode_audio(const uint8_t *source, int channels, i
uint32_t size = samples * 2;
sizes[0] = sizes[1] = 0;
for (chnum = 0; chnum < channels; chnum++)
{
sizes[chnum * 2 + 2] = size >> 8;
sizes[chnum * 2 + 3] = size;
}
put_u16be(&sizes[chnum * 2 + 2], size);
}
#endif
@ -727,19 +718,19 @@ avhuff_error avhuff_decoder::decode_data(const uint8_t *source, uint32_t complen
return AVHERR_INVALID_DATA;
uint32_t metasize = source[0];
uint32_t channels = source[1];
uint32_t samples = (source[2] << 8) + source[3];
uint32_t width = (source[4] << 8) + source[5];
uint32_t height = (source[6] << 8) + source[7];
uint32_t samples = get_u16be(&source[2]);
uint32_t width = get_u16be(&source[4]);
uint32_t height = get_u16be(&source[6]);
// validate that the sizes make sense
if (complength < 10 + 2 * channels)
return AVHERR_INVALID_DATA;
uint32_t totalsize = 10 + 2 * channels;
uint32_t treesize = (source[8] << 8) | source[9];
uint32_t treesize = get_u16be(&source[8]);
if (treesize != 0xffff)
totalsize += treesize;
for (int chnum = 0; chnum < channels; chnum++)
totalsize += (source[10 + 2 * chnum] << 8) | source[11 + 2 * chnum];
totalsize += get_u16be(&source[10 + 2 * chnum]);
if (totalsize >= complength)
return AVHERR_INVALID_DATA;
@ -758,12 +749,9 @@ avhuff_error avhuff_decoder::decode_data(const uint8_t *source, uint32_t complen
dest[3] = 'v';
dest[4] = metasize;
dest[5] = channels;
dest[6] = samples >> 8;
dest[7] = samples;
dest[8] = width >> 8;
dest[9] = width;
dest[10] = height >> 8;
dest[11] = height;
put_u16be(&dest[6], samples);
put_u16be(&dest[8], width);
put_u16be(&dest[10], height);
dest += 12;
// determine the start of each piece of data
@ -829,11 +817,11 @@ avhuff_error avhuff_decoder::decode_data(const uint8_t *source, uint32_t complen
return err;
// advance the pointers past the data
treesize = (source[8] << 8) + source[9];
treesize = get_u16be(&source[8]);
if (treesize != 0xffff)
srcoffs += treesize;
for (int chnum = 0; chnum < channels; chnum++)
srcoffs += (source[10 + 2 * chnum] << 8) + source[11 + 2 * chnum];
srcoffs += get_u16be(&source[10 + 2 * chnum]);
}
// decode the video data
@ -870,7 +858,7 @@ avhuff_error avhuff_decoder::decode_data(const uint8_t *source, uint32_t complen
avhuff_error avhuff_decoder::decode_audio(int channels, int samples, const uint8_t *source, uint8_t **dest, uint32_t dxor, const uint8_t *sizes)
{
// extract the huffman trees
uint16_t treesize = (sizes[0] << 8) | sizes[1];
uint16_t treesize = get_u16be(&sizes[0]);
#if AVHUFF_USE_FLAC
@ -888,7 +876,7 @@ avhuff_error avhuff_decoder::decode_audio(int channels, int samples, const uint8
for (int chnum = 0; chnum < channels; chnum++)
{
// extract the size of this channel
uint16_t size = (sizes[chnum * 2 + 2] << 8) | sizes[chnum * 2 + 3];
uint16_t size = get_u16be(&sizes[chnum * 2 + 2]);
// only process if the data is requested
uint8_t *curdest = dest[chnum];
@ -932,7 +920,7 @@ avhuff_error avhuff_decoder::decode_audio(int channels, int samples, const uint8
for (int chnum = 0; chnum < channels; chnum++)
{
// extract the size of this channel
uint16_t size = (sizes[chnum * 2 + 2] << 8) | sizes[chnum * 2 + 3];
uint16_t size = get_u16be(&sizes[chnum * 2 + 2]);
// only process if the data is requested
uint8_t *curdest = dest[chnum];
@ -946,7 +934,7 @@ avhuff_error avhuff_decoder::decode_audio(int channels, int samples, const uint8
const uint8_t *cursource = source;
for (int sampnum = 0; sampnum < samples; sampnum++)
{
int16_t delta = (cursource[0] << 8) | cursource[1];
int16_t delta = get_s16be(cursource);
cursource += 2;
int16_t newsample = prevsample + delta;

View File

@ -19,6 +19,7 @@
#include "cdrom.h"
#include "corestr.h"
#include "multibyte.h"
#include "osdfile.h"
#include "strformat.h"
@ -502,9 +503,7 @@ bool cdrom_file::read_data(uint32_t lbasector, void *buffer, uint32_t datatype,
static const uint8_t syncbytes[12] = {0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00};
memcpy(bufptr, syncbytes, 12);
bufptr[12] = msf>>16;
bufptr[13] = msf>>8;
bufptr[14] = msf&0xff;
put_u24be(&bufptr[12], msf);
bufptr[15] = 1; // mode 1
LOG(("CDROM: promotion of mode1/form1 sector to mode1 raw is not complete!\n"));
return !read_partial_sector(bufptr+16, lbasector, chdsector, tracknum, 0, 2048, phys);
@ -1778,14 +1777,11 @@ uint32_t cdrom_file::parse_wav_sample(std::string_view filename, uint32_t *datao
uint16_t cdrom_file::read_uint16(FILE *infile)
{
uint16_t res = 0;
unsigned char buffer[2];
fread(buffer, 2, 1, infile);
res = buffer[1] | buffer[0]<<8;
return res;
return get_u16be(buffer);
}
/**
@ -1800,14 +1796,11 @@ uint16_t cdrom_file::read_uint16(FILE *infile)
uint32_t cdrom_file::read_uint32(FILE *infile)
{
uint32_t res = 0;
unsigned char buffer[4];
fread(buffer, 4, 1, infile);
res = buffer[3] | buffer[2]<<8 | buffer[1]<<16 | buffer[0]<<24;
return res;
return get_u32be(buffer);
}
/**
@ -1822,18 +1815,11 @@ uint32_t cdrom_file::read_uint32(FILE *infile)
uint64_t cdrom_file::read_uint64(FILE *infile)
{
uint64_t res0(0), res1(0);
uint64_t res;
unsigned char buffer[8];
fread(buffer, 8, 1, infile);
res0 = buffer[3] | buffer[2]<<8 | buffer[1]<<16 | buffer[0]<<24;
res1 = buffer[7] | buffer[6]<<8 | buffer[5]<<16 | buffer[4]<<24;
res = res0<<32 | res1;
return res;
return get_u64be(buffer);
}
/*-------------------------------------------------
@ -1883,7 +1869,7 @@ std::error_condition cdrom_file::parse_nero(std::string_view tocfname, toc &outt
return chd_file::error::UNSUPPORTED_FORMAT;
}
chain_offs = buffer[11] | (buffer[10]<<8) | (buffer[9]<<16) | (buffer[8]<<24);
chain_offs = get_u32be(&buffer[8]);
if ((buffer[7] != 0) || (buffer[6] != 0) || (buffer[5] != 0) || (buffer[4] != 0))
{
@ -1899,7 +1885,7 @@ std::error_condition cdrom_file::parse_nero(std::string_view tocfname, toc &outt
fseek(infile, chain_offs, SEEK_SET);
fread(buffer, 8, 1, infile);
chunk_size = (buffer[7] | buffer[6]<<8 | buffer[5]<<16 | buffer[4]<<24);
chunk_size = get_u32be(&buffer[4]);
// printf("Chunk type: %c%c%c%c, size %x\n", buffer[0], buffer[1], buffer[2], buffer[3], chunk_size);

View File

@ -12,6 +12,7 @@
#include "coretmpl.h"
#include "ioprocs.h"
#include "multibyte.h"
#include "eminline.h"
#include "osdcore.h"
@ -141,20 +142,12 @@ msdib_error dib_read_file_header(read_stream &fp, std::uint32_t &filelen) noexce
return msdib_error::BAD_SIGNATURE;
// do a very basic check on the file length
std::uint32_t const file_length(
(std::uint32_t(file_header[2]) << 0) |
(std::uint32_t(file_header[3]) << 8) |
(std::uint32_t(file_header[4]) << 16) |
(std::uint32_t(file_header[5]) << 24));
std::uint32_t const file_length(get_u32le(&file_header[2]));
if ((sizeof(file_header) + sizeof(bitmap_core_header)) > file_length)
return msdib_error::FILE_CORRUPT;
// check that the offset to the pixel data looks half sane
std::uint32_t const pixel_offset(
(std::uint32_t(file_header[10]) << 0) |
(std::uint32_t(file_header[11]) << 8) |
(std::uint32_t(file_header[12]) << 16) |
(std::uint32_t(file_header[13]) << 24));
std::uint32_t const pixel_offset(get_u32le(&file_header[10]));
if (((sizeof(file_header) + sizeof(bitmap_core_header)) > pixel_offset) || (file_length < pixel_offset))
return msdib_error::FILE_CORRUPT;

View File

@ -2,7 +2,7 @@
// copyright-holders:Aaron Giles, Vas Crabb
/***************************************************************************
unzip.c
unzip.cpp
Functions to manipulate data within ZIP files.
@ -13,6 +13,7 @@
#include "corestr.h"
#include "hashing.h"
#include "ioprocs.h"
#include "multibyte.h"
#include "timeconv.h"
#include "osdcore.h"
@ -393,29 +394,15 @@ protected:
}
std::uint16_t read_word(std::size_t offs) const noexcept
{
return
(std::uint16_t(m_buffer[offs + 1]) << 8) |
(std::uint16_t(m_buffer[offs + 0]) << 0);
return get_u16le(&m_buffer[offs]);
}
std::uint32_t read_dword(std::size_t offs) const noexcept
{
return
(std::uint32_t(m_buffer[offs + 3]) << 24) |
(std::uint32_t(m_buffer[offs + 2]) << 16) |
(std::uint32_t(m_buffer[offs + 1]) << 8) |
(std::uint32_t(m_buffer[offs + 0]) << 0);
return get_u32le(&m_buffer[offs]);
}
std::uint64_t read_qword(std::size_t offs) const noexcept
{
return
(std::uint64_t(m_buffer[offs + 7]) << 56) |
(std::uint64_t(m_buffer[offs + 6]) << 48) |
(std::uint64_t(m_buffer[offs + 5]) << 40) |
(std::uint64_t(m_buffer[offs + 4]) << 32) |
(std::uint64_t(m_buffer[offs + 3]) << 24) |
(std::uint64_t(m_buffer[offs + 2]) << 16) |
(std::uint64_t(m_buffer[offs + 1]) << 8) |
(std::uint64_t(m_buffer[offs + 0]) << 0);
return get_u64le(&m_buffer[offs]);
}
std::string read_string(std::size_t offs, std::string::size_type len) const
{
@ -1418,7 +1405,7 @@ std::error_condition zip_file_impl::decompress_data_type_14(std::uint64_t offset
m_header.file_name, m_filename);
return archive_file::error::FILE_TRUNCATED;
}
std::uint16_t const props_size((std::uint16_t(m_buffer[3]) << 8) | std::uint16_t(m_buffer[2]));
std::uint16_t const props_size(get_u16le(&m_buffer[2]));
if (props_size > m_buffer.size())
{
osd_printf_error(

View File

@ -9,6 +9,9 @@
***************************************************************************/
#include "vbiparse.h"
#include "multibyte.h"
#include <cmath>
#include <cstdio>
#include <cstring>
@ -363,22 +366,12 @@ void vbi_parse_all(const uint16_t *source, int sourcerowpixels, int sourcewidth,
void vbi_metadata_pack(uint8_t *dest, uint32_t framenum, const vbi_metadata *vbi)
{
dest[0] = framenum >> 16;
dest[1] = framenum >> 8;
dest[2] = framenum >> 0;
put_u24be(&dest[0], framenum);
dest[3] = vbi->white;
dest[4] = vbi->line16 >> 16;
dest[5] = vbi->line16 >> 8;
dest[6] = vbi->line16 >> 0;
dest[7] = vbi->line17 >> 16;
dest[8] = vbi->line17 >> 8;
dest[9] = vbi->line17 >> 0;
dest[10] = vbi->line18 >> 16;
dest[11] = vbi->line18 >> 8;
dest[12] = vbi->line18 >> 0;
dest[13] = vbi->line1718 >> 16;
dest[14] = vbi->line1718 >> 8;
dest[15] = vbi->line1718 >> 0;
put_u24be(&dest[4], vbi->line16);
put_u24be(&dest[7], vbi->line17);
put_u24be(&dest[10], vbi->line18);
put_u24be(&dest[13], vbi->line1718);
}
@ -400,10 +393,10 @@ void vbi_metadata_pack(uint8_t *dest, uint32_t framenum, const vbi_metadata *vbi
void vbi_metadata_unpack(vbi_metadata *vbi, uint32_t *framenum, const uint8_t *source)
{
if (framenum != nullptr)
*framenum = (source[0] << 16) | (source[1] << 8) | source[2];
*framenum = get_u24be(&source[0]);
vbi->white = source[3];
vbi->line16 = (source[4] << 16) | (source[5] << 8) | source[6];
vbi->line17 = (source[7] << 16) | (source[8] << 8) | source[9];
vbi->line18 = (source[10] << 16) | (source[11] << 8) | source[12];
vbi->line1718 = (source[13] << 16) | (source[14] << 8) | source[15];
vbi->line16 = get_u24be(&source[4]);
vbi->line17 = get_u24be(&source[7]);
vbi->line18 = get_u24be(&source[10]);
vbi->line1718 = get_u24be(&source[13]);
}