blob: 6eba16e36c2a21627f2d3ae9139b0791d541d3a0 [file] [log] [blame]
Rafael Espindola8e713012013-06-05 02:32:26 +00001//===- YAML.cpp - YAMLIO utilities for object files -----------------------===//
2//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Rafael Espindola8e713012013-06-05 02:32:26 +00006//
7//===----------------------------------------------------------------------===//
8//
9// This file defines utility classes for handling the YAML representation of
10// object files.
11//
12//===----------------------------------------------------------------------===//
13
Rafael Espindolaebd91932016-03-01 19:15:06 +000014#include "llvm/ObjectYAML/YAML.h"
Dmitri Gribenkodf73c302013-08-07 05:51:27 +000015#include "llvm/ADT/StringExtras.h"
Sean Silva23f3b822013-06-05 22:59:00 +000016#include "llvm/Support/raw_ostream.h"
Will Dietz981af002013-10-12 00:55:57 +000017#include <cctype>
Eugene Zelenko28082ab2017-07-01 01:35:55 +000018#include <cstdint>
Rafael Espindola8e713012013-06-05 02:32:26 +000019
20using namespace llvm;
21
Rafael Espindola97de4742014-07-03 02:01:39 +000022void yaml::ScalarTraits<yaml::BinaryRef>::output(
Eugene Zelenko28082ab2017-07-01 01:35:55 +000023 const yaml::BinaryRef &Val, void *, raw_ostream &Out) {
Sean Silva42fa38f2013-06-05 23:47:23 +000024 Val.writeAsHex(Out);
Rafael Espindola8e713012013-06-05 02:32:26 +000025}
26
Rafael Espindola97de4742014-07-03 02:01:39 +000027StringRef yaml::ScalarTraits<yaml::BinaryRef>::input(StringRef Scalar, void *,
28 yaml::BinaryRef &Val) {
Sean Silva23f3b822013-06-05 22:59:00 +000029 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)
Roman Lebedev41828012019-02-06 08:57:01 +000034 if (!llvm::isHexDigit(Scalar[I]))
Sean Silva23f3b822013-06-05 22:59:00 +000035 return "BinaryRef hex string must contain only hex digits.";
Rafael Espindola97de4742014-07-03 02:01:39 +000036 Val = yaml::BinaryRef(Scalar);
Eugene Zelenko28082ab2017-07-01 01:35:55 +000037 return {};
Rafael Espindola8e713012013-06-05 02:32:26 +000038}
Sean Silva23f3b822013-06-05 22:59:00 +000039
Rafael Espindola97de4742014-07-03 02:01:39 +000040void yaml::BinaryRef::writeAsBinary(raw_ostream &OS) const {
Sean Silvab7e82ad2013-06-05 23:32:31 +000041 if (!DataIsHexString) {
Sean Silva23f3b822013-06-05 22:59:00 +000042 OS.write((const char *)Data.data(), Data.size());
43 return;
44 }
45 for (unsigned I = 0, N = Data.size(); I != N; I += 2) {
Roman Lebedev41828012019-02-06 08:57:01 +000046 uint8_t Byte = llvm::hexDigitValue(Data[I]);
47 Byte <<= 4;
48 Byte |= llvm::hexDigitValue(Data[I + 1]);
Sean Silva23f3b822013-06-05 22:59:00 +000049 OS.write(Byte);
50 }
51}
Sean Silva42fa38f2013-06-05 23:47:23 +000052
Rafael Espindola97de4742014-07-03 02:01:39 +000053void yaml::BinaryRef::writeAsHex(raw_ostream &OS) const {
David Majnemerd9eb2d12014-03-20 06:28:52 +000054 if (binary_size() == 0)
Sean Silva2f672d62013-07-09 00:54:46 +000055 return;
Sean Silva42fa38f2013-06-05 23:47:23 +000056 if (DataIsHexString) {
57 OS.write((const char *)Data.data(), Data.size());
58 return;
59 }
Simon Atanasyan255689c2016-03-01 10:11:27 +000060 for (uint8_t Byte : Data)
61 OS << hexdigit(Byte >> 4) << hexdigit(Byte & 0xf);
Sean Silva42fa38f2013-06-05 23:47:23 +000062}