Home | History | Annotate | Download | only in utils
      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 (at) gmail.com)
     13 //          Urvang (urvang (at) 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 #if defined(__cplusplus) || defined(c_plusplus)
     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 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);
     42 
     43 //------------------------------------------------------------------------------
     44 // Reading/writing data.
     45 
     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);
     49 }
     50 
     51 static WEBP_INLINE int GetLE24(const uint8_t* const data) {
     52   return GetLE16(data) | (data[2] << 16);
     53 }
     54 
     55 static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
     56   return (uint32_t)GetLE16(data) | (GetLE16(data + 2) << 16);
     57 }
     58 
     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));
     62   data[0] = (val >> 0);
     63   data[1] = (val >> 8);
     64 }
     65 
     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);
     70 }
     71 
     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));
     75 }
     76 
     77 //------------------------------------------------------------------------------
     78 
     79 #if defined(__cplusplus) || defined(c_plusplus)
     80 }    // extern "C"
     81 #endif
     82 
     83 #endif  /* WEBP_UTILS_UTILS_H_ */
     84