gfx/layers/BufferUnrotate.cpp

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

     1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
     2  * This Source Code Form is subject to the terms of the Mozilla Public
     3  * License, v. 2.0. If a copy of the MPL was not distributed with this
     4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     6 #include <algorithm> // min & max
     7 #include <cstdlib>
     8 #include <stdint.h>
     9 #include <stdio.h>
    10 #include <stdlib.h>
    11 #include <string.h>
    13 void BufferUnrotate(uint8_t* aBuffer, int aByteWidth, int aHeight,
    14                     int aByteStride, int aXBoundary, int aYBoundary)
    15 {
    16   if (aXBoundary != 0) {
    17     uint8_t* line = new uint8_t[aByteWidth];
    18     uint32_t smallStart = 0;
    19     uint32_t smallLen = aXBoundary;
    20     uint32_t smallDest = aByteWidth - aXBoundary;
    21     uint32_t largeStart = aXBoundary;
    22     uint32_t largeLen = aByteWidth - aXBoundary;
    23     uint32_t largeDest = 0;
    24     if (aXBoundary > aByteWidth / 2) {
    25       smallStart = aXBoundary;
    26       smallLen = aByteWidth - aXBoundary;
    27       smallDest = 0;
    28       largeStart = 0;
    29       largeLen = aXBoundary;
    30       largeDest = smallLen;
    31     }
    33     for (int y = 0; y < aHeight; y++) {
    34       int yOffset = y * aByteStride;
    35       memcpy(line, &aBuffer[yOffset + smallStart], smallLen);
    36       memmove(&aBuffer[yOffset + largeDest], &aBuffer[yOffset + largeStart], largeLen);
    37       memcpy(&aBuffer[yOffset + smallDest], line, smallLen);
    38     }
    40     delete[] line;
    41   }
    43   if (aYBoundary != 0) {
    44     uint32_t smallestHeight = std::min(aHeight - aYBoundary, aYBoundary);
    45     uint32_t largestHeight = std::max(aHeight - aYBoundary, aYBoundary);
    46     uint32_t smallOffset = 0;
    47     uint32_t largeOffset = aYBoundary * aByteStride;
    48     uint32_t largeDestOffset = 0;
    49     uint32_t smallDestOffset = largestHeight * aByteStride;
    50     if (aYBoundary > aHeight / 2) {
    51       smallOffset = aYBoundary * aByteStride;
    52       largeOffset = 0;
    53       largeDestOffset = smallestHeight * aByteStride;
    54       smallDestOffset = 0;
    55     }
    57     uint8_t* smallestSide = new uint8_t[aByteStride * smallestHeight];
    58     memcpy(smallestSide, &aBuffer[smallOffset], aByteStride * smallestHeight);
    59     memmove(&aBuffer[largeDestOffset], &aBuffer[largeOffset], aByteStride * largestHeight);
    60     memcpy(&aBuffer[smallDestOffset], smallestSide, aByteStride * smallestHeight);
    61     delete[] smallestSide;
    62   }
    63 }

mercurial