Added SILC Server library.
[silc.git] / lib / silcutil / silcutil.h
index 156d86661a528919ed64ce5f8f7b1def16c65a94..1e2a68eacfcabe8c3b34b38f04b4b0a044a3bdd4 100644 (file)
@@ -4,7 +4,7 @@
 
   Author: Pekka Riikonen <priikone@silcnet.org>
 
-  Copyright (C) 1997 - 2002 Pekka Riikonen
+  Copyright (C) 1997 - 2005 Pekka Riikonen
 
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
@@ -21,7 +21,7 @@
  *
  * DESCRIPTION
  *
- *    XXX
+ *    Utility functions.
  *
  ***/
 
@@ -63,84 +63,61 @@ int silc_check_line(char *buf);
  *
  * SYNOPSIS
  *
- *    char *silc_get_time();
+ *    const char *silc_get_time(SilcUInt32 timeval)
  *
  * DESCRIPTION
  *
- *    Returns current time as string.
+ *    Returns time as string.  If the the `timeval' is non-zero that
+ *    value is returned as string.  If it is zero the current time of the
+ *    local machine is returned.
  *
  ***/
-char *silc_get_time();
+const char *silc_get_time(SilcUInt32 timeval);
 
 /****f* silcutil/SilcUtilAPI/silc_to_upper
  *
  * SYNOPSIS
  *
- *    char *silc_to_upper(char *string);
+ *    SilcBool silc_to_upper(const char *string, char *dest, SilcUInt32 dest_size);
  *
  * DESCRIPTION
  *
  *    Converts string to capital characters.
  *
  ***/
-char *silc_to_upper(char *string);
+SilcBool silc_to_upper(const char *string, char *dest, SilcUInt32 dest_size);
 
-/****f* silcutil/SilcUtilAPI/silc_encode_pem
+/****f* silcutil/SilcUtilAPI/silc_to_lower
  *
  * SYNOPSIS
  *
- *    char *silc_encode_pem(unsigned char *data, SilcUInt32 len);
+ *    SilcBool silc_to_lower(const char *string, char *dest, SilcUInt32 dest_size);
  *
  * DESCRIPTION
  *
- *    Encodes data into PEM encoding. Returns NULL terminated PEM encoded
- *    data string. Note: This is originally public domain code and is
- *    still PD.
- *
- ***/
-char *silc_encode_pem(unsigned char *data, SilcUInt32 len);
-
-/****f* silcutil/SilcUtilAPI/silc_encode_pem_file
- *
- * SYNOPSIS
- *
- *    char *silc_encode_pem_file(unsigned char *data, SilcUInt32 data_len);
- *
- * DESCRIPTION
- *
- *    Same as silc_encode_pem() but puts newline ('\n') every 72 characters.
- *
- ***/
-char *silc_encode_pem_file(unsigned char *data, SilcUInt32 data_len);
-
-/****f* silcutil/SilcUtilAPI/silc_decode_pem
- *
- * SYNOPSIS
- *
- *    unsigned char *silc_decode_pem(unsigned char *pem, SilcUInt32 pem_len,
- *                                   SilcUInt32 *ret_len);
- *
- * DESCRIPTION
- *
- *    Decodes PEM into data. Returns the decoded data. Note: This is
- *    originally public domain code and is still PD.
+ *    Converts string to capital characters.
  *
  ***/
-unsigned char *silc_decode_pem(unsigned char *pem, SilcUInt32 pem_len,
-                              SilcUInt32 *ret_len);
+SilcBool silc_to_lower(const char *string, char *dest, SilcUInt32 dest_size);
 
 /****f* silcutil/SilcUtilAPI/silc_parse_userfqdn
  *
  * SYNOPSIS
  *
- *    bool silc_parse_userfqdn(const char *string, char **left, char **right);
+ *    int silc_parse_userfqdn(const char *string,
+ *                            char *user, SilcUInt32 user_size,
+ *                            char *fqdn, SilcUInt32 fqdn_size);
  *
  * DESCRIPTION
  *
- *    Parse userfqdn string which is in user@fqdn format.
+ *    Parse userfqdn string which is in user@fqdn format.  Returns 0 on
+ *    error, 1 if `user' was filled and 2 if both `user' and `fqdn'
+ *    was filled.
  *
  ***/
-bool silc_parse_userfqdn(const char *string, char **left, char **right);
+int silc_parse_userfqdn(const char *string,
+                       char *user, SilcUInt32 user_size,
+                       char *fqdn, SilcUInt32 fqdn_size);
 
 /****f* silcutil/SilcUtilAPI/silc_parse_command_line
  *
@@ -225,6 +202,21 @@ int silc_string_compare(char *string1, char *string2);
  ***/
 SilcUInt32 silc_hash_string(void *key, void *user_context);
 
+/****f* silcutil/SilcUtilAPI/silc_hash_utf8_string
+ *
+ * SYNOPSIS
+ *
+ *    SilcUInt32 silc_hash_utf8_string(void *key, void *user_context);
+ *
+ * DESCRIPTION
+ *
+ *    Basic has function to hash UTF-8 strings. May be used with the
+ *    SilcHashTable.  Used with identifier strings.  The key is
+ *    expected to be casefolded.
+ *
+ ***/
+SilcUInt32 silc_hash_utf8_string(void *key, void *user_context);
+
 /****f* silcutil/SilcUtilAPI/silc_hash_uint
  *
  * SYNOPSIS
@@ -264,6 +256,19 @@ SilcUInt32 silc_hash_ptr(void *key, void *user_context);
  ***/
 SilcUInt32 silc_hash_id(void *key, void *user_context);
 
+/****f* silcutil/SilcUtilAPI/silc_hash_client_id_hash
+ *
+ * SYNOPSIS
+ *
+ *    SilcUInt32 silc_hash_client_id_hash(void *key, void *user_context)
+ *
+ * DESCRIPTION
+ *
+ *    Hash Client ID's hash.
+ *
+ ***/
+SilcUInt32 silc_hash_client_id_hash(void *key, void *user_context);
+
 /****f* silcutil/SilcUtilAPI/silc_hash_data
  *
  * SYNOPSIS
@@ -294,7 +299,7 @@ SilcUInt32 silc_hash_public_key(void *key, void *user_context);
  *
  * SYNOPSIS
  *
- *    bool silc_hash_string_compare(void *key1, void *key2,
+ *    SilcBool silc_hash_string_compare(void *key1, void *key2,
  *                                  void *user_context);
  *
  * DESCRIPTION
@@ -303,13 +308,14 @@ SilcUInt32 silc_hash_public_key(void *key, void *user_context);
  *    function.
  *
  ***/
-bool silc_hash_string_compare(void *key1, void *key2, void *user_context);
+SilcBool silc_hash_string_compare(void *key1, void *key2, void *user_context);
 
 /****f* silcutil/SilcUtilAPI/silc_hash_id_compare
  *
  * SYNOPSIS
  *
- *    bool silc_hash_id_compare(void *key1, void *key2, void *user_context);
+ *    SilcBool silc_hash_id_compare(void *key1, void *key2,
+ *                                  void *user_context);
  *
  * DESCRIPTION
  *
@@ -318,39 +324,74 @@ bool silc_hash_string_compare(void *key1, void *key2, void *user_context);
  *    part of the Client ID. Other ID's are fully compared.
  *
  ***/
-bool silc_hash_id_compare(void *key1, void *key2, void *user_context);
+SilcBool silc_hash_id_compare(void *key1, void *key2, void *user_context);
+
+/****f* silcutil/SilcUtilAPI/silc_hash_id_compare_full
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_hash_id_compare_full(void *key1, void *key2,
+ *                                       void *user_context)
+ *
+ * DESCRIPTION
+ *
+ *    Compares two ID's. May be used as SilcHashTable comparison function.
+ *    To compare full ID's instead of only partial, like the
+ *    silc_hash_id_compare does, use this function.
+ *
+ ***/
+SilcBool silc_hash_id_compare_full(void *key1, void *key2, void *user_context);
 
 /****f* silcutil/SilcUtilAPI/silc_hash_client_id_compare
  *
  * SYNOPSIS
  *
- *    bool silc_hash_client_id_compare(void *key1, void *key2, void *user_context);
+ *    SilcBool silc_hash_client_id_compare(void *key1, void *key2,
+ *                                         void *user_context);
  *
  * DESCRIPTION
  *
  *    Compare two Client ID's entirely and not just the hash from the ID.
  *
  ***/
-bool silc_hash_client_id_compare(void *key1, void *key2, void *user_context);
+SilcBool silc_hash_client_id_compare(void *key1, void *key2,
+                                    void *user_context);
 
 /****f* silcutil/SilcUtilAPI/silc_hash_data_compare
  *
  * SYNOPSIS
  *
- *    bool silc_hash_data_compare(void *key1, void *key2, void *user_context);
+ *    SilcBool silc_hash_data_compare(void *key1, void *key2,
+ *                                    void *user_context);
  *
  * DESCRIPTION
  *
  *    Compares binary data. May be used as SilcHashTable comparison function.
  *
  ***/
-bool silc_hash_data_compare(void *key1, void *key2, void *user_context);
+SilcBool silc_hash_data_compare(void *key1, void *key2, void *user_context);
+
+/****f* silcutil/SilcUtilAPI/silc_hash_utf8_compare
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_hash_utf8_compare(void *key1, void *key2,
+ *                                    void *user_context);
+ *
+ * DESCRIPTION
+ *
+ *    Compares UTF-8 strings.  Casefolded and NULL terminated strings are
+ *    expected.  May be used as SilcHashTable comparison function.
+ *
+ ***/
+SilcBool silc_hash_utf8_compare(void *key1, void *key2, void *user_context);
 
 /****f* silcutil/SilcUtilAPI/silc_hash_public_key_compare
  *
  * SYNOPSIS
  *
- *    bool silc_hash_public_key_compare(void *key1, void *key2, void *user_context);
+ *    SilcBool silc_hash_public_key_compare(void *key1, void *key2,
+ *                                          void *user_context);
  *
  * DESCRIPTION
  *
@@ -358,7 +399,8 @@ bool silc_hash_data_compare(void *key1, void *key2, void *user_context);
  *    comparison function.
  *
  ***/
-bool silc_hash_public_key_compare(void *key1, void *key2, void *user_context);
+SilcBool silc_hash_public_key_compare(void *key1, void *key2,
+                                     void *user_context);
 
 /****f* silcutil/SilcUtilAPI/silc_client_chmode
  *
@@ -409,7 +451,8 @@ char *silc_client_chumode_char(SilcUInt32 mode);
  *
  * DESCRIPTION
  *
- *    Creates fingerprint from data, usually used with SHA1 digests.
+ *    Return a textual representation of the fingerprint in *data, the
+ *    caller must free the returned string.
  *
  ***/
 char *silc_fingerprint(const unsigned char *data, SilcUInt32 data_len);
@@ -418,7 +461,7 @@ char *silc_fingerprint(const unsigned char *data, SilcUInt32 data_len);
  *
  * SYNOPSIS
  *
- *    bool silc_string_is_ascii(const unsigned char *data,
+ *    SilcBool silc_string_is_ascii(const unsigned char *data,
  *                              SilcUInt32 data_len);
  *
  * DESCRIPTION
@@ -426,13 +469,13 @@ char *silc_fingerprint(const unsigned char *data, SilcUInt32 data_len);
  *    Return TRUE if the `data' is ASCII string.
  *
  ***/
