This file is indexed.

/usr/include/ucommon/stream.h is in libucommon-dev 7.0.0-9.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
// Copyright (C) 2006-2014 David Sugar, Tycho Softworks.
// Copyright (C) 2015 Cherokees of Idaho.
//
// This file is part of GNU uCommon C++.
//
// GNU uCommon C++ is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU uCommon C++ is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with GNU uCommon C++.  If not, see <http://www.gnu.org/licenses/>.

/**
 * Any ucommon streaming classes that are built from std::streamio facilities
 * and that support ANSI C++ stream operators.
 * @file ucommon/stream.h
 */

#ifndef UCOMMON_SYSRUNTIME
#ifndef _UCOMMON_STREAM_H_
#define _UCOMMON_STREAM_H_

#ifndef _UCOMMON_CONFIG_H
#include <ucommon/platform.h>
#endif

#ifndef _UCOMMON_PROTOCOLS_H_
#include <ucommon/protocols.h>
#endif

#ifndef _UCOMMON_THREAD_H_
#include <ucommon/thread.h>
#endif

#ifndef _UCOMMON_SOCKET_H_
#include <ucommon/socket.h>
#endif

#ifndef _UCOMMON_FSYS_H_
#include <ucommon/fsys.h>
#endif

#ifndef _UCOMMON_SHELL_H_
#include <ucommon/shell.h>
#endif

#include <iostream>
#include <fstream>

namespace ucommon {

/**
 * Common stream buffer for std C++ i/o classes.    This both binds the
 * character protocol to iostream and offers a common base class for all
 * other c++ stdlib based streaming classes.
 * @author David Sugar <dyfet@gnutelephony.org>
 */
class __EXPORT StreamBuffer : protected std::streambuf, public std::iostream
{
private:
	__DELETE_COPY(StreamBuffer);

protected:
    size_t bufsize;
    char *gbuf, *pbuf;

    StreamBuffer();

    /**
     * This streambuf method is used for doing unbuffered reads
     * through the establish tcp socket connection when in interactive mode.
     * Also this method will handle proper use of buffers if not in
     * interactive mode.
     *
     * @return char from tcp socket connection, EOF if not connected.
     */
    int uflow() __OVERRIDE;

    void release(void);

    void allocate(size_t size);

public:
    /**
     * Flush the stream input and output buffers, writes pending output.
     * @return 0 on success, or error code.
     */
    int sync(void);

    inline bool is_open(void) const
        {return bufsize > 0;}

    inline operator bool() const
        {return bufsize > 0;}

    inline bool operator!() const
        {return bufsize == 0;}
};

/**
 * Streamable tcp connection between client and server.  The tcp stream
 * class can represent a client connection to a server or an instance of
 * a service generated by a tcp listener.  As a stream class, data can
 * be manipulated using the << and >> operators.
 *
 * @author David Sugar <dyfet@gnutelephony.org>
 */
class __EXPORT tcpstream : public StreamBuffer
{
private:
    __LOCAL void allocate(unsigned size);
    __LOCAL void reset(void);

protected:
    socket_t so;
    timeout_t timeout;

    virtual ssize_t _read(char *buffer, size_t size);

    virtual ssize_t _write(const char *buffer, size_t size);

    virtual bool _wait(void);

    /**
     * Release the tcp stream and destroy the underlying socket.
     */
    void release(void);

    /**
     * This streambuf method is used to load the input buffer
     * through the established tcp socket connection.
     *
     * @return char from get buffer, EOF if not connected.
     */
    int underflow(void) __OVERRIDE;

    /**
     * This streambuf method is used to write the output
     * buffer through the established tcp connection.
     * @param ch char to push through.
     * @return char pushed through.
     */
    int overflow(int ch) __OVERRIDE;

    inline socket_t getsocket(void) const {
        return so;
	}

public:
    /**
     * Copy constructor...
     * @param copy for object.
     */
    tcpstream(const tcpstream& copy);

    /**
     * Create a stream from an existing tcp listener.
     * @param server to accept connection from.
     * @param segsize for tcp segments and buffering.
     * @param timeout for socket i/o operations.
     */
    tcpstream(const TCPServer *server, unsigned segsize = 536, timeout_t timeout = 0);

    /**
     * Create an unconnected tcp stream object that is idle until opened.
     * @param family of protocol to create.
     * @param timeout for socket i/o operations.
     */
    tcpstream(int family = PF_INET, timeout_t timeout = 0);

    /**
     * A convenience constructor that creates a connected tcp stream directly
     * from an address.  The socket is constructed to match the type of the
     * the address family in the socket address that is passed.
     * @param address of service to connect to.
     * @param segsize for tcp segments and buffering.
     * @param timeout for socket i/o operations.
     */
    tcpstream(Socket::address& address, unsigned segsize = 536, timeout_t timeout = 0);

