1// Copyright (c) 2009, Google Inc.
2// All rights reserved.
3//
4// Redistribution and use in source and binary forms, with or without
5// modification, are permitted provided that the following conditions are
6// met:
7//
8//     * Redistributions of source code must retain the above copyright
9// notice, this list of conditions and the following disclaimer.
10//     * Redistributions in binary form must reproduce the above
11// copyright notice, this list of conditions and the following disclaimer
12// in the documentation and/or other materials provided with the
13// distribution.
14//     * Neither the name of Google Inc. nor the names of its
15// contributors may be used to endorse or promote products derived from
16// this software without specific prior written permission.
17//
18// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30#ifndef CLIENT_LINUX_MINIDUMP_WRITER_LINE_READER_H_
31#define CLIENT_LINUX_MINIDUMP_WRITER_LINE_READER_H_
32
33#include <stdint.h>
34#include <assert.h>
35#include <string.h>
36
37#include "common/linux/linux_libc_support.h"
38#include "third_party/lss/linux_syscall_support.h"
39
40namespace google_breakpad {
41
42// A class for reading a file, line by line, without using fopen/fgets or other
43// functions which may allocate memory.
44class LineReader {
45 public:
46  LineReader(int fd)
47      : fd_(fd),
48        hit_eof_(false),
49        buf_used_(0) {
50  }
51
52  // The maximum length of a line.
53  static const size_t kMaxLineLen = 512;
54
55  // Return the next line from the file.
56  //   line: (output) a pointer to the start of the line. The line is NUL
57  //     terminated.
58  //   len: (output) the length of the line (not inc the NUL byte)
59  //
60  // Returns true iff successful (false on EOF).
61  //
62  // One must call |PopLine| after this function, otherwise you'll continue to
63  // get the same line over and over.
64  bool GetNextLine(const char **line, unsigned *len) {
65    for (;;) {
66      if (buf_used_ == 0 && hit_eof_)
67        return false;
68
69      for (unsigned i = 0; i < buf_used_; ++i) {
70        if (buf_[i] == '\n' || buf_[i] == 0) {
71          buf_[i] = 0;
72          *len = i;
73          *line = buf_;
74          return true;
75        }
76      }
77
78      if (buf_used_ == sizeof(buf_)) {
79        // we scanned the whole buffer and didn't find an end-of-line marker.
80        // This line is too long to process.
81        return false;
82      }
83
84      // We didn't find any end-of-line terminators in the buffer. However, if
85      // this is the last line in the file it might not have one:
86      if (hit_eof_) {
87        assert(buf_used_);
88        // There's room for the NUL because of the buf_used_ == sizeof(buf_)
89        // check above.
90        buf_[buf_used_] = 0;
91        *len = buf_used_;
92        buf_used_ += 1;  // since we appended the NUL.
93        *line = buf_;
94        return true;
95      }
96
97      // Otherwise, we should pull in more data from the file
98      const ssize_t n = sys_read(fd_, buf_ + buf_used_,
99                                 sizeof(buf_) - buf_used_);
100      if (n < 0) {
101        return false;
102      } else if (n == 0) {
103        hit_eof_ = true;
104      } else {
105        buf_used_ += n;
106      }
107
108      // At this point, we have either set the hit_eof_ flag, or we have more
109      // data to process...
110    }
111  }
112
113  void PopLine(unsigned len) {
114    // len doesn't include the NUL byte at the end.
115
116    assert(buf_used_ >= len + 1);
117    buf_used_ -= len + 1;
118    my_memmove(buf_, buf_ + len + 1, buf_used_);
119  }
120
121 private:
122  const int fd_;
123
124  bool hit_eof_;
125  unsigned buf_used_;
126  char buf_[kMaxLineLen];
127};
128
129}  // namespace google_breakpad
130
131#endif  // CLIENT_LINUX_MINIDUMP_WRITER_LINE_READER_H_
132