Coverage Report

Created: 2025-10-10 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/FreeRDP/winpr/libwinpr/utils/json/json.c
Line
Count
Source
1
/**
2
 * FreeRDP: A Remote Desktop Protocol Implementation
3
 * JSON parser wrapper
4
 *
5
 * Copyright 2024 Armin Novak <anovak@thincast.com>
6
 * Copyright 2024 Thincast Technologies GmbH
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
#include <math.h>
21
#include <errno.h>
22
23
#include <winpr/file.h>
24
#include <winpr/json.h>
25
#include <winpr/assert.h>
26
27
WINPR_JSON* WINPR_JSON_ParseFromFile(const char* filename)
28
0
{
29
0
  FILE* fp = winpr_fopen(filename, "r");
30
0
  if (!fp)
31
0
    return NULL;
32
0
  WINPR_JSON* json = WINPR_JSON_ParseFromFileFP(fp);
33
0
  (void)fclose(fp);
34
0
  return json;
35
0
}
36
37
WINPR_JSON* WINPR_JSON_ParseFromFileFP(FILE* fp)
38
0
{
39
0
  if (!fp)
40
0
    return NULL;
41
42
0
  if (fseek(fp, 0, SEEK_END) != 0)
43
0
    return NULL;
44
45
0
  const INT64 size = _ftelli64(fp);
46
0
  if (size < 0)
47
0
    return NULL;
48
49
0
  if (fseek(fp, 0, SEEK_SET) != 0)
50
0
    return NULL;
51
52
0
  const size_t usize = WINPR_ASSERTING_INT_CAST(size_t, size);
53
0
  char* str = calloc(usize + 1, sizeof(char));
54
0
  if (!str)
55
0
    return NULL;
56
57
0
  WINPR_JSON* json = NULL;
58
0
  const size_t s = fread(str, sizeof(char), usize, fp);
59
0
  if (s == usize)
60
0
    json = WINPR_JSON_ParseWithLength(str, usize);
61
0
  free(str);
62
0
  return json;
63
0
}