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" 16#include "llvm/ADT/StringExtras.h" 17#include "llvm/Support/raw_ostream.h" 18 19using namespace llvm; 20using namespace object::yaml; 21 22void yaml::ScalarTraits<object::yaml::BinaryRef>::output( 23 const object::yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) { 24 Val.writeAsHex(Out); 25} 26 27StringRef yaml::ScalarTraits<object::yaml::BinaryRef>::input( 28 StringRef Scalar, void *, object::yaml::BinaryRef &Val) { 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) 34 if (!isxdigit(Scalar[I])) 35 return "BinaryRef hex string must contain only hex digits."; 36 Val = object::yaml::BinaryRef(Scalar); 37 return StringRef(); 38} 39 40void BinaryRef::writeAsBinary(raw_ostream &OS) const { 41 if (!DataIsHexString) { 42 OS.write((const char *)Data.data(), Data.size()); 43 return; 44 } 45 for (unsigned I = 0, N = Data.size(); I != N; I += 2) { 46 uint8_t Byte; 47 StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte); 48 OS.write(Byte); 49 } 50} 51 52void BinaryRef::writeAsHex(raw_ostream &OS) const { 53 if (binary_size() == 0) { 54 OS << "\"\""; 55 return; 56 } 57 if (DataIsHexString) { 58 OS.write((const char *)Data.data(), Data.size()); 59 return; 60 } 61 for (ArrayRef<uint8_t>::iterator I = Data.begin(), E = Data.end(); I != E; 62 ++I) { 63 uint8_t Byte = *I; 64 OS << hexdigit(Byte >> 4); 65 OS << hexdigit(Byte & 0xf); 66 } 67} 68