libcoap  4.1.1
 All Data Structures Files Functions Variables Typedefs Macros Groups Pages
Data Structures | Macros | Typedefs | Functions
uri.c File Reference
#include "config.h"
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "mem.h"
#include "debug.h"
#include "pdu.h"
#include "option.h"
#include "uri.h"

Go to the source code of this file.

Data Structures

struct  cnt_str
 

Macros

#define hexchar_to_dec(c)
 Calculates decimal value from hexadecimal ASCII character given in c. More...
 
#define min(a, b)   ((a) < (b) ? (a) : (b))
 
#define URI_DATA(uriobj)   ((unsigned char *)(uriobj) + sizeof(coap_uri_t))
 

Typedefs

typedef void(* segment_handler_t )(unsigned char *, size_t, void *)
 

Functions

static unsigned char * strnchr (unsigned char *s, size_t len, unsigned char c)
 A length-safe version of strchr(). More...
 
int coap_split_uri (unsigned char *str_var, size_t len, coap_uri_t *uri)
 Parses a given string into URI components. More...
 
void decode_segment (const unsigned char *seg, size_t length, unsigned char *buf)
 Decodes percent-encoded characters while copying the string seg of size length to buf. More...
 
int check_segment (const unsigned char *s, size_t length)
 Runs through the given path (or query) segment and checks if percent-encodings are correct. More...
 
int make_decoded_option (const unsigned char *s, size_t length, unsigned char *buf, size_t buflen)
 Writes a coap option from given string s to buf. More...
 
size_t coap_split_path_impl (coap_parse_iterator_t *parse_iter, segment_handler_t h, void *data)
 Splits the given string into segments. More...
 
void write_option (unsigned char *s, size_t len, void *data)
 
int coap_split_path (const unsigned char *s, size_t length, unsigned char *buf, size_t *buflen)
 Splits the given URI path into segments. More...
 
int coap_split_query (const unsigned char *s, size_t length, unsigned char *buf, size_t *buflen)
 Splits the given URI query into segments. More...
 
coap_uri_tcoap_new_uri (const unsigned char *uri, unsigned int length)
 Creates a new coap_uri_t object from the specified URI. More...
 
coap_uri_tcoap_clone_uri (const coap_uri_t *uri)
 Clones the specified coap_uri_t object. More...
 
static void hash_segment (unsigned char *s, size_t len, void *data)
 
int coap_hash_path (const unsigned char *path, size_t len, coap_key_t key)
 Calculates a hash over the given path and stores the result in key. More...
 
coap_parse_iterator_tcoap_parse_iterator_init (unsigned char *s, size_t n, unsigned char separator, unsigned char *delim, size_t dlen, coap_parse_iterator_t *pi)
 Initializes the given iterator pi. More...
 
unsigned char * coap_parse_next (coap_parse_iterator_t *pi)
 Updates the iterator pi to point to the next token. More...
 

Macro Definition Documentation

#define hexchar_to_dec (   c)

Calculates decimal value from hexadecimal ASCII character given in c.

The caller must ensure that c actually represents a valid heaxdecimal character, e.g. with isxdigit(3).

Definition at line 182 of file uri.c.

#define min (   a,
 
)    ((a) < (b) ? (a) : (b))

Definition at line 295 of file uri.c.

#define URI_DATA (   uriobj)    ((unsigned char *)(uriobj) + sizeof(coap_uri_t))

Definition at line 382 of file uri.c.

Typedef Documentation

typedef void(* segment_handler_t)(unsigned char *, size_t, void *)

Definition at line 298 of file uri.c.

Function Documentation

int check_segment ( const unsigned char *  s,
size_t  length 
)

Runs through the given path (or query) segment and checks if percent-encodings are correct.

This function returns -1 on error or the length of s when decoded.

Definition at line 219 of file uri.c.

coap_uri_t* coap_clone_uri ( const coap_uri_t uri)

Clones the specified coap_uri_t object.

Thie function allocates sufficient memory to hold the coap_uri_t structure and its contents. The object must be released with coap_free().

Definition at line 404 of file uri.c.

int coap_hash_path ( const unsigned char *  path,
size_t  len,
coap_key_t  key 
)

Calculates a hash over the given path and stores the result in key.

This function returns 0 on error or 1 on success.

Parameters
pathThe URI path to generate hash for.
lenThe length of path.
keyThe output buffer.
Returns
1 if key was set, 0 otherwise.

Definition at line 454 of file uri.c.

coap_uri_t* coap_new_uri ( const unsigned char *  uri,
unsigned int  length 
)

Creates a new coap_uri_t object from the specified URI.

Returns the new object or NULL on error. The memory allocated by the new coap_uri_t must be released using coap_free().

Parameters
uriThe URI path to copy. length The length of uri.
Returns
New URI object or NULL on error.

Definition at line 385 of file uri.c.

size_t coap_split_path_impl ( coap_parse_iterator_t parse_iter,
segment_handler_t  h,
void *  data 
)

Splits the given string into segments.

You should call one of the macros coap_split_path() or coap_split_query() instead.

Parameters
parse_iterThe iterator used for tokenizing.
hA handler that is called with every token.
dataOpaque data that is passed to h when called.
Returns
The number of characters that have been parsed from s.

Definition at line 311 of file uri.c.

void decode_segment ( const unsigned char *  seg,
size_t  length,
unsigned char *  buf 
)

Decodes percent-encoded characters while copying the string seg of size length to buf.

The caller of this function must ensure that the percent-encodings are correct (i.e. the character '' is always followed by two hex digits. and that buf provides sufficient space to hold the result. This function is supposed to be called by make_decoded_option() only.

Parameters
segThe segment to decode and copy.
lengthLength of seg.
bufThe result buffer.

Definition at line 197 of file uri.c.

static void hash_segment ( unsigned char *  s,
size_t  len,
void *  data 
)
inlinestatic

Definition at line 449 of file uri.c.

int make_decoded_option ( const unsigned char *  s,
size_t  length,
unsigned char *  buf,
size_t  buflen 
)

Writes a coap option from given string s to buf.

s should point to a (percent-encoded) path or query segment of a coap_uri_t object. The created option will have type 0, and the length parameter will be set according to the size of the decoded string. On success, this function returns the option's size, or a value less than zero on error. This function must be called from coap_split_path_impl() only.

Parameters
sThe string to decode.
lengthThe size of the percent-encoded string s.
bufThe buffer to store the new coap option.
buflenThe maximum size of buf.
Returns
The option's size, or -1 on error.
Bug:
This function does not split segments that are bigger than 270 bytes.

Definition at line 258 of file uri.c.

static unsigned char* strnchr ( unsigned char *  s,
size_t  len,
unsigned char  c 
)
inlinestatic

A length-safe version of strchr().

This function returns a pointer to the first occurrence of c in s, or NULL if not found.

Parameters
sThe string to search for c.
lenThe length of s.
cThe character to search.
Returns
A pointer to the first occurence of c, or NULL if not found.

Definition at line 37 of file uri.c.

void write_option ( unsigned char *  s,
size_t  len,
void *  data 
)

Definition at line 336 of file uri.c.