Andrew MacDonald | cb05b72 | 2015-05-07 22:17:51 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 The WebRTC project authors. All Rights Reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | |
Mirko Bonadei | 92ea95e | 2017-09-15 06:47:31 +0200 | [diff] [blame] | 11 | #include "modules/audio_processing/test/protobuf_utils.h" |
Mirko Bonadei | 7120742 | 2017-09-15 13:58:09 +0200 | [diff] [blame] | 12 | #include "typedefs.h" // NOLINT(build/include) |
Andrew MacDonald | cb05b72 | 2015-05-07 22:17:51 -0700 | [diff] [blame] | 13 | |
| 14 | namespace webrtc { |
| 15 | |
kwiberg | 62eaacf | 2016-02-17 06:39:05 -0800 | [diff] [blame] | 16 | size_t ReadMessageBytesFromFile(FILE* file, std::unique_ptr<uint8_t[]>* bytes) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame^] | 17 | // The "wire format" for the size is little-endian. Assume we're running on |
| 18 | // a little-endian machine. |
Andrew MacDonald | cb05b72 | 2015-05-07 22:17:51 -0700 | [diff] [blame] | 19 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 20 | #error "Need to convert messsage from little-endian." |
| 21 | #endif |
| 22 | int32_t size = 0; |
| 23 | if (fread(&size, sizeof(size), 1, file) != 1) |
| 24 | return 0; |
| 25 | if (size <= 0) |
| 26 | return 0; |
| 27 | |
| 28 | bytes->reset(new uint8_t[size]); |
| 29 | return fread(bytes->get(), sizeof((*bytes)[0]), size, file); |
| 30 | } |
| 31 | |
| 32 | // Returns true on success, false on error or end-of-file. |
mbonadei | 7c2c843 | 2017-04-07 00:59:12 -0700 | [diff] [blame] | 33 | bool ReadMessageFromFile(FILE* file, MessageLite* msg) { |
kwiberg | 62eaacf | 2016-02-17 06:39:05 -0800 | [diff] [blame] | 34 | std::unique_ptr<uint8_t[]> bytes; |
Andrew MacDonald | cb05b72 | 2015-05-07 22:17:51 -0700 | [diff] [blame] | 35 | size_t size = ReadMessageBytesFromFile(file, &bytes); |
| 36 | if (!size) |
| 37 | return false; |
| 38 | |
| 39 | msg->Clear(); |
| 40 | return msg->ParseFromArray(bytes.get(), size); |
| 41 | } |
| 42 | |
| 43 | } // namespace webrtc |