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 | |
| 15 | #include "llvm/Object/YAML.h" |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 16 | #include "llvm/Support/raw_ostream.h" |
Rafael Espindola | 5fd5fe0 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 17 | |
| 18 | using namespace llvm; |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 19 | using namespace object::yaml; |
Rafael Espindola | 5fd5fe0 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 20 | |
| 21 | void yaml::ScalarTraits<object::yaml::BinaryRef>::output( |
| 22 | const object::yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) { |
Sean Silva | 4370ddb | 2013-06-05 23:47:23 +0000 | [diff] [blame] | 23 | Val.writeAsHex(Out); |
Rafael Espindola | 5fd5fe0 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 24 | } |
| 25 | |
| 26 | StringRef yaml::ScalarTraits<object::yaml::BinaryRef>::input( |
| 27 | StringRef Scalar, void *, object::yaml::BinaryRef &Val) { |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 28 | if (Scalar.size() % 2 != 0) |
| 29 | return "BinaryRef hex string must contain an even number of nybbles."; |
| 30 | // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here? |
| 31 | // (e.g. a caret pointing to the offending character). |
| 32 | for (unsigned I = 0, N = Scalar.size(); I != N; ++I) |
Rafael Espindola | 149d1a1 | 2013-06-07 18:05:03 +0000 | [diff] [blame^] | 33 | if (!isxdigit(Scalar[I])) |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 34 | return "BinaryRef hex string must contain only hex digits."; |
Rafael Espindola | 5fd5fe0 | 2013-06-05 02:32:26 +0000 | [diff] [blame] | 35 | Val = object::yaml::BinaryRef(Scalar); |
| 36 | return StringRef(); |
| 37 | } |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 38 | |
| 39 | void BinaryRef::writeAsBinary(raw_ostream &OS) const { |
Sean Silva | 6acc982 | 2013-06-05 23:32:31 +0000 | [diff] [blame] | 40 | if (!DataIsHexString) { |
Sean Silva | 639adc5 | 2013-06-05 22:59:00 +0000 | [diff] [blame] | 41 | OS.write((const char *)Data.data(), Data.size()); |
| 42 | return; |
| 43 | } |
| 44 | for (unsigned I = 0, N = Data.size(); I != N; I += 2) { |
| 45 | uint8_t Byte; |
| 46 | StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte); |
| 47 | OS.write(Byte); |
| 48 | } |
| 49 | } |
Sean Silva | 4370ddb | 2013-06-05 23:47:23 +0000 | [diff] [blame] | 50 | |
| 51 | void BinaryRef::writeAsHex(raw_ostream &OS) const { |
| 52 | if (DataIsHexString) { |
| 53 | OS.write((const char *)Data.data(), Data.size()); |
| 54 | return; |
| 55 | } |
| 56 | for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E; |
| 57 | ++I) { |
| 58 | uint8_t Byte = *I; |
| 59 | OS << hexdigit(Byte >> 4); |
| 60 | OS << hexdigit(Byte & 0xf); |
| 61 | } |
| 62 | } |