libyang  2.0.112
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
date_and_time.c
Go to the documentation of this file.
1 
15 #define _GNU_SOURCE /* strdup */
16 
17 #include "plugins_types.h"
18 
19 #include <ctype.h>
20 #include <stdint.h>
21 #include <stdlib.h>
22 #include <string.h>
23 
24 #include "libyang.h"
25 
26 #include "common.h"
27 #include "compat.h"
28 
39 static void lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value);
40 
44 static LY_ERR
45 lyplg_type_store_date_and_time(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
46  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
47  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
48  struct ly_err_item **err)
49 {
50  LY_ERR ret = LY_SUCCESS;
51  struct lysc_type_str *type_dat = (struct lysc_type_str *)type;
52  struct lyd_value_date_and_time *val;
53  uint32_t i;
54  char c;
55 
56  /* init storage */
57  memset(storage, 0, sizeof *storage);
58  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
59  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
60  storage->realtype = type;
61 
62  if (format == LY_VALUE_LYB) {
63  /* validation */
64  if (value_len < 8) {
65  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time value size %zu "
66  "(expected at least 8).", value_len);
67  goto cleanup;
68  }
69  for (i = 8; i < value_len; ++i) {
70  c = ((char *)value)[i];
71  if (!isdigit(c)) {
72  ret = ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid LYB date-and-time character '%c' "
73  "(expected a digit).", c);
74  goto cleanup;
75  }
76  }
77 
78  /* store timestamp */
79  memcpy(&val->time, value, sizeof val->time);
80 
81  /* store fractions of second */
82  if (value_len > 8) {
83  val->fractions_s = strndup(((char *)value) + 8, value_len - 8);
84  LY_CHECK_ERR_GOTO(!val->fractions_s, ret = LY_EMEM, cleanup);
85  }
86 
87  /* success */
88  goto cleanup;
89  }
90 
91  /* check hints */
92  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
93  LY_CHECK_GOTO(ret, cleanup);
94 
95  /* length restriction, there can be only ASCII chars */
96  if (type_dat->length) {
97  ret = lyplg_type_validate_range(LY_TYPE_STRING, type_dat->length, value_len, value, value_len, err);
98  LY_CHECK_GOTO(ret, cleanup);
99  }
100 
101  /* date-and-time pattern */
102  ret = lyplg_type_validate_patterns(type_dat->patterns, value, value_len, err);
103  LY_CHECK_GOTO(ret, cleanup);
104 
105  /* pattern validation succeeded, convert to UNIX time and fractions of second */
106  ret = ly_time_str2time(value, &val->time, &val->fractions_s);
107  LY_CHECK_GOTO(ret, cleanup);
108 
109  if (format == LY_VALUE_CANON) {
110  /* store canonical value */
111  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
112  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
113  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
114  LY_CHECK_GOTO(ret, cleanup);
115  } else {
116  ret = lydict_insert(ctx, value, value_len, &storage->_canonical);
117  LY_CHECK_GOTO(ret, cleanup);
118  }
119  }
120 
121 cleanup:
122  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
123  free((void *)value);
124  }
125 
126  if (ret) {
127  lyplg_type_free_date_and_time(ctx, storage);
128  }
129  return ret;
130 }
131 
135 static LY_ERR
136 lyplg_type_compare_date_and_time(const struct lyd_value *val1, const struct lyd_value *val2)
137 {
138  struct lyd_value_date_and_time *v1, *v2;
139 
140  if (val1->realtype != val2->realtype) {
141  return LY_ENOT;
142  }
143 
144  LYD_VALUE_GET(val1, v1);
145  LYD_VALUE_GET(val2, v2);
146 
147  /* compare timestamp */
148  if (v1->time != v2->time) {
149  return LY_ENOT;
150  }
151 
152  /* compare second fractions */
153  if ((!v1->fractions_s && !v2->fractions_s) ||
154  (v1->fractions_s && v2->fractions_s && !strcmp(v1->fractions_s, v2->fractions_s))) {
155  return LY_SUCCESS;
156  }
157  return LY_ENOT;
158 }
159 
163 static const void *
164 lyplg_type_print_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
165  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
166 {
167  struct lyd_value_date_and_time *val;
168  char *ret;
169 
170  LYD_VALUE_GET(value, val);
171 
172  if (format == LY_VALUE_LYB) {
173  if (val->fractions_s) {
174  ret = malloc(8 + strlen(val->fractions_s));
175  LY_CHECK_ERR_RET(!ret, LOGMEM(ctx), NULL);
176 
177  *dynamic = 1;
178  if (value_len) {
179  *value_len = 8 + strlen(val->fractions_s);
180  }
181  memcpy(ret, &val->time, sizeof val->time);
182  memcpy(ret + 8, val->fractions_s, strlen(val->fractions_s));
183  } else {
184  *dynamic = 0;
185  if (value_len) {
186  *value_len = 8;
187  }
188  ret = (char *)&val->time;
189  }
190  return ret;
191  }
192 
193  /* generate canonical value if not already */
194  if (!value->_canonical) {
195  /* get the canonical value */
196  if (ly_time_time2str(val->time, val->fractions_s, &ret)) {
197  return NULL;
198  }
199 
200  /* store it */
201  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
202  LOGMEM(ctx);
203  return NULL;
204  }
205  }
206 
207  /* use the cached canonical value */
208  if (dynamic) {
209  *dynamic = 0;
210  }
211  if (value_len) {
212  *value_len = strlen(value->_canonical);
213  }
214  return value->_canonical;
215 }
216 
220 static LY_ERR
221 lyplg_type_dup_date_and_time(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
222 {
223  LY_ERR ret;
224  struct lyd_value_date_and_time *orig_val, *dup_val;
225 
226  memset(dup, 0, sizeof *dup);
227 
228  /* optional canonical value */
229  ret = lydict_insert(ctx, original->_canonical, 0, &dup->_canonical);
230  LY_CHECK_GOTO(ret, error);
231 
232  /* allocate value */
233  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
234  LY_CHECK_ERR_GOTO(!dup_val, ret = LY_EMEM, error);
235 
236  LYD_VALUE_GET(original, orig_val);
237 
238  /* copy timestamp */
239  dup_val->time = orig_val->time;
240 
241  /* duplicate second fractions */
242  if (orig_val->fractions_s) {
243  dup_val->fractions_s = strdup(orig_val->fractions_s);
244  LY_CHECK_ERR_GOTO(!dup_val->fractions_s, ret = LY_EMEM, error);
245  }
246 
247  dup->realtype = original->realtype;
248  return LY_SUCCESS;
249 
250 error:
251  lyplg_type_free_date_and_time(ctx, dup);
252  return ret;
253 }
254 
258 static void
259 lyplg_type_free_date_and_time(const struct ly_ctx *ctx, struct lyd_value *value)
260 {
261  struct lyd_value_date_and_time *val;
262 
263  lydict_remove(ctx, value->_canonical);
264  value->_canonical = NULL;
265  LYD_VALUE_GET(value, val);
266  if (val) {
267  free(val->fractions_s);
269  }
270 }
271 
280  {
281  .module = "ietf-yang-types",
282  .revision = "2013-07-15",
283  .name = "date-and-time",
284 
285  .plugin.id = "libyang 2 - date-and-time, version 1",
286  .plugin.store = lyplg_type_store_date_and_time,
287  .plugin.validate = NULL,
288  .plugin.compare = lyplg_type_compare_date_and_time,
289  .plugin.sort = NULL,
290  .plugin.print = lyplg_type_print_date_and_time,
291  .plugin.duplicate = lyplg_type_dup_date_and_time,
292  .plugin.free = lyplg_type_free_date_and_time,
293  .plugin.lyb_data_len = -1,
294  },
295  {0}
296 };
struct lysc_type * realtype
Definition: tree_data.h:539
Compiled YANG data node.
Definition: tree_schema.h:1666
LY_ERR ly_err_new(struct ly_err_item **err, LY_ERR ecode, LY_VECODE vecode, char *path, char *apptag, const char *err_format,...) _FORMAT_PRINTF(6
Create and fill error structure.
Definition: log.h:248
uint8_t ly_bool
Type to indicate boolean value.
Definition: log.h:25
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 ...
#define LYPLG_TYPE_STORE_DYNAMIC
#define LOGMEM(CTX)
Definition: tree_edit.h:22
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...
Special lyd_value structure for ietf-yang-types date-and-time values.
Definition: tree_data.h:671
The main libyang public header.
struct lysc_pattern ** patterns
Definition: tree_schema.h:1580
LY_ERR lyplg_type_check_hints(uint32_t hints, const char *value, size_t value_len, LY_DATA_TYPE type, int *base, struct ly_err_item **err)
Check that the type is suitable for the parser&#39;s hints (if any) in the specified format.
struct lysc_range * length
Definition: tree_schema.h:1579
struct lyplg_type_record plugins_date_and_time[]
Plugin information for date-and-time type implementation.
YANG data representation.
Definition: tree_data.h:535
const char * _canonical
Definition: tree_data.h:536
Libyang full error structure.
Definition: log.h:291
Definition: log.h:283
#define LYD_VALUE_GET(value, type_val)
Get the value in format specific to the type.
Definition: tree_data.h:578
Definition: log.h:254
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, only a reference counter is incremented and no memory allocation is performed. This insert function variant avoids duplication of specified value - it is inserted into the dictionary directly.
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...
const char * module
Definition: log.h:260
#define LYPLG_TYPE_VAL_INLINE_DESTROY(type_val)
Destroy a prepared value.
LY_VALUE_FORMAT
All kinds of supported value formats and prefix mappings to modules.
Definition: tree.h:235
#define LYPLG_TYPE_VAL_INLINE_PREPARE(storage, type_val)
Prepare value memory for storing a specific type value, may be allocated dynamically.
LY_DATA_TYPE basetype
Definition: tree_schema.h:1552
LY_ERR
libyang&#39;s error codes returned by the libyang functions.
Definition: log.h:245
LY_ERR lyplg_type_validate_patterns(struct lysc_pattern **patterns, const char *str, size_t str_len, struct ly_err_item **err)
Data type validator for pattern-restricted string values.
LY_ERR lyplg_type_validate_range(LY_DATA_TYPE basetype, struct lysc_range *range, int64_t value, const char *strval, size_t strval_len, struct ly_err_item **err)
Data type validator for a range/length-restricted values.
API for (user) types plugins.
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.
libyang context handler.