1ebd9193bSRafael Espindola //===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
2ebd9193bSRafael Espindola //
32946cd70SChandler Carruth // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
42946cd70SChandler Carruth // See https://llvm.org/LICENSE.txt for license information.
52946cd70SChandler Carruth // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6ebd9193bSRafael Espindola //
7ebd9193bSRafael Espindola //===----------------------------------------------------------------------===//
8ebd9193bSRafael Espindola //
9ebd9193bSRafael Espindola // This file defines utility classes for handling the YAML representation of
10ebd9193bSRafael Espindola // object files.
11ebd9193bSRafael Espindola //
12ebd9193bSRafael Espindola //===----------------------------------------------------------------------===//
13ebd9193bSRafael Espindola
14ebd9193bSRafael Espindola #include "llvm/ObjectYAML/YAML.h"
15ebd9193bSRafael Espindola #include "llvm/ADT/StringExtras.h"
16ebd9193bSRafael Espindola #include "llvm/Support/raw_ostream.h"
17ebd9193bSRafael Espindola #include <cctype>
1828082ab0SEugene Zelenko #include <cstdint>
19ebd9193bSRafael Espindola
20ebd9193bSRafael Espindola using namespace llvm;
21ebd9193bSRafael Espindola
output(const yaml::BinaryRef & Val,void *,raw_ostream & Out)22ebd9193bSRafael Espindola void yaml::ScalarTraits<yaml::BinaryRef>::output(
2328082ab0SEugene Zelenko const yaml::BinaryRef &Val, void *, raw_ostream &Out) {
24ebd9193bSRafael Espindola Val.writeAsHex(Out);
25ebd9193bSRafael Espindola }
26ebd9193bSRafael Espindola
input(StringRef Scalar,void *,yaml::BinaryRef & Val)27ebd9193bSRafael Espindola StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *,
28ebd9193bSRafael Espindola yaml::BinaryRef &Val) {
29ebd9193bSRafael Espindola if (Scalar.size() % 2 != 0)
30ebd9193bSRafael Espindola return "BinaryRef hex string must contain an even number of nybbles.";
31ebd9193bSRafael Espindola // TODO: Can we improve YAMLIO to permit a more accurate diagnostic here?
32ebd9193bSRafael Espindola // (e.g. a caret pointing to the offending character).
33*32de467fSKazu Hirata if (!llvm::all_of(Scalar, llvm::isHexDigit))
34ebd9193bSRafael Espindola return "BinaryRef hex string must contain only hex digits.";
35ebd9193bSRafael Espindola Val = yaml::BinaryRef(Scalar);
3628082ab0SEugene Zelenko return {};
37ebd9193bSRafael Espindola }
38ebd9193bSRafael Espindola
writeAsBinary(raw_ostream & OS,uint64_t N) const3906456daaSGeorgii Rymar void yaml::BinaryRef::writeAsBinary(raw_ostream &OS, uint64_t N) const {
40ebd9193bSRafael Espindola if (!DataIsHexString) {
416b15c5dfSGeorgii Rymar OS.write((const char *)Data.data(), std::min<uint64_t>(N, Data.size()));
42ebd9193bSRafael Espindola return;
43ebd9193bSRafael Espindola }
4406456daaSGeorgii Rymar
45a26d7b62SGeorgii Rymar for (uint64_t I = 0, E = std::min<uint64_t>(N, Data.size() / 2); I != E;
46a26d7b62SGeorgii Rymar ++I) {
4706456daaSGeorgii Rymar uint8_t Byte = llvm::hexDigitValue(Data[I * 2]);
4841828010SRoman Lebedev Byte <<= 4;
4906456daaSGeorgii Rymar Byte |= llvm::hexDigitValue(Data[I * 2 + 1]);
50ebd9193bSRafael Espindola OS.write(Byte);
51ebd9193bSRafael Espindola }
52ebd9193bSRafael Espindola }
53ebd9193bSRafael Espindola
writeAsHex(raw_ostream & OS) const54ebd9193bSRafael Espindola void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {
55ebd9193bSRafael Espindola if (binary_size() == 0)
56ebd9193bSRafael Espindola return;
57ebd9193bSRafael Espindola if (DataIsHexString) {
58ebd9193bSRafael Espindola OS.write((const char *)Data.data(), Data.size());
59ebd9193bSRafael Espindola return;
60ebd9193bSRafael Espindola }
61ebd9193bSRafael Espindola for (uint8_t Byte : Data)
62ebd9193bSRafael Espindola OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
63ebd9193bSRafael Espindola }
64