Rafael Espindola | 8e71301 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 1 | //===- YAML.cpp - YAMLIO utilities for object files -----------------------===// |
| 2 | // |
Chandler Carruth | 2946cd7 | 2019-01-19 08:50:56 +0000 | [diff] [blame] | 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
Rafael Espindola | 8e71301 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | // |
| 9 | // This file defines utility classes for handling the YAML representation of |
| 10 | // object files. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Rafael Espindola | ebd9193 | 2016-03-01 19:15:06 +0000 | [diff] [blame] | 14 | #include "llvm/ObjectYAML/YAML.h" |
Dmitri Gribenko | df73c30 | 2013-08-07 05:51:27 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/StringExtras.h" |
Sean Silva | 23f3b82 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Will Dietz | 981af00 | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 17 | #include <cctype> |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 18 | #include <cstdint> |
Rafael Espindola | 8e71301 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 19 | |
| 20 | using namespace llvm; |
| 21 | |
Rafael Espindola | 97de474 | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 22 | void yaml::ScalarTraits<yaml::BinaryRef>::output( |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 23 | const yaml::BinaryRef &Val, void *, raw_ostream &Out) { |
Sean Silva | 42fa38f | 2013-06-05 23:47:23 +0000 | [diff] [blame] | 24 | Val.writeAsHex(Out); |
Rafael Espindola | 8e71301 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 25 | } |
| 26 | |
Rafael Espindola | 97de474 | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 27 | StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *, |
| 28 | yaml::BinaryRef &Val) { |
Sean Silva | 23f3b82 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 29 | if (Scalar.size() % 2 != 0) |
| 30 | return "BinaryRef hex string must contain an even number of nybbles."; |
| 31 | // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here? |
| 32 | // (e.g. a caret pointing to the offending character). |
| 33 | for (unsigned I = 0, N = Scalar.size(); I != N; ++I) |
Roman Lebedev | 4182801 | 2019-02-06 08:57:01 +0000 | [diff] [blame] | 34 | if (!llvm::isHexDigit(Scalar[I])) |
Sean Silva | 23f3b82 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 35 | return "BinaryRef hex string must contain only hex digits."; |
Rafael Espindola | 97de474 | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 36 | Val = yaml::BinaryRef(Scalar); |
Eugene Zelenko | 28082ab | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 37 | return {}; |
Rafael Espindola | 8e71301 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 38 | } |
Sean Silva | 23f3b82 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 39 | |
Rafael Espindola | 97de474 | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 40 | void yaml::BinaryRef::writeAsBinary(raw_ostream &OS) const { |
Sean Silva | b7e82ad | 2013-06-05 23:32:31 +0000 | [diff] [blame] | 41 | if (!DataIsHexString) { |
Sean Silva | 23f3b82 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 42 | OS.write((const char *)Data.data(), Data.size()); |
| 43 | return; |
| 44 | } |
| 45 | for (unsigned I = 0, N = Data.size(); I != N; I += 2) { |
Roman Lebedev | 4182801 | 2019-02-06 08:57:01 +0000 | [diff] [blame] | 46 | uint8_t Byte = llvm::hexDigitValue(Data[I]); |
| 47 | Byte <<= 4; |
| 48 | Byte |= llvm::hexDigitValue(Data[I + 1]); |
Sean Silva | 23f3b82 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 49 | OS.write(Byte); |
| 50 | } |
| 51 | } |
Sean Silva | 42fa38f | 2013-06-05 23:47:23 +0000 | [diff] [blame] | 52 | |
Rafael Espindola | 97de474 | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 53 | void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const { |
David Majnemer | d9eb2d1 | 2014-03-20 06:28:52 +0000 | [diff] [blame] | 54 | if (binary_size() == 0) |
Sean Silva | 2f672d6 | 2013-07-09 00:54:46 +0000 | [diff] [blame] | 55 | return; |
Sean Silva | 42fa38f | 2013-06-05 23:47:23 +0000 | [diff] [blame] | 56 | if (DataIsHexString) { |
| 57 | OS.write((const char *)Data.data(), Data.size()); |
| 58 | return; |
| 59 | } |
Simon Atanasyan | 255689c | 2016-03-01 10:11:27 +0000 | [diff] [blame] | 60 | for (uint8_t Byte : Data) |
| 61 | OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf); |
Sean Silva | 42fa38f | 2013-06-05 23:47:23 +0000 | [diff] [blame] | 62 | } |