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" |
Jonas Olsson | a4d8737 | 2019-07-05 19:08:33 +0200 | [diff] [blame] | 12 | |
Niels Möller | a12c42a | 2018-07-25 16:05:48 +0200 | [diff] [blame] | 13 | #include "rtc_base/system/arch.h" |
Andrew MacDonald | cb05b72 | 2015-05-07 22:17:51 -0700 | [diff] [blame] | 14 | |
| 15 | namespace webrtc { |
| 16 | |
kwiberg | 62eaacf | 2016-02-17 06:39:05 -0800 | [diff] [blame] | 17 | size_t ReadMessageBytesFromFile(FILE* file, std::unique_ptr<uint8_t[]>* bytes) { |
Yves Gerey | 665174f | 2018-06-19 15:03:05 +0200 | [diff] [blame] | 18 | // The "wire format" for the size is little-endian. Assume we're running on |
| 19 | // a little-endian machine. |
Andrew MacDonald | cb05b72 | 2015-05-07 22:17:51 -0700 | [diff] [blame] | 20 | #ifndef WEBRTC_ARCH_LITTLE_ENDIAN |
| 21 | #error "Need to convert messsage from little-endian." |
| 22 | #endif |
| 23 | int32_t size = 0; |
| 24 | if (fread(&size, sizeof(size), 1, file) != 1) |
| 25 | return 0; |
| 26 | if (size <= 0) |
| 27 | return 0; |
| 28 | |
| 29 | bytes->reset(new uint8_t[size]); |
| 30 | return fread(bytes->get(), sizeof((*bytes)[0]), size, file); |
| 31 | } |
| 32 | |
| 33 | // Returns true on success, false on error or end-of-file. |
mbonadei | 7c2c843 | 2017-04-07 00:59:12 -0700 | [diff] [blame] | 34 | bool ReadMessageFromFile(FILE* file, MessageLite* msg) { |
kwiberg | 62eaacf | 2016-02-17 06:39:05 -0800 | [diff] [blame] | 35 | std::unique_ptr<uint8_t[]> bytes; |
Andrew MacDonald | cb05b72 | 2015-05-07 22:17:51 -0700 | [diff] [blame] | 36 | size_t size = ReadMessageBytesFromFile(file, &bytes); |
| 37 | if (!size) |
| 38 | return false; |
| 39 | |
| 40 | msg->Clear(); |
| 41 | return msg->ParseFromArray(bytes.get(), size); |
| 42 | } |
| 43 | |
| 44 | } // namespace webrtc |