130625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka/* Copyright 2013 Google Inc. All Rights Reserved.
230625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka
330625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   Licensed under the Apache License, Version 2.0 (the "License");
430625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   you may not use this file except in compliance with the License.
530625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   You may obtain a copy of the License at
630625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka
730625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   http://www.apache.org/licenses/LICENSE-2.0
830625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka
930625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   Unless required by applicable law or agreed to in writing, software
1030625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   distributed under the License is distributed on an "AS IS" BASIS,
1130625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1230625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   See the License for the specific language governing permissions and
1330625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   limitations under the License.
1430625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka
1530625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   Size-checked memory allocation.
1630625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka*/
1704163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka
1804163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka#ifndef BROTLI_UTILS_UTILS_H_
1904163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka#define BROTLI_UTILS_UTILS_H_
2004163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka
2104163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka#include <assert.h>
2204163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka
2304163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka#include "./types.h"
2404163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka
2504163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka#if defined(__cplusplus) || defined(c_plusplus)
2604163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadkaextern "C" {
2704163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka#endif
2804163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka
2930625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka/* This is the maximum memory amount that we will ever try to allocate. */
30dfc5a9f2151a7c88914c236c7db8fa119fee249cZoltan Szabadka#define BROTLI_MAX_ALLOCABLE_MEMORY (1 << 30)
3104163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka
3230625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka/* size-checking safe malloc/calloc: verify that the requested size is not too
3330625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   large, or return NULL. You don't need to call these for constructs like
3430625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   malloc(sizeof(foo)), but only if there's font-dependent size involved
3530625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   somewhere (like: malloc(decoded_size * sizeof(*something))). That's why this
3630625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   safe malloc() borrows the signature from calloc(), pointing at the dangerous
3730625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka   underlying multiply involved.
3830625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka*/
3904163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadkavoid* BrotliSafeMalloc(uint64_t nmemb, size_t size);
4004163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka
4104163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka#if defined(__cplusplus) || defined(c_plusplus)
4230625ba238fcb360c80a093164347503bbedf7adZoltan Szabadka}    /* extern "C" */
4304163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka#endif
4404163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka
4504163a87d098f14e890c2a3fd240af7f09099711Zoltan Szabadka#endif  /* BROTLI_UTILS_UTILS_H_ */
46