tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

utils.h (7152B)


      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 #ifdef HAVE_CONFIG_H
     19 #include "src/webp/config.h"
     20 #endif
     21 
     22 #include <assert.h>
     23 
     24 #include "src/webp/types.h"
     25 
     26 #ifdef __cplusplus
     27 extern "C" {
     28 #endif
     29 
     30 //------------------------------------------------------------------------------
     31 // Memory allocation
     32 
     33 // This is the maximum memory amount that libwebp will ever try to allocate.
     34 #ifndef WEBP_MAX_ALLOCABLE_MEMORY
     35 #if SIZE_MAX > (1ULL << 34)
     36 #define WEBP_MAX_ALLOCABLE_MEMORY (1ULL << 34)
     37 #else
     38 // For 32-bit targets keep this below INT_MAX to avoid valgrind warnings.
     39 #define WEBP_MAX_ALLOCABLE_MEMORY ((1ULL << 31) - (1 << 16))
     40 #endif
     41 #endif  // WEBP_MAX_ALLOCABLE_MEMORY
     42 
     43 static WEBP_INLINE int CheckSizeOverflow(uint64_t size) {
     44  return size == (size_t)size;
     45 }
     46 
     47 // size-checking safe malloc/calloc: verify that the requested size is not too
     48 // large, or return NULL. You don't need to call these for constructs like
     49 // malloc(sizeof(foo)), but only if there's picture-dependent size involved
     50 // somewhere (like: malloc(num_pixels * sizeof(*something))). That's why this
     51 // safe malloc() borrows the signature from calloc(), pointing at the dangerous
     52 // underlying multiply involved.
     53 WEBP_EXTERN void* WebPSafeMalloc(uint64_t nmemb, size_t size);
     54 // Note that WebPSafeCalloc() expects the second argument type to be 'size_t'
     55 // in order to favor the "calloc(num_foo, sizeof(foo))" pattern.
     56 WEBP_EXTERN void* WebPSafeCalloc(uint64_t nmemb, size_t size);
     57 
     58 // Companion deallocation function to the above allocations.
     59 WEBP_EXTERN void WebPSafeFree(void* const ptr);
     60 
     61 //------------------------------------------------------------------------------
     62 // Alignment
     63 
     64 #define WEBP_ALIGN_CST 31
     65 #define WEBP_ALIGN(PTR) (((uintptr_t)(PTR) + WEBP_ALIGN_CST) & \
     66                         ~(uintptr_t)WEBP_ALIGN_CST)
     67 
     68 #include <string.h>
     69 // memcpy() is the safe way of moving potentially unaligned 32b memory.
     70 static WEBP_INLINE uint32_t WebPMemToUint32(const uint8_t* const ptr) {
     71  uint32_t A;
     72  memcpy(&A, ptr, sizeof(A));
     73  return A;
     74 }
     75 
     76 static WEBP_INLINE int32_t WebPMemToInt32(const uint8_t* const ptr) {
     77  return (int32_t)WebPMemToUint32(ptr);
     78 }
     79 
     80 static WEBP_INLINE void WebPUint32ToMem(uint8_t* const ptr, uint32_t val) {
     81  memcpy(ptr, &val, sizeof(val));
     82 }
     83 
     84 static WEBP_INLINE void WebPInt32ToMem(uint8_t* const ptr, int val) {
     85  WebPUint32ToMem(ptr, (uint32_t)val);
     86 }
     87 
     88 //------------------------------------------------------------------------------
     89 // Reading/writing data.
     90 
     91 // Read 16, 24 or 32 bits stored in little-endian order.
     92 static WEBP_INLINE int GetLE16(const uint8_t* const data) {
     93  return (int)(data[0] << 0) | (data[1] << 8);
     94 }
     95 
     96 static WEBP_INLINE int GetLE24(const uint8_t* const data) {
     97  return GetLE16(data) | (data[2] << 16);
     98 }
     99 
    100 static WEBP_INLINE uint32_t GetLE32(const uint8_t* const data) {
    101  return GetLE16(data) | ((uint32_t)GetLE16(data + 2) << 16);
    102 }
    103 
    104 // Store 16, 24 or 32 bits in little-endian order.
    105 static WEBP_INLINE void PutLE16(uint8_t* const data, int val) {
    106  assert(val < (1 << 16));
    107  data[0] = (val >> 0) & 0xff;
    108  data[1] = (val >> 8) & 0xff;
    109 }
    110 
    111 static WEBP_INLINE void PutLE24(uint8_t* const data, int val) {
    112  assert(val < (1 << 24));
    113  PutLE16(data, val & 0xffff);
    114  data[2] = (val >> 16) & 0xff;
    115 }
    116 
    117 static WEBP_INLINE void PutLE32(uint8_t* const data, uint32_t val) {
    118  PutLE16(data, (int)(val & 0xffff));
    119  PutLE16(data + 2, (int)(val >> 16));
    120 }
    121 
    122 // use GNU builtins where available.
    123 #if defined(__GNUC__) && \
    124    ((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ >= 4)
    125 // Returns (int)floor(log2(n)). n must be > 0.
    126 static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
    127  return 31 ^ __builtin_clz(n);
    128 }
    129 // counts the number of trailing zero
    130 static WEBP_INLINE int BitsCtz(uint32_t n) { return __builtin_ctz(n); }
    131 #elif defined(_MSC_VER) && _MSC_VER > 1310 && \
    132      (defined(_M_X64) || defined(_M_IX86))
    133 #include <intrin.h>
    134 #pragma intrinsic(_BitScanReverse)
    135 #pragma intrinsic(_BitScanForward)
    136 
    137 static WEBP_INLINE int BitsLog2Floor(uint32_t n) {
    138  unsigned long first_set_bit;  // NOLINT (runtime/int)
    139  _BitScanReverse(&first_set_bit, n);
    140  return first_set_bit;
    141 }
    142 static WEBP_INLINE int BitsCtz(uint32_t n) {
    143  unsigned long first_set_bit;  // NOLINT (runtime/int)
    144  _BitScanForward(&first_set_bit, n);
    145  return first_set_bit;
    146 }
    147 #else   // default: use the (slow) C-version.
    148 #define WEBP_HAVE_SLOW_CLZ_CTZ   // signal that the Clz/Ctz function are slow
    149 // Returns 31 ^ clz(n) = log2(n). This is the default C-implementation, either
    150 // based on table or not. Can be used as fallback if clz() is not available.
    151 #define WEBP_NEED_LOG_TABLE_8BIT
    152 extern const uint8_t WebPLogTable8bit[256];
    153 static WEBP_INLINE int WebPLog2FloorC(uint32_t n) {
    154  int log_value = 0;
    155  while (n >= 256) {
    156    log_value += 8;
    157    n >>= 8;
    158  }
    159  return log_value + WebPLogTable8bit[n];
    160 }
    161 
    162 static WEBP_INLINE int BitsLog2Floor(uint32_t n) { return WebPLog2FloorC(n); }
    163 
    164 static WEBP_INLINE int BitsCtz(uint32_t n) {
    165  int i;
    166  for (i = 0; i < 32; ++i, n >>= 1) {
    167    if (n & 1) return i;
    168  }
    169  return 32;
    170 }
    171 
    172 #endif
    173 
    174 //------------------------------------------------------------------------------
    175 // Pixel copying.
    176 
    177 struct WebPPicture;
    178 
    179 // Copy width x height pixels from 'src' to 'dst' honoring the strides.
    180 WEBP_EXTERN void WebPCopyPlane(const uint8_t* src, int src_stride,
    181                               uint8_t* dst, int dst_stride,
    182                               int width, int height);
    183 
    184 // Copy ARGB pixels from 'src' to 'dst' honoring strides. 'src' and 'dst' are
    185 // assumed to be already allocated and using ARGB data.
    186 WEBP_EXTERN void WebPCopyPixels(const struct WebPPicture* const src,
    187                                struct WebPPicture* const dst);
    188 
    189 //------------------------------------------------------------------------------
    190 // Unique colors.
    191 
    192 // Returns count of unique colors in 'pic', assuming pic->use_argb is true.
    193 // If the unique color count is more than MAX_PALETTE_SIZE, returns
    194 // MAX_PALETTE_SIZE+1.
    195 // If 'palette' is not NULL and number of unique colors is less than or equal to
    196 // MAX_PALETTE_SIZE, also outputs the actual unique colors into 'palette'.
    197 // Note: 'palette' is assumed to be an array already allocated with at least
    198 // MAX_PALETTE_SIZE elements.
    199 // TODO(vrabaud) remove whenever we can break the ABI.
    200 WEBP_EXTERN int WebPGetColorPalette(const struct WebPPicture* const pic,
    201                                    uint32_t* const palette);
    202 
    203 //------------------------------------------------------------------------------
    204 
    205 #ifdef __cplusplus
    206 }    // extern "C"
    207 #endif
    208 
    209 #endif  // WEBP_UTILS_UTILS_H_