Skip to content

Commit

Permalink
refactor, cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
farindk committed Oct 14, 2024
1 parent a8d7348 commit b370e22
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 86 deletions.
2 changes: 1 addition & 1 deletion libheif/api/libheif/heif.cc
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
#include <cassert>


const struct heif_error heif_error_success = {heif_error_Ok, heif_suberror_Unspecified, kSuccess};
const struct heif_error heif_error_success = {heif_error_Ok, heif_suberror_Unspecified, Error::kSuccess};
static struct heif_error error_unsupported_parameter = {heif_error_Usage_error,
heif_suberror_Unsupported_parameter,
"Unsupported encoder parameter"};
Expand Down
3 changes: 2 additions & 1 deletion libheif/codecs/uncompressed/unc_codec.cc
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <map>
#include <iostream>
#include <cassert>
#include "security_limits.h"


bool isKnownUncompressedFrameConfigurationBoxProfile(const std::shared_ptr<const Box_uncC>& uncC)
Expand Down Expand Up @@ -602,7 +603,7 @@ Error UncompressedImageCodec::decode_uncompressed_image(const HeifContext* conte
assert(ispe);
uint32_t width = ispe->get_width();
uint32_t height = ispe->get_height();
error = context->check_resolution(width, height);
error = check_for_valid_image_size(context->get_security_limits(), width, height);
if (error) {
return error;
}
Expand Down
30 changes: 2 additions & 28 deletions libheif/context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ struct heif_error heif_encoder::alloc()
return error;
}

struct heif_error err = {heif_error_Ok, heif_suberror_Unspecified, kSuccess};
struct heif_error err = {heif_error_Ok, heif_suberror_Unspecified, Error::kSuccess};
return err;
}

Expand Down Expand Up @@ -185,32 +185,6 @@ void HeifContext::reset_to_empty_heif()
m_primary_image.reset();
}

Error HeifContext::check_resolution(uint32_t width, uint32_t height) const {

// TODO: remove this. Has been moved to ImageItem::check_for_valid_image_size()

// --- check whether the image size is "too large"
uint32_t max_width_height = static_cast<uint32_t>(std::numeric_limits<int>::max());
if ((width > max_width_height || height > max_width_height) ||
(height != 0 && width > get_security_limits()->max_image_size_pixels / height)) {
std::stringstream sstr;
sstr << "Image size " << width << "x" << height << " exceeds the maximum image size "
<< get_security_limits()->max_image_size_pixels << "\n";

return Error(heif_error_Memory_allocation_error,
heif_suberror_Security_limit_exceeded,
sstr.str());
}

if (width==0 || height==0) {
return Error(heif_error_Memory_allocation_error,
heif_suberror_Invalid_image_size,
"zero width or height");
}

return Error::Ok;
}


