diff options
| author | Anghelo Carvajal <angheloalf95@gmail.com> | 2022-01-20 23:05:23 -0300 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2022-01-20 21:05:23 -0500 |
| commit | be71e26d9afc470a9293e74a6f56ca87f7412be0 (patch) | |
| tree | ebcaf100b26c91ef492bb139c0ee579f09a28c9e /ZAPD/ZTexture.cpp | |
| parent | 0ba78130478ee1272bc0e2f2fec2d162e7f7f995 (diff) | |
Fix out-of-bounds reading when converting textures to C (#236)
* Fill buffer to a multiple of 8 bytes
* Remove overloaded bitconverter methods and use boundary-checker .at method when possible
* Add a warning in oob reads in the bitconverter
* Remove repeated code
* format
* Whoops
* whoops++
Diffstat (limited to 'ZAPD/ZTexture.cpp')
| -rw-r--r-- | ZAPD/ZTexture.cpp | 107 |
1 files changed, 26 insertions, 81 deletions
diff --git a/ZAPD/ZTexture.cpp b/ZAPD/ZTexture.cpp index 255d308..a9cb539 100644 --- a/ZAPD/ZTexture.cpp +++ b/ZAPD/ZTexture.cpp @@ -350,34 +350,42 @@ void ZTexture::DeclareReferences([[maybe_unused]] const std::string& prefix) void ZTexture::PrepareRawDataFromFile(const fs::path& pngFilePath) { + textureData.ReadPng(pngFilePath); + + width = textureData.GetWidth(); + height = textureData.GetHeight(); + + textureDataRaw.clear(); + textureDataRaw.resize(ALIGN8(GetRawDataSize())); + switch (format) { case TextureType::RGBA16bpp: - PrepareRawDataRGBA16(pngFilePath); + PrepareRawDataRGBA16(); break; case TextureType::RGBA32bpp: - PrepareRawDataRGBA32(pngFilePath); + PrepareRawDataRGBA32(); break; case TextureType::Grayscale4bpp: - PrepareRawDataGrayscale4(pngFilePath); + PrepareRawDataGrayscale4(); break; case TextureType::Grayscale8bpp: - PrepareRawDataGrayscale8(pngFilePath); + PrepareRawDataGrayscale8(); break; case TextureType::GrayscaleAlpha4bpp: - PrepareRawDataGrayscaleAlpha4(pngFilePath); + PrepareRawDataGrayscaleAlpha4(); break; case TextureType::GrayscaleAlpha8bpp: - PrepareRawDataGrayscaleAlpha8(pngFilePath); + PrepareRawDataGrayscaleAlpha8(); break; case TextureType::GrayscaleAlpha16bpp: - PrepareRawDataGrayscaleAlpha16(pngFilePath); + PrepareRawDataGrayscaleAlpha16(); break; case TextureType::Palette4bpp: - PrepareRawDataPalette4(pngFilePath); + PrepareRawDataPalette4(); break; case TextureType::Palette8bpp: - PrepareRawDataPalette8(pngFilePath); + PrepareRawDataPalette8(); break; case TextureType::Error: HANDLE_ERROR_PROCESS(WarningType::InvalidPNG, "Input PNG file has invalid format type", ""); @@ -385,15 +393,8 @@ void ZTexture::PrepareRawDataFromFile(const fs::path& pngFilePath) } } -void ZTexture::PrepareRawDataRGBA16(const fs::path& rgbaPath) +void ZTexture::PrepareRawDataRGBA16() { - textureData.ReadPng(rgbaPath); - - width = textureData.GetWidth(); - height = textureData.GetHeight(); - - textureDataRaw.clear(); - textureDataRaw.resize(GetRawDataSize()); for (uint16_t y = 0; y < height; y++) { for (uint16_t x = 0; x < width; x++) @@ -415,15 +416,8 @@ void ZTexture::PrepareRawDataRGBA16(const fs::path& rgbaPath) } } -void ZTexture::PrepareRawDataRGBA32(const fs::path& rgbaPath) +void ZTexture::PrepareRawDataRGBA32() { - textureData.ReadPng(rgbaPath); - - width = textureData.GetWidth(); - height = textureData.GetHeight(); - - textureDataRaw.clear(); - textureDataRaw.resize(GetRawDataSize()); for (uint16_t y = 0; y < height; y++) { for (uint16_t x = 0; x < width; x++) @@ -439,15 +433,8 @@ void ZTexture::PrepareRawDataRGBA32(const fs::path& rgbaPath) } } -void ZTexture::PrepareRawDataGrayscale4(const fs::path& grayPath) +void ZTexture::PrepareRawDataGrayscale4() { - textureData.ReadPng(grayPath); - - width = textureData.GetWidth(); - height = textureData.GetHeight(); - - textureDataRaw.clear(); - textureDataRaw.resize(GetRawDataSize()); for (uint16_t y = 0; y < height; y++) { for (uint16_t x = 0; x < width; x += 2) @@ -461,15 +448,8 @@ void ZTexture::PrepareRawDataGrayscale4(const fs::path& grayPath) } } -void ZTexture::PrepareRawDataGrayscale8(const fs::path& grayPath) +void ZTexture::PrepareRawDataGrayscale8() { - textureData.ReadPng(grayPath); - - width = textureData.GetWidth(); - height = textureData.GetHeight(); - - textureDataRaw.clear(); - textureDataRaw.resize(GetRawDataSize()); for (uint16_t y = 0; y < height; y++) { for (uint16_t x = 0; x < width; x++) @@ -481,15 +461,8 @@ void ZTexture::PrepareRawDataGrayscale8(const fs::path& grayPath) } } -void ZTexture::PrepareRawDataGrayscaleAlpha4(const fs::path& grayAlphaPath) +void ZTexture::PrepareRawDataGrayscaleAlpha4() { - textureData.ReadPng(grayAlphaPath); - - width = textureData.GetWidth(); - height = textureData.GetHeight(); - - textureDataRaw.clear(); - textureDataRaw.resize(GetRawDataSize()); for (uint16_t y = 0; y < height; y++) { for (uint16_t x = 0; x < width; x += 2) @@ -514,15 +487,8 @@ void ZTexture::PrepareRawDataGrayscaleAlpha4(const fs::path& grayAlphaPath) } } -void ZTexture::PrepareRawDataGrayscaleAlpha8(const fs::path& grayAlphaPath) +void ZTexture::PrepareRawDataGrayscaleAlpha8() { - textureData.ReadPng(grayAlphaPath); - - width = textureData.GetWidth(); - height = textureData.GetHeight(); - - textureDataRaw.clear(); - textureDataRaw.resize(GetRawDataSize()); for (uint16_t y = 0; y < height; y++) { for (uint16_t x = 0; x < width; x++) @@ -538,15 +504,8 @@ void ZTexture::PrepareRawDataGrayscaleAlpha8(const fs::path& grayAlphaPath) } } -void ZTexture::PrepareRawDataGrayscaleAlpha16(const fs::path& grayAlphaPath) +void ZTexture::PrepareRawDataGrayscaleAlpha16() { - textureData.ReadPng(grayAlphaPath); - - width = textureData.GetWidth(); - height = textureData.GetHeight(); - - textureDataRaw.clear(); - textureDataRaw.resize(GetRawDataSize()); for (uint16_t y = 0; y < height; y++) { for (uint16_t x = 0; x < width; x++) @@ -563,15 +522,8 @@ void ZTexture::PrepareRawDataGrayscaleAlpha16(const fs::path& grayAlphaPath) } } -void ZTexture::PrepareRawDataPalette4(const fs::path& palPath) +void ZTexture::PrepareRawDataPalette4() { - textureData.ReadPng(palPath); - - width = textureData.GetWidth(); - height = textureData.GetHeight(); - - textureDataRaw.clear(); - textureDataRaw.resize(GetRawDataSize()); for (uint16_t y = 0; y < height; y++) { for (uint16_t x = 0; x < width; x += 2) @@ -586,15 +538,8 @@ void ZTexture::PrepareRawDataPalette4(const fs::path& palPath) } } -void ZTexture::PrepareRawDataPalette8(const fs::path& palPath) +void ZTexture::PrepareRawDataPalette8() { - textureData.ReadPng(palPath); - - width = textureData.GetWidth(); - height = textureData.GetHeight(); - - textureDataRaw.clear(); - textureDataRaw.resize(GetRawDataSize()); for (uint16_t y = 0; y < height; y++) { for (uint16_t x = 0; x < width; x++) |
