xref: /freebsd/contrib/llvm-project/llvm/lib/ObjectYAML/YAML.cpp (revision 2f513db72b034fd5ef7f080b11be5c711c15186a)
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 
20 using namespace llvm;
21 
22 void yaml::ScalarTraits<yaml::BinaryRef>::output(
23     const yaml::BinaryRef &Val, void *, raw_ostream &Out) {
24   Val.writeAsHex(Out);
25 }
26 
27 StringRef 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   for (unsigned I = 0, N = Scalar.size(); I != N; ++I)
34     if (!llvm::isHexDigit(Scalar[I]))
35       return "BinaryRef hex string must contain only hex digits.";
36   Val = yaml::BinaryRef(Scalar);
37   return {};
38 }
39 
40 void yaml::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 = llvm::hexDigitValue(Data[I]);
47     Byte <<= 4;
48     Byte |= llvm::hexDigitValue(Data[I + 1]);
49     OS.write(Byte);
50   }
51 }
52 
53 void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {
54   if (binary_size() == 0)
55     return;
56   if (DataIsHexString) {
57     OS.write((const char *)Data.data(), Data.size());
58     return;
59   }
60   for (uint8_t Byte : Data)
61     OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
62 }
63