std::vector<std::shared_ptr<ImageItem>> HeifContext::get_top_level_images(bool return_error_images)
{
Expand Down Expand Up @@ -1201,7 +1175,7 @@ Error HeifContext::encode_image(const std::shared_ptr<HeifPixelImage>& pixel_ima

out_image = image_item;

insert_new_image(image_item->get_id(), image_item);
insert_image_item(image_item->get_id(), image_item);


// --- if there is an alpha channel, add it as an additional image
Expand Down
4 changes: 1 addition & 3 deletions libheif/context.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,11 @@ class HeifContext : public ErrorBuffer

Error read_from_memory(const void* data, size_t size, bool copy);

Error check_resolution(uint32_t width, uint32_t height) const;

std::shared_ptr<HeifFile> get_heif_file() const { return m_heif_file; }

std::vector<std::shared_ptr<ImageItem>> get_top_level_images(bool return_error_images);

void insert_new_image(heif_item_id id, std::shared_ptr<ImageItem> img) {
void insert_image_item(heif_item_id id, std::shared_ptr<ImageItem> img) {
m_all_images.insert(std::make_pair(id, img));
}

Expand Down
2 changes: 0 additions & 2 deletions libheif/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,6 @@
#include "libheif/heif.h"
#include <cassert>

static constexpr char kSuccess[] = "Success";


class ErrorBuffer
{
Expand Down
9 changes: 5 additions & 4 deletions libheif/image-items/grid.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <set>
#include <algorithm>
#include <libheif/api_structs.h>
#include "security_limits.h"


Error ImageGrid::parse(const std::vector<uint8_t>& data)
Expand Down Expand Up @@ -247,7 +248,7 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c
const uint32_t w = grid.get_width();
const uint32_t h = grid.get_height();

Error err = check_resolution(w, h);
Error err = check_for_valid_image_size(get_context()->get_security_limits(), w, h);
if (err) {
return err;
}
Expand Down Expand Up @@ -292,7 +293,7 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Grid::decode_full_grid_image(c

uint32_t src_width = tileImg->get_width();
uint32_t src_height = tileImg->get_height();
err = check_resolution(src_width, src_height);
err = check_for_valid_image_size(get_context()->get_security_limits(), src_width, src_height);
if (err) {
return err;
}
Expand Down Expand Up @@ -588,7 +589,7 @@ Result<std::shared_ptr<ImageItem_Grid>> ImageItem_Grid::add_new_grid_item(HeifCo
grid_image->set_grid_spec(grid);
grid_image->set_resolution(output_width, output_height);

ctx->insert_new_image(grid_id, grid_image);
ctx->insert_image_item(grid_id, grid_image);
const int construction_method = 1; // 0=mdat 1=idat
file->append_iloc_data(grid_id, grid_data, construction_method);

Expand Down Expand Up @@ -682,7 +683,7 @@ Result<std::shared_ptr<ImageItem_Grid>> ImageItem_Grid::add_and_encode_full_grid
// Create Grid Item
heif_item_id grid_id = file->add_new_image(fourcc("grid"));
griditem = std::make_shared<ImageItem_Grid>(ctx, grid_id);
ctx->insert_new_image(grid_id, griditem);
ctx->insert_image_item(grid_id, griditem);
const int construction_method = 1; // 0=mdat 1=idat
file->append_iloc_data(grid_id, grid_data, construction_method);

Expand Down
39 changes: 3 additions & 36 deletions libheif/image-items/image_item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
#include "color-conversion/colorconversion.h"
#include "api/libheif/api_structs.h"
#include "plugin_registry.h"
#include "security_limits.h"

#include <limits>
#include <cassert>
#include <cstring>
Expand Down Expand Up @@ -65,12 +67,6 @@ std::shared_ptr<HeifFile> ImageItem::get_file() const
}


Error ImageItem::check_resolution(uint32_t w, uint32_t h) const
{
return m_heif_context->check_resolution(w, h);
}


Error ImageItem::init_decoder_from_item(heif_item_id id)
{
m_id = id;
Expand Down Expand Up @@ -668,35 +664,6 @@ void ImageItem::CodedImageData::append_with_4bytes_size(const uint8_t* data, siz
}


Error ImageItem::check_for_valid_image_size(uint32_t width, uint32_t height) const
{
uint64_t maximum_image_size_limit = m_heif_context->get_maximum_image_size_limit();

// --- check whether the image size is "too large"

auto max_width_height = static_cast<uint32_t>(std::numeric_limits<int>::max());
if ((width > max_width_height || height > max_width_height) ||
(height != 0 && width > maximum_image_size_limit / height)) {
std::stringstream sstr;
sstr << "Image size " << width << "x" << height << " exceeds the maximum image size "
<< maximum_image_size_limit << "\n";

return {heif_error_Memory_allocation_error,
heif_suberror_Security_limit_exceeded,
sstr.str()};
}

if (width == 0 || height == 0) {
return {heif_error_Memory_allocation_error,
heif_suberror_Invalid_image_size,
"zero width or height"};
}

return Error::Ok;

}


Error ImageItem::transform_requested_tile_position_to_original_tile_position(uint32_t& tile_x, uint32_t& tile_y) const
{
Result<std::vector<std::shared_ptr<Box>>> propertiesResult = get_properties();
Expand Down Expand Up @@ -764,7 +731,7 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem::decode_image(const struct hei
if (!decode_tile_only) {
auto ispe = m_heif_context->get_heif_file()->get_property<Box_ispe>(m_id);
if (ispe) {
Error err = check_for_valid_image_size(ispe->get_width(), ispe->get_height());
Error err = check_for_valid_image_size(get_context()->get_security_limits(), ispe->get_width(), ispe->get_height());
if (err) {
return err;
}
Expand Down
5 changes: 0 additions & 5 deletions libheif/image-items/image_item.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ class ImageMetadata
};



class ImageItem : public ErrorBuffer
{
public:
Expand Down Expand Up @@ -97,8 +96,6 @@ class ImageItem : public ErrorBuffer

std::shared_ptr<class HeifFile> get_file() const;

Error check_resolution(uint32_t w, uint32_t h) const;

void set_resolution(uint32_t w, uint32_t h)
{
m_width = w;
Expand Down Expand Up @@ -275,8 +272,6 @@ class ImageItem : public ErrorBuffer

virtual Result<std::vector<uint8_t>> get_compressed_image_data() const;

Error check_for_valid_image_size(uint32_t width, uint32_t height) const;

Result<std::vector<std::shared_ptr<Box>>> get_properties() const;

// === encoding ===
Expand Down
5 changes: 3 additions & 2 deletions libheif/image-items/mask_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
* along with libheif. If not, see <http://www.gnu.org/licenses/>.
*/


#include <cstdint>
#include <cstring>
#include <algorithm>
Expand All @@ -31,6 +30,8 @@
#include "logging.h"
#include "mask_image.h"
#include "image_item.h"
#include "security_limits.h"


Error Box_mskC::parse(BitstreamRange& range, const heif_security_limits* limits)
{
Expand Down Expand Up @@ -71,7 +72,7 @@ Error MaskImageCodec::decode_mask_image(const HeifContext* context,
width = ispe->get_width();
height = ispe->get_height();

Error error = context->check_resolution(width, height);
Error error = check_for_valid_image_size(context->get_security_limits(), width, height);
if (error) {
return error;
}
Expand Down
5 changes: 3 additions & 2 deletions libheif/image-items/overlay.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
#include "context.h"
#include "file.h"
#include "color-conversion/colorconversion.h"
#include "security_limits.h"


template<typename I>
Expand Down Expand Up @@ -287,7 +288,7 @@ Result<std::shared_ptr<HeifPixelImage>> ImageItem_Overlay::decode_overlay_image(
uint32_t w = m_overlay_spec.get_canvas_width();
uint32_t h = m_overlay_spec.get_canvas_height();

Error err = check_resolution(w, h);
Error err = check_for_valid_image_size(get_context()->get_security_limits(), w, h);
if (err) {
return err;
}
Expand Down Expand Up @@ -416,7 +417,7 @@ Result<std::shared_ptr<ImageItem_Overlay>> ImageItem_Overlay::add_new_overlay_it

heif_item_id iovl_id = file->add_new_image(fourcc("iovl"));
std::shared_ptr<ImageItem_Overlay> iovl_image = std::make_shared<ImageItem_Overlay>(ctx, iovl_id);
ctx->insert_new_image(iovl_id, iovl_image);
ctx->insert_image_item(iovl_id, iovl_image);
const int construction_method = 1; // 0=mdat 1=idat
file->append_iloc_data(iovl_id, iovl_data, construction_method);

Expand Down
2 changes: 1 addition & 1 deletion libheif/image-items/tiled.cc
Original file line number Diff line number Diff line change
Expand Up @@ -518,7 +518,7 @@ ImageItem_Tiled::add_new_tiled_item(HeifContext* ctx, const heif_tiled_image_par
heif_item_id tild_id = ctx->get_heif_file()->add_new_image(fourcc("tili"));
auto tild_image = std::make_shared<ImageItem_Tiled>(ctx, tild_id);
tild_image->set_resolution(parameters->image_width, parameters->image_height);
ctx->insert_new_image(tild_id, tild_image);
ctx->insert_image_item(tild_id, tild_image);

// Create tilC box

Expand Down
2 changes: 1 addition & 1 deletion libheif/image-items/unc_image.cc
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ Result<std::shared_ptr<ImageItem_uncompressed>> ImageItem_uncompressed::add_unci
heif_item_id unci_id = ctx->get_heif_file()->add_new_image(fourcc("unci"));
auto unci_image = std::make_shared<ImageItem_uncompressed>(ctx, unci_id);
unci_image->set_resolution(parameters->image_width, parameters->image_height);
ctx->insert_new_image(unci_id, unci_image);
ctx->insert_image_item(unci_id, unci_image);


// Generate headers
Expand Down
28 changes: 28 additions & 0 deletions libheif/security_limits.cc
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,31 @@ struct heif_security_limits global_security_limits {
.max_color_profile_size = 100 * 1024 * 1024, // 100 MB
.max_memory_block_size = 512 * 1024 * 1024 // 512 MB
};


Error check_for_valid_image_size(const heif_security_limits* limits, uint32_t width, uint32_t height)
{
uint64_t maximum_image_size_limit = limits->max_image_size_pixels;

// --- check whether the image size is "too large"

auto max_width_height = static_cast<uint32_t>(std::numeric_limits<int>::max());
if ((width > max_width_height || height > max_width_height) ||
(height != 0 && width > maximum_image_size_limit / height)) {
std::stringstream sstr;
sstr << "Image size " << width << "x" << height << " exceeds the maximum image size "
<< maximum_image_size_limit << "\n";

return {heif_error_Memory_allocation_error,
heif_suberror_Security_limit_exceeded,
sstr.str()};
}

if (width == 0 || height == 0) {
return {heif_error_Memory_allocation_error,
heif_suberror_Invalid_image_size,
"zero width or height"};
}

return Error::Ok;
}
5 changes: 5 additions & 0 deletions libheif/security_limits.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#include "libheif/heif.h"
#include <cinttypes>
#include <cstddef>
#include "error.h"


extern heif_security_limits global_security_limits;

Expand All @@ -35,4 +37,7 @@ static const int64_t MAX_LARGE_BOX_SIZE = 0x0FFFFFFFFFFFFFFF;
static const int64_t MAX_FILE_POS = 0x007FFFFFFFFFFFFFLL; // maximum file position
static const int MAX_FRACTION_VALUE = 0x10000;


Error check_for_valid_image_size(const heif_security_limits* limits, uint32_t width, uint32_t height);

#endif // LIBHEIF_SECURITY_LIMITS_H

0 comments on commit b370e22

Please sign in to comment.