1*9880d681SAndroid Build Coastguard Worker //===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file defines utility classes for handling the YAML representation of
11*9880d681SAndroid Build Coastguard Worker // object files.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "llvm/ObjectYAML/YAML.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/ADT/StringExtras.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/raw_ostream.h"
18*9880d681SAndroid Build Coastguard Worker #include <cctype>
19*9880d681SAndroid Build Coastguard Worker
20*9880d681SAndroid Build Coastguard Worker using namespace llvm;
21*9880d681SAndroid Build Coastguard Worker
output(const yaml::BinaryRef & Val,void *,llvm::raw_ostream & Out)22*9880d681SAndroid Build Coastguard Worker void yaml::ScalarTraits<yaml::BinaryRef>::output(
23*9880d681SAndroid Build Coastguard Worker const yaml::BinaryRef &Val, void *, llvm::raw_ostream &Out) {
24*9880d681SAndroid Build Coastguard Worker Val.writeAsHex(Out);
25*9880d681SAndroid Build Coastguard Worker }
26*9880d681SAndroid Build Coastguard Worker
input(StringRef Scalar,void *,yaml::BinaryRef & Val)27*9880d681SAndroid Build Coastguard Worker StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *,
28*9880d681SAndroid Build Coastguard Worker yaml::BinaryRef &Val) {
29*9880d681SAndroid Build Coastguard Worker if (Scalar.size() % 2 != 0)
30*9880d681SAndroid Build Coastguard Worker return "BinaryRef hex string must contain an even number of nybbles.";
31*9880d681SAndroid Build Coastguard Worker // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
32*9880d681SAndroid Build Coastguard Worker // (e.g. a caret pointing to the offending character).
33*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
34*9880d681SAndroid Build Coastguard Worker if (!isxdigit(Scalar[I]))
35*9880d681SAndroid Build Coastguard Worker return "BinaryRef hex string must contain only hex digits.";
36*9880d681SAndroid Build Coastguard Worker Val = yaml::BinaryRef(Scalar);
37*9880d681SAndroid Build Coastguard Worker return StringRef();
38*9880d681SAndroid Build Coastguard Worker }
39*9880d681SAndroid Build Coastguard Worker
writeAsBinary(raw_ostream & OS) const40*9880d681SAndroid Build Coastguard Worker void yaml::BinaryRef::writeAsBinary(raw_ostream &OS) const {
41*9880d681SAndroid Build Coastguard Worker if (!DataIsHexString) {
42*9880d681SAndroid Build Coastguard Worker OS.write((const char *)Data.data(), Data.size());
43*9880d681SAndroid Build Coastguard Worker return;
44*9880d681SAndroid Build Coastguard Worker }
45*9880d681SAndroid Build Coastguard Worker for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
46*9880d681SAndroid Build Coastguard Worker uint8_t Byte;
47*9880d681SAndroid Build Coastguard Worker StringRef((const char *)&Data[I], 2).getAsInteger(16, Byte);
48*9880d681SAndroid Build Coastguard Worker OS.write(Byte);
49*9880d681SAndroid Build Coastguard Worker }
50*9880d681SAndroid Build Coastguard Worker }
51*9880d681SAndroid Build Coastguard Worker
writeAsHex(raw_ostream & OS) const52*9880d681SAndroid Build Coastguard Worker void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {
53*9880d681SAndroid Build Coastguard Worker if (binary_size() == 0)
54*9880d681SAndroid Build Coastguard Worker return;
55*9880d681SAndroid Build Coastguard Worker if (DataIsHexString) {
56*9880d681SAndroid Build Coastguard Worker OS.write((const char *)Data.data(), Data.size());
57*9880d681SAndroid Build Coastguard Worker return;
58*9880d681SAndroid Build Coastguard Worker }
59*9880d681SAndroid Build Coastguard Worker for (uint8_t Byte : Data)
60*9880d681SAndroid Build Coastguard Worker OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
61*9880d681SAndroid Build Coastguard Worker }
62