xref: /aosp_15_r20/external/skia/src/core/SkStreamPriv.h (revision c8dee2aa9b3f27cf6c858bd81872bdeb2c07ed17)
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 /**
20  *  Copy the provided stream to an SkData variable.
21  *
22  *  Note: Assumes the stream is at the beginning. If it has a length,
23  *  but is not at the beginning, this call will fail (return NULL).
24  *
25  *  @param stream SkStream to be copied into data.
26  *  @return The resulting SkData after the copy, nullptr on failure.
27  */
28 sk_sp<SkData> SkCopyStreamToData(SkStream* stream);
29 
30 /**
31  *  Copies the input stream from the current position to the end.
32  *  Does not rewind the input stream.
33  */
34 bool SkStreamCopy(SkWStream* out, SkStream* input);
35 
36 /** A SkWStream that writes all output to SkDebugf, for debugging purposes. */
37 class SkDebugfStream final : public SkWStream {
38 public:
39     bool write(const void* buffer, size_t size) override;
40     size_t bytesWritten() const override;
41 
42 private:
43     size_t fBytesWritten = 0;
44 };
45 
46 /**
47  * Helper functions to write big-endian values to a stream.
48  */
SkWStreamWriteU16BE(SkWStream * s,uint16_t value)49 inline bool SkWStreamWriteU16BE(SkWStream* s, uint16_t value) {
50     value = SkEndian_SwapBE16(value);
51     return s->write(&value, sizeof(value));
52 }
53 
SkWStreamWriteU32BE(SkWStream * s,uint32_t value)54 inline bool SkWStreamWriteU32BE(SkWStream* s, uint32_t value) {
55     value = SkEndian_SwapBE32(value);
56     return s->write(&value, sizeof(value));
57 }
58 
SkWStreamWriteS32BE(SkWStream * s,int32_t value)59 inline bool SkWStreamWriteS32BE(SkWStream* s, int32_t value) {
60     value = SkEndian_SwapBE32(value);
61     return s->write(&value, sizeof(value));
62 }
63 
64 // If the stream supports identifying the current position and total length, this returns
65 // true if there are not enough bytes in the stream to fulfill a read of the given length.
66 // Otherwise, it returns false.
67 // False does *not* mean a read will succeed of the given length, but true means we are
68 // certain it will fail.
69 bool StreamRemainingLengthIsBelow(SkStream* stream, size_t len);
70 
71 #endif  // SkStreamPriv_DEFINED
72