aboutsummaryrefslogtreecommitdiffstats
path: root/src/json-common.h
blob: c8e708b0f709b3d5a8af54aaef4e85d8d398f93f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/* json-common.h - Common defs for gpgme-json et al.
 * Copyright (C) 2025 g10 Code GmbH
 *
 * This file is part of GPGME.
 *
 * GPGME is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * GPGME is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, see <https://gnu.org/licenses/>.
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#ifndef GPGME_JSON_COMMON_H
#define GNUPG_JSON_COMMON_H


#ifndef BUILD_COMMITID
# error config.h not yet included
#endif
#ifdef GPGME_VERSION_NUMBER
# error gpgme.h already included
#endif

#define GPGRT_ENABLE_ES_MACROS 1
#define GPGRT_ENABLE_LOG_MACROS 1
#define GPGRT_ENABLE_ARGPARSE_MACROS 1
#include "gpgme.h"
#include "cJSON.h"


/* Only use calloc. */
#define CALLOC_ONLY 1


/* An object to keep state for the gpgme-json tools.  For the classic
 * gpgme-json tool tehre is just one instance of it but for a server
 * there will be one per connection.  */
struct json_common_s
{
  /* True if interactive mode is active - this changes the way json
   * objects are formatted.  */
  int interactive;


  /* Pending data to be returned by a getmore command.  */
  struct
  {
    char  *buffer;   /* Malloced data or NULL if not used.  */
    size_t length;   /* Length of that data.  */
    size_t written;  /* # of already written bytes from BUFFER.  */
  } pending_data;

};
typedef struct json_common_s *ctrl_t;


#define xtrystrdup(a)  gpgrt_strdup ((a))
#define xcalloc(a,b) ({                         \
      void *_r = gpgrt_calloc ((a), (b));       \
      if (!_r)                                  \
        xoutofcore ("calloc");                  \
      _r; })
#define xstrdup(a) ({                           \
      char *_r = gpgrt_strdup ((a));            \
      if (!_r)                                  \
        xoutofcore ("strdup");                  \
      _r; })
#define xstrconcat(a, ...) ({                           \
      char *_r = gpgrt_strconcat ((a), __VA_ARGS__);    \
      if (!_r)                                          \
        xoutofcore ("strconcat");                       \
      _r; })
#define xfree(a) gpgrt_free ((a))


#if CALLOC_ONLY
#define xtrymalloc(a)  gpgrt_calloc (1, (a))
#define xmalloc(a) xcalloc(1, (a))
#else
#define xtrymalloc(a)  gpgrt_malloc ((a))
#define xmalloc(a) ({                           \
      void *_r = gpgrt_malloc ((a));            \
      if (!_r)                                  \
        xoutofcore ("malloc");                  \
      _r; })
#endif

#define spacep(p)   (*(p) == ' ' || *(p) == '\t')

#ifndef HAVE_STPCPY
static GPGRT_INLINE char *
_my_stpcpy (char *a, const char *b)
{
  while (*b)
    *a++ = *b++;
  *a = 0;
  return a;
}
#define stpcpy(a,b) _my_stpcpy ((a), (b))
#endif /*!HAVE_STPCPY*/


/*-- json-util.c --*/

void xoutofcore (const char *type) GPGRT_ATTR_NORETURN;
void xfree_array (char **array);

const char *data_type_to_string (gpgme_data_type_t dt);

char *xjson_Print (cjson_t object);
cjson_t xjson_CreateObject (void);
cjson_t xjson_CreateArray (void);
gpg_error_t cjson_AddStringToObject (cjson_t object, const char *name,
                                     const char *string);
void xjson_AddStringToObject (cjson_t object, const char *name,
                              const char *string);
void xjson_AddStringToObject0 (cjson_t object, const char *name,
                               const char *string);
void xjson_AddBoolToObject (cjson_t object, const char *name, int abool);
void xjson_AddNumberToObject (cjson_t object, const char *name, double dbl);
void xjson_AddItemToObject (cjson_t object, const char *name, cjson_t item);

cjson_t error_object (cjson_t json, const char *message, ...);
cjson_t gpg_error_object (cjson_t json, gpg_error_t err,
                          const char *message, ...);
char *error_object_string (const char *message, ...);


/*-- json-core.c --*/


char *json_core_process_request (ctrl_t ctrl, const char *request);



#endif /*GNUPG_JSON_COMMON_H*/