jsondump.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * OpenConnect (SSL + DTLS) VPN client
  3. *
  4. * Copyright © 2020 David Woodhouse
  5. *
  6. * Author: David Woodhouse <dwmw2@infradead.org>
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public License
  10. * version 2.1, as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. */
  17. #include <config.h>
  18. #include "openconnect-internal.h"
  19. #include "json.h"
  20. #include <string.h>
  21. #include <ctype.h>
  22. #include <errno.h>
  23. /*
  24. * Copyright (C) 2015 Mirko Pasqualetti All rights reserved.
  25. * https://github.com/udp/json-parser
  26. *
  27. * Redistribution and use in source and binary forms, with or without
  28. * modification, are permitted provided that the following conditions
  29. * are met:
  30. *
  31. * 1. Redistributions of source code must retain the above copyright
  32. * notice, this list of conditions and the following disclaimer.
  33. *
  34. * 2. Redistributions in binary form must reproduce the above copyright
  35. * notice, this list of conditions and the following disclaimer in the
  36. * documentation and/or other materials provided with the distribution.
  37. *
  38. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  39. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  40. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  41. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  42. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  43. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  44. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  45. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  46. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  47. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  48. * SUCH DAMAGE.
  49. */
  50. static void print_buf(struct openconnect_info *vpninfo, int lvl, struct oc_text_buf *buf)
  51. {
  52. if (!buf_error(buf))
  53. vpn_progress(vpninfo, lvl, "%s", buf->data);
  54. buf_truncate(buf);
  55. }
  56. static void print_depth_shift(struct oc_text_buf *buf, int depth)
  57. {
  58. int j;
  59. for (j=0; j < depth; j++) {
  60. buf_append(buf, " ");
  61. }
  62. }
  63. static void dump_json_value(struct openconnect_info *vpninfo, int lvl,
  64. struct oc_text_buf *buf, json_value *value, int depth);
  65. static void dump_json_object(struct openconnect_info *vpninfo, int lvl,
  66. struct oc_text_buf *buf, json_value* value, int depth)
  67. {
  68. int length, x;
  69. if (value == NULL) {
  70. return;
  71. }
  72. length = value->u.object.length;
  73. for (x = 0; x < length; x++) {
  74. print_depth_shift(buf, depth);
  75. buf_append(buf, "object[%d].name = %s\n", x, value->u.object.values[x].name);
  76. print_buf(vpninfo, lvl, buf);
  77. dump_json_value(vpninfo, lvl, buf, value->u.object.values[x].value, depth+1);
  78. }
  79. }
  80. static void dump_json_array(struct openconnect_info *vpninfo, int lvl,
  81. struct oc_text_buf *buf, json_value* value, int depth)
  82. {
  83. int length, x;
  84. if (value == NULL) {
  85. return;
  86. }
  87. length = value->u.array.length;
  88. buf_append(buf, "array\n");
  89. print_buf(vpninfo, lvl, buf);
  90. for (x = 0; x < length; x++) {
  91. dump_json_value(vpninfo, lvl, buf, value->u.array.values[x], depth);
  92. }
  93. }
  94. static void dump_json_value(struct openconnect_info *vpninfo, int lvl,
  95. struct oc_text_buf *buf, json_value *value, int depth)
  96. {
  97. if (!value)
  98. return;
  99. if (value->type != json_object) {
  100. print_depth_shift(buf, depth);
  101. }
  102. switch (value->type) {
  103. case json_none:
  104. default:
  105. buf_append(buf, "none\n");
  106. break;
  107. case json_object:
  108. dump_json_object(vpninfo, lvl, buf, value, depth+1);
  109. return;
  110. case json_array:
  111. dump_json_array(vpninfo, lvl, buf, value, depth+1);
  112. return;
  113. case json_integer:
  114. buf_append(buf, "int: %10" PRId64 "\n", value->u.integer);
  115. break;
  116. case json_double:
  117. buf_append(buf, "double: %f\n", value->u.dbl);
  118. break;
  119. case json_string:
  120. buf_append(buf, "string: %s\n", value->u.string.ptr);
  121. break;
  122. case json_boolean:
  123. buf_append(buf, "bool: %d\n", value->u.boolean);
  124. break;
  125. }
  126. print_buf(vpninfo, lvl, buf);
  127. }
  128. void dump_json(struct openconnect_info *vpninfo, int lvl, json_value *value)
  129. {
  130. struct oc_text_buf *buf = buf_alloc();
  131. if (!buf)
  132. return;
  133. dump_json_value(vpninfo, lvl, buf, value, 0);
  134. buf_free(buf);
  135. }