    /**
     * Destroy a tcp stream.
     */
    virtual ~tcpstream();

    /**
     * See if stream connection is active.
     * @return true if stream is active.
     */
    inline operator bool() const {
        return so != INVALID_SOCKET && bufsize > 0;
	}

    /**
     * See if stream is disconnected.
     * @return true if stream disconnected.
     */
    inline bool operator!() const {
        return so == INVALID_SOCKET || bufsize == 0;
	}

    /**
     * Open a stream connection to a tcp service.
     * @param address of service to access.
     * @param segment buffering size to use.
     */
    void open(Socket::address& address, unsigned segment = 536);

    /**
     * Open a stream connectoion to a host and service.
     * @param host to connect to.
     * @param service to connect to by name or number as string.
     * @param segment buffering size to use.
     */
    void open(const char *host, const char *service, unsigned segment = 536);

    /**
     * Close an active stream connection.  This does not release the
     * socket but is a disconnect.
     */
    void close(void);
};

/**
 * Streamable pipe socket connection.  This creates a stream between
 * a parent and child process.  As a stream class, data can be
 * exchanged using the << and >> operators.
 *
 * @author David Sugar <dyfet@gnutelephony.org>
 */
class __EXPORT pipestream : public StreamBuffer
{
public:
    typedef enum {
        RDONLY,
        WRONLY,
        RDWR
    } access_t;

private:
    __LOCAL void allocate(size_t size, access_t mode);

	__DELETE_COPY(pipestream);

protected:
    fsys_t rd, wr;
    shell::pid_t pid;

    /**
     * Release the stream, detach/do not wait for the process.
     */
    void release(void);

    /**
     * This streambuf method is used to load the input buffer
     * through the established pipe connection.
     *
     * @return char from get buffer, EOF if not connected.
     */
    int underflow(void) __OVERRIDE;

    /**
     * This streambuf method is used to write the output
     * buffer through the established pipe connection.
     *
     * @param ch char to push through.
     * @return char pushed through.
     */
    int overflow(int ch) __OVERRIDE;

public:
    /**
     * Create an unopened pipe stream.
     */
    pipestream();

    /**
     * Create child process and start pipe.
     * @param path to execute.
     * @param access mode of pipe stream.
     * @param args to pass to command.
     * @param env to create in child.
     * @param size of buffer.
     */
    pipestream(const char *command, access_t access, char **args, char **env = NULL, size_t size = 512);

    /**
     * Destroy a pipe stream.
     */
    virtual ~pipestream();

    /**
     * See if stream connection is active.
     * @return true if stream is active.
     */
    inline operator bool() const {
        return (bufsize > 0);
	}

    /**
     * See if stream is disconnected.
     * @return true if stream disconnected.
     */
    inline bool operator!() const {
        return bufsize == 0;
	}

    /**
     * Open a stream connection to a pipe service.
     * @param path to execute.
     * @param access mode of stream.
     * @param args to pass to command.
     * @param env to create in child process.
     * @param buffering size to use.
     */
    void open(const char *path, access_t access, char **args, char **env = NULL, size_t buffering = 512);

    /**
     * Close an active stream connection.  This waits for the child to
     * terminate.
     */
    int close(void);

    /**
     * Force terminate child and close.
     */
    void terminate(void);

    inline void cancel(void) {
        terminate();
	}
};

/**
 * Streamable file class based on low level fsys io.  This differs from
 * the normal fstream classes as it may apply to other kinds of
 * file streams.
 *
 * @author David Sugar <dyfet@gnutelephony.org>
 */
class __EXPORT filestream : public StreamBuffer
{
public:
    typedef enum {
        RDONLY,
        WRONLY,
        RDWR
    } access_t;

private:
    __LOCAL void allocate(size_t size, fsys::access_t mode);

protected:
    fsys_t fd;
    fsys::access_t ac;

    /**
     * This streambuf method is used to load the input buffer
     * through the established pipe connection.
     *
     * @return char from get buffer, EOF if not connected.
     */
    int underflow(void) __OVERRIDE;

    /**
     * This streambuf method is used to write the output
     * buffer through the established pipe connection.
     *
     * @param ch char to push through.
     * @return char pushed through.
     */
    int overflow(int ch) __OVERRIDE;

public:
    /**
     * Create an unopened pipe stream.
     */
    filestream();

    /**
     * Create duplicate stream.
     */
    filestream(const filestream& copy);

    /**
     * Create and open a file stream.
     */
    filestream(const char *path, unsigned mode, fsys::access_t access, size_t bufsize = 512);

