tor-browser

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

fast_masking.i (3312B)


      1 // Copyright 2013, Google Inc.
      2 // All rights reserved.
      3 //
      4 // Redistribution and use in source and binary forms, with or without
      5 // modification, are permitted provided that the following conditions are
      6 // met:
      7 //
      8 //     * Redistributions of source code must retain the above copyright
      9 // notice, this list of conditions and the following disclaimer.
     10 //     * Redistributions in binary form must reproduce the above
     11 // copyright notice, this list of conditions and the following disclaimer
     12 // in the documentation and/or other materials provided with the
     13 // distribution.
     14 //     * Neither the name of Google Inc. nor the names of its
     15 // contributors may be used to endorse or promote products derived from
     16 // this software without specific prior written permission.
     17 //
     18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
     24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
     28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     29 
     30 
     31 %module fast_masking
     32 
     33 %include "cstring.i"
     34 
     35 %{
     36 #include <cstring>
     37 
     38 #ifdef __SSE2__
     39 #include <emmintrin.h>
     40 #endif
     41 %}
     42 
     43 %apply (char *STRING, int LENGTH) {
     44    (const char* payload, int payload_length),
     45    (const char* masking_key, int masking_key_length) };
     46 %cstring_output_allocate_size(
     47    char** result, int* result_length, delete [] *$1);
     48 
     49 %inline %{
     50 
     51 void mask(
     52    const char* payload, int payload_length,
     53    const char* masking_key, int masking_key_length,
     54    int masking_key_index,
     55    char** result, int* result_length) {
     56  *result = new char[payload_length];
     57  *result_length = payload_length;
     58  memcpy(*result, payload, payload_length);
     59 
     60  char* cursor = *result;
     61  char* cursor_end = *result + *result_length;
     62 
     63 #ifdef __SSE2__
     64  while ((cursor < cursor_end) &&
     65         (reinterpret_cast<size_t>(cursor) & 0xf)) {
     66    *cursor ^= masking_key[masking_key_index];
     67    ++cursor;
     68    masking_key_index = (masking_key_index + 1) % masking_key_length;
     69  }
     70  if (cursor == cursor_end) {
     71    return;
     72  }
     73 
     74  const int kBlockSize = 16;
     75  __m128i masking_key_block;
     76  for (int i = 0; i < kBlockSize; ++i) {
     77    *(reinterpret_cast<char*>(&masking_key_block) + i) =
     78        masking_key[masking_key_index];
     79    masking_key_index = (masking_key_index + 1) % masking_key_length;
     80  }
     81 
     82  while (cursor + kBlockSize <= cursor_end) {
     83    __m128i payload_block =
     84        _mm_load_si128(reinterpret_cast<__m128i*>(cursor));
     85    _mm_stream_si128(reinterpret_cast<__m128i*>(cursor),
     86                     _mm_xor_si128(payload_block, masking_key_block));
     87    cursor += kBlockSize;
     88  }
     89 #endif
     90 
     91  while (cursor < cursor_end) {
     92    *cursor ^= masking_key[masking_key_index];
     93    ++cursor;
     94    masking_key_index = (masking_key_index + 1) % masking_key_length;
     95  }
     96 }
     97 
     98 %}