libyang  2.0.7
libyang is YANG data modelling language parser and toolkit written (and providing API) in C.
binary.c
Go to the documentation of this file.
1 
15 #include "plugins_types.h"
16 
17 #include <ctype.h>
18 #include <stdint.h>
19 #include <stdio.h>
20 #include <stdlib.h>
21 
22 #include "libyang.h"
23 
24 /* additional internal headers for some useful simple macros */
25 #include "common.h"
26 #include "compat.h"
27 #include "plugins_internal.h" /* LY_TYPE_*_STR */
28 
41 static const char b64_etable[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
42 
55 static LY_ERR
56 binary_base64_encode(const struct ly_ctx *ctx, const char *data, size_t size, char **str, size_t *str_len)
57 {
58  uint32_t i;
59  char *ptr;
60 
61  *str_len = (size + 2) / 3 * 4;
62  *str = malloc(*str_len + 1);
63  LY_CHECK_ERR_RET(!*str, LOGMEM(ctx), LY_EMEM);
64 
65  ptr = *str;
66  for (i = 0; i < size - 2; i += 3) {
67  *ptr++ = b64_etable[(data[i] >> 2) & 0x3F];
68  *ptr++ = b64_etable[((data[i] & 0x3) << 4) | ((int)(data[i + 1] & 0xF0) >> 4)];
69  *ptr++ = b64_etable[((data[i + 1] & 0xF) << 2) | ((int)(data[i + 2] & 0xC0) >> 6)];
70  *ptr++ = b64_etable[data[i + 2] & 0x3F];
71  }
72  if (i < size) {
73  *ptr++ = b64_etable[(data[i] >> 2) & 0x3F];
74  if (i == (size - 1)) {
75  *ptr++ = b64_etable[((data[i] & 0x3) << 4)];
76  *ptr++ = '=';
77  } else {
78  *ptr++ = b64_etable[((data[i] & 0x3) << 4) | ((int)(data[i + 1] & 0xF0) >> 4)];
79  *ptr++ = b64_etable[((data[i + 1] & 0xF) << 2)];
80  }
81  *ptr++ = '=';
82  }
83  *ptr = '\0';
84 
85  return LY_SUCCESS;
86 }
87 
91 static const int b64_dtable[256] = {
92  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
93  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
94  0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 62, 63, 62, 62, 63, 52, 53, 54, 55,
95  56, 57, 58, 59, 60, 61, 0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 5, 6,
96  7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 0,
97  0, 0, 0, 63, 0, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40,
98  41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51
99 };
100 
112 static LY_ERR
113 binary_base64_decode(const char *value, size_t value_len, void **data, size_t *size)
114 {
115  unsigned char *ptr = (unsigned char *)value;
116  uint32_t pad_chars, octet_count;
117  char *str;
118 
119  if (!value_len || (ptr[value_len - 1] != '=')) {
120  pad_chars = 0;
121  } else if (ptr[value_len - 2] == '=') {
122  pad_chars = 1;
123  } else {
124  pad_chars = 2;
125  }
126 
127  octet_count = ((value_len + 3) / 4 - (pad_chars ? 1 : 0)) * 4;
128  *size = octet_count / 4 * 3 + pad_chars;
129 
130  str = malloc(*size + 1);
131  LY_CHECK_RET(!str, LY_EMEM);
132  str[*size] = '\0';
133 
134  for (uint32_t i = 0, j = 0; i < octet_count; i += 4) {
135  int n = b64_dtable[ptr[i]] << 18 | b64_dtable[ptr[i + 1]] << 12 | b64_dtable[ptr[i + 2]] << 6 | b64_dtable[ptr[i + 3]];
136  str[j++] = n >> 16;
137  str[j++] = n >> 8 & 0xFF;
138  str[j++] = n & 0xFF;
139  }
140  if (pad_chars) {
141  int n = b64_dtable[ptr[octet_count]] << 18 | b64_dtable[ptr[octet_count + 1]] << 12;
142  str[*size - pad_chars] = n >> 16;
143 
144  if (pad_chars == 2) {
145  n |= b64_dtable[ptr[octet_count + 2]] << 6;
146  n >>= 8 & 0xFF;
147  str[*size - pad_chars + 1] = n;
148  }
149  }
150 
151  *data = str;
152  return LY_SUCCESS;
153 }
154 
164 static LY_ERR
165 binary_base64_validate(const char *value, size_t value_len, const struct lysc_type_bin *type, struct ly_err_item **err)
166 {
167  uint32_t idx, pad;
168 
169  /* check correct characters in base64 */
170  idx = 0;
171  while ((idx < value_len) &&
172  ((('A' <= value[idx]) && (value[idx] <= 'Z')) ||
173  (('a' <= value[idx]) && (value[idx] <= 'z')) ||
174  (('0' <= value[idx]) && (value[idx] <= '9')) ||
175  ('+' == value[idx]) || ('/' == value[idx]))) {
176  idx++;
177  }
178 
179  /* find end of padding */
180  pad = 0;
181  while ((idx + pad < value_len) && (pad < 2) && (value[idx + pad] == '=')) {
182  pad++;
183  }
184 
185  /* check if value is valid base64 value */
186  if (value_len != idx + pad) {
187  if (isprint(value[idx + pad])) {
188  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid Base64 character '%c'.", value[idx + pad]);
189  } else {
190  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Invalid Base64 character 0x%x.", value[idx + pad]);
191  }
192  }
193 
194  if (value_len & 3) {
195  /* base64 length must be multiple of 4 chars */
196  return ly_err_new(err, LY_EVALID, LYVE_DATA, NULL, NULL, "Base64 encoded value length must be divisible by 4.");
197  }
198 
199  /* length restriction of the binary value */
200  if (type->length) {
201  const uint32_t octet_count = ((idx + pad) / 4) * 3 - pad;
202  LY_CHECK_RET(lyplg_type_validate_range(LY_TYPE_BINARY, type->length, octet_count, value, value_len, err));
203  }
204 
205  return LY_SUCCESS;
206 }
207 
208 API LY_ERR
209 lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len,
210  uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints,
211  const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres),
212  struct ly_err_item **err)
213 {
214  LY_ERR ret = LY_SUCCESS;
215  struct lysc_type_bin *type_bin = (struct lysc_type_bin *)type;
216  struct lyd_value_binary *val;
217 
218  /* init storage */
219  memset(storage, 0, sizeof *storage);
220  LYPLG_TYPE_VAL_INLINE_PREPARE(storage, val);
221  LY_CHECK_ERR_GOTO(!val, ret = LY_EMEM, cleanup);
222  storage->realtype = type;
223 
224  if (format == LY_VALUE_LYB) {
225  /* store value */
226  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
227  val->data = (void *)value;
228  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
229  } else {
230  val->data = malloc(value_len);
231  LY_CHECK_ERR_GOTO(!val->data, ret = LY_EMEM, cleanup);
232  memcpy(val->data, value, value_len);
233  }
234 
235  /* store size */
236  val->size = value_len;
237 
238  /* success */
239  goto cleanup;
240  }
241 
242  /* check hints */
243  ret = lyplg_type_check_hints(hints, value, value_len, type->basetype, NULL, err);
244  LY_CHECK_GOTO(ret, cleanup);
245 
246  /* validate */
247  if (format != LY_VALUE_CANON) {
248  ret = binary_base64_validate(value, value_len, type_bin, err);
249  LY_CHECK_GOTO(ret, cleanup);
250  }
251 
252  /* get the binary value */
253  ret = binary_base64_decode(value, value_len, &val->data, &val->size);
254  LY_CHECK_GOTO(ret, cleanup);
255 
256  /* store canonical value, it always is */
257  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
258  ret = lydict_insert_zc(ctx, (char *)value, &storage->_canonical);
259  options &= ~LYPLG_TYPE_STORE_DYNAMIC;
260  LY_CHECK_GOTO(ret, cleanup);
261  } else {
262  ret = lydict_insert(ctx, value_len ? value : "", value_len, &storage->_canonical);
263  LY_CHECK_GOTO(ret, cleanup);
264  }
265 
266 cleanup:
267  if (options & LYPLG_TYPE_STORE_DYNAMIC) {
268  free((void *)value);
269  }
270 
271  if (ret) {
272  lyplg_type_free_binary(ctx, storage);
273  }
274  return ret;
275 }
276 
277 API LY_ERR
278 lyplg_type_compare_binary(const struct lyd_value *val1, const struct lyd_value *val2)
279 {
280  struct lyd_value_binary *v1, *v2;
281 
282  if (val1->realtype != val2->realtype) {
283  return LY_ENOT;
284  }
285 
286  LYD_VALUE_GET(val1, v1);
287  LYD_VALUE_GET(val2, v2);
288 
289  if ((v1->size != v2->size) || memcmp(v1->data, v2->data, v1->size)) {
290  return LY_ENOT;
291  }
292  return LY_SUCCESS;
293 }
294 
295 API const void *
296 lyplg_type_print_binary(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format,
297  void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
298 {
299  struct lyd_value_binary *val;
300  char *ret;
301  size_t ret_len = 0;
302 
303  LYD_VALUE_GET(value, val);
304 
305  if (format == LY_VALUE_LYB) {
306  *dynamic = 0;
307  if (value_len) {
308  *value_len = val->size;
309  }
310  return val->data;
311  }
312 
313  /* generate canonical value if not already */
314  if (!value->_canonical) {
315  /* get the base64 string value */
316  if (binary_base64_encode(ctx, val->data, val->size, &ret, &ret_len)) {
317  return NULL;
318  }
319 
320  /* store it */
321  if (lydict_insert_zc(ctx, ret, (const char **)&value->_canonical)) {
322  LOGMEM(ctx);
323  return NULL;
324  }
325  }
326 
327  /* use the cached canonical value */
328  if (dynamic) {
329  *dynamic = 0;
330  }
331  if (value_len) {
332  *value_len = ret_len ? ret_len : strlen(value->_canonical);
333  }
334  return value->_canonical;
335 }
336 
337 API LY_ERR
338 lyplg_type_dup_binary(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
339 {
340  LY_ERR ret;
341  struct lyd_value_binary *orig_val, *dup_val;
342 
343  ret = lydict_insert(ctx, original->_canonical, ly_strlen(original->_canonical), &dup->_canonical);
344  LY_CHECK_RET(ret);
345 
346  LYPLG_TYPE_VAL_INLINE_PREPARE(dup, dup_val);
347  if (!dup_val) {
348  lydict_remove(ctx, dup->_canonical);
349  return LY_EMEM;
350  }
351 
352  LYD_VALUE_GET(original, orig_val);
353  dup_val->data = malloc(orig_val->size);
354  if (!dup_val->data) {
355  lydict_remove(ctx, dup->_canonical);
357  return LY_EMEM;
358  }
359  memcpy(dup_val->data, orig_val->data, orig_val->size);
360  dup_val->size = orig_val->size;
361 
362  dup->realtype = original->realtype;
363  return LY_SUCCESS;
364 }
365 
366 API void
367 lyplg_type_free_binary(const struct ly_ctx *ctx, struct lyd_value *value)
368 {
369  struct lyd_value_binary *val;
370 
371  lydict_remove(ctx, value->_canonical);
372  LYD_VALUE_GET(value, val);
373  if (val) {
374  free(val->data);
376  }
377 }
378 
386 const struct lyplg_type_record plugins_binary[] = {
387  {
388  .module = "",
389  .revision = NULL,
390  .name = LY_TYPE_BINARY_STR,
391 
392  .plugin.id = "libyang 2 - binary, version 1",
393  .plugin.store = lyplg_type_store_binary,
394  .plugin.validate = NULL,
395  .plugin.compare = lyplg_type_compare_binary,
396  .plugin.sort = NULL,
397  .plugin.print = lyplg_type_print_binary,
398  .plugin.duplicate = lyplg_type_dup_binary,
399  .plugin.free = lyplg_type_free_binary,
400  },
401  {0}
402 };
const struct lyplg_type_record plugins_binary[]
Plugin information for binray type implementation.
Definition: binary.c:386
API LY_ERR lyplg_type_store_binary(const struct ly_ctx *ctx, const struct lysc_type *type, const void *value, size_t value_len, uint32_t options, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), uint32_t hints, const struct lysc_node *UNUSED(ctx_node), struct lyd_value *storage, struct lys_glob_unres *UNUSED(unres), struct ly_err_item **err)
Definition: binary.c:209
API const void * lyplg_type_print_binary(const struct ly_ctx *ctx, const struct lyd_value *value, LY_VALUE_FORMAT format, void *UNUSED(prefix_data), ly_bool *dynamic, size_t *value_len)
Definition: binary.c:296
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
API LY_ERR lyplg_type_dup_binary(const struct ly_ctx *ctx, const struct lyd_value *original, struct lyd_value *dup)
Implementation of lyplg_type_dup_clb for the built-in binary type.
Definition: binary.c:338
API LY_ERR lyplg_type_compare_binary(const struct lyd_value *val1, const struct lyd_value *val2)
Implementation of lyplg_type_compare_clb for the built-in binary type.
Definition: binary.c:278
API void lyplg_type_free_binary(const struct ly_ctx *ctx, struct lyd_value *value)
Implementation of lyplg_type_free_clb for the built-in binary type.
Definition: binary.c:367
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.
#define LYPLG_TYPE_STORE_DYNAMIC
LY_DATA_TYPE basetype
Definition: tree_schema.h:1531
struct lysc_range * length
Definition: tree_schema.h:1633
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_BINARY
Definition: tree.h:205
@ 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
#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 built-in binary values.
Definition: tree_data.h:612
#define LOGMEM(CTX)
Definition: tree_edit.h:25