/*	Newhash is a SAM module, intended to be used for standardisation of
 *	hashing within all Netwin products.
 *
 *	hashing is performed by calling hash_new():
 *
 *		int hash_new(char *dir, char *xto, char *invalid, int level);
 *
 *	hash_new() hashes the directory 'dir', using the username 'xto', and the 
 *	hashing level 'level'.  It strips all characters in 'invalid' from the
 *	username when hashing.  
 *
 *	hash_new modifies 'dir' so that it contains the original 'dir', the hashed
 *	component, and the encoded username.  The encoded username will only be
 *	appended if 'append' is TRUE.
 *
 *	hash_new also puts the encoded username in string 'userpart', if 'userpart'
 *	is not NULL. (i.e. it is safe for 'userpart' to be NULL).
 *
 *	hash_new() returns TRUE if it was able to create a valid hash name, and 
 *	FALSE otherwise.
 *
 *	As far as possible, it is backwards-compatible with old hashing functions.
 *	However, it has been designed to be as robust as possible; with this in
 *	mind, it now strips invalid characters from usernames prior to hashing,
 *	and encodes such invalid characters prior to appending the username to the
 *	end of the hash results.  On win32 systems, trailing and leading '.' are 
 *	considered to be invalid.
 *
 *	For example, using old-style hashing in these cases would be problematic:
 *
 *				a:        {c:\dmail\in\a\.\a}
 *		       b.:        {c:\dmail\in\b\.\b.}
 *             .c:        {c:\dmail\in\.\c\.c}
 *     t.wil$lacy:        {c:\dmail\in\t\.\t.wil$lacy}
 *     tamwillacy:        {c:\dmail\in\t\a\tamwillacy}
 *     +.wil lacy:        {c:\dmail\in\+\.\+.wil lacy}
 *
 *	wheras now they can be handled as follows:
 *
 *              a:        {c:\dmail\in\a\a\a}
 *             b.:        {c:\dmail\in\b\b\b$2e}
 *             .c:        {c:\dmail\in\c\c\$2ec}
 *     t.wil$lacy:        {c:\dmail\in\t\w\t.wil$lacy}
 *     tamwillacy:        {c:\dmail\in\t\a\tamwillacy}
 *     +.wil lacy:        {c:\dmail\in\w\i\$2a.wil lacy}
 *
 *	There is also a third hashing level, 3.  This level is more suitable for
 *	systems with many users:
 *              
 *				a:        {c:\dmail\in\n_j7\a0\a}
 *             b.:        {c:\dmail\in\n_o8\n7\b$2e}
 *             .c:        {c:\dmail\in\n_n7\o9\$2ec}
 *     t.wil$lacy:        {c:\dmail\in\n_d8\o9\t.wil$lacy}
 *     tamwillacy:        {c:\dmail\in\n_c5\d0\tamwillacy}
 *     +.wil lacy:        {c:\dmail\in\n_f5\t5\$2a.wil lacy}
 */

#ifndef INCLUDE_NEWHASH

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>

#define _NH_BFSZ 499
#define NEWHASH_BFSZ 499
#define CHAR_ENCODE ','

#ifndef TRUE
#define TRUE 1
#define FALSE (!TRUE)
#endif

#ifdef WIN32
#define NH_SEP '\\'
#define NH_SEP_S "\\"
#else
#define NH_SEP '/'
#define NH_SEP_S "/"
#endif

/* hash_mask gives a wild mask which will match the results of hash_new.
 */
int hash_mask(char *dir, int level, int append);

/*	hash_new hashes the directory 'dir', using the username 'xto', and the 
 *	hashing level 'level'.  It strips all characters in 'invalid' from the
 *	username when hashing.  
 *
 *	hash_new modifies 'dir' so that it contains the original 'dir', the hashed
 *	component, and the encoded username.  The encoded username will only be
 *	appended if 'append' is TRUE.
 *
 *	hash_new also puts the encoded username in string 'userpart', if 'userpart'
 *	is not NULL. (i.e. it is safe for 'userpart' to be NULL).
 *
 *	hash_new returns TRUE if it was able to create a valid hash name, and FALSE
 *	otherwise.
 */
int hash_new(char *dir, char *xto, char *invalid, int level, int append, 
			 char *userpart);

/*	encode_invalid encodes all invalid characters in 'dir'.  On
 *	non-UNIX systems, leading or trailing '.' are considered invalid.  It 
 *	returns a pointer to a static string containing the encoded user name.
 */
char *encode_invalid(char *dir, char *invalid, char *tmp);

/*	encode_last encodes the last level of 'path' as if it were a username.
 *	Invalid characters are encoded as per encode_invalid(), using 'invalid'
 *	as a base set of invalid characters.
 */
void encode_last(char *path, char *invalid);

/*	decode_invalid() performs the reverse of encode_invalid().  It returns
 *	a pointer to a string containing the string 'username', with all
 *	encoded characters in 'invalid' replaced by their originals.
 */
char *decode_invalid(char *username, char *invalid, char *tmp);

#define INCLUDE_NEWHASH
#endif










