[Pkg-bitcoin-commits] [libunivalue] 12/76: univalue_read: fix \u escapes. we now pass all important tests.

Jonas Smedegaard dr at jones.dk
Mon Apr 4 09:18:27 UTC 2016


This is an automated email from the git hooks/post-receive script.

js pushed a commit to branch master
in repository libunivalue.

commit e122f34c560e49fe712e66db7c466ab025d21a1a
Author: Jeff Garzik <jgarzik at bitpay.com>
Date:   Thu Jun 12 21:46:32 2014 -0400

    univalue_read: fix \u escapes. we now pass all important tests.
---
 unitester.cpp     |  2 +-
 univalue_read.cpp | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++---
 2 files changed, 51 insertions(+), 4 deletions(-)

diff --git a/unitester.cpp b/unitester.cpp
index f160421..9eb5a33 100644
--- a/unitester.cpp
+++ b/unitester.cpp
@@ -107,7 +107,7 @@ static const char *filenames[] = {
         "fail7.json",
         "fail8.json",
         "fail9.json",               // extra comma
-        //"pass1.json",             // investigate
+        "pass1.json",
         "pass2.json",
         "pass3.json",
 };
diff --git a/univalue_read.cpp b/univalue_read.cpp
index dc805eb..77db512 100644
--- a/univalue_read.cpp
+++ b/univalue_read.cpp
@@ -24,6 +24,33 @@ enum tokentype {
     TOK_STRING,
 };
 
+// convert hexadecimal string to unsigned integer
+static const char *hatoui(const char *first, const char *last,
+                          unsigned int& out)
+{
+    unsigned int result = 0;
+    for (; first != last; ++first)
+    {
+        int digit;
+        if (isdigit(*first))
+            digit = *first - '0';
+
+        else if (*first >= 'a' && *first <= 'f')
+            digit = *first - 'a' + 10;
+
+        else if (*first >= 'A' && *first <= 'F')
+            digit = *first - 'A' + 10;
+
+        else
+            break;
+
+        result = 16 * result + digit;
+    }
+    out = result;
+
+    return first;
+}
+
 enum tokentype getJsonToken(string& tokenVal, unsigned int& consumed,
                             const char *raw)
 {
@@ -174,10 +201,30 @@ enum tokentype getJsonToken(string& tokenVal, unsigned int& consumed,
                 case 'n':  valStr += "\n"; break;
                 case 'r':  valStr += "\r"; break;
                 case 't':  valStr += "\t"; break;
-                case 'u':
-                    // TODO: not supported yet
-                    assert(0);
+
+                case 'u': {
+                    char buf[4] = {0,0,0,0};
+                    char *last = &buf[0];
+                    unsigned int codepoint;
+                    if (hatoui(raw + 1, raw + 1 + 4, codepoint) !=
+                               raw + 1 + 4)
+                        return TOK_ERR;
+
+                    if (codepoint <= 0x7f)
+                         *last = (char)codepoint;
+                    else if (codepoint <= 0x7FF) {
+                        *last++ = (char)(0xC0 | (codepoint >> 6));
+                        *last = (char)(0x80 | (codepoint & 0x3F));
+                    } else if (codepoint <= 0xFFFF) {
+                        *last++ = (char)(0xE0 | (codepoint >> 12));
+                        *last++ = (char)(0x80 | ((codepoint >> 6) & 0x3F));
+                        *last = (char)(0x80 | (codepoint & 0x3F));
+                    }
+
+                    valStr += buf;
+                    raw += 4;
                     break;
+                    }
                 default:
                     return TOK_ERR;
 

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-bitcoin/libunivalue.git



More information about the Pkg-bitcoin-commits mailing list