Debian Codegraph @

src/core/SkStreamPriv.h

raw
1 /*
2 * Copyright 2013 Google Inc.
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 SkStreamPriv_DEFINED
9 #define SkStreamPriv_DEFINED
10
11 #include "include/core/SkRefCnt.h"
12 #include "include/core/SkStream.h"
13 #include "src/base/SkEndian.h"
14
15 #include <cstdint>
16
17 class SkData;
18
19 namespace SkStreamPriv {
20
21 /**
22 * Copy the provided stream to an SkData variable.
23 *
24 * Note: Assumes the stream is at the beginning. If it has a length,
25 * but is not at the beginning, this call will fail (return NULL).
26 *
27 * @param stream SkStream to be copied into data.
28 * @return The resulting SkData after the copy, nullptr on failure.
29 */
30 sk_sp<SkData> CopyStreamToData(SkStream* stream);
31
32 /**
33 * Copies the input stream from the current position to the end.
34 * Does not rewind the input stream.
35 */
36 bool Copy(SkWStream* out, SkStream* input);
37
38 /** A SkWStream that writes all output to SkDebugf, for debugging purposes. */
39 class DebugfStream final : public SkWStream {
40 public:
41 bool write(const void* buffer, size_t size) override;
42 size_t bytesWritten() const override;
43
44 private:
45 size_t fBytesWritten = 0;
46 };
47
48 /**
49 * Helper functions to read and write big-endian values to a stream.
50 */
51 inline bool WriteU16BE(SkWStream* s, uint16_t value) {
52 value = SkEndian_SwapBE16(value);
53 return s->write(&value, sizeof(value));
54 }
55
56 inline bool WriteU32BE(SkWStream* s, uint32_t value) {
57 value = SkEndian_SwapBE32(value);
58 return s->write(&value, sizeof(value));
59 }
60
61 inline bool WriteS32BE(SkWStream* s, int32_t value) {
62 value = SkEndian_SwapBE32(value);
63 return s->write(&value, sizeof(value));
64 }
65
66 inline bool ReadU16BE(SkStream* s, uint16_t* value) {
67 if (!s->readU16(value)) {
68 return false;
69 }
70 *value = SkEndian_SwapBE16(*value);
71 return true;
72 }
73
74 inline bool ReadU32BE(SkStream* s, uint32_t* value) {
75 if (!s->readU32(value)) {
76 return false;
77 }
78 *value = SkEndian_SwapBE32(*value);
79 return true;
80 }
81
82 inline bool ReadS32BE(SkStream* s, int32_t* value) {
83 if (!s->readS32(value)) {
84 return false;
85 }
86 *value = SkEndian_SwapBE32(*value);
87 return true;
88 }
89
90 // If the stream supports identifying the current position and total length, this returns
91 // true if there are not enough bytes in the stream to fulfill a read of the given length.
92 // Otherwise, it returns false.
93 // False does *not* mean a read will succeed of the given length, but true means we are
94 // certain it will fail.
95 bool RemainingLengthIsBelow(SkStream* stream, size_t len);
96
97 } // namespace SkStreamPriv
98
99 #endif // SkStreamPriv_DEFINED