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