libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
encode.c
Go to the documentation of this file.
1 /* encode.c -- encoding and decoding of CoAP data types
2  *
3  * Copyright (C) 2010,2011 Olaf Bergmann <bergmann@tzi.org>
4  *
5  * This file is part of the CoAP library libcoap. Please see
6  * README for terms of use.
7  */
8 
9 #ifndef NDEBUG
10 # include <stdio.h>
11 #endif
12 
13 #include "config.h"
14 #include "encode.h"
15 
16 /* Carsten suggested this when fls() is not available: */
17 int coap_fls(unsigned int i) {
18  int n;
19  for (n = 0; i; n++)
20  i >>= 1;
21  return n;
22 }
23 
24 unsigned int
25 coap_decode_var_bytes(unsigned char *buf,unsigned int len) {
26  unsigned int i, n = 0;
27  for (i = 0; i < len; ++i)
28  n = (n << 8) + buf[i];
29 
30  return n;
31 }
32 
33 unsigned int
34 coap_encode_var_bytes(unsigned char *buf, unsigned int val) {
35  unsigned int n, i;
36 
37  for (n = 0, i = val; i && n < sizeof(val); ++n)
38  i >>= 8;
39 
40  i = n;
41  while (i--) {
42  buf[i] = val & 0xff;
43  val >>= 8;
44  }
45 
46  return n;
47 }
48 
int coap_fls(unsigned int i)
Definition: encode.c:17
unsigned int coap_encode_var_bytes(unsigned char *buf, unsigned int val)
Encodes multiple-length byte sequences.
Definition: encode.c:34
unsigned int coap_decode_var_bytes(unsigned char *buf, unsigned int len)
Decodes multiple-length byte sequences.
Definition: encode.c:25