diff options
| author | hugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2005-08-01 11:34:43 +0000 |
|---|---|---|
| committer | hugetoad <hugetoad@97f52cf1-0a1b-0410-bd0e-c28be96e8082> | 2005-08-01 11:34:43 +0000 |
| commit | e7f88affa5641d95bb357741fb20ce89949a18a1 (patch) | |
| tree | 0fb65dfa2ae220a9127d6dc14f4ff4ff696d77fe /src | |
| parent | 95acfa6fc0e8b351f2c255d07fb20ace25d6495d (diff) | |
| download | zabbix-e7f88affa5641d95bb357741fb20ce89949a18a1.tar.gz zabbix-e7f88affa5641d95bb357741fb20ce89949a18a1.tar.xz zabbix-e7f88affa5641d95bb357741fb20ce89949a18a1.zip | |
Minor changes.
git-svn-id: svn://svn.zabbix.com/trunk@1911 97f52cf1-0a1b-0410-bd0e-c28be96e8082
Diffstat (limited to 'src')
| -rw-r--r-- | src/libs/zbxcommon/Makefile.am | 3 | ||||
| -rw-r--r-- | src/libs/zbxcommon/base64.c | 253 |
2 files changed, 256 insertions, 0 deletions
diff --git a/src/libs/zbxcommon/Makefile.am b/src/libs/zbxcommon/Makefile.am new file mode 100644 index 00000000..3e8aacf0 --- /dev/null +++ b/src/libs/zbxcommon/Makefile.am @@ -0,0 +1,3 @@ +SUBDIRS=. +lib_LIBRARIES=libzbxcommon.a +libzbxcommon_a_SOURCES=base64.c diff --git a/src/libs/zbxcommon/base64.c b/src/libs/zbxcommon/base64.c new file mode 100644 index 00000000..45d4219b --- /dev/null +++ b/src/libs/zbxcommon/base64.c @@ -0,0 +1,253 @@ +static char base64_set [] = + "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; + +void str_base64_encode(char *p_str, char *p_b64str, int in_size); +void str_base64_decode(char *p_b64str, char *p_str, int *p_out_size); +char char_base64_encode(unsigned char uc); +unsigned char char_base64_decode(char c); +int is_base64 (char c); + +/*------------------------------------------------------------------------ + * + * Function : is_base64 + * + * Purpose : Is the character passed in a base64 character ? + * + * Parameters : + * + * Returns : + * + * Comments : + * + *----------------------------------------------------------------------*/ +private int is_base64 (char c) +{ + if ( (c >= '0' && c <= '9') + || (c >= 'a' && c <= 'z') + || (c >= 'A' && c <= 'Z') + || c == '/' + || c == '+' + || c == '=' ) + + { + return 1; + } + + return 0; +} +/*------------------------------------------------------------------------ + * + * Function : char_base64_encode + * + * Purpose : Encode a byte into a base64 character + * + * Parameters : + * + * Returns : + * + * Comments : + * + *----------------------------------------------------------------------*/ +private char char_base64_encode(unsigned char uc) +{ + return base64_set[uc]; +} + +/*------------------------------------------------------------------------ + * + * Function : char_base64_decode + * + * Purpose : Decode a base64 character into a byte + * + * Parameters : + * + * Returns : + * + * Comments : + * + *----------------------------------------------------------------------*/ +private unsigned char char_base64_decode(char c) +{ + if (c >= 'A' && c <= 'Z') + { + return c - 'A'; + } + + if (c >= 'a' && c <= 'z') + { + return c - 'a' + 26; + } + + if (c >= '0' && c <= '9') + { + return c - '0' + 52; + } + + if (c == '+') + { + return 62; + } + + return 63; +} +/*------------------------------------------------------------------------ + * + * Function : str_base64_encode + * + * Purpose : Encode a string into a base64 string + * + * Parameters : p_str (in) - the string to encode + * p_b64str (out) - the encoded str to return + * in_size (in) - size (length) of input str + * Returns : + * + * Comments : + * + *----------------------------------------------------------------------*/ +public void str_base64_encode(char *p_str, char *p_b64str, int in_size) +{ + int i; + unsigned char from1=0,from2=0,from3=0; + unsigned char to1=0,to2=0,to3=0,to4=0; + char *p_b64init = p_b64str; + + if ( 0 == in_size ) + { + return; + }; + + for ( i = 0; i < in_size ; i += 3 ) + { + from1 = from2 = from3 = 0; + from1 = p_str[i]; + if (i+1 < in_size) + { + from2 = p_str[i+1]; + } + if (i+2 < in_size) + { + from3 = p_str[i+2]; + } + + to1 = to2 = to3 = to4 = 0; + to1 = (from1>>2) & 0x3f; + to2 = ((from1&0x3)<<4)|(from2>>4); + to3 = ((from2&0xf)<<2)|(from3>>6); + to4 = from3&0x3f; + + *(p_b64str++) = char_base64_encode(to1); + *(p_b64str++) = char_base64_encode(to2); + + if (i+1 < in_size) + { + *(p_b64str++) = char_base64_encode(to3); + } + else + { + *(p_b64str++) = '='; /* Padding */ + } + if (i+2 < in_size) + { + *(p_b64str++) = char_base64_encode(to4); + } + else + { + *(p_b64str++) = '='; /* Padding */ + }; + + if ( i % (76/4*3) == 0) + { + *(p_b64str++) = '\r'; + *(p_b64str++) = '\n'; + } + }; + + return; +} +/*------------------------------------------------------------------------ + * + * Function : str_base64_decode + * + * Purpose : Decode a base64 string into a string + * + * Parameters : p_b64str (in) - the base64 string to decode + * p_str (out) - the encoded str to return + * p_out_size (out) - the size (len) of the str decoded + * + * Returns : + * + * Comments : + * + *----------------------------------------------------------------------*/ +public void str_base64_decode(char *p_b64str, char *p_str, int *p_out_size) +{ + int i; + int j = 0; + int in_size; + char from1='A',from2='A',from3='A',from4='A'; + unsigned char to1=0,to2=0,to3=0,to4=0; + char str_clean[MAX_B64_SIZE];/* str_clean is the string + * after removing the non-base64 + * characters + */ + + in_size = strlen(p_b64str); + memset(str_clean, 0, sizeof(str_clean)); + *p_out_size = 0; + + /* Clean-up input string */ + for ( i=0; i < in_size; i++ ) + { + if (is_base64(p_b64str[i])) + { + str_clean[j++] = p_b64str[i]; + } + } + + /* Re-define in_size after clean-up */ + in_size = strlen(str_clean); + + if ( 0 == in_size ) + { + return; + } + + for ( i=0; i < in_size ;i+=4) + { + from1 = from2 = from3 = from4 = 'A'; + from1 = str_clean[i]; + if ( i+1 < in_size ) + { + from2 = str_clean[i+1]; + } + if ( i+2 < in_size ) + { + from3 = str_clean[i+2]; + } + if ( i+3 < in_size ) + { + from4 = str_clean[i+3]; + }; + + to1 = to2 = to3 = to4 = 0; + to1 = char_base64_decode(from1); + to2 = char_base64_decode(from2); + to3 = char_base64_decode(from3); + to4 = char_base64_decode(from4); + + *(p_str++) = ( (to1<<2)|(to2>>4) ); + (*p_out_size)++; + if (from3 != '=') + { + *(p_str++) = ( ((to2&0xf)<<4)|(to3>>2) ); + (*p_out_size)++; + } + if (from4 != '=') + { + *(p_str++) = ( ((to3&0x3)<<6)|to4 ); + (*p_out_size)++; + } + } + + return; +} |
