'w', 'x', 'y', 'z', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', '+', '/'};
+static char b64_decoding_table[256] = {0};
+
static int b64_mod_table[] = {0, 2, 1};
bool dir_exists(const char *path) {
FILE *f = fopen(fname, "w");
if(f == 0) {
- log_error("fopen failed");
+ log_error("fopen failed\n");
return;
}
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
- buffer = (char*)malloc(sizeof(char) * length);
+ buffer = (char*)malloc(sizeof(char) * (length + 1));
if(buffer) {
fread(buffer, 1, length, f);
}
fclose(f);
}
+ buffer[length] = 0;
return buffer;
}
long int useconds;
gettimeofday(&tv, 0);
- useconds = t*1000 + tv.tv_usec; //add the microseconds to the seconds
+ useconds = t*1000000 + tv.tv_usec; //add the microseconds to the seconds
return useconds;
}
}
freeaddrinfo(res);
- if(returnStatus == 0) {
- close(simpleSocket);
+ close(simpleSocket);
+ if(returnStatus == 0) {
return true;
}
return false;
}
-char *b64_encode(const unsigned char *data, size_t input_length) {
+char *b64_encode(const uint8_t *data, size_t input_length) {
assert(data);
assert(input_length);
return encoded_data;
}
+uint8_t *b64_decode(const char *data, size_t input_length, size_t *output_length) {
+ assert(data);
+ assert(input_length);
+ assert(output_length);
+
+ int i, j;
+
+ //one time compute decoding table
+ if(b64_decoding_table['A'] == 0) {
+ for(i = 0; i < 64; i++) {
+ b64_decoding_table[(unsigned char)b64_encoding_table[i]] = i;
+ }
+ }
+
+ if(input_length % 4 != 0) {
+ return 0;
+ }
+
+ *output_length = input_length / 4 * 3;
+ if(data[input_length - 1] == '=') {
+ (*output_length )--;
+ }
+ if(data[input_length - 2] == '=') {
+ (*output_length )--;
+ }
+
+ uint8_t *decoded_data = (uint8_t*)malloc(*output_length + 1);
+ if(decoded_data == 0) {
+ return 0;
+ }
+
+ for(i = 0, j = 0; i < input_length;) {
+ uint32_t sextet_a = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
+ uint32_t sextet_b = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
+ uint32_t sextet_c = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
+ uint32_t sextet_d = data[i] == '=' ? 0 & i++ : b64_decoding_table[(int)data[i++]];
+ uint32_t triple = ( sextet_a << 3 * 6 ) + ( sextet_b << 2 * 6 ) + ( sextet_c << 1 * 6 ) + ( sextet_d << 0 * 6 );
+
+ if(j < *output_length) {
+ decoded_data[j++] = (triple >> 2 * 8) & 0xFF;
+ }
+
+ if(j < *output_length) {
+ decoded_data[j++] = (triple >> 1 * 8) & 0xFF;
+ }
+
+ if(j < *output_length) {
+ decoded_data[j++] = (triple >> 0 * 8) & 0xFF;
+ }
+ }
+
+ return decoded_data;
+}
+
char *str_replace(const char *orig, const char *rep, const char *with) {
assert(orig);
assert(rep);
fp = fopen(filename, "r");
if(fp == 0) {
- log_error("could not open file %s", filename);
+ log_error("could not open file %s\n", filename);
return 0;
}
if(key_string) {
key_string = (char *)realloc(key_string, strlen(key_string) + read + 1);
if(key_string == 0) {
- log_error("bad allocation");
+ log_error("bad allocation\n");
free(line);
return 0;
}
else {
key_string = strdup(line);
if(key_string == 0) {
- log_error("bad allocation");
+ log_error("bad allocation\n");
free(line);
return 0;
}
return key_string;
}
+
+void vsftp_daemon_init(void) {
+ system("/usr/sbin/vsftpd &");
+}
+
+void vsftp_daemon_deinit(void) {
+ system("killall -9 vsftpd");
+}
+
+void sftp_daemon_init(void) {
+ system("/usr/sbin/sshd -D &");
+}
+
+void sftp_daemon_deinit(void) {
+ system("killall -9 sshd");
+}