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 a URL-encoded query string.
char * qurl_encode (const void *bin, size_t size)
 Encode data using URL encoding (percent encoding).
size_t qurl_decode (char *str)
 Decode a URL-encoded string.
char * qbase64_encode (const void *bin, size_t size)
 Encode data using BASE64.
size_t qbase64_decode (char *str)
 Decode a BASE64 string.
char * qhex_encode (const void *bin, size_t size)
 Encode data as hexadecimal digits.
size_t qhex_decode (char *str)
 Decode hexadecimal 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 a URL-encoded query string.

Parameters
tblqlisttbl_t pointer. If NULL, a new table is created.
queryURL-encoded string
equalcharseparator between key and value
sepcharseparator between entries
countnumber of parsed entries is stored here if not NULL
Returns
qlisttbl_t pointer on success, or NULL on failure.
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 a URL-encoded query string.
Definition qencode.c:61

Definition at line 61 of file qencode.c.

◆ qurl_encode()

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

Encode data using URL encoding (percent encoding).

Parameters
bininput data
sizelength of the input data
Returns
allocated URL-encoded string on success, or NULL on failure.
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).
Definition qencode.c:123
size_t qurl_decode(char *str)
Decode a URL-encoded string.
Definition qencode.c:188

Definition at line 123 of file qencode.c.

◆ qurl_decode()

size_t qurl_decode ( char * str)

Decode a URL-encoded string.

Parameters
strURL-encoded string
Returns
number of bytes stored in str.
Note
This function modifies str in place. The result is always null-terminated.

Definition at line 188 of file qencode.c.

◆ qbase64_encode()

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

Encode data using BASE64.

Parameters
bininput data
sizelength of the input data
Returns
allocated BASE64 string on success, or NULL on failure.
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 a BASE64 string.
Definition qencode.c:301
char * qbase64_encode(const void *bin, size_t size)
Encode data using BASE64.
Definition qencode.c:244

Definition at line 244 of file qencode.c.

◆ qbase64_decode()

size_t qbase64_decode ( char * str)

Decode a BASE64 string.

Parameters
strBASE64-encoded string
Returns
number of bytes stored in str.
Note
This function modifies str in place. The result is always null-terminated.

Definition at line 301 of file qencode.c.

◆ qhex_encode()

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

Encode data as hexadecimal digits.

Parameters
bininput data
sizelength of the input data
Returns
allocated hexadecimal string on success, or NULL on failure.
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 data.
Definition qencode.c:416
char * qhex_encode(const void *bin, size_t size)
Encode data as hexadecimal digits.
Definition qencode.c:385

Definition at line 385 of file qencode.c.

◆ qhex_decode()

size_t qhex_decode ( char * str)

Decode hexadecimal data.

Parameters
strhexadecimal-encoded string
Returns
number of bytes stored in str.
Note
This function modifies str in place. The result is always null-terminated.

Definition at line 416 of file qencode.c.