1 // Copyright 2012 Google Inc. All Rights Reserved. 2 // 3 // This code is licensed under the same terms as WebM: 4 // Software License Agreement: http://www.webmproject.org/license/software/ 5 // Additional IP Rights Grant: http://www.webmproject.org/license/additional/ 6 // ----------------------------------------------------------------------------- 7 // 8 // Misc. common utility functions 9 // 10 // Authors: Skal (pascal.massimino (at) gmail.com) 11 // Urvang (urvang (at) google.com) 12 13 #ifndef WEBP_UTILS_UTILS_H_ 14 #define WEBP_UTILS_UTILS_H_ 15 16 #include <assert.h> 17 18 #include "webp/types.h" 19 20 #if defined(__cplusplus) || defined(c_plusplus) 21 extern "C" { 22 #endif 23 24 //------------------------------------------------------------------------------ 25 // Memory allocation 26 27 // This is the maximum memory amount that libwebp will ever try to allocate. 28 #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 40) 29 30 // size-checking safe malloc/calloc: verify that the requested size is not too 31 // large, or return NULL. You don't need to call these for constructs like 32 // malloc(sizeof(foo)), but only if there's picture-dependent size involved 33 // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this 34 // safe malloc() borrows the signature from calloc(), pointing at the dangerous 35 // underlying multiply involved. 36 void* WebPSafeMalloc(uint64_t nmemb, size_t size); 37 // Note that WebPSafeCalloc() expects the second argument type to be 'size_t' 38 // in order to favor the "calloc(num_foo, sizeof(foo))" pattern. 39 void* WebPSafeCalloc(uint64_t nmemb, size_t size); 40 41 //------------------------------------------------------------------------------ 42 // Reading/writing data. 43 44 // Read 16, 24 or 32 bits stored in little-endian order. 45 static WEBP_INLINE int GetLE16(const uint8_t* const data) { 46 return (int)(data[0] << 0) | (data[1] << 8); 47 } 48 49 static WEBP_INLINE int GetLE24(const uint8_t* const data) { 50 return GetLE16(data) | (data[2] << 16); 51 } 52 53 static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) { 54 return (uint32_t)GetLE16(data) | (GetLE16(data + 2) << 16); 55 } 56 57 // Store 16, 24 or 32 bits in little-endian order. 58 static WEBP_INLINE void PutLE16(uint8_t* const data, int val) { 59 assert(val < (1 << 16)); 60 data[0] = (val >> 0); 61 data[1] = (val >> 8); 62 } 63 64 static WEBP_INLINE void PutLE24(uint8_t* const data, int val) { 65 assert(val < (1 << 24)); 66 PutLE16(data, val & 0xffff); 67 data[2] = (val >> 16); 68 } 69 70 static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) { 71 PutLE16(data, (int)(val & 0xffff)); 72 PutLE16(data + 2, (int)(val >> 16)); 73 } 74 75 //------------------------------------------------------------------------------ 76 77 #if defined(__cplusplus) || defined(c_plusplus) 78 } // extern "C" 79 #endif 80 81 #endif /* WEBP_UTILS_UTILS_H_ */ 82