gfx/angle/src/libGLESv2/BinaryStream.h

Sat, 03 Jan 2015 20:18:00 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Sat, 03 Jan 2015 20:18:00 +0100
branch
TOR_BUG_3246
changeset 7
129ffea94266
permissions
-rw-r--r--

Conditionally enable double key logic according to:
private browsing mode or privacy.thirdparty.isolate preference and
implement in GetCookieStringCommon and FindCookie where it counts...
With some reservations of how to convince FindCookie users to test
condition and pass a nullptr when disabling double key logic.

     1 //
     2 // Copyright (c) 2012 The ANGLE Project Authors. All rights reserved.
     3 // Use of this source code is governed by a BSD-style license that can be
     4 // found in the LICENSE file.
     5 //
     7 // BinaryStream.h: Provides binary serialization of simple types.
     9 #ifndef LIBGLESV2_BINARYSTREAM_H_
    10 #define LIBGLESV2_BINARYSTREAM_H_
    12 #include "common/angleutils.h"
    14 namespace gl
    15 {
    17 class BinaryInputStream
    18 {
    19   public:
    20     BinaryInputStream(const void *data, size_t length)
    21     {
    22         mError = false;
    23         mOffset = 0;
    24         mData = static_cast<const char*>(data);
    25         mLength = length;
    26     }
    28     template <typename T>
    29     void read(T *v, size_t num)
    30     {
    31         union
    32         {
    33             T dummy;  // Compilation error for non-trivial types
    34         } dummy;
    35         (void) dummy;
    37         if (mError)
    38         {
    39             return;
    40         }
    42         size_t length = num * sizeof(T);
    44         if (mOffset + length > mLength)
    45         {
    46             mError = true;
    47             return;
    48         }
    50         memcpy(v, mData + mOffset, length);
    51         mOffset += length;
    52     }
    54     template <typename T>
    55     void read(T * v)
    56     {
    57         read(v, 1);
    58     }
    60     void read(std::string *v)
    61     {
    62         size_t length;
    63         read(&length);
    65         if (mError)
    66         {
    67             return;
    68         }
    70         if (mOffset + length > mLength)
    71         {
    72             mError = true;
    73             return;
    74         }
    76         v->assign(mData + mOffset, length);
    77         mOffset += length;
    78     }
    80     void skip(size_t length)
    81     {
    82         if (mOffset + length > mLength)
    83         {
    84             mError = true;
    85             return;
    86         }
    88         mOffset += length;
    89     }
    91     size_t offset() const
    92     {
    93         return mOffset;
    94     }
    96     bool error() const
    97     {
    98         return mError;
    99     }
   101     bool endOfStream() const
   102     {
   103         return mOffset == mLength;
   104     }
   106   private:
   107     DISALLOW_COPY_AND_ASSIGN(BinaryInputStream);
   108     bool mError;
   109     size_t mOffset;
   110     const char *mData;
   111     size_t mLength;
   112 };
   114 class BinaryOutputStream
   115 {
   116   public:
   117     BinaryOutputStream()
   118     {
   119     }
   121     template <typename T>
   122     void write(const T *v, size_t num)
   123     {
   124         union
   125         {
   126             T dummy;  // Compilation error for non-trivial types
   127         } dummy;
   128         (void) dummy;
   130         const char *asBytes = reinterpret_cast<const char*>(v);
   131         mData.insert(mData.end(), asBytes, asBytes + num * sizeof(T));
   132     }
   134     template <typename T>
   135     void write(const T &v)
   136     {
   137         write(&v, 1);
   138     }
   140     void write(const std::string &v)
   141     {
   142         size_t length = v.length();
   143         write(length);
   145         write(v.c_str(), length);
   146     }
   148     size_t length() const
   149     {
   150         return mData.size();
   151     }
   153     const void* data() const
   154     {
   155         return mData.size() ? &mData[0] : NULL;
   156     }
   158   private:
   159     DISALLOW_COPY_AND_ASSIGN(BinaryOutputStream);
   160     std::vector<char> mData;
   161 };
   162 }
   164 #endif  // LIBGLESV2_BINARYSTREAM_H_

mercurial