Diff for /libelwix/src/vars.c between versions 1.9.56.1 and 1.12.6.1

version 1.9.56.1, 2022/01/21 23:14:31 version 1.12.6.1, 2022/10/24 00:09:07
Line 250  buffer2vars(u_char * __restrict buf, int buflen, int v Line 250  buffer2vars(u_char * __restrict buf, int buflen, int v
 }  }
   
 /*  /*
    * ait_var2tlv() - Marshaling data from variable to TLV buffer
    *
    * @buf = Buffer, If =NULL then we return only needed buffer size
    * @buflen = Size of buffer
    * @var = Variable
    * return: -1 error, 0 nothing done or >0 size of marshaled data
    */
   int
   ait_var2tlv(u_char * __restrict buf, int buflen, ait_val_t * __restrict v)
   {
           int Limit = 0;
           u_char *dat;
   
           assert(v);
           if (!v)
                   return -1;
           if (AIT_TYPE(v) == empty)
                   return 0;
   
           /* calculate amount of data into buffer */
           Limit = 5 + AIT_LEN(v);
           /* check only needed buffer size */
           if (!buf)
                   return Limit;
           else
                   dat = buf;
   
           if (Limit > buflen) {
                   elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", 
                                   buflen, Limit);
                   return -1;
           } else
                   memset(buf, 0, buflen);
   
           /* marshaling */
           *dat++ = AIT_TYPE(v); 
   
           *((uint32_t*) dat) = htonl(AIT_LEN(v));
           dat += sizeof(uint32_t);
   
           switch (AIT_TYPE(v)) {
                   case i8:
                           *((int8_t*) dat) = AIT_GET_I8(v);
                           break;
                   case u8:
                           *((uint8_t*) dat) = AIT_GET_U8(v);
                           break;
                   case i16:
                           *((int16_t*) dat) = AIT_GET_I16(v);
                           break;
                   case u16:
                           *((uint16_t*) dat) = AIT_GET_U16(v);
                           break;
                   case i32:
                           *((int32_t*) dat) = AIT_GET_I32(v);
                           break;
                   case blob:
                   case u32:
                           *((uint32_t*) dat) = AIT_GET_U32(v);
                           break;
                   case i64:
                           *((int64_t*) dat) = AIT_GET_I64(v);
                           break;
                   case u64:
                           *((uint64_t*) dat) = AIT_GET_U64(v);
                           break;
                   case f32:
                           *((float*) dat) = AIT_GET_F32(v);
                           break;
                   case f64:
                           *((double*) dat) = AIT_GET_F64(v);
                           break;
                   case data:
                           memcpy(dat, AIT_GET_DATA(v), AIT_LEN(v));
                           break;
                   case buffer:
                           memcpy(dat, AIT_GET_BUF(v), AIT_LEN(v));
                           break;
                   case string:
                           memcpy(dat, AIT_GET_STR(v), AIT_LEN(v));
                           break;
                   case ptr:
                           memcpy(dat, AIT_GET_PTR(v), AIT_LEN(v));
                           break;
                   default:
                           elwix_SetErr(EINVAL, "Unsupported variable type=%d", AIT_TYPE(v));
                           return -1;
           }
   
           return Limit;
   }
   
   /*
  * ait_vars2tlv() - Marshaling data from array with variables to TLV buffer   * ait_vars2tlv() - Marshaling data from array with variables to TLV buffer
  *   *
 * @buf = Buffer * @buf = Buffer, If =NULL then we return only needed buffer size
  * @buflen = Size of buffer   * @buflen = Size of buffer
  * @vars = Variable array   * @vars = Variable array
  * return: -1 error, 0 nothing done or >0 size of marshaled data   * return: -1 error, 0 nothing done or >0 size of marshaled data
Line 265  ait_vars2tlv(u_char * __restrict buf, int buflen, arra Line 358  ait_vars2tlv(u_char * __restrict buf, int buflen, arra
         ait_val_t *val;          ait_val_t *val;
         u_char *dat;          u_char *dat;
   
         assert(buf);  
         assert(vars);          assert(vars);
        if (!buf || !vars)        if (!vars)
                 return -1;                  return -1;
        if (!buflen || !array_Size(vars))        if (!array_Size(vars))
                 return 0;                  return 0;
   
         /* calculate amount of data into buffer */          /* calculate amount of data into buffer */
        for (i = 0, Limit = 0; i < array_Size(vars); i++,         for (i = 0, Limit = 0; i < array_Size(vars); 
                        Limit += 5 + AIT_LEN(array(vars, i, ait_val_t*)));                        Limit += 5 + AIT_LEN(array(vars, i, ait_val_t*)), i++);
         /* check only needed buffer size */
         if (!buf)
                 return Limit;
 
         if (Limit > buflen) {          if (Limit > buflen) {
                 elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d",                   elwix_SetErr(EMSGSIZE, "Short buffer buflen=%d needed min %d", 
                                 buflen, Limit);                                  buflen, Limit);
