/****************************************************************************

   mod_dav_pgsql (DASL) for apache 2.X
   CopyRight(C) 2005 SolutionBox Inc. All Rights reserved.
   Author : sean kim (sean@solutionbox.co.kr)

   $Id: util.h,v 1.3 2007/01/25 06:52:16 elenoa Exp $

   Redistribution and use in source and binary forms, with or with out
   modification, are not permitted in outside of SolutionBox Inc.

 ****************************************************************************/
#ifndef __DAV_REPOS_UTIL_H__
#define __DAV_REPOS_UTIL_H__

//#include <libpq-fe.h>
#include <iconv.h>
#include <apr_md5.h>
#include <stdbool.h>		/* ADD 2010-08-10 huibong bool type 사용을 위한 Header 추가 */

#include <apr_dbd.h>
#include <mod_dbd.h>


#define DAV_REPOS_MAX_NAMESPACE 1024

/*-----------------------------------------------------------------
* Utility function
*----------------------------------------------------------------*/

#define MaxSizeOfAcctUserID        32
#define MaxSizeOfAcctVolume        32
#define MaxSizeOfAcctSession       128
#define MaxSizeOfPathLength        4096
#define MaxSizeOfFileNameLength    1024

/* user_login_name@partner_login_name */
typedef struct _acct_info {
	char szVolumeID[MaxSizeOfAcctVolume];
#ifdef __CLASSIFIED_TRAFFIC_MANAGE__
	char szTCClassID[MaxSizeOfAcctVolume];
#endif
	char szUserID[MaxSizeOfAcctUserID];
	char szSessionID[MaxSizeOfAcctSession];
} acct_info;

// UTF-8 -> Database Character Set
char* transformDBCharacter(request_rec *r, char* srcbuf, char *prefixmsg );
// Database Character Set -> UTF-8
char* transformUTF8(request_rec *r, char* srcbuf, char *prefixmsg );

int dav_repos_remove_base_uri(const char *base_uri, const char *req_uri, char *result);
int dav_repos_remove_unsafe_chars(const char *pOld, char *pNew);
int dav_repos_replace_unsafe_chars(const char *pOld, char *pNew);
int dav_repos_restore_unsafe_chars(const char *pOld, char *pNew);
int dav_repos_replace_multi_slash_chars(const char *pOld, char *pNew);
//char *dav_repos_remove_unsafe_chars(const char *pStr);
int remove_user_root_dir_from_fake_uri(const char *pcszFakeURI, char *pURI);
int get_filename_in_path(const char *pcszPath, char *pszPathName, char *pszFileName);

// CHG 2010-08-10 huibong
// Hash 정보 생성 처리시 생성 결과를 static 변수에 저장하고
// 해당 변수를 반환처리 하는 경우 ..
// 동시간 접근시 Lock 을 사용하지 않게 되면 문제가 발생하게 됨. ( File Name에 대한 Hash 정보가 동일하게 생성)
// 하지만 Lock 을 처리속도에 영향을 미칠수 있으므로 
// 결과 값을 저장할 출력변수를 매개변수로 전달받도록 수정처리 한다.
//char *get_hashed_filepath(const char *plain_filepath);
bool get_hashed_filepath( const char *plain_filepath, char * result, int result_length);

int at_split(const char *pszAcctInfo, acct_info *pAcctInfo);
int check_string(char *to, const char *from, size_t length);
/*
** Get DB handler from request_rec
*/
dav_repos_server_conf *dav_repos_get_server_conf(server_rec *s);
ap_dbd_t *dav_repos_get_db(request_rec *r);
void dav_repos_release_db(request_rec *r, ap_dbd_t *db, int rv);
int dav_repos_begin_trans(const char *func, request_rec *r, ap_dbd_t *db,
		        apr_dbd_transaction_t **trans);
int dav_repos_end_trans(const char *func, request_rec *r, ap_dbd_t *db,
		        apr_dbd_transaction_t *trans);
//int dav_repos_init_db(apr_pool_t *p, server_rec * s);
int dav_repos_destroy_db(server_rec * s);

/* make date string*/
void dav_repos_format_time(int style, apr_time_t sec, char *buf);
void dav_repos_get_ns_name_key(const char *key, dav_prop_name * pname,
			       apr_pool_t * pool);
const char *dav_repos_build_ns_name_key(const char *ns, const char *name,
					apr_pool_t * pool);


/* Make XML reponse */
dav_response *dav_repos_mkresponse(dav_repos_resource * db_r);


/* Build dead property hash
 * Build the hash before makeresponse
 */
void dav_repos_build_pr_hash(dav_repos_resource * db_r);

/* Build live property */
void dav_repos_build_lpr_hash(dav_repos_resource * db_r);

/*# FIXME : This is a insert lock info for search.
** Do we really need a lock info when we search ?
** Then we should make params in search semewhere.
*/
dav_error *dav_repos_insert_lock_prop(const dav_walk_params * params,
				      dav_repos_resource * db_r);


/* make sure the pathname does not have a trailing "/" 
** Make sure uri, pathname in DB must not have a trailing
** Use this function.
*/
void dav_repos_no_trail(char *str);

#ifdef __OPENDISK_SOLUTION__
/* resource type change */
int dav_repos_change_resource_type(int resource_type);
#endif

void tea_decrypt(char *buf, int len);
int file_trylock(int fd);
int file_unlock(int fd);

/* apache version 업에 따른 apr_dbd_get_row 처리 방법 변경 */
int solbox_apr_dbd_get_row(const apr_dbd_driver_t *driver, apr_pool_t *pool,
	apr_dbd_results_t *res, apr_dbd_row_t **row, int rownum);


dav_error* solbox_dav_new_error(apr_pool_t *p, int status,
	int error_id, const char *desc);

const char *dav_http_scheme(const request_rec *r);
#endif
