1 // Copyright (c) 2015 Nuxi, https://nuxi.nl/
2 //
3 // This file is distributed under a 2-clause BSD license.
4 // See the LICENSE file for details.
5 
6 #include <errno.h>
7 #include <stdlib.h>
8 
9 #include "argdata_impl.h"
10 
argdata_create_str(const char * buf,size_t len)11 argdata_t *argdata_create_str(const char *buf, size_t len) {
12   // Validate the string for encoding errors.
13   int error = validate_string(buf, len);
14   if (error != 0) {
15     errno = error;
16     return NULL;
17   }
18 
19   argdata_t *ad = malloc(sizeof(*ad));
20   if (ad == NULL)
21     return NULL;
22 
23   // Add two additional byte to the length to hold the type number and
24   // the terminating null byte of the string.
25   ad->type = AD_STR;
26   ad->str = buf;
27   ad->length = len + 2;
28   return ad;
29 }
30