Line 347  ait_vars2tlv(u_char * __restrict buf, int buflen, arra Line 443  ait_vars2tlv(u_char * __restrict buf, int buflen, arra
 }  }
   
 /*  /*
    * ait_tlv2var() - De-marshaling data from TLV buffer to variable
    *
    * @buf = Buffer
    * @buflen = Size of buffer
    * @next_tlv = Next TLV position, if it is !=NULL 
    * return: =NULL error, !=NULL allocated variable array, after use must free with ait_freeVar()
    */
   ait_val_t *
   ait_tlv2var(u_char * __restrict buf, int buflen, off_t *next_tlv)
   {
           ait_val_t *val;
           u_char *dat;
   
           assert(buf);
           if (!buf || !buflen)
                   return NULL;
   
           if (!(val = ait_allocVar()))
                   return NULL;
           else
                   dat = buf;
   
           /* de-marshaling */
           if (*dat != empty) {
                   val->val_type = *dat++;
                   AIT_LEN(val) = ntohl(*((uint32_t*) dat));
                   dat += sizeof(uint32_t);
                   
                   switch (AIT_TYPE(val)) {
                           case f32:
                                   AIT_SET_F32(val, *((float*) dat));
                                   break;
                           case f64:
                                   AIT_SET_F64(val, *((double*) dat));
                                   break;
                           case i8:
                                   AIT_SET_I8(val, *((int8_t*) dat));
                                   break;
                           case i16:
                                   AIT_SET_I16(val, *((int16_t*) dat));
                                   break;
                           case i32:
                                   AIT_SET_I32(val, *((int32_t*) dat));
                                   break;
                           case i64:
                                   AIT_SET_I64(val, *((int64_t*) dat));
                                   break;
                           case u8:
                                   AIT_SET_U8(val, *((uint8_t*) dat));
                                   break;
                           case u16:
                                   AIT_SET_U16(val, *((uint16_t*) dat));
                                   break;
                           case blob:
                           case u32:
                                   AIT_SET_U32(val, *((uint32_t*) dat));
                                   break;
                           case u64:
                                   AIT_SET_U64(val, *((uint64_t*) dat));
                                   break;
                           case data:
                                   AIT_SET_DATA(val, dat, AIT_LEN(val));
                                   break;
                           case ptr:
                           case buffer:
                                   AIT_SET_BUF(val, dat, AIT_LEN(val));
                                   break;
                           case string:
                                   AIT_SET_STR(val, (char*) dat);
                                   break;
                           default:
                                   elwix_SetErr(EINVAL, "Unsupported variable type=%d", AIT_TYPE(val));
                                   ait_freeVar(&val);
                                   return NULL;
                   }
                   dat += AIT_LEN(val);
           }
   
           if (next_tlv)
                   *next_tlv = dat - buf;
   
           return val;
   }
   
   /*
  * ait_tlv2vars() - De-marshaling data from TLV buffer to array with variables   * ait_tlv2vars() - De-marshaling data from TLV buffer to array with variables
  *   *
  * @buf = Buffer   * @buf = Buffer
Line 369  ait_tlv2vars(u_char * __restrict buf, int buflen) Line 550  ait_tlv2vars(u_char * __restrict buf, int buflen)
                 return NULL;                  return NULL;
   
         /* de-marshaling */          /* de-marshaling */
        for (i = 0, dat = buf; i < buflen; i++) {        for (i = 0, dat = buf; *dat != empty && dat < (buf + buflen); i++) {
                 val = ait_getVars(&vars, i);                  val = ait_getVars(&vars, i);
                 if (!val) {                  if (!val) {
                         ait_freeVars(&vars);                          ait_freeVars(&vars);
Line 512  ait_array2vars(const char **args, int dn) Line 693  ait_array2vars(const char **args, int dn)
         ait_val_t *val;          ait_val_t *val;
         register int i;          register int i;
         long n;          long n;
         float f;  
         double d;          double d;
         char *str;          char *str;
   
Line 532  ait_array2vars(const char **args, int dn) Line 712  ait_array2vars(const char **args, int dn)
   
                 if (dn) {                  if (dn) {
                         n = strtol(*args, &str, 0);                          n = strtol(*args, &str, 0);
                        if (!str) {                        if (!str || !*str) {
                                 AIT_SET_I64(val, (int64_t) n);                                  AIT_SET_I64(val, (int64_t) n);
                                 continue;                                  continue;
                         }                          }
                         f = strtof(*args, &str);  
                         if (!str) {  
                                 AIT_SET_F32(val, f);  
                                 continue;  
                         }  
                         d = strtod(*args, &str);                          d = strtod(*args, &str);
                        if (!str) {                        if (!str || !*str) {
                                 AIT_SET_F64(val, d);                                  AIT_SET_F64(val, d);
                                 continue;                                  continue;
                         }                          }
Line 1033  ait_sprintfVar(ait_val_t * __restrict v, const char *f Line 1208  ait_sprintfVar(ait_val_t * __restrict v, const char *f
 {  {
         int ret = 0;          int ret = 0;
         va_list lst;          va_list lst;
        char str[STRSIZ] = { [0 ... STRSIZ - 1] = 0 };        char str[65536] = { [0 ... 65535] = 0 };
   
         if (!v || !fmt)          if (!v || !fmt)
                 return -1;                  return -1;

Removed from v.1.9.56.1  
changed lines
  Added in v.1.12.6.1


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>