afl.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Copyright (c) 2009-2017 Dave Gamble and cJSON contributors
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include "../cJSON.h"
  23. static char *read_file(const char *filename)
  24. {
  25. FILE *file = NULL;
  26. long length = 0;
  27. char *content = NULL;
  28. size_t read_chars = 0;
  29. /* open in read binary mode */
  30. file = fopen(filename, "rb");
  31. if (file == NULL)
  32. {
  33. goto cleanup;
  34. }
  35. /* get the length */
  36. if (fseek(file, 0, SEEK_END) != 0)
  37. {
  38. goto cleanup;
  39. }
  40. length = ftell(file);
  41. if (length < 0)
  42. {
  43. goto cleanup;
  44. }
  45. if (fseek(file, 0, SEEK_SET) != 0)
  46. {
  47. goto cleanup;
  48. }
  49. /* allocate content buffer */
  50. content = (char*)malloc((size_t)length + sizeof(""));
  51. if (content == NULL)
  52. {
  53. goto cleanup;
  54. }
  55. /* read the file into memory */
  56. read_chars = fread(content, sizeof(char), (size_t)length, file);
  57. if ((long)read_chars != length)
  58. {
  59. free(content);
  60. content = NULL;
  61. goto cleanup;
  62. }
  63. content[read_chars] = '\0';
  64. cleanup:
  65. if (file != NULL)
  66. {
  67. fclose(file);
  68. }
  69. return content;
  70. }
  71. int main(int argc, char** argv)
  72. {
  73. const char *filename = NULL;
  74. cJSON *item = NULL;
  75. char *json = NULL;
  76. int status = EXIT_FAILURE;
  77. char *printed_json = NULL;
  78. if ((argc < 2) || (argc > 3))
  79. {
  80. printf("Usage:\n");
  81. printf("%s input_file [enable_printing]\n", argv[0]);
  82. printf("\t input_file: file containing the test data\n");
  83. printf("\t enable_printing: print after parsing, 'yes' or 'no', defaults to 'no'\n");
  84. goto cleanup;
  85. }
  86. filename = argv[1];
  87. #if __AFL_HAVE_MANUAL_CONTROL
  88. while (__AFL_LOOP(1000))
  89. {
  90. #endif
  91. status = EXIT_SUCCESS;
  92. json = read_file(filename);
  93. if ((json == NULL) || (json[0] == '\0') || (json[1] == '\0'))
  94. {
  95. status = EXIT_FAILURE;
  96. goto cleanup;
  97. }
  98. item = cJSON_Parse(json + 2);
  99. if (item == NULL)
  100. {
  101. goto cleanup;
  102. }
  103. if ((argc == 3) && (strncmp(argv[2], "yes", 3) == 0))
  104. {
  105. int do_format = 0;
  106. if (json[1] == 'f')
  107. {
  108. do_format = 1;
  109. }
  110. if (json[0] == 'b')
  111. {
  112. /* buffered printing */
  113. printed_json = cJSON_PrintBuffered(item, 1, do_format);
  114. }
  115. else
  116. {
  117. /* unbuffered printing */
  118. if (do_format)
  119. {
  120. printed_json = cJSON_Print(item);
  121. }
  122. else
  123. {
  124. printed_json = cJSON_PrintUnformatted(item);
  125. }
  126. }
  127. if (printed_json == NULL)
  128. {
  129. status = EXIT_FAILURE;
  130. goto cleanup;
  131. }
  132. printf("%s\n", printed_json);
  133. }
  134. cleanup:
  135. if (item != NULL)
  136. {
  137. cJSON_Delete(item);
  138. item = NULL;
  139. }
  140. if (json != NULL)
  141. {
  142. free(json);
  143. json = NULL;
  144. }
  145. if (printed_json != NULL)
  146. {
  147. free(printed_json);
  148. printed_json = NULL;
  149. }
  150. #if __AFL_HAVE_MANUAL_CONTROL
  151. }
  152. #endif
  153. return status;
  154. }