1 // Copyright 2012 Google Inc. All Rights Reserved.
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 // -----------------------------------------------------------------------------
10 // Misc. common utility functions
12 // Authors: Skal (pascal.massimino@gmail.com)
13 // Urvang (urvang@google.com)
15 #ifndef WEBP_UTILS_UTILS_H_
16 #define WEBP_UTILS_UTILS_H_
20 #include "../webp/types.h"
22 #if defined(__cplusplus) || defined(c_plusplus)
26 //------------------------------------------------------------------------------
29 // This is the maximum memory amount that libwebp will ever try to allocate.
30 #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40)
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 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 void* WebPSafeCalloc(uint64_t nmemb
, size_t size
);
43 //------------------------------------------------------------------------------
44 // Reading/writing data.
46 // Read 16, 24 or 32 bits stored in little-endian order.
47 static WEBP_INLINE
int GetLE16(const uint8_t* const data
) {
48 return (int)(data
[0] << 0) | (data
[1] << 8);
51 static WEBP_INLINE
int GetLE24(const uint8_t* const data
) {
52 return GetLE16(data
) | (data
[2] << 16);
55 static WEBP_INLINE
uint32_t GetLE32(const uint8_t* const data
) {
56 return (uint32_t)GetLE16(data
) | (GetLE16(data
+ 2) << 16);
59 // Store 16, 24 or 32 bits in little-endian order.
60 static WEBP_INLINE
void PutLE16(uint8_t* const data
, int val
) {
61 assert(val
< (1 << 16));
66 static WEBP_INLINE
void PutLE24(uint8_t* const data
, int val
) {
67 assert(val
< (1 << 24));
68 PutLE16(data
, val
& 0xffff);
69 data
[2] = (val
>> 16);
72 static WEBP_INLINE
void PutLE32(uint8_t* const data
, uint32_t val
) {
73 PutLE16(data
, (int)(val
& 0xffff));
74 PutLE16(data
+ 2, (int)(val
>> 16));
77 //------------------------------------------------------------------------------
79 #if defined(__cplusplus) || defined(c_plusplus)
83 #endif /* WEBP_UTILS_UTILS_H_ */