tor-browser

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

Compression.h (2604B)


      1 /*  GRAPHITE2 LICENSING
      2 
      3    Copyright 2015, SIL International
      4    All rights reserved.
      5 
      6    This library is free software; you can redistribute it and/or modify
      7    it under the terms of the GNU Lesser General Public License as published
      8    by the Free Software Foundation; either version 2.1 of License, or
      9    (at your option) any later version.
     10 
     11    This program is distributed in the hope that it will be useful,
     12    but WITHOUT ANY WARRANTY; without even the implied warranty of
     13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
     14    Lesser General Public License for more details.
     15 
     16    You should also have received a copy of the GNU Lesser General Public
     17    License along with this library in the file named "LICENSE".
     18    If not, write to the Free Software Foundation, 51 Franklin Street,
     19    Suite 500, Boston, MA 02110-1335, USA or visit their web page on the
     20    internet at http://www.fsf.org/licenses/lgpl.html.
     21 
     22 Alternatively, the contents of this file may be used under the terms of the
     23 Mozilla Public License (http://mozilla.org/MPL) or the GNU General Public
     24 License, as published by the Free Software Foundation, either version 2
     25 of the License or (at your option) any later version.
     26 */
     27 
     28 #pragma once
     29 
     30 #include <cassert>
     31 #include <cstddef>
     32 #include <cstring>
     33 
     34 namespace
     35 {
     36 
     37 #if defined(_MSC_VER)
     38 typedef unsigned __int8 u8;
     39 typedef unsigned __int16 u16;
     40 typedef unsigned __int32 u32;
     41 typedef unsigned __int64 u64;
     42 #else
     43 #include <stdint.h>
     44 typedef uint8_t u8;
     45 typedef uint16_t u16;
     46 typedef uint32_t u32;
     47 typedef uint64_t u64;
     48 #endif
     49 
     50 ptrdiff_t const     MINMATCH = 4,
     51                    LASTLITERALS = 5,
     52                    MINCODA  = LASTLITERALS+1,
     53                    MINSRCSIZE = 13;
     54 
     55 template<int S>
     56 inline
     57 void unaligned_copy(void * d, void const * s) {
     58  ::memcpy(d, s, S);
     59 }
     60 
     61 inline
     62 size_t align(size_t p) {
     63    return (p + sizeof(unsigned long)-1) & ~(sizeof(unsigned long)-1);
     64 }
     65 
     66 inline
     67 u8 * safe_copy(u8 * d, u8 const * s, size_t n) {
     68    while (n--) *d++ = *s++;
     69    return d;
     70 }
     71 
     72 inline
     73 u8 * overrun_copy(u8 * d, u8 const * s, size_t n) {
     74    size_t const WS = sizeof(unsigned long);
     75    u8 const * e = s + n;
     76    do
     77    {
     78        unaligned_copy<WS>(d, s);
     79        d += WS;
     80        s += WS;
     81    }
     82    while (s < e);
     83    d-=(s-e);
     84 
     85    return d;
     86 }
     87 
     88 
     89 inline
     90 u8 * fast_copy(u8 * d, u8 const * s, size_t n) {
     91    size_t const WS = sizeof(unsigned long);
     92    size_t wn = n/WS;
     93    while (wn--)
     94    {
     95        unaligned_copy<WS>(d, s);
     96        d += WS;
     97        s += WS;
     98    }
     99    n &= WS-1;
    100    return safe_copy(d, s, n);
    101 }
    102 
    103 
    104 } // end of anonymous namespace