ppd_line_reader.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. // Copyright 2017 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef CHROMEOS_PRINTING_PPD_LINE_READER_H_
  5. #define CHROMEOS_PRINTING_PPD_LINE_READER_H_
  6. #include <memory>
  7. #include <string>
  8. #include "base/component_export.h"
  9. namespace chromeos {
  10. // Class supporting line-oriented input from unencoded and gzip-encoded PPDs.
  11. // Decompression, when required, is streamed to avoid excessive memory usage due
  12. // to malicious gzip contents.
  13. class COMPONENT_EXPORT(CHROMEOS_PRINTING) PpdLineReader {
  14. public:
  15. // Create a new PpdLineReader using ppd contents in |contents|. The max
  16. // allowed line length in the ppd is also parameterized.
  17. //
  18. // |contents| may or may not be gzip-compressed, and must remain valid and
  19. // unchanged while the Created PpdReader exists.
  20. static std::unique_ptr<PpdLineReader> Create(const std::string& contents,
  21. size_t max_line_length);
  22. // Checks to see whether the file contents in |contents| contains the magic
  23. // number which is found at the beginning of every PPD file. To verify this,
  24. // a line reader is created which simply attempts to read the first line and
  25. // checks whether it contains the magic number.
  26. static bool ContainsMagicNumber(const std::string& contents,
  27. size_t max_line_length);
  28. virtual ~PpdLineReader() = default;
  29. // Get the contents of the next non-empty line from the ppd into |line|.
  30. // Returns true on success, false if there was nothing left to read or an
  31. // error occurred. Lines longer than max_line_length are skipped. To
  32. // distinguish between end of input and error, use Error().
  33. virtual bool NextLine(std::string* line) = 0;
  34. // Return true if we encountered an error while reading.
  35. virtual bool Error() const = 0;
  36. };
  37. } // namespace chromeos
  38. #endif // CHROMEOS_PRINTING_PPD_LINE_READER_H_