-bool silc_string_is_ascii(const unsigned char *data, SilcUInt32 data_len);
+SilcBool silc_string_is_ascii(const unsigned char *data, SilcUInt32 data_len);
 
 /****f* silcutil/SilcUtilAPI/silc_parse_version_string
  *
  * SYNOPSIS
  *
- *    bool silc_parse_version_string(const char *version,
+ *    SilcBool silc_parse_version_string(const char *version,
  *                                   SilcUInt32 *protocol_version,
  *                                   char **protocol_version_string,
  *                                   SilcUInt32 *software_version,
@@ -444,7 +487,7 @@ bool silc_string_is_ascii(const unsigned char *data, SilcUInt32 data_len);
  *    Parses SILC protocol style version string.
  *
  ***/
-bool silc_parse_version_string(const char *version,
+SilcBool silc_parse_version_string(const char *version,
                               SilcUInt32 *protocol_version,
                               char **protocol_version_string,
                               SilcUInt32 *software_version,
@@ -468,22 +511,183 @@ SilcUInt32 silc_version_to_num(const char *version);
  *
  * SYNOPSIS
  *
- *    char *silc_get_input(const char *prompt, bool echo_off);
+ *    char *silc_get_input(const char *prompt, SilcBool echo_off);
  *
  * DESCRIPTION
  *
  *    Displays input prompt on command line and takes input data from user.
  *
  ***/
-char *silc_get_input(const char *prompt, bool echo_off);
+char *silc_get_input(const char *prompt, SilcBool echo_off);
 
-/* XXX - System dependant prototypes */
+/* System dependant prototypes */
 
+/****f* silcutil/SilcUtilAPI/silc_gettimeofday
+ *
+ * SYNOPSIS
+ *
+ *    int silc_gettimeofday(struct timeval *p);
+ *
+ * DESCRIPTION
+ *
+ *    Return current time to struct timeval.  This function is system
+ *    dependant.  Returns 0 on success and -1 on error.
+ *
+ ***/
 int silc_gettimeofday(struct timeval *p);
+
+/****f* silcutil/SilcUtilAPI/silc_compare_timeval
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_compare_timeval(struct timeval *smaller,
+ *                              struct timeval *bigger)
+ *
+ * DESCRIPTION
+ *
+ *    Compare two timeval structures and return TRUE if the first
+ *    time value is smaller than the second time value.
+ *
+ ***/
+SilcBool silc_compare_timeval(struct timeval *smaller,
+                         struct timeval *bigger);
+
+/****f* silcutil/SilcUtilAPI/silc_string_regexify
+ *
+ * SYNOPSIS
+ *
+ *    char *silc_string_regexify(const char *string);
+ *
+ * DESCRIPTION
+ *
+ *    Inspects the `string' for wildcards and returns regex string that can
+ *    be used by the GNU regex library. A comma (`,') in the `string' means
+ *    that the string is list.
+ *
+ *    This function is system dependant.
+ *
+ ***/
 char *silc_string_regexify(const char *string);
+
+/****f* silcutil/SilcUtilAPI/silc_string_regex_match
+ *
+ * SYNOPSIS
+ *
+ *    int silc_string_regex_match(const char *regex, const char *string);
+ *
+ * DESCRIPTION
+ *
+ *    Matches the two strings and returns TRUE if the strings match.
+ *
+ *    This function is system dependant.
+ *
+ ***/
 int silc_string_regex_match(const char *regex, const char *string);
+
+/****f* silcutil/SilcUtilAPI/silc_string_match
+ *
+ * SYNOPSIS
+ *
+ *    int silc_string_match(const char *string1, const char *string2);
+ *
+ * DESCRIPTION
+ *
+ *    Do regex match to the two strings `string1' and `string2'. If the
+ *    `string2' matches the `string1' this returns TRUE.
+ *
+ *    This function is system dependant.
+ *
+ ***/
 int silc_string_match(const char *string1, const char *string2);
+
+/****f* silcutil/SilcUtilAPI/silc_get_username
+ *
+ * SYNOPSIS
+ *
+ *    char *silc_get_username();
+ *
+ * DESCRIPTION
+ *
+ *    Returns the username of the user. If the global variable LOGNAME
+ *    does not exists we will get the name from the passwd file.  The
+ *    caller must free the returned name.
+ *
+ *    This function is system dependant.
+ *
+ ***/
 char *silc_get_username();
+
+/****f* silcutil/SilcUtilAPI/silc_get_real_name
+ *
+ * SYNOPSIS
+ *
+ *    char *silc_get_real_name();
+ *
+ * DESCRIPTION
+ *
+ *    Returns the real name of ther user from the passwd file.  The
+ *    caller must free the returned name.
+ *
+ *    This function is system dependant.
+ *
+ ***/
 char *silc_get_real_name();
 
+/****f* silcutil/SilcUtilAPI/silc_get_mode_list
+ *
+ * SYNOPSIS
+ *
+ *    SilcBool silc_get_mode_list(SilcBuffer mode_list, SilcUInt32 mode_list_count,
+ *                            SilcUInt32 **list);
+ *
+ * DESCRIPTION
+ *
+ *    Returns modes from list of 32 bit MSB first order values that are
+ *    encoded one after the other in the `mode_list' into the `list'
+ *    array.  The caller must free the returned list.  Return FALSE if
+ *    there is error parsing the list.
+ *
+ ***/
+SilcBool silc_get_mode_list(SilcBuffer mode_list, SilcUInt32 mode_list_count,
+                       SilcUInt32 **list);
+
+/****f* silcutil/SilcUtilAPI/silc_get_status_message
+ *
+ * SYNOPSIS
+ *
+ *    char *silc_get_status_message(SilcStatus status)
+ *
+ * DESCRIPTION
+ *
+ *    Returns status message string
+ *
+ ***/
+const char *silc_get_status_message(unsigned char status);
+
+/****f* silcutil/SilcUtilAPI/silc_get_packet_name
+ *
+ * SYNOPSIS
+ *
+ *    char *silc_get_packet_name(SilcPacketType type);
+ *
+ * DESCRIPTION
+ *
+ *    Returns the name corresponding packet type `type'.
+ *
+ ***/
+const char *silc_get_packet_name(unsigned char type);
+
+/****f* silcutil/SilcUtilAPI/silc_get_command_name
+ *
+ * SYNOPSIS
+ *
+ *    char *silc_get_command_name(SilcCommand command);
+ *
+ * DESCRIPTION
+ *
+ *    Returns the name corresponding SILC command `command'.
+ *
+ ***/
+const char *silc_get_command_name(unsigned char command);
+
 #endif /* !SILCUTIL_H */