This file is indexed.

/usr/include/pdal/util/IStream.hpp is in libpdal-dev 1.4.0-1+b1.

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
/******************************************************************************
* Copyright (c) 2014, Andrew Bell
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following
* conditions are met:
*
*     * Redistributions of source code must retain the above copyright
*       notice, this list of conditions and the following disclaimer.
*     * Redistributions in binary form must reproduce the above copyright
*       notice, this list of conditions and the following disclaimer in
*       the documentation and/or other materials provided
*       with the distribution.
*     * Neither the name of Hobu, Inc. or Flaxen Geo Consulting nor the
*       names of its contributors may be used to endorse or promote
*       products derived from this software without specific prior
*       written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
* OF SUCH DAMAGE.
****************************************************************************/

#pragma once

#include <sys/types.h>
#include <stdint.h>

#include <cassert>
#include <fstream>
#include <memory>
#include <stack>
#include <vector>
#include <cstring>

#include "portable_endian.hpp"
#include "pdal_util_export.hpp"

namespace pdal
{

class IStreamMarker;

/**
  Stream wrapper for input of binary data.
*/
class IStream
{
public:
    /**
      Default constructor.
    */
    PDAL_DLL IStream() : m_stream(NULL), m_fstream(NULL)
        {}

    /**
      Construct an IStream from a filename.

      \param filename  File from which to read.
    */
    PDAL_DLL IStream(const std::string& filename) :
        m_stream(NULL), m_fstream(NULL)
    { open(filename); }

    /**
      Construct an IStream from an input stream pointer.

      \param stream  Stream from which to read.
    */
    PDAL_DLL IStream(std::istream *stream) : m_stream(stream), m_fstream(NULL)
        {}

    PDAL_DLL ~IStream()
        { delete m_fstream; }

    /**
      Open a file to extract.

      \param filename  Filename.
      \return  -1 if a stream is already assigned, 0 otherwise.
    */
    PDAL_DLL int open(const std::string& filename)
    {
        if (m_stream)
             return -1;
        m_stream = m_fstream = new std::ifstream(filename,
            std::ios_base::in | std::ios_base::binary);
        return 0;
    }

    /**
      Close the underlying stream.
    */
    PDAL_DLL void close()
    {
        delete m_fstream;
        m_fstream = NULL;
        m_stream = NULL;
    }

    /**
      Return the state of the stream.

      \return  The state of the underlying stream.
    */
    PDAL_DLL operator bool ()
        { return (bool)(*m_stream); }

    /**
      Seek to a position in the underlying stream.

      \param pos  Position to seek to,
    */
    PDAL_DLL void seek(std::streampos pos)
        { m_stream->seekg(pos, std::istream::beg); }


    /**
      Seek to an offset from a specified position.

      \param off  Offset.
      \param way  Absolute position for offset (beg, end or cur)
    */
    PDAL_DLL void seek(std::streampos off, std::ios_base::seekdir way)
        { m_stream->seekg(off, way); }

    /**
      Skip relative to the current position.

      \param offset  Offset from the current position.
    */
    PDAL_DLL void skip(std::streamoff offset)
        { m_stream->seekg(offset, std::istream::cur); }

    /**
      Determine the position of the get pointer.

      \return  Current get position.
    */
    PDAL_DLL std::streampos position() const
        { return m_stream->tellg(); }

    /**
      Determine if the underlying stream is good.

      \return  Whether the underlying stream is good.
    */
    PDAL_DLL bool good() const
        { return m_stream->good(); }

    /**
      Fetch a pointer to the underlying stream.

      \return  Pointer to the underlying stream.
    */
    PDAL_DLL std::istream *stream()
        { return m_stream; }

    /**
      Temporarily push a stream to read from.

      \param strm  New stream to read from.
    */
    PDAL_DLL void pushStream(std::istream *strm)
    {
        m_streams.push(m_stream);
        m_stream = strm;
    }

    /**
      Pop the current stream and return it.  The last stream on the stack
      cannot be popped.

      \return  Pointer to the popped stream.
    */
    PDAL_DLL std::istream *popStream()
    {
        // Can't pop the last stream for now.
        if (m_streams.empty())
            return nullptr;
        std::istream *strm = m_stream;
        m_stream = m_streams.top();
        m_streams.pop();
        return strm;
    }

    /**
      Fetch data from the stream into a string.

      \param s  String to fill.
      \param size  Number of bytes to extract.
    */
    PDAL_DLL void get(std::string& s, size_t size)
    {
        // Could do this by appending to a string with a stream, but this
        // is probably fast enough for now (there's only a simple increment
        // to advance an istream iterator, which you'd have to call in a loop).
        std::unique_ptr<char[]> buf(new char[size+1]);
        m_stream->read(buf.get(), size);
        buf[size] = '\0';
        s = buf.get();
    }

