qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Qemu-devel] [PATCH 3/8] Add QBuffer


From: Anthony Liguori
Subject: Re: [Qemu-devel] [PATCH 3/8] Add QBuffer
Date: Fri, 14 May 2010 13:15:52 -0500
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1.5) Gecko/20091209 Fedora/3.0-4.fc12 Lightning/1.0pre Thunderbird/3.0

On 05/14/2010 08:20 AM, Jan Kiszka wrote:
diff --git a/qjson.c b/qjson.c
index 483c667..4d1c21a 100644
--- a/qjson.c
+++ b/qjson.c
@@ -19,7 +19,9 @@
  #include "qlist.h"
  #include "qbool.h"
  #include "qfloat.h"
+#include "qbuffer.h"
  #include "qdict.h"
+#include "base64.h"

  typedef struct JSONParsingState
  {
@@ -235,6 +237,20 @@ static void to_json(const QObject *obj, QString *str)
          }
          break;
      }
+    case QTYPE_QBUFFER: {
+        QBuffer *val = qobject_to_qbuffer(obj);
+        size_t data_size = qbuffer_get_size(val);
+        size_t str_len = ((data_size + 2) / 3) * 4;
+        char *buffer = qemu_malloc(str_len + 3);
+
+        buffer[0] = '"';
+        base64_encode(qbuffer_get_data(val), data_size, buffer + 1);
+        buffer[str_len + 1] = '"';
+        buffer[str_len + 2] = 0;
+        qstring_append(str, buffer);
+        qemu_free(buffer);
+        break;
+    }

Instead of encoding just as a string, it would be a good idea to encode it as something like:

{'__class__': 'base64', 'data': ...}

We've discussed using hidden properties to describe special things like abstract classes and since we already have this namespace reserved, I think it's a good time to use it.

The advantage is that in a dynamic language like Python, the parser can convert base64 to binary strings automatically without having to understand the QMP protocol.

Regards,

Anthony Liguori

      case QTYPE_QERROR:
          /* XXX: should QError be emitted? */
      case QTYPE_NONE:
diff --git a/qobject.h b/qobject.h
index 07de211..45c4fa0 100644
--- a/qobject.h
+++ b/qobject.h
@@ -44,6 +44,7 @@ typedef enum {
      QTYPE_QFLOAT,
      QTYPE_QBOOL,
      QTYPE_QERROR,
+    QTYPE_QBUFFER,
  } qtype_code;

  struct QObject;




reply via email to

[Prev in Thread] Current Thread [Next in Thread]