1/*
2 * This program is free software; you can redistribute it and/or
3 * modify it under the terms of the GNU General Public License as
4 * published by the Free Software Foundation; either version 2 of
5 * the License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
15 * MA 02111-1307 USA
16 */
17/*
18 * Part of Very Secure FTPd
19 * Licence: GPL v2
20 * Author: Chris Evans
21 * utility.c
22 */
23
24#include "utility.h"
25#include "sysutil.h"
26#include "str.h"
27#include "defs.h"
28#include <stdarg.h>	// Jiahao
29//#include <iconv.h>
30#include <stdlib.h>
31#include <stdio.h>
32#include <unistd.h>
33#include <string.h>
34#include <bcmnvram.h>
35#include "tunables.h"
36
37const char* NLS_NVRAM_U2C="asusnlsu2c";	// Jiahao
38const char* NLS_NVRAM_C2U="asusnlsc2u";
39static char *xfr_buf=NULL;
40//static int xfr_buf_init=0;
41char tmp[1024];
42
43#define DIE_DEBUG
44
45void
46die(const char* p_text)
47{
48#ifdef DIE_DEBUG
49  bug(p_text);
50#endif
51  vsf_sysutil_exit(1);
52}
53
54void
55die2(const char* p_text1, const char* p_text2)
56{
57  struct mystr die_str = INIT_MYSTR;
58  str_alloc_text(&die_str, p_text1);
59  str_append_text(&die_str, p_text2);
60  die(str_getbuf(&die_str));
61}
62
63void
64bug(const char* p_text)
65{
66  /* Rats. Try and write the reason to the network for diagnostics */
67  vsf_sysutil_activate_noblock(VSFTP_COMMAND_FD);
68  (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, "500 OOPS: ", 10);
69  (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, p_text,
70                                vsf_sysutil_strlen(p_text));
71  (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, "\r\n", 2);
72  vsf_sysutil_exit(1);
73}
74
75void
76vsf_exit(const char* p_text)
77{
78  (void) vsf_sysutil_write_loop(VSFTP_COMMAND_FD, p_text,
79                                vsf_sysutil_strlen(p_text));
80  vsf_sysutil_exit(0);
81}
82
83char *
84local2remote(const char *buf)
85{
86	if (tunable_enable_iconv == 0) return NULL;
87	char *p;
88
89	xfr_buf = (char *)malloc(2048);
90	memset(tmp, 0, 1024);
91	sprintf(tmp, "%s%s_%s", NLS_NVRAM_U2C, tunable_remote_charset, buf);
92	if((p = (char *)nvram_xfr(tmp)) != NULL){
93		strcpy(xfr_buf, p);
94		return xfr_buf;
95	}
96	else
97	{
98		free(xfr_buf);
99		return NULL;
100	}
101}
102
103char *
104remote2local(const char *buf)
105{
106	if (tunable_enable_iconv == 0) return NULL;
107	char *p;
108
109	xfr_buf = (char *)malloc(2048);
110	memset(tmp, 0, 1024);
111	sprintf(tmp, "%s%s_%s", NLS_NVRAM_C2U, tunable_remote_charset, buf);
112	if((p = (char *)nvram_xfr(tmp)) != NULL){
113		strcpy(xfr_buf, p);
114		return xfr_buf;
115	}
116	else
117	{
118		free(xfr_buf);
119		return NULL;
120	}
121}
122