    /**
      Fetch data from the stream into a vector of char.

      \param buf  Buffer to fill.
    */
    PDAL_DLL void get(std::vector<char>& buf) {
        assert(buf.size() != 0);
        m_stream->read((char *)&buf[0], buf.size());
    }

    /**
      Fetch data from the stream into a vector of unsigned char.

      \param buf  Buffer to fill.
    */
    PDAL_DLL void get(std::vector<unsigned char>& buf) {
        assert(buf.size() != 0);
        m_stream->read((char *)&buf[0], buf.size());
    }

    /**
      Fetch data from the stream into the specified buffer of char.

      \param buf  Buffer to fill.
      \param size  Number of bytes to extract.
    */
    PDAL_DLL void get(char *buf, size_t size)
        { m_stream->read(buf, size); }

    /**
      Fetch data from the stream into the specified buffer of unsigned char.

      \param buf  Buffer to fill.
      \param size  Number of bytes to extract.
    */
    PDAL_DLL void get(unsigned char *buf, size_t size)
        { m_stream->read((char *)buf, size); }

protected:
    std::istream *m_stream;
    std::ifstream *m_fstream; // Dup of above to facilitate cleanup.

private:
    std::stack<std::istream *> m_streams;
	IStream(const IStream&);
};

/**
  Stream wrapper for input of binary data that converts from little-endian
  to host ordering.
*/
class ILeStream : public IStream
{
public:
    /**
      Default constructor.
    */
    PDAL_DLL ILeStream()
    {}

    /**
      Constructor that opens the file and maps it to a stream.

      \param filename  Filename.
    */
    PDAL_DLL ILeStream(const std::string& filename) : IStream(filename)
    {}

    /**
      Constructor that maps to a provided stream.

      \param stream  Stream to extract from.
    */
    PDAL_DLL ILeStream(std::istream *stream) : IStream(stream)
    {}

    /**
      Extract an unsigned byte from the stream.

      \param v  unsigned byte to populate
      \return  This stream.
    */
    PDAL_DLL ILeStream& operator >> (uint8_t& v)
    {
        v = (uint8_t)m_stream->get();
        return *this;
    }

    /**
      Extract an unsigned byte from the stream.

      \param v  unsigned byte to populate
      \return  This stream.
    */
    PDAL_DLL ILeStream& operator >> (int8_t& v)
    {
        v = (int8_t)m_stream->get();
        return *this;
    }

    /**
      Extract an unsigned short from the stream.

      \param v  unsigned short to populate
      \return  This stream.
    */
    PDAL_DLL ILeStream& operator >> (uint16_t& v)
    {
        m_stream->read((char *)&v, sizeof(v));
        v = le16toh(v);
        return *this;
    }

    /**
      Extract an short from the stream.

      \param v  short to populate
      \return  This stream.
    */
    PDAL_DLL ILeStream& operator >> (int16_t& v)
    {
        m_stream->read((char *)&v, sizeof(v));
        v = (int16_t)le16toh((uint16_t)v);
        return *this;
    }

    /**
      Extract an unsigned int from the stream.

      \param v  unsigned int to populate
      \return  This stream.
    */
    PDAL_DLL ILeStream& operator >> (uint32_t& v)
    {
        m_stream->read((char *)&v, sizeof(v));
        v = le32toh(v);
        return *this;
    }

    /**
      Extract an int from the stream.

      \param v  int to populate
      \return  This stream.
    */
    PDAL_DLL ILeStream& operator >> (int32_t& v)
    {
        m_stream->read((char *)&v, sizeof(v));
        v = (int32_t)le32toh((uint32_t)v);
        return *this;
    }

    /**
      Extract an unsigned long int from the stream.

      \param v  unsigned long int to populate
      \return  This stream.
    */
    PDAL_DLL ILeStream& operator >> (uint64_t& v)
    {
        m_stream->read((char *)&v, sizeof(v));
        v = le64toh(v);
        return *this;
    }

    /**
      Extract a long int from the stream.

      \param v  long int to populate
      \return  This stream.
    */
    PDAL_DLL ILeStream& operator >> (int64_t& v)
    {
        m_stream->read((char *)&v, sizeof(v));
        v = (int64_t)le64toh((uint64_t)v);
        return *this;
    }

    /**
      Extract a float from the stream.

      \param v  float to populate
      \return  This stream.
    */
    PDAL_DLL ILeStream& operator >> (float& v)
    {
        m_stream->read((char *)&v, sizeof(v));
        uint32_t tmp = le32toh(*(uint32_t *)(&v));
        std::memcpy(&v, &tmp, sizeof(tmp));
        return *this;
    }

