1 // Copyright 2012 Google Inc. All Rights Reserved.
2 //
3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the COPYING file in the root of the source
5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree.
8 // -----------------------------------------------------------------------------
9 //
10 // Misc. common utility functions
11 //
12 // Authors: Skal (pascal.massimino@gmail.com)
13 //          Urvang (urvang@google.com)
14 
15 #ifndef WEBP_UTILS_UTILS_H_
16 #define WEBP_UTILS_UTILS_H_
17 
18 #include <assert.h>
19 
20 #include "../webp/types.h"
21 
22 #ifdef __cplusplus
23 extern "C" {
24 #endif
25 
26 //------------------------------------------------------------------------------
27 // Memory allocation
28 
29 // This is the maximum memory amount that libwebp will ever try to allocate.
30 #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40)
31 
32 // size-checking safe malloc/calloc: verify that the requested size is not too
33 // large, or return NULL. You don't need to call these for constructs like
34 // malloc(sizeof(foo)), but only if there's picture-dependent size involved
35 // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this
36 // safe malloc() borrows the signature from calloc(), pointing at the dangerous
37 // underlying multiply involved.
38 WEBP_EXTERN(void*) WebPSafeMalloc(uint64_t nmemb, size_t size);
39 // Note that WebPSafeCalloc() expects the second argument type to be 'size_t'
40 // in order to favor the "calloc(num_foo, sizeof(foo))" pattern.
41 WEBP_EXTERN(void*) WebPSafeCalloc(uint64_t nmemb, size_t size);
42 
43 // Companion deallocation function to the above allocations.
44 WEBP_EXTERN(void) WebPSafeFree(void* const ptr);
45 
46 //------------------------------------------------------------------------------
47 // Reading/writing data.
48 
49 // Read 16, 24 or 32 bits stored in little-endian order.
GetLE16(const uint8_t * const data)50 static WEBP_INLINE int GetLE16(const uint8_t* const data) {
51   return (int)(data[0] << 0) | (data[1] << 8);
52 }
53 
GetLE24(const uint8_t * const data)54 static WEBP_INLINE int GetLE24(const uint8_t* const data) {
55   return GetLE16(data) | (data[2] << 16);
56 }
57 
GetLE32(const uint8_t * const data)58 static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
59   return (uint32_t)GetLE16(data) | (GetLE16(data + 2) << 16);
60 }
61 
62 // Store 16, 24 or 32 bits in little-endian order.
PutLE16(uint8_t * const data,int val)63 static WEBP_INLINE void PutLE16(uint8_t* const data, int val) {
64   assert(val < (1 << 16));
65   data[0] = (val >> 0);
66   data[1] = (val >> 8);
67 }
68 
PutLE24(uint8_t * const data,int val)69 static WEBP_INLINE void PutLE24(uint8_t* const data, int val) {
70   assert(val < (1 << 24));
71   PutLE16(data, val & 0xffff);
72   data[2] = (val >> 16);
73 }
74 
PutLE32(uint8_t * const data,uint32_t val)75 static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {
76   PutLE16(data, (int)(val & 0xffff));
77   PutLE16(data + 2, (int)(val >> 16));
78 }
79 
80 // Returns (int)floor(log2(n)). n must be > 0.
81 // use GNU builtins where available.
82 #if defined(__GNUC__) && \
83     ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
BitsLog2Floor(uint32_t n)84 static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
85   return 31 ^ __builtin_clz(n);
86 }
87 #elif defined(_MSC_VER) && _MSC_VER > 1310 && \
88       (defined(_M_X64) || defined(_M_IX86))
89 #include <intrin.h>
90 #pragma intrinsic(_BitScanReverse)
91 
BitsLog2Floor(uint32_t n)92 static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
93   uint32_t first_set_bit;
94   _BitScanReverse(&first_set_bit, n);
95   return first_set_bit;
96 }
97 #else
BitsLog2Floor(uint32_t n)98 static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
99   int log = 0;
100   uint32_t value = n;
101   int i;
102 
103   for (i = 4; i >= 0; --i) {
104     const int shift = (1 << i);
105     const uint32_t x = value >> shift;
106     if (x != 0) {
107       value = x;
108       log += shift;
109     }
110   }
111   return log;
112 }
113 #endif
114 
115 //------------------------------------------------------------------------------
116 
117 #ifdef __cplusplus
118 }    // extern "C"
119 #endif
120 
121 #endif  /* WEBP_UTILS_UTILS_H_ */
122