[fidl] Expose CHANNEL_MAX_MSG_BYTES via zx library

Take the opportunity to make sure that we're using the actual #define to
avoid skew possibilities.

Test: everything still builds, a future change using this works.

Change-Id: I35e971beb6bddfa63a2250befe9982838a4e556a
diff --git a/zircon/system/host/fidl/compiler/main.cpp b/zircon/system/host/fidl/compiler/main.cpp
index ea2f2c5..3386260 100644
--- a/zircon/system/host/fidl/compiler/main.cpp
+++ b/zircon/system/host/fidl/compiler/main.cpp
@@ -100,7 +100,7 @@
     exit(1);
 }
 
-[[noreturn]] void Fail(const char* message, ...) {
+    [[noreturn]] void Fail(const char* message, ...) {
     va_list args;
     va_start(args, message);
     vfprintf(stderr, message, args);
@@ -324,7 +324,7 @@
     // Prepare source files.
     std::vector<fidl::SourceManager> source_managers;
     source_managers.push_back(fidl::SourceManager());
-    std::string library_zx_data(fidl::LibraryZX::kData, strlen(fidl::LibraryZX::kData) + 1);
+    std::string library_zx_data = fidl::LibraryZX::kData;
     source_managers.back().AddSourceFile(
         std::make_unique<fidl::SourceFile>(fidl::LibraryZX::kFilename, std::move(library_zx_data)));
     source_managers.push_back(fidl::SourceManager());
diff --git a/zircon/system/host/fidl/include/fidl/lexer.h b/zircon/system/host/fidl/include/fidl/lexer.h
index 4c5d87f..926f8da 100644
--- a/zircon/system/host/fidl/include/fidl/lexer.h
+++ b/zircon/system/host/fidl/include/fidl/lexer.h
@@ -25,13 +25,13 @@
     // simplifies advancing to the next character.
     Lexer(const SourceFile& source_file, ErrorReporter* error_reporter)
         : source_file_(source_file), error_reporter_(error_reporter) {
-        assert(data()[data().size() - 1] == 0);
         keyword_table_ = {
 #define KEYWORD(Name, Spelling) {Spelling, Token::Subkind::k##Name},
 #include "fidl/token_definitions.inc"
 #undef KEYWORD
         };
         current_ = data().data();
+        end_of_file_ = current_ + data().size();
         previous_end_ = token_start_ = current_;
     }
 
@@ -61,6 +61,7 @@
     ErrorReporter* error_reporter_;
 
     const char* current_ = nullptr;
+    const char* end_of_file_ = nullptr;
     const char* token_start_ = nullptr;
     const char* previous_end_ = nullptr;
     size_t token_size_ = 0u;
diff --git a/zircon/system/host/fidl/include/fidl/library_zx.h b/zircon/system/host/fidl/include/fidl/library_zx.h
index ae54bce..ddd41ad 100644
--- a/zircon/system/host/fidl/include/fidl/library_zx.h
+++ b/zircon/system/host/fidl/include/fidl/library_zx.h
@@ -5,11 +5,13 @@
 #ifndef ZIRCON_SYSTEM_HOST_FIDL_INCLUDE_FIDL_LIBRARY_ZX_H_
 #define ZIRCON_SYSTEM_HOST_FIDL_INCLUDE_FIDL_LIBRARY_ZX_H_
 
+#include <string>
+
 namespace fidl {
 namespace LibraryZX {
 
-extern const char kFilename[];
-extern const char kData[];
+extern const std::string kFilename;
+extern const std::string kData;
 
 } // namespace LibraryZX
 } // namespace fidl
diff --git a/zircon/system/host/fidl/include/fidl/string_view.h b/zircon/system/host/fidl/include/fidl/string_view.h
index 23dc3db..43c0788 100644
--- a/zircon/system/host/fidl/include/fidl/string_view.h
+++ b/zircon/system/host/fidl/include/fidl/string_view.h
@@ -14,11 +14,15 @@
 
 class StringView {
 public:
-    constexpr StringView() : data_(nullptr), size_(0u) {}
+    constexpr StringView()
+        : data_(nullptr), size_(0u) {}
     StringView(const StringView& view) = default;
-    constexpr StringView(const std::string& string) : StringView(string.data(), string.size()) {}
-    constexpr StringView(const char* data, size_t size) : data_(data), size_(size) {}
-    StringView(const char* string) : data_(string), size_(strlen(string)) {}
+    constexpr StringView(const std::string& string)
+        : StringView(string.data(), string.size()) {}
+    constexpr StringView(const char* data, size_t size)
+        : data_(data), size_(size) {}
+    StringView(const char* string)
+        : data_(string), size_(strlen(string)) {}
 
     operator std::string() const { return std::string(data(), size()); }
 
diff --git a/zircon/system/host/fidl/lib/lexer.cpp b/zircon/system/host/fidl/lib/lexer.cpp
index 1e19614..2d7460c 100644
--- a/zircon/system/host/fidl/lib/lexer.cpp
+++ b/zircon/system/host/fidl/lib/lexer.cpp
@@ -126,7 +126,7 @@
 } // namespace
 
 constexpr char Lexer::Peek() const {
-    return *current_;
+    return current_ < end_of_file_ ? *current_ : 0;
 }
 
 void Lexer::Skip() {
@@ -135,7 +135,7 @@
 }
 
 char Lexer::Consume() {
-    auto current = *current_;
+    auto current = Peek();
     ++current_;
     ++token_size_;
     return current;
diff --git a/zircon/system/host/fidl/lib/library_zx.cpp b/zircon/system/host/fidl/lib/library_zx.cpp
index 5a641cf..52cab3d 100644
--- a/zircon/system/host/fidl/lib/library_zx.cpp
+++ b/zircon/system/host/fidl/lib/library_zx.cpp
@@ -3,13 +3,21 @@
 // found in the LICENSE file.
 
 #include "fidl/library_zx.h"
+#include <sstream>
+// TODO(FIDL-478): make fidlc not depend on zircon
+#include <zircon/types.h>
 
 namespace fidl {
 namespace LibraryZX {
 
-const char kFilename[] = "zx.fidl";
+const std::string kFilename = "zx.fidl";
 
-const char kData[] = R"FIDL(
+namespace {
+std::string GenerateData() {
+    std::ostringstream out;
+
+    out <<
+        R"FIDL(
 [Internal]
 library zx;
 
@@ -25,5 +33,13 @@
 using procarg = uint32;
 )FIDL";
 
+    out << "const uint64 CHANNEL_MAX_MSG_BYTES = " << ZX_CHANNEL_MAX_MSG_BYTES << ";\n";
+
+    return out.str();
+}
+} // namespace
+
+const std::string kData = GenerateData();
+
 } // namespace LibraryZX
 } // namespace fidl
diff --git a/zircon/system/host/fidl/lib/source_manager.cpp b/zircon/system/host/fidl/lib/source_manager.cpp
index 2d7f416..8a67c7c 100644
--- a/zircon/system/host/fidl/lib/source_manager.cpp
+++ b/zircon/system/host/fidl/lib/source_manager.cpp
@@ -4,8 +4,8 @@
 
 #include "fidl/source_manager.h"
 
-#include <utility>
 #include <sys/stat.h>
+#include <utility>
 
 namespace fidl {
 
@@ -25,10 +25,9 @@
     std::string data;
     fseek(file, 0, SEEK_END);
     auto filesize = ftell(file);
-    data.resize(filesize + 1);
+    data.resize(filesize);
     rewind(file);
     fread(&data[0], 1, filesize, file);
-    data[filesize] = 0;
     fclose(file);
 
     AddSourceFile(std::make_unique<SourceFile>(filename, std::move(data)));