Rafael Espindola | 5fd5fe0 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 1 | //===- YAML.cpp - YAMLIO utilities for object files -----------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines utility classes for handling the YAML representation of |
| 11 | // object files. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Rafael Espindola | 18903ff | 2016-03-01 19:15:06 +0000 | [diff] [blame] | 15 | #include "llvm/ObjectYAML/YAML.h" |
Dmitri Gribenko | 9e8eafa | 2013-08-07 05:51:27 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Will Dietz | e3ba15c | 2013-10-12 00:55:57 +0000 | [diff] [blame] | 18 | #include <cctype> |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 19 | #include <cstdint> |
Rafael Espindola | 5fd5fe0 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 20 | |
| 21 | using namespace llvm; |
| 22 | |
Rafael Espindola | 7413fef | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 23 | void yaml::ScalarTraits<yaml::BinaryRef>::output( |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 24 | const yaml::BinaryRef &Val, void *, raw_ostream &Out) { |
Sean Silva | 4370ddb | 2013-06-05 23:47:23 +0000 | [diff] [blame] | 25 | Val.writeAsHex(Out); |
Rafael Espindola | 5fd5fe0 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 26 | } |
| 27 | |
Rafael Espindola | 7413fef | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 28 | StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *, |
| 29 | yaml::BinaryRef &Val) { |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 30 | if (Scalar.size() % 2 != 0) |
| 31 | return "BinaryRef hex string must contain an even number of nybbles."; |
| 32 | // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here? |
| 33 | // (e.g. a caret pointing to the offending character). |
| 34 | for (unsigned I = 0, N = Scalar.size(); I != N; ++I) |
Rafael Espindola | 149d1a1 | 2013-06-07 18:05:03 +0000 | [diff] [blame] | 35 | if (!isxdigit(Scalar[I])) |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 36 | return "BinaryRef hex string must contain only hex digits."; |
Rafael Espindola | 7413fef | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 37 | Val = yaml::BinaryRef(Scalar); |
Eugene Zelenko | cd90344 | 2017-07-01 01:35:55 +0000 | [diff] [blame] | 38 | return {}; |
Rafael Espindola | 5fd5fe0 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 39 | } |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 40 | |
Rafael Espindola | 7413fef | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 41 | void yaml::BinaryRef::writeAsBinary(raw_ostream &OS) const { |
Sean Silva | 6acc982 | 2013-06-05 23:32:31 +0000 | [diff] [blame] | 42 | if (!DataIsHexString) { |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 43 | OS.write((const char *)Data.data(), Data.size()); |
| 44 | return; |
| 45 | } |
| 46 | for (unsigned I = 0, N = Data.size(); I != N; I += 2) { |
| 47 | uint8_t Byte; |
| 48 | StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte); |
| 49 | OS.write(Byte); |
| 50 | } |
| 51 | } |
Sean Silva | 4370ddb | 2013-06-05 23:47:23 +0000 | [diff] [blame] | 52 | |
Rafael Espindola | 7413fef | 2014-07-03 02:01:39 +0000 | [diff] [blame] | 53 | void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const { |
David Majnemer | 8bd2799 | 2014-03-20 06:28:52 +0000 | [diff] [blame] | 54 | if (binary_size() == 0) |
Sean Silva | 845e196 | 2013-07-09 00:54:46 +0000 | [diff] [blame] | 55 | return; |
Sean Silva | 4370ddb | 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 | 940260b | 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 | 4370ddb | 2013-06-05 23:47:23 +0000 | [diff] [blame] | 62 | } |