qLibc
qencode.c File Reference

Encoding/decoding APIs. More...

Go to the source code of this file.

Functions

qlisttbl_t * qparse_queries (qlisttbl_t *tbl, const char *query, char equalchar, char sepchar, int *count)
 Parse URL encoded query string.
 
char * qurl_encode (const void *bin, size_t size)
 Encode data using URL encoding(Percent encoding) algorithm.
 
size_t qurl_decode (char *str)
 Decode URL encoded string.
 
char * qbase64_encode (const void *bin, size_t size)
 Encode data using BASE64 algorithm.
 
size_t qbase64_decode (char *str)
 Decode BASE64 encoded string.
 
char * qhex_encode (const void *bin, size_t size)
 Encode data to Hexadecimal digit format.
 
size_t qhex_decode (char *str)
 Decode Hexadecimal encoded data.
 

Detailed Description

Encoding/decoding APIs.

Definition in file qencode.c.

Function Documentation

◆ qparse_queries()

qlisttbl_t * qparse_queries ( qlisttbl_t *  tbl,
const char *  query,
char  equalchar,
char  sepchar,
int *  count 
)

Parse URL encoded query string.

Parameters
tbla pointer of qlisttbl_t container. NULL can be used to create new table.
queryURL encoded string.
equalcharseparater of key, value pair.
sepcharseparater of line.
countif count is not NULL, a number of parsed entries are stored.
Returns
qlisttbl container pointer, otherwise returns NULL.
cont char query = "category=love&str=%C5%A5%B5%F0%C4%DA%B4%F5&sort=asc";
qlisttbl_t *tbl = qparse_queries(NULL, req->pszQueryString, '=', '&', NULL);
printf("category = %s\n", tbl->get_str(tbl, "category", false));
printf("str = %s\n", tbl->get_str(tbl, "str", false));
printf("sort = %s\n", tbl->get_str(tbl, "sort", false));
tbl->free(tbl);
qlisttbl_t * qparse_queries(qlisttbl_t *tbl, const char *query, char equalchar, char sepchar, int *count)
Parse URL encoded query string.
Definition qencode.c:62

Definition at line 62 of file qencode.c.

◆ qurl_encode()

char * qurl_encode ( const void *  bin,
size_t  size 
)

Encode data using URL encoding(Percent encoding) algorithm.

Parameters
bina pointer of input data.
sizethe length of input data.
Returns
a malloced string pointer of URL encoded string in case of successful, otherwise returns NULL
const char *text = "hello 'qLibc' world";
char *encstr = qurl_encode(text, strlen(text));
if(encstr == NULL) return -1;
printf("Original: %s\n", text);
printf("Encoded : %s\n", encstr);
size_t decsize = qurl_decode(encstr);
printf("Decoded : %s (%zu bytes)\n", encstr, decsize);
free(encstr);
--[output]--
Original: hello 'qLibc' world
Encoded: hello%20%27qLibc%27%20world
Decoded: hello 'qLibc' world (19 bytes)
char * qurl_encode(const void *bin, size_t size)
Encode data using URL encoding(Percent encoding) algorithm.
Definition qencode.c:125
size_t qurl_decode(char *str)
Decode URL encoded string.
Definition qencode.c:192

Definition at line 125 of file qencode.c.

◆ qurl_decode()

size_t qurl_decode ( char *  str)

Decode URL encoded string.

Parameters
stra pointer of URL encoded string.
Returns
the length of bytes stored in the str memory in case of successful, otherwise returns NULL
Note
This modify str directly. And the 'str' is always terminated by NULL character.

Definition at line 192 of file qencode.c.

◆ qbase64_encode()

char * qbase64_encode ( const void *  bin,
size_t  size 
)

Encode data using BASE64 algorithm.

Parameters
bina pointer of input data.
sizethe length of input data.
Returns
a malloced string pointer of BASE64 encoded string in case of successful, otherwise returns NULL
const char *text = "hello world";
char *encstr = qbase64_encode(text, strlen(text));
if(encstr == NULL) return -1;
printf("Original: %s\n", text);
printf("Encoded : %s\n", encstr);
size_t decsize = qbase64_decode(encstr);
printf("Decoded : %s (%zu bytes)\n", encstr, decsize);
free(encstr);
--[output]--
Original: hello world
Encoded: aGVsbG8gd29ybGQ=
Decoded: hello world (11 bytes)
size_t qbase64_decode(char *str)
Decode BASE64 encoded string.
Definition qencode.c:308
char * qbase64_encode(const void *bin, size_t size)
Encode data using BASE64 algorithm.
Definition qencode.c:249

Definition at line 249 of file qencode.c.

◆ qbase64_decode()

size_t qbase64_decode ( char *  str)

Decode BASE64 encoded string.

Parameters
stra pointer of Base64 encoded string.
Returns
the length of bytes stored in the str memory in case of successful, otherwise returns NULL
Note
This modify str directly. And the 'str' is always terminated by NULL character.

Definition at line 308 of file qencode.c.

◆ qhex_encode()

char * qhex_encode ( const void *  bin,
size_t  size 
)

Encode data to Hexadecimal digit format.

Parameters
bina pointer of input data.
sizethe length of input data.
Returns
a malloced string pointer of Hexadecimal encoded string in case of successful, otherwise returns NULL
const char *text = "hello world";
char *encstr = qhex_encode(text, strlen(text));
if(encstr == NULL) return -1;
printf("Original: %s\n", text);
printf("Encoded : %s\n", encstr);
size_t decsize = qhex_decode(encstr);
printf("Decoded : %s (%zu bytes)\n", encstr, decsize);
free(encstr);
return 0;
--[output]--
Original: hello world
Encoded : 68656c6c6f20776f726c64
Decoded : hello world (11 bytes)
size_t qhex_decode(char *str)
Decode Hexadecimal encoded data.
Definition qencode.c:426
char * qhex_encode(const void *bin, size_t size)
Encode data to Hexadecimal digit format.
Definition qencode.c:393

Definition at line 393 of file qencode.c.

◆ qhex_decode()

size_t qhex_decode ( char *  str)

Decode Hexadecimal encoded data.

Parameters
stra pointer of Hexadecimal encoded string.
Returns
the length of bytes stored in the str memory in case of successful, otherwise returns NULL
Note
This modify str directly. And the 'str' is always terminated by NULL character.

Definition at line 426 of file qencode.c.