    /**
      Extract a double from the stream.

      \param v  double to populate
      \return  This stream.
    */
    PDAL_DLL ILeStream& operator >> (double& v)
    {
        m_stream->read((char *)&v, sizeof(v));
        uint64_t tmp = le64toh(*(uint64_t *)(&v));
        std::memcpy(&v, &tmp, sizeof(tmp));
        return *this;
    }
};


/**
  Stream wrapper for input of binary data that converts from either
  little-endian or big-endian to host ordering, depending on object
  settings.
*/
class ISwitchableStream : public IStream
{
public:
    static const bool DefaultIsLittleEndian = true;

    PDAL_DLL ISwitchableStream() : m_isLittleEndian(DefaultIsLittleEndian)
    {}

    PDAL_DLL ISwitchableStream(const std::string& filename)
        : IStream(filename)
        , m_isLittleEndian(DefaultIsLittleEndian)
    {}
    
    PDAL_DLL ISwitchableStream(std::istream* stream)
        : IStream(stream)
        , m_isLittleEndian(DefaultIsLittleEndian)
    {}

    PDAL_DLL bool isLittleEndian() const
        { return m_isLittleEndian; }
    PDAL_DLL void switchToLittleEndian()
        { m_isLittleEndian = true; }
    PDAL_DLL void switchToBigEndian()
        { m_isLittleEndian = false; }

    PDAL_DLL ISwitchableStream& operator>>(uint8_t& v)
    {
        v = (uint8_t)m_stream->get();
        return *this;
    }

    PDAL_DLL ISwitchableStream& operator>>(int8_t& v)
    {
        v = (int8_t)m_stream->get();
        return *this;
    }

    PDAL_DLL ISwitchableStream& operator>>(uint16_t& v)
    {
        m_stream->read((char*)&v, sizeof(v));
        v = isLittleEndian() ? le16toh(v) : be16toh(v);
        return *this;
    }

    PDAL_DLL ISwitchableStream& operator>>(int16_t& v)
    {
        m_stream->read((char*)&v, sizeof(v));
        v = isLittleEndian() ? (int16_t)le16toh((uint16_t)v)
                             : (int16_t)be16toh((uint16_t)v);
        return *this;
    }

    PDAL_DLL ISwitchableStream& operator>>(uint32_t& v)
    {
        m_stream->read((char*)&v, sizeof(v));
        v = isLittleEndian() ? le32toh(v) : be32toh(v);
        return *this;
    }

    PDAL_DLL ISwitchableStream& operator>>(int32_t& v)
    {
        m_stream->read((char*)&v, sizeof(v));
        v = isLittleEndian() ? (int32_t)le32toh((uint32_t)v)
                             : (int32_t)be32toh((uint32_t)v);
        return *this;
    }

    PDAL_DLL ISwitchableStream& operator>>(uint64_t& v)
    {
        m_stream->read((char*)&v, sizeof(v));
        v = isLittleEndian() ? le64toh(v) : be64toh(v);
        return *this;
    }

    PDAL_DLL ISwitchableStream& operator>>(int64_t& v)
    {
        m_stream->read((char*)&v, sizeof(v));
        v = isLittleEndian() ? (int64_t)le64toh((uint64_t)v)
                             : (int64_t)be64toh((uint64_t)v);
        return *this;
    }

    PDAL_DLL ISwitchableStream& operator>>(float& v)
    {
        m_stream->read((char*)&v, sizeof(v));
        uint32_t tmp = isLittleEndian() ? le32toh(*(uint32_t*)(&v))
                                        : be32toh(*(uint32_t*)(&v));
        std::memcpy(&v, &tmp, sizeof(tmp));
        return *this;
    }

    PDAL_DLL ISwitchableStream& operator>>(double& v)
    {
        m_stream->read((char*)&v, sizeof(v));
        uint64_t tmp = isLittleEndian() ? be64toh(*(uint64_t*)(&v))
                                        : be64toh(*(uint64_t*)(&v));
        std::memcpy(&v, &tmp, sizeof(tmp));
        return *this;
    }

private:
    bool m_isLittleEndian;
};


/// Stream position marker with rewinding support.
class IStreamMarker
{
public:
    PDAL_DLL IStreamMarker(IStream& stream) : m_stream(stream)
        { m_pos = m_stream.position(); }

    PDAL_DLL void rewind()
        { m_stream.seek(m_pos); }

private:
    std::streampos m_pos;
    IStream& m_stream;
	IStreamMarker(const IStreamMarker&);
    IStreamMarker& operator=(const IStreamMarker&); // not implemented
};

} // namespace pdal