1/*
2 * Copyright (c) 2000-2001,2003-2004,2011,2014 Apple Inc. All Rights Reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
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
11 * file.
12 *
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.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
23
24
25//
26// streams.h - lightweight source and sink objects
27//
28#include "streams.h"
29#include <security_utilities/memutils.h>
30
31
32namespace Security {
33
34
35//
36// Source and Sink abstract superclasses
37//
38Source::State Source::state() const
39{
40    return mState;
41}
42
43size_t Source::getSize()
44{
45    return unknownSize;
46}
47
48void Sink::setSize(size_t)
49{
50    // ignored
51}
52
53
54//
55// Null sources and sinks
56//
57void NullSource::produce(void *, size_t &length)
58{
59    length = 0;
60}
61
62Source::State NullSource::state() const
63{
64    return endOfData;
65}
66
67void NullSink::consume(const void *, size_t)
68{
69    // ignore the data
70}
71
72
73//
74// File sources and sinks
75//
76void FileSource::produce(void *data, size_t &length)
77{
78    if ((length = read(data, length)) == 0)
79        mState = endOfData;
80}
81
82size_t FileSource::getSize()
83{
84    return fileSize();
85}
86
87
88void FileSink::consume(const void *data, size_t length)
89{
90	write(data, length);
91}
92
93
94//
95// Memory sources
96//
97void MemorySource::produce(void *data, size_t &length)
98{
99    if (mRemaining < length)
100        length = mRemaining;
101    memcpy(data, mData, length);
102    mData = LowLevelMemoryUtilities::increment(mData, length);
103    mRemaining -= length;
104}
105
106size_t MemorySource::getSize()
107{
108    return mRemaining;
109}
110
111Source::State MemorySource::state() const
112{
113    return mRemaining ? producing : endOfData;
114}
115
116
117//
118// Memory sinks
119//
120void MemorySink::consume(const void *data, size_t length)
121{
122    if (mSize + length > mMax)
123        grow(mSize * 3 / 2);
124    assert(mSize + length <= mMax);
125    memcpy(((char *)mBuffer) + mSize, data, length);
126    mSize += length;
127}
128
129void MemorySink::setSize(size_t expectedSize)
130{
131    grow(expectedSize);
132}
133
134void MemorySink::grow(size_t newSize)
135{
136    if (void *p = realloc(mBuffer, newSize)) {
137        mBuffer = p;
138        mMax = newSize;
139    } else
140        UnixError::throwMe();
141}
142
143
144}	// end namespace Security
145