]>
git.saurik.com Git - apple/security.git/blob - cdsa/cdsa_utilities/streams.h
2 * Copyright (c) 2000-2001 Apple Computer, Inc. All Rights Reserved.
4 * The contents of this file constitute Original Code as defined in and are
5 * subject to the Apple Public Source License Version 1.2 (the 'License').
6 * You may not use this file except in compliance with the License. Please obtain
7 * a copy of the License at http://www.apple.com/publicsource and read it before
10 * This Original Code and all software distributed under the License are
11 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESS
12 * OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES, INCLUDING WITHOUT
13 * LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
14 * PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see the License for the
15 * specific language governing rights and limitations under the License.
20 // streams.h - lightweight source and sink objects
30 using UnixPlusPlus::FileDesc
;
34 // An abstract Source object.
35 // Source can yield data when its produce method is called. Produce can yield
36 // anything between zero and length bytes and sets length accordingly.
37 // If the last call to produce returned zero bytes (and only then), the state method
38 // will yield an explanation:
39 // producing -> we're in business; there just no data quite yet (try again)
40 // stalled -> there may be more data coming, but not in the near future;
41 // wait a while then call state again to see
42 // endOfData -> no more data will be produced by this Source
43 // When called *before* the first call to produce, getSize may return the number
44 // of bytes that all calls to produce will yield together. If getSize returns unknownSize,
45 // this value cannot be determined beforehand. GetSize *may* yield the number of bytes
46 // yet to come when called after produce, but this is not guaranteed for all Sources.
50 virtual void produce(void *data
, size_t &length
) = 0;
53 static const size_t unknownSize
= size_t(-1);
54 virtual size_t getSize();
57 producing
, // yielding data (go ahead)
58 stalled
, // no data now, perhaps more later
59 endOfData
// end of data (no more data)
61 virtual State
state() const;
64 State mState
; // auto-regulated state (can be overridden)
69 // An abstract Sink object.
70 // Sinks can cansume data when their consume method is called.
71 // Sinks cannot refuse data; they always consume all data given to consume.
72 // There is currently no flow control/throttle mechanism (one will probably
78 virtual void consume(const void *data
, size_t length
) = 0;
79 virtual void setSize(size_t expectedSize
);
84 // The NullSource produces no data.
86 class NullSource
: public Source
{
88 void produce(void *addr
, size_t &len
);
94 // A FileSource reads from a UNIX file or file descriptor.
95 // Note that getSize will yield the size of the underlying i-node,
96 // which is usually correct but may not be in the case of simultaneous
99 class FileSource
: public Source
, public FileDesc
{
101 FileSource(const char *path
, int mode
= O_RDONLY
) : FileDesc(path
, mode
) { mState
= producing
; }
102 FileSource(int fd
) : FileDesc(fd
) { mState
= producing
; }
103 void produce(void *data
, size_t &length
);
109 // A MemorySource yields the contents of a preset contiguous memory block.
111 class MemorySource
: public Source
{
113 MemorySource(const void *data
, size_t length
) : mData(data
), mRemaining(length
) { }
115 template <class Data
>
116 MemorySource(const Data
&data
) : mData(data
.data()), mRemaining(data
.length()) { }
118 void produce(void *data
, size_t &length
);
129 // A NullSink eats all data and discards it quietly.
131 class NullSink
: public Sink
{
133 void consume(const void *data
, size_t length
);
138 // A FileSink writes its received data to a UNIX file or file descriptor.
140 class FileSink
: public Sink
, public FileDesc
{
142 FileSink(const char *path
, int mode
= O_WRONLY
| O_CREAT
| O_TRUNC
)
143 : FileDesc(path
, mode
) { }
144 FileSink(int fd
) : FileDesc(fd
) { }
145 void consume(const void *data
, size_t length
);
150 // MemorySinks collect output in a contiguous memory block.
151 // This is not often a good idea, so if you find yourself using this,
152 // consider consuming on-the-fly or streaming to secondary media,
153 // or (at least) use a BufferFifo instead.
155 class MemorySink
: public Sink
{
157 MemorySink() : mBuffer(NULL
), mSize(0), mMax(0) { }
158 ~MemorySink() { free(mBuffer
); }
160 void consume(const void *data
, size_t length
);
161 void setSize(size_t expectedSize
);
163 void *data() const { return mBuffer
; }
164 size_t length() const { return mSize
; }
166 void clear() { free(mBuffer
); mBuffer
= NULL
; mSize
= mMax
= 0; }
169 void grow(size_t newSize
);
172 void *mBuffer
; // buffer base
173 size_t mSize
; // currently used
174 size_t mMax
; // currently allocated
178 } // end namespace Security