libyang  6.4.33
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
date.c
Go to the documentation of this file.
1 
15 #define _GNU_SOURCE /* strdup */
16 #define _XOPEN_SOURCE /* strptime */
17 
18 #include "plugins_types.h"
19 
20 #include <assert.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <time.h>
27 
28 #include "compat.h"
29 #include "dict.h"
30 #include "ly_common.h"
31 #include "plugins_internal.h"
32 
43 static void lyplg_type_free_date(const struct ly_ctx *ctx, struct lyd_value *value);
44 
45 static void
46 lyplg_type_lyb_size_date_nz(const struct lysc_type *UNUSED(type), enum lyplg_lyb_size_type *size_type,
47  uint64_t *fixed_size_bits)
48 {
49  *size_type = LYPLG_LYB_SIZE_FIXED_BITS;
50  *fixed_size_bits = 64;
51 }
52 
56 static LY_ERR
57 lyplg_type_store_date(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, uint64_t value_size_bits,
58  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
59  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
60  struct ly_err_item **err)
61 {
62  LY_ERR ret = LY_SUCCESS;
63  struct lyd_value_date *val = NULL;
64  struct lyd_value_date_nz *val_nz = NULL;
65  struct tm tm = {0};
66  char *ptr;
67  uint32_t value_size;
68  char *str = NULL;
69 
70  /* init storage */
71  memset(storage, 0, sizeof *storage);
72  if (!strcmp(type->name, "date")) {
73  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
74  } else {
75  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val_nz);
76  }
77  LY_CHECK_ERR_GOTO(!val && !val_nz, ret = ly_err_new_no_mem(err), cleanup);
78  storage->realtype = type;
79 
80  if (format == LY_VALUE_LYB) {
81  /* validation */
82  if (val) {
83  if ((value_size_bits != 64) && (value_size_bits != 65)) {
84  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB %s value size %" PRIu64
85  " b (expected 64 or 65 b).", type->name, value_size_bits);
86  goto cleanup;
87  }
88  } else {
89  if (value_size_bits != 64) {
90  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB %s value size %" PRIu64
91  " b (expected 64 b).", type->name, value_size_bits);
92  goto cleanup;
93  }
94  }
95  value_size = LYPLG_BITS2BYTES(value_size_bits);
96 
97  /* store timestamp */
98  if (val) {
99  memcpy(&val->time, value, sizeof val->time);
100  } else {
101  memcpy(&val_nz->time, value, sizeof val_nz->time);
102  }
103 
104  /* store unknown timezone */
105  if (val && (value_size > 8)) {
106  val->unknown_tz = *(((uint8_t *)value) + 8) ? 1 : 0;
107  }
108 
109  /* success */
110  goto cleanup;
111  }
112 
113  /* get value byte length */
114  if (val) {
115  ret = lyplg_type_check_value_size(type->name, format, value_size_bits, LYPLG_LYB_SIZE_VARIABLE_BITS, 0,
116  &value_size, err);
117  } else {
118  ret = lyplg_type_check_value_size(type->name, format, value_size_bits, LYPLG_LYB_SIZE_FIXED_BITS, 64,
119  &value_size, err);
120  }
121  LY_CHECK_GOTO(ret, cleanup);
122 
123  /* check hints */
124  ret = lyplg_type_check_hints(hints, value, value_size, type->basetype, NULL, err);
125  LY_CHECK_GOTO(ret, cleanup);
126 
127  if (!(options & LYPLG_TYPE_STORE_ONLY)) {
128  /* validate value */
129  ret = lyplg_type_validate_patterns(ctx, ((struct lysc_type_str *)type)->patterns, value, value_size, err);
130  LY_CHECK_GOTO(ret, cleanup);
131  }
132 
133  if (val) {
134  if (value_size < 10) {
135  /* not even a full date, the pattern was not checked with LYPLG_TYPE_STORE_ONLY */
136  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to parse %s value \"%.*s\".", type->name,
137  (int)value_size, (char *)value);
138  goto cleanup;
139  }
140 
141  /* create date-and-time value */
142  if (asprintf(&str, "%.*sT00:00:00%.*s", 10, (char *)value, (int)(value_size - 10), (char *)value + 10) == -1) {
143  ret = ly_err_new_no_mem(err);
144  goto cleanup;
145  }
146 
147  /* convert to UNIX time */
148  ret = ly_time_str2time(str, &val->time, NULL);
149  if (ret) {
150  ret = ly_err_new(err, ret, 0, NULL, NULL, "%s", ly_last_logmsg());
151  goto cleanup;
152  }
153  } else {
154  /* fill tm */
155  ptr = strptime(value, "%Y-%m-%d", &tm);
156  if (!ptr || ((uint32_t)(ptr - (char *)value) != value_size)) {
157  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Failed to parse %s value \"%.*s\".", type->name,
158  (int)value_size, (char *)value);
159  goto cleanup;
160  }
161 
162  /* convert to UNIX time */
163  val_nz->time = timegm(&tm);
164  }
165 
166  if (val && (((char *)value)[value_size - 1] == 'Z')) {
167  /* unknown timezone */
168  val->unknown_tz = 1;
169  }
170 
171  if (format == LY_VALUE_CANON) {
172  /* store canonical value */
173  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
174  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
175  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
176  LY_CHECK_GOTO(ret, cleanup);
177  } else {
178  ret = lydict_insert(ctx, value, value_size, &storage->_canonical);
179  LY_CHECK_GOTO(ret, cleanup);
180  }
181  }
182 
183 cleanup:
184  free(str);
185  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
186  free((void *)value);
187  }
188 
189  if (ret) {
190  lyplg_type_free_date(ctx, storage);
191  }
192  return ret;
193 }
194 
198 static LY_ERR
199 lyplg_type_compare_date(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1,
200  const struct lyd_value *val2)
201 {
202  struct lyd_value_date *v1, *v2;
203  struct lyd_value_date_nz *vn1, *vn2;
204 
205  if (!strcmp(val1->realtype->name, "date")) {
206  LYD_VALUE_GET(val1, v1);
207  LYD_VALUE_GET(val2, v2);
208 
209  /* compare timestamp and unknown tz */
210  if ((v1->time != v2->time) || (v1->unknown_tz != v2->unknown_tz)) {
211  return LY_ENOT;
212  }
213  } else {
214  LYD_VALUE_GET(val1, vn1);
215  LYD_VALUE_GET(val2, vn2);
216 
217  /* compare timestamp */
218  if (vn1->time != vn2->time) {
219  return LY_ENOT;
220  }
221  }
222 
223  return LY_SUCCESS;
224 }
225 
229 static int
230 lyplg_type_sort_date(const struct ly_ctx *UNUSED(ctx), const struct lyd_value *val1, const struct lyd_value *val2)
231 {
232  struct lyd_value_date *v1, *v2;
233  struct lyd_value_date_nz *vn1, *vn2;
234 
235  if (!strcmp(val1->realtype->name, "date")) {
236  LYD_VALUE_GET(val1, v1);
237  LYD_VALUE_GET(val2, v2);
238 
239  /* compare timestamps */
240  return difftime(v1->time, v2->time);
241  } else {
242  LYD_VALUE_GET(val1, vn1);
243  LYD_VALUE_GET(val2, vn2);
244 
245  /* compare timestamps */
246  return difftime(vn1->time, vn2->time);
247  }
248 }
249 
253 static const void *
254 lyplg_type_print_date(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
255  void *UNUSED(prefix_data), ly_bool *dynamic, uint64_t *value_size_bits)
256 {
257  struct lyd_value_date *val = NULL;
258  struct lyd_value_date_nz *val_nz = NULL;
259  struct tm tm;
260  char *ret;
261 
262  if (!strcmp(value->realtype->name, "date")) {
263  LYD_VALUE_GET(value, val);
264  } else {
265  LYD_VALUE_GET(value, val_nz);
266  }
267 
268  if (format == LY_VALUE_LYB) {
269  if (val && val->unknown_tz) {
270  ret = malloc(8 + 1);
271  LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
272 
273  *dynamic = 1;
274  if (value_size_bits) {
275  *value_size_bits = 64 + 8;
276  }
277  memcpy(ret, &val->time, sizeof val->time);
278  memcpy(ret + 8, &val->unknown_tz, sizeof val->unknown_tz);
279  } else {
280  *dynamic = 0;
281  if (value_size_bits) {
282  *value_size_bits = 64;
283  }
284  ret = val ? (char *)&val->time : (char *)&val_nz->time;
285  }
286  return ret;
287  }
288 
289  /* generate canonical value if not already */
290  if (!value->_canonical) {
291  if (val_nz || (val && val->unknown_tz)) {
292  /* ly_time_time2str but always using GMT */
293  if (!gmtime_r(val_nz ? &val_nz->time : &val->time, &tm)) {
294  return NULL;
295  }
296 
297  if (asprintf(&ret, "%04d-%02d-%02d%s", tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, val ? "Z" : "") == -1) {
298  return NULL;
299  }
300  } else {
301  if (ly_time_time2str(val->time, NULL, &ret)) {
302  return NULL;
303  }
304 
305  /* truncate the time segment */
306  assert(ret[10] == 'T');
307  memmove(ret + 10, ret + 19, strlen(ret + 19) + 1);
308  }
309 
310  /* store it */
311  if (lydict_insert(ctx, ret, 0, (const char **)&value->_canonical)) {
312  free(ret);
313  LOGMEM(ctx);
314  return NULL;
315  }
316  free(ret);
317  }
318 
319  /* use the cached canonical value */
320  if (dynamic) {
321  *dynamic = 0;
322  }
323  if (value_size_bits) {
324  *value_size_bits = strlen(value->_canonical) * 8;
325  }
326  return value->_canonical;
327 }
328 
332 static LY_ERR
333 lyplg_type_dup_date(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
334 {
335  LY_ERR ret;
336  struct lyd_value_date *orig_val, *dup_val;
337  struct lyd_value_date *orig_val_nz, *dup_val_nz;
338 
339  memset(dup, 0, sizeof *dup);
340 
341  /* optional canonical value */
342  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
343  LY_CHECK_GOTO(ret, error);
344 
345  /* allocate value */
346  if (!strcmp(original->realtype->name, "date")) {
347  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
348  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
349 
350  LYD_VALUE_GET(original, orig_val);
351 
352  /* copy timestamp and unknown tz */
353  dup_val->time = orig_val->time;
354  dup_val->unknown_tz = orig_val->unknown_tz;
355  } else {
356  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val_nz);
357  LY_CHECK_ERR_GOTO(!dup_val_nz, ret = LY_EMEM, error);
358 
359  LYD_VALUE_GET(original, orig_val_nz);
360 
361  /* copy timestamp */
362  dup_val_nz->time = orig_val_nz->time;
363  }
364 
365  dup->realtype = original->realtype;
366  return LY_SUCCESS;
367 
368 error:
369  lyplg_type_free_date(ctx, dup);
370  return ret;
371 }
372 
376 static void
377 lyplg_type_free_date(const struct ly_ctx *ctx, struct lyd_value *value)
378 {
379  struct lyd_value_date *val, *val_nz;
380 
381  lydict_remove(ctx, value->_canonical);
382  value->_canonical = NULL;
383 
384  if (!strcmp(value->realtype->name, "date")) {
385  LYD_VALUE_GET(value, val);
386  if (val) {
388  }
389  } else {
390  LYD_VALUE_GET(value, val_nz);
391  if (val_nz) {
393  }
394  }
395 }
396 
404 const struct lyplg_type_record plugins_date[] = {
405  {
406  .module = "ietf-yang-types",
407  .revision = NULL,
408  .name = "date",
409 
410  .plugin.id = "ly2 date",
411  .plugin.lyb_size = lyplg_type_lyb_size_variable_bytes,
412  .plugin.store = lyplg_type_store_date,
413  .plugin.validate_value = lyplg_type_validate_value_string,
414  .plugin.validate_tree = NULL,
415  .plugin.compare = lyplg_type_compare_date,
416  .plugin.sort = lyplg_type_sort_date,
417  .plugin.print = lyplg_type_print_date,
418  .plugin.duplicate = lyplg_type_dup_date,
419  .plugin.free = lyplg_type_free_date,
420  },
421  {
422  .module = "ietf-yang-types",
423  .revision = NULL,
424  .name = "date-no-zone",
425 
426  .plugin.id = "ly2 date",
427  .plugin.lyb_size = lyplg_type_lyb_size_date_nz,
428  .plugin.store = lyplg_type_store_date,
429  .plugin.validate_value = lyplg_type_validate_value_string,
430  .plugin.validate_tree = NULL,
431  .plugin.compare = lyplg_type_compare_date,
432  .plugin.sort = lyplg_type_sort_date,
433  .plugin.print = lyplg_type_print_date,
434  .plugin.duplicate = lyplg_type_dup_date,
435  .plugin.free = lyplg_type_free_date,
436  },
437  {0}
438 };
const struct lyplg_type_record plugins_date[]
Plugin information for date and date-no-zone type implementation.
Definition: date.c:404
libyang dictionary
libyang context handler.
LIBYANG_API_DECL LY_ERR lydict_insert(const struct ly_ctx *ctx, const char *value, size_t len, const char **str_p)
Insert string into dictionary. If the string is already present, only a reference counter is incremen...
LIBYANG_API_DECL LY_ERR lydict_remove(const struct ly_ctx *ctx, const char *value)
Remove specified string from the dictionary. It decrement reference counter for the string and if it ...
LIBYANG_API_DECL LY_ERR lydict_insert_zc(const struct ly_ctx *ctx, char *value, const char **str_p)
Insert string into dictionary - zerocopy version. If the string is already present,...
LY_ERR
libyang's error codes returned by the libyang functions.
Definition: log.h:252
LIBYANG_API_DECL const char * ly_last_logmsg(void)
Get the last (thread-specific) full logged error message.
@ LYVE_DATA
Definition: log.h:290
@ LY_EMEM
Definition: log.h:254
@ LY_ENOT
Definition: log.h:266
@ LY_EVALID
Definition: log.h:260
@ LY_SUCCESS
Definition: log.h:253
Libyang full error structure.
Definition: log.h:298
const char * module
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_value_string(const struct ly_ctx *ctx, const struct lysc_type *type, struct lyd_value *storage, struct ly_err_item **err)
Implementation of lyplg_type_validate_value_clb for the string type.
Definition: string.c:116
lyplg_lyb_size_type
Type of the LYB size of a value of a particular type.
LIBYANG_API_DECL LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, uint32_t value_len, LY_DATA_TYPE type, int *base, struct ly_err_item **err)
Check that the type is suitable for the parser's hints (if any) in the specified format.
LIBYANG_API_DECL LY_ERR lyplg_type_validate_patterns(const struct ly_ctx *ctx, struct lysc_pattern **patterns, const char *str, uint32_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LIBYANG_API_DECL LY_ERR lyplg_type_check_value_size(const char *type_name, LY_VALUE_FORMAT format, uint64_t value_size_bits, enum lyplg_lyb_size_type lyb_size_type, uint64_t lyb_fixed_size_bits, uint32_t *value_size, struct ly_err_item **err)
Check a value type in bits is correct and as expected.
LIBYANG_API_DECL LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *data_path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill an error structure.
#define LYPLG_BITS2BYTES(bits)
Convert bits to bytes.
LIBYANG_API_DECL LY_ERR LIBYANG_API_DECL LY_ERR ly_err_new_no_mem(struct ly_err_item **err)
Create and fill an error structure with a no-memory error.
@ LYPLG_LYB_SIZE_VARIABLE_BITS
@ LYPLG_LYB_SIZE_FIXED_BITS
LIBYANG_API_DECL void lyplg_type_lyb_size_variable_bytes(const struct lysc_type *type, enum lyplg_lyb_size_type *size_type, uint64_t *fixed_size_bits)
Implementation of lyplg_type_lyb_size_clb for a type with variable length rounded to bytes.
#define LYPLG_TYPE_STORE_DYNAMIC
#define LYPLG_TYPE_STORE_ONLY
LY_DATA_TYPE basetype
Definition: tree_schema.h:1278
const char * name
Definition: tree_schema.h:1275
Compiled YANG data node.
Definition: tree_schema.h:1412
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:29
API for (user) types plugins.
const struct lysc_type * realtype
Definition: tree_data.h:614
ly_bool unknown_tz
Definition: tree_data.h:757
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:653
const char * _canonical
Definition: tree_data.h:611
YANG data representation.
Definition: tree_data.h:610
Special lyd_value structure for ietf-yang-types date values.
Definition: tree_data.h:755
Special lyd_value structure for ietf-yang-types date-no-zone values.
Definition: tree_data.h:763
LIBYANG_API_DECL LY_ERR ly_time_str2time(const char *value, time_t *time, char **fractions_s)
Convert date-and-time from string to UNIX timestamp and fractions of a second.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: utils.h:93
@ LY_VALUE_CANON
Definition: utils.h:94
@ LY_VALUE_LYB
Definition: utils.h:100
LIBYANG_API_DECL LY_ERR ly_time_time2str(time_t time, const char *fractions_s, char **str)
Convert UNIX timestamp and fractions of a second into canonical date-and-time string value.