1793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Copyright 2012 Google Inc. All Rights Reserved.
2793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
3793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Use of this source code is governed by a BSD-style license
4793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// that can be found in the COPYING file in the root of the source
5793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// tree. An additional intellectual property rights grant can be found
6793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// in the file PATENTS. All contributing project authors may
7793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// be found in the AUTHORS file in the root of the source tree.
8793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// -----------------------------------------------------------------------------
9793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
10793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Misc. common utility functions
11793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//
12793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Authors: Skal (pascal.massimino@gmail.com)
13793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//          Urvang (urvang@google.com)
14793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
15793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#ifndef WEBP_UTILS_UTILS_H_
16793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define WEBP_UTILS_UTILS_H_
17793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
18793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include <assert.h>
19793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
20793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#include "../webp/types.h"
21793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
22793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#if defined(__cplusplus) || defined(c_plusplus)
23793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerextern "C" {
24793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif
25793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
26793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//------------------------------------------------------------------------------
27793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Memory allocation
28793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
29793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// This is the maximum memory amount that libwebp will ever try to allocate.
30793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40)
31793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
32793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// size-checking safe malloc/calloc: verify that the requested size is not too
33793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// large, or return NULL. You don't need to call these for constructs like
34793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// malloc(sizeof(foo)), but only if there's picture-dependent size involved
35793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this
36793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// safe malloc() borrows the signature from calloc(), pointing at the dangerous
37793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// underlying multiply involved.
38793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid* WebPSafeMalloc(uint64_t nmemb, size_t size);
39793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Note that WebPSafeCalloc() expects the second argument type to be 'size_t'
40793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// in order to favor the "calloc(num_foo, sizeof(foo))" pattern.
41793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslervoid* WebPSafeCalloc(uint64_t nmemb, size_t size);
42793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
43793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//------------------------------------------------------------------------------
44793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Reading/writing data.
45793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
46793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Read 16, 24 or 32 bits stored in little-endian order.
47793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerstatic WEBP_INLINE int GetLE16(const uint8_t* const data) {
48793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  return (int)(data[0] << 0) | (data[1] << 8);
49793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
50793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
51793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerstatic WEBP_INLINE int GetLE24(const uint8_t* const data) {
52793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  return GetLE16(data) | (data[2] << 16);
53793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
54793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
55793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerstatic WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
56793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  return (uint32_t)GetLE16(data) | (GetLE16(data + 2) << 16);
57793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
58793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
59793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler// Store 16, 24 or 32 bits in little-endian order.
60793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerstatic WEBP_INLINE void PutLE16(uint8_t* const data, int val) {
61793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  assert(val < (1 << 16));
62793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  data[0] = (val >> 0);
63793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  data[1] = (val >> 8);
64793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
65793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
66793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerstatic WEBP_INLINE void PutLE24(uint8_t* const data, int val) {
67793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  assert(val < (1 << 24));
68793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  PutLE16(data, val & 0xffff);
69793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  data[2] = (val >> 16);
70793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
71793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
72793ee12c6df9cad3806238d32528c49a3ff9331dNoah Preslerstatic WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {
73793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  PutLE16(data, (int)(val & 0xffff));
74793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler  PutLE16(data + 2, (int)(val >> 16));
75793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}
76793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
77793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler//------------------------------------------------------------------------------
78793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
79793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#if defined(__cplusplus) || defined(c_plusplus)
80793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler}    // extern "C"
81793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif
82793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler
83793ee12c6df9cad3806238d32528c49a3ff9331dNoah Presler#endif  /* WEBP_UTILS_UTILS_H_ */
84