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