]>
git.saurik.com Git - apple/security.git/blob - OSX/libsecurity_utilities/lib/streams.cpp
2 * Copyright (c) 2000-2001,2003-2004,2011,2014 Apple Inc. All Rights Reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
26 // streams.h - lightweight source and sink objects
29 #include <security_utilities/memutils.h>
36 // Source and Sink abstract superclasses
38 Source::State
Source::state() const
43 size_t Source::getSize()
48 void Sink::setSize(size_t)
55 // Null sources and sinks
57 void NullSource::produce(void *, size_t &length
)
62 Source::State
NullSource::state() const
67 void NullSink::consume(const void *, size_t)
74 // File sources and sinks
76 void FileSource::produce(void *data
, size_t &length
)
78 if ((length
= read(data
, length
)) == 0)
82 size_t FileSource::getSize()
88 void FileSink::consume(const void *data
, size_t length
)
97 void MemorySource::produce(void *data
, size_t &length
)
99 if (mRemaining
< length
)
101 memcpy(data
, mData
, length
);
102 mData
= LowLevelMemoryUtilities::increment(mData
, length
);
103 mRemaining
-= length
;
106 size_t MemorySource::getSize()
111 Source::State
MemorySource::state() const
113 return mRemaining
? producing
: endOfData
;
120 void MemorySink::consume(const void *data
, size_t length
)
122 if (mSize
+ length
> mMax
)
124 assert(mSize
+ length
<= mMax
);
125 memcpy(((char *)mBuffer
) + mSize
, data
, length
);
129 void MemorySink::setSize(size_t expectedSize
)
134 void MemorySink::grow(size_t newSize
)
136 if (void *p
= realloc(mBuffer
, newSize
)) {
140 UnixError::throwMe();
144 } // end namespace Security