1//===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file defines utility classes for handling the YAML representation of
10// object files.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ObjectYAML/YAML.h"
15#include "llvm/ADT/StringExtras.h"
16#include "llvm/Support/raw_ostream.h"
17#include <cctype>
18#include <cstdint>
19
20using namespace llvm;
21
22void yaml::ScalarTraits<yaml::BinaryRef>::output(
23    const yaml::BinaryRef &Val, void *, raw_ostream &Out) {
24  Val.writeAsHex(Out);
25}
26
27StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *,
28                                                     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  if (!llvm::all_of(Scalar, llvm::isHexDigit))
34    return "BinaryRef hex string must contain only hex digits.";
35  Val = yaml::BinaryRef(Scalar);
36  return {};
37}
38
39void yaml::BinaryRef::writeAsBinary(raw_ostream &OS, uint64_t N) const {
40  if (!DataIsHexString) {
41    OS.write((const char *)Data.data(), std::min<uint64_t>(N, Data.size()));
42    return;
43  }
44
45  for (uint64_t I = 0, E = std::min<uint64_t>(N, Data.size() / 2); I != E;
46       ++I) {
47    uint8_t Byte = llvm::hexDigitValue(Data[I * 2]);
48    Byte <<= 4;
49    Byte |= llvm::hexDigitValue(Data[I * 2 + 1]);
50    OS.write(Byte);
51  }
52}
53
54void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {
55  if (binary_size() == 0)
56    return;
57  if (DataIsHexString) {
58    OS.write((const char *)Data.data(), Data.size());
59    return;
60  }
61  for (uint8_t Byte : Data)
62    OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
63}
64