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