You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

protobuf_utils.cc 3.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "tools/common/protobuf_utils.h"
  17. #include <fstream>
  18. #include <string>
  19. #include "google/protobuf/io/zero_copy_stream_impl.h"
  20. #include "google/protobuf/text_format.h"
  21. #include "google/protobuf/io/coded_stream.h"
  22. #include "src/common/file_utils.h"
  23. namespace mindspore {
  24. namespace lite {
  25. static const int PROTO_READ_BYTES_LIMIT = INT_MAX; // Max size of 2 GB minus 1 byte.
  26. static const int WARNING_THRESHOLD = 536870912 * 2;
  27. bool ReadProtoFromCodedInputStream(google::protobuf::io::CodedInputStream *coded_stream,
  28. google::protobuf::Message *proto) {
  29. if (proto == nullptr) {
  30. MS_LOG(ERROR) << "incorrect parameter. nullptr == proto";
  31. return false;
  32. }
  33. coded_stream->SetTotalBytesLimit(PROTO_READ_BYTES_LIMIT, WARNING_THRESHOLD);
  34. return proto->ParseFromCodedStream(coded_stream);
  35. }
  36. STATUS ReadProtoFromText(const char *file, google::protobuf::Message *message) {
  37. if (file == nullptr || message == nullptr) {
  38. return RET_ERROR;
  39. }
  40. std::string realPath = RealPath(file);
  41. if (realPath.empty()) {
  42. MS_LOG(ERROR) << "Proto file path " << file << " is not valid";
  43. return RET_ERROR;
  44. }
  45. std::ifstream fs(realPath.c_str(), std::ifstream::in);
  46. if (!fs.is_open()) {
  47. MS_LOG(ERROR) << "Open proto file " << file << " failed.";
  48. return RET_ERROR;
  49. }
  50. google::protobuf::io::IstreamInputStream input(&fs);
  51. bool status = google::protobuf::TextFormat::Parse(&input, message);
  52. if (!status) {
  53. MS_LOG(ERROR) << "call [google::protobuf::TextFormat::Parse] func status fail, please check your text file.";
  54. return RET_ERROR;
  55. }
  56. fs.close();
  57. return RET_OK;
  58. }
  59. STATUS ReadProtoFromBinaryFile(const char *file, google::protobuf::Message *message) {
  60. if (file == nullptr || message == nullptr) {
  61. return RET_ERROR;
  62. }
  63. std::string realPath = RealPath(file);
  64. if (realPath.empty()) {
  65. MS_LOG(ERROR) << "Binary proto file path " << file << " is not valid";
  66. return RET_ERROR;
  67. }
  68. std::ifstream fs(realPath, std::ifstream::in | std::ifstream::binary);
  69. if (!fs.is_open()) {
  70. MS_LOG(ERROR) << "Open binary proto file " << file << " failed.";
  71. return RET_ERROR;
  72. }
  73. google::protobuf::io::IstreamInputStream istream(&fs);
  74. google::protobuf::io::CodedInputStream coded_stream(&istream);
  75. bool success = ReadProtoFromCodedInputStream(&coded_stream, message);
  76. fs.close();
  77. if (!success) {
  78. MS_LOG(ERROR) << "Parse " << file << " failed.";
  79. return RET_ERROR;
  80. }
  81. return RET_OK;
  82. }
  83. } // namespace lite
  84. } // namespace mindspore