The qDecoder Project

qEncode.c File Reference

Encoding/decoding API. More...


Functions

Q_ENTRYqParseQueries (Q_ENTRY *entry, const char *query, char equalchar, char sepchar, int *count)
 Parse URL encoded query string.
char * qUrlEncode (const void *bin, size_t size)
 Encode data using URL encoding(Percent encoding) algorithm.
size_t qUrlDecode (char *str)
 Decode URL encoded string.
char * qBase64Encode (const void *bin, size_t size)
 Encode data using BASE64 algorithm.
size_t qBase64Decode (char *str)
 Decode BASE64 encoded string.


Detailed Description

Encoding/decoding API.


Function Documentation

Q_ENTRY* qParseQueries ( Q_ENTRY entry,
const char *  query,
char  equalchar,
char  sepchar,
int *  count 
)

Parse URL encoded query string.

Parameters:
entry a pointer of Q_ENTRY structure. NULL can be used.
query URL encoded string.
equalchar separater of key, value pair.
sepchar separater of line.
count if count is not NULL, a number of parsed entries are stored.
Returns:
a pointer of Q_ENTRY if successful, otherwise returns NULL.
   cont char query = "category=love&str=%C5%A5%B5%F0%C4%DA%B4%F5&sort=asc";
   Q_ENTRY *entry = qDecodeQueryString(NULL, req->pszQueryString, '=', '&', NULL);
   printf("category = %s\n", entry->getStr(entry, "category"));
   printf("str = %s\n", entry->getStr(entry, "str"));
   printf("sort = %s\n", entry->getStr(entry, "sort"));
   entry->free(entry);

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

Encode data using URL encoding(Percent encoding) algorithm.

Parameters:
bin a pointer of input data.
size the length of input data.
Returns:
a malloced string pointer of URL encoded string in case of successful, otherwise returns NULL
   const char *text = strdup("hello 'qDecoder' world");

   char *encstr = qUrlEncode(text, strlen(text));
   if(encstr == NULL) return -1;

   printf("Original: %s\n", text);
   printf("Encoded : %s\n", encstr);

   size_t decsize = qUrlDecode(encstr);

   printf("Decoded : %s (%zu bytes)\n", encstr, decsize);
   free(encstr);

   --[output]--
   Original: hello 'qDecoder' world
   Encoded:  hello%20%27qDecoder%27%20world
   Decoded:  hello 'qDecoder' world (22 bytes)

size_t qUrlDecode ( char *  str  ) 

Decode URL encoded string.

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

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

Encode data using BASE64 algorithm.

Parameters:
bin a pointer of input data.
size the length of input data.
Returns:
a malloced string pointer of BASE64 encoded string in case of successful, otherwise returns NULL
   const char *text = strdup("hello 'qDecoder' world");

   char *encstr = qBase64Encode(text, strlen(text));
   if(encstr == NULL) return -1;

   printf("Original: %s\n", text);
   printf("Encoded : %s\n", encstr);

   size_t decsize = qBase64Decode(encstr);

   printf("Decoded : %s (%zu bytes)\n", encstr, decsize);
   free(encstr);

   --[output]--
   Original: hello 'qDecoder' world
   Encoded:  hello%20%27qDecoder%27%20world
   Decoded:  hello 'qDecoder' world (22 bytes)

size_t qBase64Decode ( char *  str  ) 

Decode BASE64 encoded string.

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


Copyright (c) 2000-2010 The qDecoder Project