• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2006 The Android Open Source Project
3  *
4  * Use of this source code is governed by a BSD-style license that can be
5  * found in the LICENSE file.
6  */
7 
8 #ifndef SkBuffer_DEFINED
9 #define SkBuffer_DEFINED
10 
11 #include "include/private/base/SkAssert.h"
12 #include "include/private/base/SkNoncopyable.h"
13 #include "src/base/SkSafeMath.h"
14 
15 #include <cstddef>
16 #include <cstdint>
17 
18 typedef float SkScalar;
19 
20 /** \class SkRBuffer
21 
22     Light weight class for reading data from a memory block.
23     The RBuffer is given the buffer to read from, with either a specified size
24     or no size (in which case no range checking is performed). It is iillegal
25     to attempt to read a value from an empty RBuffer (data == null).
26 */
27 class SkRBuffer : SkNoncopyable {
28 public:
SkRBuffer()29     SkRBuffer() : fData(nullptr), fPos(nullptr), fStop(nullptr) {}
30 
31     /** Initialize RBuffer with a data point and length.
32     */
SkRBuffer(const void * data,size_t size)33     SkRBuffer(const void* data, size_t size) {
34         SkASSERT(data != nullptr || size == 0);
35         fData = (const char*)data;
36         fPos = (const char*)data;
37         fStop = (const char*)data + size;
38     }
39 
40     /** Return the number of bytes that have been read from the beginning
41         of the data pointer.
42     */
pos()43     size_t pos() const { return fPos - fData; }
44     /** Return the total size of the data pointer. Only defined if the length was
45         specified in the constructor or in a call to reset().
46     */
size()47     size_t size() const { return fStop - fData; }
48     /** Return true if the buffer has read to the end of the data pointer.
49         Only defined if the length was specified in the constructor or in a call
50         to reset(). Always returns true if the length was not specified.
51     */
eof()52     bool eof() const { return fPos >= fStop; }
53 
available()54     size_t available() const { return fStop - fPos; }
55 
isValid()56     bool isValid() const { return fValid; }
57 
58     /** Read the specified number of bytes from the data pointer. If buffer is not
59         null, copy those bytes into buffer.
60     */
61     bool read(void* buffer, size_t size);
62     bool skipToAlign4();
63 
readU8(uint8_t * x)64     bool readU8(uint8_t* x)   { return this->read(x, 1); }
readS32(int32_t * x)65     bool readS32(int32_t* x)  { return this->read(x, 4); }
readU32(uint32_t * x)66     bool readU32(uint32_t* x) { return this->read(x, 4); }
67 
68     // returns nullptr on failure
69     const void* skip(size_t bytes);
skipCount(size_t count)70     template <typename T> const T* skipCount(size_t count) {
71         return static_cast<const T*>(this->skip(SkSafeMath::Mul(count, sizeof(T))));
72     }
73 
74 private:
75     const char* fData;
76     const char* fPos;
77     const char* fStop;
78     bool        fValid = true;
79 };
80 
81 /** \class SkWBuffer
82 
83     Light weight class for writing data to a memory block.
84     The WBuffer is given the buffer to write into, with either a specified size
85     or no size, in which case no range checking is performed. An empty WBuffer
86     is legal, in which case no data is ever written, but the relative pos()
87     is updated.
88 */
89 class SkWBuffer : SkNoncopyable {
90 public:
SkWBuffer()91     SkWBuffer() : fData(nullptr), fPos(nullptr), fStop(nullptr) {}
SkWBuffer(void * data)92     SkWBuffer(void* data) { reset(data); }
SkWBuffer(void * data,size_t size)93     SkWBuffer(void* data, size_t size) { reset(data, size); }
94 
reset(void * data)95     void reset(void* data) {
96         fData = (char*)data;
97         fPos = (char*)data;
98         fStop = nullptr;  // no bounds checking
99     }
100 
reset(void * data,size_t size)101     void reset(void* data, size_t size) {
102         SkASSERT(data != nullptr || size == 0);
103         fData = (char*)data;
104         fPos = (char*)data;
105         fStop = (char*)data + size;
106     }
107 
pos()108     size_t  pos() const { return fPos - fData; }
109     void*   skip(size_t size); // return start of skipped data
110 
write(const void * buffer,size_t size)111     void write(const void* buffer, size_t size) {
112         if (size) {
113             this->writeNoSizeCheck(buffer, size);
114         }
115     }
116 
117     size_t  padToAlign4();
118 
writePtr(const void * x)119     void    writePtr(const void* x) { this->writeNoSizeCheck(&x, sizeof(x)); }
writeScalar(SkScalar x)120     void    writeScalar(SkScalar x) { this->writeNoSizeCheck(&x, 4); }
write32(int32_t x)121     void    write32(int32_t x) { this->writeNoSizeCheck(&x, 4); }
write16(int16_t x)122     void    write16(int16_t x) { this->writeNoSizeCheck(&x, 2); }
write8(int8_t x)123     void    write8(int8_t x) { this->writeNoSizeCheck(&x, 1); }
writeBool(bool x)124     void    writeBool(bool x) { this->write8(x); }
125 
126 private:
127     void    writeNoSizeCheck(const void* buffer, size_t size);
128 
129     char* fData;
130     char* fPos;
131     char* fStop;
132 };
133 
134 #endif
135