    /**
     * Open file stream.
     */
    filestream(const char *path, fsys::access_t access, size_t bufsize = 512);

    /**
     * Destroy a file stream.
     */
    virtual ~filestream();

    /**
     * See if stream connection is active.
     * @return true if stream is active.
     */
    inline operator bool() const {
        return (bufsize > 0);
	}

    /**
     * See if stream is disconnected.
     * @return true if stream disconnected.
     */
    inline bool operator!() const {
        return bufsize == 0;
	}

    /**
     * Open a stream connection to a tcp service.
     */
    void open(const char *filename, fsys::access_t access, size_t buffering = 512);

    /**
     * Create a stream connection to a tcp service.
     */
    void open(const char *filename, unsigned mode, fsys::access_t access, size_t buffering = 512);

    /**
     * Close an active stream connection.
     */
    void close(void);

    /**
     * Seek position.
     */
    void seek(fsys::offset_t offset);

	void rewind(void);

    /**
     * Get error flag from last i/o operation.
     * @return last error.
     */
    inline int err(void) const
        {return fd.err();}
};

/**
 * Stream class to read from a memory buffer.  May optionally be 0 byte
 * terminated if no length is specified.
 */
class __EXPORT imemstream : protected std::streambuf, public std::istream
{
private:
	__DELETE_DEFAULTS(imemstream);

	size_t count;
	const uint8_t *pos, *bp;
	
public:
	imemstream(const uint8_t *data, size_t size);
	imemstream(const char *data);

	int underflow() __OVERRIDE;

	int uflow() __OVERRIDE;

	inline size_t remains() const {
		return count;
	}

	inline const uint8_t *mem() const {
		return bp;
	}

	inline const char *chr() const {
		return (const char *)bp;
	}

	inline size_t len() const {
		return (size_t)(pos - bp) + count;
	}
};

/**
 * Stream class to write to memory buffer.
 */
class __EXPORT omemstream : protected std::streambuf, public std::ostream
{
private:
	__DELETE_DEFAULTS(omemstream);

	size_t count;
	uint8_t *pos, *bp;
	bool zb;
	
public:
	explicit omemstream(uint8_t *data, size_t size);
	omemstream(char *data, size_t size);

	int overflow(int ch) __OVERRIDE;

	inline size_t remains() const {
		return count;
	}

	inline uint8_t *mem() const {
		return bp;
	}

	inline char *chr() const {
		return (char *)bp;
	}

	inline size_t len() const {
		return (size_t)(pos - bp);
	}
};

bool __EXPORT getline(std::istream& in, char *buffer, size_t size);

bool __EXPORT putline(std::ostream& out, const char *buffer);

/**
 * At least with gcc, linking of stream operators was broken.  This provides
 * an auxillory class to solve the issue.
 */
class __EXPORT _stream_operators
{
private:
	__DELETE_DEFAULTS(_stream_operators);

public:
    static std::ostream& print(std::ostream& out, const PrintProtocol& format);

    static std::istream& input(std::istream& inp, InputProtocol& format);

    static std::ostream& print(std::ostream& out, const string_t& str);

    static std::istream& input(std::istream& inp, string_t& str);

    static std::ostream& print(std::ostream& out, const stringlist_t& list);

    static std::istream& input(std::istream& in, stringlist_t& list);

    static std::string& append(std::string& target, String& source);
};

inline std::ostream& operator<< (std::ostream& out, const PrintProtocol& format) {
    return _stream_operators::print(out, format);
}

inline std::istream& operator>> (std::istream& inp, InputProtocol& format) {
    return _stream_operators::input(inp, format);
}

inline std::ostream& operator<< (std::ostream& out, const string_t& str) {
    return _stream_operators::print(out, str);
}

inline std::istream& operator>> (std::istream& inp, string_t& str) {
    return _stream_operators::input(inp, str);
}

inline std::ostream& operator<< (std::ostream& out, const stringlist_t& list) {
    return _stream_operators::print(out, list);
}

inline std::istream& operator>> (std::istream& in, stringlist_t& list) {
    return _stream_operators::input(in, list);
}

inline std::string& operator+(std::string& target, String& source) {
    return _stream_operators::append(target, source);
}

inline std::string& operator+=(std::string& target, String& source) {
    return _stream_operators::append(target, source);
}
 
inline std::ostream& operator<<(std::ostream& os, Socket::address& addr) {
#ifdef  AF_INET6
    char buf[INET6_ADDRSTRLEN];
#else
    char buf[INET_ADDRSTRLEN];
#endif
    addr.print(buf, sizeof(buf), false, true);
    os << buf;
    return os;
}

} // namespace ucommon

namespace std {
	extern __EXPORT iostream& null;
}

#endif
#endif