[esnacc-dev] [PATCH 02/11] cxx-lib/asn-bits: Update the xml printing

Aaron Conole aconole at bytheb.org
Tue Dec 6 19:47:00 UTC 2016


Commit b9d213af105366 ("cxx-lib: PrintXML cleanups") updated most of the
printing routines, but failed to properly update the asn.1 bitstring
class.  This means data was not properly being printed properly (with 1,
0 and whitespace being the only acceptable characters).

This change updates that to ensure compliance with XER rules.

Signed-off-by: Aaron Conole <aconole at bytheb.org>
---
 cxx-lib/src/asn-bits.cpp | 36 ++++++++++++++++++++++++++++++------
 1 file changed, 30 insertions(+), 6 deletions(-)

diff --git a/cxx-lib/src/asn-bits.cpp b/cxx-lib/src/asn-bits.cpp
index 54cdef1..afe5a74 100644
--- a/cxx-lib/src/asn-bits.cpp
+++ b/cxx-lib/src/asn-bits.cpp
@@ -876,12 +876,36 @@ void AsnBits::Print(std::ostream& os, unsigned short /*indent*/) const
 
 void AsnBits::PrintXML(std::ostream &os, const char *lpszTitle) const
 {
-   os << "<BIT_STRING>";
-   if (lpszTitle)
-      os << lpszTitle;
-   os << "-";
-   Print(os);
-   os << "</BIT_STRING>\n";
+    const char *tagName = "BIT_STRING";
+    if (lpszTitle)
+        tagName = lpszTitle;
+    os << "<" << tagName << ">";
+
+    size_t octetsLessOne = (bitLen-1)/8;
+    size_t usedBits = bitLen % 8;
+
+    if (!bitLen)
+        octetsLessOne = 0;
+
+    for (size_t i = 0; i < octetsLessOne; ++i) {
+        unsigned char c = bits[i];
+        for (size_t j = 0; j < sizeof c; ++j) {
+            os << ((c & (1 << j)) ? "1" : "0");
+        }
+        os << " ";
+    }
+
+    if (usedBits) {
+        unsigned char c = bits[octetsLessOne];
+        for (size_t i = 0; i < usedBits; ++i) {
+            os << ((c & (1 << i)) ? "1" : "0");
+        }
+    }
+
+    if (lpszTitle)
+        os << "</" << lpszTitle << ">\n";
+    else
+        os << "</BIT_STRING>\n";
 }
 
 
-- 
2.7.4




More information about the dev mailing list