monotone

monotone Mtn Source Tree

Root/cryptopp/filters.h

1#ifndef CRYPTOPP_FILTERS_H
2#define CRYPTOPP_FILTERS_H
3
4#include "simple.h"
5#include "secblock.h"
6#include "misc.h"
7#include "smartptr.h"
8#include "queue.h"
9#include "algparam.h"
10
11NAMESPACE_BEGIN(CryptoPP)
12
13/// provides an implementation of BufferedTransformation's attachment interface
14class Filter : public BufferedTransformation, public NotCopyable
15{
16public:
17Filter(BufferedTransformation *attachment);
18
19bool Attachable() {return true;}
20BufferedTransformation *AttachedTransformation();
21const BufferedTransformation *AttachedTransformation() const;
22void Detach(BufferedTransformation *newAttachment = NULL);
23
24unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
25unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
26
27void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1);
28bool Flush(bool hardFlush, int propagation=-1, bool blocking=true);
29bool MessageSeriesEnd(int propagation=-1, bool blocking=true);
30
31protected:
32virtual void NotifyAttachmentChange() {}
33virtual BufferedTransformation * NewDefaultAttachment() const;
34void Insert(Filter *nextFilter);// insert filter after this one
35
36virtual bool ShouldPropagateMessageEnd() const {return true;}
37virtual bool ShouldPropagateMessageSeriesEnd() const {return true;}
38
39void PropagateInitialize(const NameValuePairs &parameters, int propagation, const std::string &channel=NULL_CHANNEL);
40
41unsigned int Output(int outputSite, const byte *inString, unsigned int length, int messageEnd, bool blocking, const std::string &channel=NULL_CHANNEL);
42bool OutputMessageEnd(int outputSite, int propagation, bool blocking, const std::string &channel=NULL_CHANNEL);
43bool OutputFlush(int outputSite, bool hardFlush, int propagation, bool blocking, const std::string &channel=NULL_CHANNEL);
44bool OutputMessageSeriesEnd(int outputSite, int propagation, bool blocking, const std::string &channel=NULL_CHANNEL);
45
46private:
47member_ptr<BufferedTransformation> m_attachment;
48
49protected:
50unsigned int m_inputPosition;
51int m_continueAt;
52};
53
54struct FilterPutSpaceHelper
55{
56// desiredSize is how much to ask target, bufferSize is how much to allocate in m_tempSpace
57byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, unsigned int minSize, unsigned int desiredSize, unsigned int &bufferSize)
58{
59assert(desiredSize >= minSize && bufferSize >= minSize);
60if (m_tempSpace.size() < minSize)
61{
62byte *result = target.ChannelCreatePutSpace(channel, desiredSize);
63if (desiredSize >= minSize)
64{
65bufferSize = desiredSize;
66return result;
67}
68m_tempSpace.New(bufferSize);
69}
70
71bufferSize = m_tempSpace.size();
72return m_tempSpace.begin();
73}
74byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, unsigned int minSize)
75{return HelpCreatePutSpace(target, channel, minSize, minSize, minSize);}
76byte *HelpCreatePutSpace(BufferedTransformation &target, const std::string &channel, unsigned int minSize, unsigned int bufferSize)
77{return HelpCreatePutSpace(target, channel, minSize, minSize, bufferSize);}
78SecByteBlock m_tempSpace;
79};
80
81//! measure how many byte and messages pass through, also serves as valve
82class MeterFilter : public Bufferless<Filter>
83{
84public:
85MeterFilter(BufferedTransformation *attachment=NULL, bool transparent=true)
86: Bufferless<Filter>(attachment), m_transparent(transparent) {ResetMeter();}
87
88void SetTransparent(bool transparent) {m_transparent = transparent;}
89void ResetMeter() {m_currentMessageBytes = m_totalBytes = m_currentSeriesMessages = m_totalMessages = m_totalMessageSeries = 0;}
90
91unsigned long GetCurrentMessageBytes() const {return m_currentMessageBytes;}
92unsigned long GetTotalBytes() {return m_totalBytes;}
93unsigned int GetCurrentSeriesMessages() {return m_currentSeriesMessages;}
94unsigned int GetTotalMessages() {return m_totalMessages;}
95unsigned int GetTotalMessageSeries() {return m_totalMessageSeries;}
96
97unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking);
98bool IsolatedMessageSeriesEnd(bool blocking);
99
100private:
101bool ShouldPropagateMessageEnd() const {return m_transparent;}
102bool ShouldPropagateMessageSeriesEnd() const {return m_transparent;}
103
104bool m_transparent;
105unsigned long m_currentMessageBytes, m_totalBytes;
106unsigned int m_currentSeriesMessages, m_totalMessages, m_totalMessageSeries;
107};
108
109//! .
110class TransparentFilter : public MeterFilter
111{
112public:
113TransparentFilter(BufferedTransformation *attachment=NULL) : MeterFilter(attachment, true) {}
114};
115
116//! .
117class OpaqueFilter : public MeterFilter
118{
119public:
120OpaqueFilter(BufferedTransformation *attachment=NULL) : MeterFilter(attachment, false) {}
121};
122
123/*! FilterWithBufferedInput divides up the input stream into
124a first block, a number of middle blocks, and a last block.
125First and last blocks are optional, and middle blocks may
126be a stream instead (i.e. blockSize == 1).
127*/
128class FilterWithBufferedInput : public Filter
129{
130public:
131FilterWithBufferedInput(BufferedTransformation *attachment);
132//! firstSize and lastSize may be 0, blockSize must be at least 1
133FilterWithBufferedInput(unsigned int firstSize, unsigned int blockSize, unsigned int lastSize, BufferedTransformation *attachment);
134
135void IsolatedInitialize(const NameValuePairs &parameters);
136unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking)
137{
138return PutMaybeModifiable(const_cast<byte *>(inString), length, messageEnd, blocking, false);
139}
140unsigned int PutModifiable2(byte *inString, unsigned int length, int messageEnd, bool blocking)
141{
142return PutMaybeModifiable(inString, length, messageEnd, blocking, true);
143}
144/*! calls ForceNextPut() if hardFlush is true */
145bool IsolatedFlush(bool hardFlush, bool blocking);
146
147/*! The input buffer may contain more than blockSize bytes if lastSize != 0.
148ForceNextPut() forces a call to NextPut() if this is the case.
149*/
150void ForceNextPut();
151
152protected:
153bool DidFirstPut() {return m_firstInputDone;}
154
155virtual void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, unsigned int &firstSize, unsigned int &blockSize, unsigned int &lastSize)
156{InitializeDerived(parameters);}
157virtual void InitializeDerived(const NameValuePairs &parameters) {}
158// FirstPut() is called if (firstSize != 0 and totalLength >= firstSize)
159// or (firstSize == 0 and (totalLength > 0 or a MessageEnd() is received))
160virtual void FirstPut(const byte *inString) =0;
161// NextPut() is called if totalLength >= firstSize+blockSize+lastSize
162virtual void NextPutSingle(const byte *inString) {assert(false);}
163// Same as NextPut() except length can be a multiple of blockSize
164// Either NextPut() or NextPutMultiple() must be overriden
165virtual void NextPutMultiple(const byte *inString, unsigned int length);
166// Same as NextPutMultiple(), but inString can be modified
167virtual void NextPutModifiable(byte *inString, unsigned int length)
168{NextPutMultiple(inString, length);}
169// LastPut() is always called
170// if totalLength < firstSize then length == totalLength
171// else if totalLength <= firstSize+lastSize then length == totalLength-firstSize
172// else lastSize <= length < lastSize+blockSize
173virtual void LastPut(const byte *inString, unsigned int length) =0;
174virtual void FlushDerived() {}
175
176private:
177unsigned int PutMaybeModifiable(byte *begin, unsigned int length, int messageEnd, bool blocking, bool modifiable);
178void NextPutMaybeModifiable(byte *inString, unsigned int length, bool modifiable)
179{
180if (modifiable) NextPutModifiable(inString, length);
181else NextPutMultiple(inString, length);
182}
183
184// This function should no longer be used, put this here to cause a compiler error
185// if someone tries to override NextPut().
186virtual int NextPut(const byte *inString, unsigned int length) {assert(false); return 0;}
187
188class BlockQueue
189{
190public:
191void ResetQueue(unsigned int blockSize, unsigned int maxBlocks);
192byte *GetBlock();
193byte *GetContigousBlocks(unsigned int &numberOfBytes);
194unsigned int GetAll(byte *outString);
195void Put(const byte *inString, unsigned int length);
196unsigned int CurrentSize() const {return m_size;}
197unsigned int MaxSize() const {return m_buffer.size();}
198
199private:
200SecByteBlock m_buffer;
201unsigned int m_blockSize, m_maxBlocks, m_size;
202byte *m_begin;
203};
204
205unsigned int m_firstSize, m_blockSize, m_lastSize;
206bool m_firstInputDone;
207BlockQueue m_queue;
208};
209
210//! .
211class FilterWithInputQueue : public Filter
212{
213public:
214FilterWithInputQueue(BufferedTransformation *attachment) : Filter(attachment) {}
215unsigned int Put2(const byte *inString, unsigned int length, int messageEnd, bool blocking)
216{
217if (!blocking)
218throw BlockingInputOnly("FilterWithInputQueue");
219
220m_inQueue.Put(inString, length);
221if (messageEnd)
222{
223IsolatedMessageEnd(blocking);
224Output(0, NULL, 0, messageEnd, blocking);
225}
226return 0;
227}
228
229protected:
230virtual bool IsolatedMessageEnd(bool blocking) =0;
231void IsolatedInitialize(const NameValuePairs &parameters) {m_inQueue.Clear();}
232
233ByteQueue m_inQueue;
234};
235
236//! Filter Wrapper for StreamTransformation
237class StreamTransformationFilter : public FilterWithBufferedInput, private FilterPutSpaceHelper
238{
239public:
240enum BlockPaddingScheme {NO_PADDING, ZEROS_PADDING, PKCS_PADDING, ONE_AND_ZEROS_PADDING, DEFAULT_PADDING};
241/*! DEFAULT_PADDING means PKCS_PADDING if c.MandatoryBlockSize() > 1 && c.MinLastBlockSize() == 0 (e.g. ECB or CBC mode),
242otherwise NO_PADDING (OFB, CFB, CTR, CBC-CTS modes) */
243StreamTransformationFilter(StreamTransformation &c, BufferedTransformation *attachment = NULL, BlockPaddingScheme padding = DEFAULT_PADDING);
244
245void FirstPut(const byte *inString);
246void NextPutMultiple(const byte *inString, unsigned int length);
247void NextPutModifiable(byte *inString, unsigned int length);
248void LastPut(const byte *inString, unsigned int length);
249//byte * CreatePutSpace(unsigned int &size);
250
251protected:
252static unsigned int LastBlockSize(StreamTransformation &c, BlockPaddingScheme padding);
253
254StreamTransformation &m_cipher;
255BlockPaddingScheme m_padding;
256unsigned int m_optimalBufferSize;
257};
258
259#ifdef CRYPTOPP_MAINTAIN_BACKWARDS_COMPATIBILITY
260typedef StreamTransformationFilter StreamCipherFilter;
261#endif
262
263//! Filter Wrapper for HashTransformation
264class HashFilter : public Bufferless<Filter>, private FilterPutSpaceHelper
265{
266public:
267HashFilter(HashTransformation &hm, BufferedTransformation *attachment = NULL, bool putMessage=false)
268: Bufferless<Filter>(attachment), m_hashModule(hm), m_putMessage(putMessage) {}
269
270void IsolatedInitialize(const NameValuePairs &parameters);
271unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking);
272
273byte * CreatePutSpace(unsigned int &size) {return m_hashModule.CreateUpdateSpace(size);}
274
275private:
276HashTransformation &m_hashModule;
277bool m_putMessage;
278byte *m_space;
279};
280
281//! Filter Wrapper for HashTransformation
282class HashVerificationFilter : public FilterWithBufferedInput
283{
284public:
285class HashVerificationFailed : public Exception
286{
287public:
288HashVerificationFailed()
289: Exception(DATA_INTEGRITY_CHECK_FAILED, "HashVerifier: message hash not valid") {}
290};
291
292enum Flags {HASH_AT_BEGIN=1, PUT_MESSAGE=2, PUT_HASH=4, PUT_RESULT=8, THROW_EXCEPTION=16, DEFAULT_FLAGS = HASH_AT_BEGIN | PUT_RESULT};
293HashVerificationFilter(HashTransformation &hm, BufferedTransformation *attachment = NULL, word32 flags = DEFAULT_FLAGS);
294
295bool GetLastResult() const {return m_verified;}
296
297protected:
298void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, unsigned int &firstSize, unsigned int &blockSize, unsigned int &lastSize);
299void FirstPut(const byte *inString);
300void NextPutMultiple(const byte *inString, unsigned int length);
301void LastPut(const byte *inString, unsigned int length);
302
303private:
304static inline unsigned int FirstSize(word32 flags, HashTransformation &hm) {return flags & HASH_AT_BEGIN ? hm.DigestSize() : 0;}
305static inline unsigned int LastSize(word32 flags, HashTransformation &hm) {return flags & HASH_AT_BEGIN ? 0 : hm.DigestSize();}
306
307HashTransformation &m_hashModule;
308word32 m_flags;
309SecByteBlock m_expectedHash;
310bool m_verified;
311};
312
313typedef HashVerificationFilter HashVerifier;// for backwards compatibility
314
315//! Filter Wrapper for PK_Signer
316class SignerFilter : public Unflushable<Filter>
317{
318public:
319SignerFilter(RandomNumberGenerator &rng, const PK_Signer &signer, BufferedTransformation *attachment = NULL, bool putMessage=false)
320: Unflushable<Filter>(attachment), m_rng(rng), m_signer(signer), m_messageAccumulator(signer.NewSignatureAccumulator()), m_putMessage(putMessage) {}
321
322void IsolatedInitialize(const NameValuePairs &parameters);
323unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking);
324
325private:
326RandomNumberGenerator &m_rng;
327const PK_Signer&m_signer;
328member_ptr<PK_MessageAccumulator> m_messageAccumulator;
329bool m_putMessage;
330SecByteBlock m_buf;
331};
332
333//! Filter Wrapper for PK_Verifier
334class SignatureVerificationFilter : public FilterWithBufferedInput
335{
336public:
337class SignatureVerificationFailed : public Exception
338{
339public:
340SignatureVerificationFailed()
341: Exception(DATA_INTEGRITY_CHECK_FAILED, "VerifierFilter: digital signature not valid") {}
342};
343
344enum Flags {SIGNATURE_AT_BEGIN=1, PUT_MESSAGE=2, PUT_SIGNATURE=4, PUT_RESULT=8, THROW_EXCEPTION=16, DEFAULT_FLAGS = SIGNATURE_AT_BEGIN | PUT_RESULT};
345SignatureVerificationFilter(const PK_Verifier &verifier, BufferedTransformation *attachment = NULL, word32 flags = DEFAULT_FLAGS);
346
347bool GetLastResult() const {return m_verified;}
348
349protected:
350void InitializeDerivedAndReturnNewSizes(const NameValuePairs &parameters, unsigned int &firstSize, unsigned int &blockSize, unsigned int &lastSize);
351void FirstPut(const byte *inString);
352void NextPutMultiple(const byte *inString, unsigned int length);
353void LastPut(const byte *inString, unsigned int length);
354
355private:
356const PK_Verifier &m_verifier;
357member_ptr<PK_MessageAccumulator> m_messageAccumulator;
358word32 m_flags;
359SecByteBlock m_signature;
360bool m_verified;
361};
362
363typedef SignatureVerificationFilter VerifierFilter; // for backwards compatibility
364
365//! Redirect input to another BufferedTransformation without owning it
366class Redirector : public CustomSignalPropagation<Sink>
367{
368public:
369Redirector() : m_target(NULL), m_passSignal(true) {}
370Redirector(BufferedTransformation &target, bool passSignal=true) : m_target(&target), m_passSignal(passSignal) {}
371
372void Redirect(BufferedTransformation &target) {m_target = &target;}
373void StopRedirection() {m_target = NULL;}
374bool GetPassSignal() const {return m_passSignal;}
375void SetPassSignal(bool passSignal) {m_passSignal = passSignal;}
376
377unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
378{return m_target ? m_target->Put2(begin, length, m_passSignal ? messageEnd : 0, blocking) : 0;}
379void Initialize(const NameValuePairs &parameters, int propagation)
380{ChannelInitialize(NULL_CHANNEL, parameters, propagation);}
381bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
382{return m_target && m_passSignal ? m_target->Flush(hardFlush, propagation, blocking) : false;}
383bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
384{return m_target && m_passSignal ? m_target->MessageSeriesEnd(propagation, blocking) : false;}
385
386void ChannelInitialize(const std::string &channel, const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1);
387unsigned int ChannelPut2(const std::string &channel, const byte *begin, unsigned int length, int messageEnd, bool blocking)
388{return m_target ? m_target->ChannelPut2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking) : 0;}
389unsigned int ChannelPutModifiable2(const std::string &channel, byte *begin, unsigned int length, int messageEnd, bool blocking)
390{return m_target ? m_target->ChannelPutModifiable2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking) : 0;}
391bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
392{return m_target && m_passSignal ? m_target->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
393bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
394{return m_target && m_passSignal ? m_target->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
395
396private:
397BufferedTransformation *m_target;
398bool m_passSignal;
399};
400
401// Used By ProxyFilter
402class OutputProxy : public CustomSignalPropagation<Sink>
403{
404public:
405OutputProxy(BufferedTransformation &owner, bool passSignal) : m_owner(owner), m_passSignal(passSignal) {}
406
407bool GetPassSignal() const {return m_passSignal;}
408void SetPassSignal(bool passSignal) {m_passSignal = passSignal;}
409
410unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
411{return m_owner.AttachedTransformation()->Put2(begin, length, m_passSignal ? messageEnd : 0, blocking);}
412unsigned int PutModifiable2(byte *begin, unsigned int length, int messageEnd, bool blocking)
413{return m_owner.AttachedTransformation()->PutModifiable2(begin, length, m_passSignal ? messageEnd : 0, blocking);}
414void Initialize(const NameValuePairs &parameters=g_nullNameValuePairs, int propagation=-1)
415{if (m_passSignal) m_owner.AttachedTransformation()->Initialize(parameters, propagation);}
416bool Flush(bool hardFlush, int propagation=-1, bool blocking=true)
417{return m_passSignal ? m_owner.AttachedTransformation()->Flush(hardFlush, propagation, blocking) : false;}
418bool MessageSeriesEnd(int propagation=-1, bool blocking=true)
419{return m_passSignal ? m_owner.AttachedTransformation()->MessageSeriesEnd(propagation, blocking) : false;}
420
421unsigned int ChannelPut2(const std::string &channel, const byte *begin, unsigned int length, int messageEnd, bool blocking)
422{return m_owner.AttachedTransformation()->ChannelPut2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
423unsigned int ChannelPutModifiable2(const std::string &channel, byte *begin, unsigned int length, int messageEnd, bool blocking)
424{return m_owner.AttachedTransformation()->ChannelPutModifiable2(channel, begin, length, m_passSignal ? messageEnd : 0, blocking);}
425void ChannelInitialize(const std::string &channel, const NameValuePairs &parameters, int propagation=-1)
426{if (m_passSignal) m_owner.AttachedTransformation()->ChannelInitialize(channel, parameters, propagation);}
427bool ChannelFlush(const std::string &channel, bool completeFlush, int propagation=-1, bool blocking=true)
428{return m_passSignal ? m_owner.AttachedTransformation()->ChannelFlush(channel, completeFlush, propagation, blocking) : false;}
429bool ChannelMessageSeriesEnd(const std::string &channel, int propagation=-1, bool blocking=true)
430{return m_passSignal ? m_owner.AttachedTransformation()->ChannelMessageSeriesEnd(channel, propagation, blocking) : false;}
431
432private:
433BufferedTransformation &m_owner;
434bool m_passSignal;
435};
436
437//! Base class for Filter classes that are proxies for a chain of other filters.
438class ProxyFilter : public FilterWithBufferedInput
439{
440public:
441ProxyFilter(BufferedTransformation *filter, unsigned int firstSize, unsigned int lastSize, BufferedTransformation *attachment);
442
443bool IsolatedFlush(bool hardFlush, bool blocking);
444
445void SetFilter(Filter *filter);
446void NextPutMultiple(const byte *s, unsigned int len);
447
448protected:
449member_ptr<BufferedTransformation> m_filter;
450};
451
452//! simple proxy filter that doesn't modify the underlying filter's input or output
453class SimpleProxyFilter : public ProxyFilter
454{
455public:
456SimpleProxyFilter(BufferedTransformation *filter, BufferedTransformation *attachment)
457: ProxyFilter(filter, 0, 0, attachment) {}
458
459void FirstPut(const byte *) {}
460void LastPut(const byte *, unsigned int) {m_filter->MessageEnd();}
461};
462
463//! proxy for the filter created by PK_Encryptor::CreateEncryptionFilter
464/*! This class is here just to provide symmetry with VerifierFilter. */
465class PK_EncryptorFilter : public SimpleProxyFilter
466{
467public:
468PK_EncryptorFilter(RandomNumberGenerator &rng, const PK_Encryptor &encryptor, BufferedTransformation *attachment = NULL)
469: SimpleProxyFilter(encryptor.CreateEncryptionFilter(rng), attachment) {}
470};
471
472//! proxy for the filter created by PK_Decryptor::CreateDecryptionFilter
473/*! This class is here just to provide symmetry with SignerFilter. */
474class PK_DecryptorFilter : public SimpleProxyFilter
475{
476public:
477PK_DecryptorFilter(RandomNumberGenerator &rng, const PK_Decryptor &decryptor, BufferedTransformation *attachment = NULL)
478: SimpleProxyFilter(decryptor.CreateDecryptionFilter(rng), attachment) {}
479};
480
481//! Append input to a string object
482template <class T>
483class StringSinkTemplate : public Bufferless<Sink>
484{
485public:
486// VC60 workaround: no T::char_type
487typedef typename T::traits_type::char_type char_type;
488
489StringSinkTemplate(T &output)
490: m_output(&output) {assert(sizeof(output[0])==1);}
491
492void IsolatedInitialize(const NameValuePairs &parameters)
493{if (!parameters.GetValue("OutputStringPointer", m_output)) throw InvalidArgument("StringSink: OutputStringPointer not specified");}
494
495unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking)
496{
497if (length > 0)
498{
499typename T::size_type size = m_output->size();
500if (length < size && size + length > m_output->capacity())
501m_output->reserve(2*size);
502m_output->append((const char_type *)begin, (const char_type *)begin+length);
503}
504return 0;
505}
506
507private:
508T *m_output;
509};
510
511//! Append input to an std::string
512typedef StringSinkTemplate<std::string> StringSink;
513
514//! Copy input to a memory buffer
515class ArraySink : public Bufferless<Sink>
516{
517public:
518ArraySink(const NameValuePairs &parameters = g_nullNameValuePairs) {IsolatedInitialize(parameters);}
519ArraySink(byte *buf, unsigned int size) : m_buf(buf), m_size(size), m_total(0) {}
520
521unsigned int AvailableSize() {return m_size - STDMIN(m_total, (unsigned long)m_size);}
522unsigned long TotalPutLength() {return m_total;}
523
524void IsolatedInitialize(const NameValuePairs &parameters);
525byte * CreatePutSpace(unsigned int &size);
526unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking);
527
528protected:
529byte *m_buf;
530unsigned int m_size;
531unsigned long m_total;
532};
533
534//! Xor input to a memory buffer
535class ArrayXorSink : public ArraySink
536{
537public:
538ArrayXorSink(byte *buf, unsigned int size)
539: ArraySink(buf, size) {}
540
541unsigned int Put2(const byte *begin, unsigned int length, int messageEnd, bool blocking);
542byte * CreatePutSpace(unsigned int &size) {return BufferedTransformation::CreatePutSpace(size);}
543};
544
545//! .
546class StringStore : public Store
547{
548public:
549StringStore(const char *string = NULL)
550{StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
551StringStore(const byte *string, unsigned int length)
552{StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
553template <class T> StringStore(const T &string)
554{StoreInitialize(MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
555
556unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
557unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
558
559private:
560void StoreInitialize(const NameValuePairs &parameters);
561
562const byte *m_store;
563unsigned int m_length, m_count;
564};
565
566//! .
567class RandomNumberStore : public Store
568{
569public:
570RandomNumberStore(RandomNumberGenerator &rng, unsigned long length)
571: m_rng(rng), m_length(length), m_count(0) {}
572
573bool AnyRetrievable() const {return MaxRetrievable() != 0;}
574unsigned long MaxRetrievable() const {return m_length-m_count;}
575
576unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
577unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const
578{
579throw NotImplemented("RandomNumberStore: CopyRangeTo2() is not supported by this store");
580}
581
582private:
583void StoreInitialize(const NameValuePairs &parameters) {m_count = 0;}
584
585RandomNumberGenerator &m_rng;
586const unsigned long m_length;
587unsigned long m_count;
588};
589
590//! .
591class NullStore : public Store
592{
593public:
594NullStore(unsigned long size = ULONG_MAX) : m_size(size) {}
595void StoreInitialize(const NameValuePairs &parameters) {}
596unsigned long MaxRetrievable() const {return m_size;}
597unsigned int TransferTo2(BufferedTransformation &target, unsigned long &transferBytes, const std::string &channel=NULL_CHANNEL, bool blocking=true);
598unsigned int CopyRangeTo2(BufferedTransformation &target, unsigned long &begin, unsigned long end=ULONG_MAX, const std::string &channel=NULL_CHANNEL, bool blocking=true) const;
599
600private:
601unsigned long m_size;
602};
603
604//! A Filter that pumps data into its attachment as input
605class Source : public InputRejecting<Filter>
606{
607public:
608Source(BufferedTransformation *attachment)
609: InputRejecting<Filter>(attachment) {}
610
611unsigned long Pump(unsigned long pumpMax=ULONG_MAX)
612{Pump2(pumpMax); return pumpMax;}
613unsigned int PumpMessages(unsigned int count=UINT_MAX)
614{PumpMessages2(count); return count;}
615void PumpAll()
616{PumpAll2();}
617virtual unsigned int Pump2(unsigned long &byteCount, bool blocking=true) =0;
618virtual unsigned int PumpMessages2(unsigned int &messageCount, bool blocking=true) =0;
619virtual unsigned int PumpAll2(bool blocking=true);
620virtual bool SourceExhausted() const =0;
621
622protected:
623void SourceInitialize(bool pumpAll, const NameValuePairs &parameters)
624{
625IsolatedInitialize(parameters);
626if (pumpAll)
627PumpAll();
628}
629};
630
631//! Turn a Store into a Source
632template <class T>
633class SourceTemplate : public Source
634{
635public:
636SourceTemplate<T>(BufferedTransformation *attachment)
637: Source(attachment) {}
638SourceTemplate<T>(BufferedTransformation *attachment, T store)
639: Source(attachment), m_store(store) {}
640void IsolatedInitialize(const NameValuePairs &parameters)
641{m_store.IsolatedInitialize(parameters);}
642unsigned int Pump2(unsigned long &byteCount, bool blocking=true)
643{return m_store.TransferTo2(*AttachedTransformation(), byteCount, NULL_CHANNEL, blocking);}
644unsigned int PumpMessages2(unsigned int &messageCount, bool blocking=true)
645{return m_store.TransferMessagesTo2(*AttachedTransformation(), messageCount, NULL_CHANNEL, blocking);}
646unsigned int PumpAll2(bool blocking=true)
647{return m_store.TransferAllTo2(*AttachedTransformation(), NULL_CHANNEL, blocking);}
648bool SourceExhausted() const
649{return !m_store.AnyRetrievable() && !m_store.AnyMessages();}
650void SetAutoSignalPropagation(int propagation)
651{m_store.SetAutoSignalPropagation(propagation);}
652int GetAutoSignalPropagation() const
653{return m_store.GetAutoSignalPropagation();}
654
655protected:
656T m_store;
657};
658
659//! .
660class StringSource : public SourceTemplate<StringStore>
661{
662public:
663StringSource(BufferedTransformation *attachment = NULL)
664: SourceTemplate<StringStore>(attachment) {}
665StringSource(const char *string, bool pumpAll, BufferedTransformation *attachment = NULL)
666: SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
667StringSource(const byte *string, unsigned int length, bool pumpAll, BufferedTransformation *attachment = NULL)
668: SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string, length)));}
669
670#ifdef __MWERKS__// CW60 workaround
671StringSource(const std::string &string, bool pumpAll, BufferedTransformation *attachment = NULL)
672#else
673template <class T> StringSource(const T &string, bool pumpAll, BufferedTransformation *attachment = NULL)
674#endif
675: SourceTemplate<StringStore>(attachment) {SourceInitialize(pumpAll, MakeParameters("InputBuffer", ConstByteArrayParameter(string)));}
676};
677
678//! .
679class RandomNumberSource : public SourceTemplate<RandomNumberStore>
680{
681public:
682RandomNumberSource(RandomNumberGenerator &rng, unsigned int length, bool pumpAll, BufferedTransformation *attachment = NULL)
683: SourceTemplate<RandomNumberStore>(attachment, RandomNumberStore(rng, length)) {if (pumpAll) PumpAll();}
684};
685
686NAMESPACE_END
687
688#endif

Archive Download this file

Branches

Tags

Quick Links:     www.monotone.ca    -     Downloads    -     Documentation    -     Wiki    -     Code Forge    -     Build Status