GNUnet  0.11.x
json_generator.c
Go to the documentation of this file.
1 /*
2  This file is part of GNUnet
3  Copyright (C) 2014, 2015, 2016 GNUnet e.V.
4 
5  GNUnet is free software: you can redistribute it and/or modify it
6  under the terms of the GNU Affero General Public License as published
7  by the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  GNUnet is distributed in the hope that it will be useful, but
11  WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  Affero General Public License for more details.
14 
15  You should have received a copy of the GNU Affero General Public License
16  along with this program. If not, see <http://www.gnu.org/licenses/>.
17 
18  SPDX-License-Identifier: AGPL3.0-or-later
19  */
25 #include "platform.h"
26 #include "gnunet_util_lib.h"
27 #include "gnunet_json_lib.h"
28 
29 
38 json_t *
40  size_t size)
41 {
42  char *buf;
43  json_t *json;
44 
45  if ((size * 8 + 4) / 5 + 1 >=
47  {
48  GNUNET_break (0);
49  return NULL;
50  }
52  size);
53  json = json_string (buf);
54  GNUNET_free (buf);
55  GNUNET_break (NULL != json);
56  return json;
57 }
58 
59 
66 json_t *
68 {
69  json_t *j;
70 
72  GNUNET_TIME_round_abs (&stamp));
73 
74  j = json_object ();
75  if (NULL == j)
76  {
77  GNUNET_break (0);
78  return NULL;
79  }
80  if (stamp.abs_value_us == GNUNET_TIME_UNIT_FOREVER_ABS.abs_value_us)
81  {
82  if (0 !=
83  json_object_set_new (j,
84  "t_ms",
85  json_string ("never")))
86  {
87  GNUNET_break (0);
88  json_decref (j);
89  return NULL;
90  }
91  return j;
92  }
93  if (0 !=
94  json_object_set_new (j,
95  "t_ms",
96  json_integer ((json_int_t) (stamp.abs_value_us
97  / 1000LL))))
98  {
99  GNUNET_break (0);
100  json_decref (j);
101  return NULL;
102  }
103  return j;
104 }
105 
106 
113 json_t *
115 {
117 }
118 
119 
126 json_t *
128 {
129  json_t *j;
130 
132  GNUNET_TIME_round_rel (&stamp));
133 
134  j = json_object ();
135  if (NULL == j)
136  {
137  GNUNET_break (0);
138  return NULL;
139  }
140  if (stamp.rel_value_us == GNUNET_TIME_UNIT_FOREVER_REL.rel_value_us)
141  {
142  if (0 !=
143  json_object_set_new (j,
144  "d_ms",
145  json_string ("forever")))
146  {
147  GNUNET_break (0);
148  json_decref (j);
149  return NULL;
150  }
151  return j;
152  }
153  if (0 !=
154  json_object_set_new (j,
155  "d_ms",
156  json_integer ((json_int_t) (stamp.rel_value_us
157  / 1000LL))))
158  {
159  GNUNET_break (0);
160  json_decref (j);
161  return NULL;
162  }
163  return j;
164 }
165 
166 
173 json_t *
175 {
176  void *buf;
177  size_t buf_len;
178  json_t *ret;
179 
181  &buf);
183  buf_len);
184  GNUNET_free (buf);
185  return ret;
186 }
187 
188 
195 json_t *
197 {
198  void *buf;
199  size_t buf_len;
200  json_t *ret;
201 
202  buf_len = GNUNET_CRYPTO_rsa_signature_encode (sig,
203  &buf);
205  buf_len);
206  GNUNET_free (buf);
207  return ret;
208 }
209 
210 
211 /* End of json/json_generator.c */
static int ret
Return value of the commandline.
Definition: gnunet-abd.c:81
uint32_t data
The data value.
struct GNUNET_IDENTITY_PrivateKey pk
Private key from command line option, or NULL.
static char buf[2048]
@ GNUNET_OK
Definition: gnunet_common.h:95
size_t GNUNET_CRYPTO_rsa_public_key_encode(const struct GNUNET_CRYPTO_RsaPublicKey *key, void **buffer)
Encode the public key in a format suitable for storing it into a file.
Definition: crypto_rsa.c:300
size_t GNUNET_CRYPTO_rsa_signature_encode(const struct GNUNET_CRYPTO_RsaSignature *sig, void **buffer)
Encode the given signature in a format suitable for storing it into a file.
Definition: crypto_rsa.c:968
functions to parse JSON objects into GNUnet objects
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
#define GNUNET_MAX_MALLOC_CHECKED
Maximum allocation with GNUNET_malloc macro.
#define GNUNET_free(ptr)
Wrapper around free.
char * GNUNET_STRINGS_data_to_string_alloc(const void *buf, size_t size)
Return the base32crockford encoding of the given buffer.
Definition: strings.c:750
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
int GNUNET_TIME_round_rel(struct GNUNET_TIME_Relative *rt)
Round a time value so that it is suitable for transmission via JSON encodings.
Definition: time.c:74
struct GNUNET_TIME_Absolute GNUNET_TIME_absolute_ntoh(struct GNUNET_TIME_AbsoluteNBO a)
Convert absolute time from network byte order.
Definition: time.c:542
int GNUNET_TIME_round_abs(struct GNUNET_TIME_Absolute *at)
Round a time value so that it is suitable for transmission via JSON encodings.
Definition: time.c:62
#define GNUNET_TIME_UNIT_FOREVER_ABS
Constant used to specify "forever".
json_t * GNUNET_JSON_from_time_rel(struct GNUNET_TIME_Relative stamp)
Convert relative timestamp to a json string.
json_t * GNUNET_JSON_from_time_abs(struct GNUNET_TIME_Absolute stamp)
Convert absolute timestamp to a json string.
json_t * GNUNET_JSON_from_rsa_signature(const struct GNUNET_CRYPTO_RsaSignature *sig)
Convert RSA signature to JSON.
json_t * GNUNET_JSON_from_rsa_public_key(const struct GNUNET_CRYPTO_RsaPublicKey *pk)
Convert RSA public key to JSON.
json_t * GNUNET_JSON_from_data(const void *data, size_t size)
Convert binary data to a JSON string with the base32crockford encoding.
json_t * GNUNET_JSON_from_time_abs_nbo(struct GNUNET_TIME_AbsoluteNBO stamp)
Convert absolute timestamp to a json string.
static unsigned int size
Size of the "table".
Definition: peer.c:67
The public information of an RSA key pair.
Definition: crypto_rsa.c:52
an RSA signature
Definition: crypto_rsa.c:64
Time for absolute time used by GNUnet, in microseconds and in network byte order.
Time for absolute times used by GNUnet, in microseconds.
uint64_t abs_value_us
The actual value.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.