/*
* Copyright (c) 2008-2010 Sinosoft Ltd.
* All rights reserved.
*
* Author: Jianou Zhang
*
*/
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include "public_interface.h"
char *strestr(char *str, char *substr, int sublen)
{
char *pos;
char *dst = NULL;
pos = str;
while (pos = strstr(pos, substr))
{
dst = pos;
pos += sublen;
}
return dst;
}
char *strlstr(char *str, int strlen, char *substr, int sublen)
{
char *pos;
int length;
pos = strstr(str, substr);
if (pos == NULL)
return NULL;
length = pos - str + sublen;
if (length > strlen)
return NULL;
return pos;
}
static unsigned int str_chr(char *s, int c)
{
char ch;
char *t;
ch = c;
t = s;
while (1)
{
if (!*t)
break;
if (*t == ch)
break;
++t;
}
return t - s;
}
int base64_encoding(char *in, int inLen, char *out, int *outlen)
{
unsigned char a, b, c;
int i;
char *s;
int count = 0;
if (inLen == 0)
{
*out = '\0';
return 0;
}
//inLen / 3 * 4 + 4;
s = out;
for (i = 0; i < inLen; i += 3)
{
a = in[i];
b = i + 1 < inLen ? in[i + 1] : 0;
c = i + 2 < inLen ? in[i + 2] : 0;
*s++ = b64alpha[a >> 2];
*s++ = b64alpha[((a & 3 ) << 4) | (b >> 4)];
if (i + 1 >= inLen)
*s++ = B64PAD;
else
*s++ = b64alpha[((b & 15) << 2) | (c >> 6)];
if (i + 2 >= inLen)
*s++ = B64PAD;
else
*s++ = b64alpha[c & 63];
count += 4;
if (count == 128)
{
count = 0;
*s++ = '\r';
*s++ = '\n';
}
}
if (count != 0)
{
*s++ = '\r';
*s++ = '\n';
}
out[s - out]= '\0';
*outlen = strlen(out);
return 0;
}
int base64_decoding(char *in, int inlen, char *out, int *outLen)
{
int i, j;
unsigned char a[4];
unsigned char b[3];
char *s;
if (inlen == 0)
{
*out='\0';
*outLen =0;
return 0;
}
s = out;
for (i = 0; i < inlen; i += 4)
{
for (j = 0; j < 4; j++)
{
if ((i + j) < inlen && in[i + j] != B64PAD)
{
a[j] = str_chr(b64alpha, in[i + j]);
if (a[j] > 63)
return 1;
}
else
{
a[j] = 0;
}
}
b[0] = (a[0] << 2) | (a[1] >> 4);
b[1] = (a[1] << 4) | (a[2] >> 2);
b[2] = (a[2] << 6) | (a[3]);
*s++ = b[0];
if (in[i + 1] == B64PAD)
break;
*s++ = b[1];
if (in[i + 2] == B64PAD)
break;
*s++ = b[2];
}
*outLen = s - out;
while ( *(outLen) && !out[*outLen - 1])
--(*outLen);
return 0;
}
int quoted_encoding(const unsigned char* in, char* out, int inlen, int max_line_length)
{
int outlen = 0;
int line_length = 0;
int i;
for (i = 0; i < inlen; i++, in++)
{
if ((*in >= '!') && (*in <= '~') && (*in != '='))
{
*out++ = (char)*in;
outlen++;
line_length++;
}
else
{
sprintf(out, "=%02X", *in);
out += 3;
outlen += 3;
line_length += 3;
}
if (line_length >= max_line_length - 3)
{
sprintf(out, "=\r\n");
out += 3;
outlen += 3;
line_length = 0;
}
}
*out = '\0';
return outlen;
}
int quoted_decoding(const char* in, unsigned char* out, int inlen)
{
int outlen = 0;
int i = 0;
while (i < inlen)
{
if (strncmp(in, "=\r\n", 3) == 0)
{
in += 3;
i += 3;
}
else
{
if (*in == '=')
{
sscanf(in, "=%02X", out);
out++;
in += 3;
i += 3;
}
else
{
*out++ = (unsigned char)*in++;
i++;
}
outlen++;
}
}
*out = '\0';
return outlen;
}
void write_file(char *buf, int len, int recv, int send)
{
FILE *file;
char buffer[256];
sprintf(buffer, "%d_to_%d_http.log", recv, send);
if ((file = fopen(buffer, "ab+")) == NULL)
{
printf("open file failed\n");
return;
}
fwrite(buf, len, 1, file);
fclose(file);
}
void write_log(char *buf, int len, const char *path)
{
FILE *file;
time_t local_time;
struct tm *current_time = NULL;
char write_time[256] = {0};
if ((file = fopen(path, "ab+")) == NULL)
{
printf("open log file failed\n");
return;
}
time(&local_time);
current_time = localtime(&local_time);
sprintf(
write_time,
"%04d/%02d/%02d %02d:%02d:%02d: ",
current_time->tm_year + 1900,
current_time->tm_mon + 1,
current_time->tm_mday,
current_time->tm_hour,
current_time->tm_min,
current_time->tm_sec);
fwrite(write_time, strlen(write_time), 1, file);
fwrite(buf, len, 1, file);
fclose(file);
}
int read_file(char *dst, char *path, int is_base64)
{
FILE *file;
int length;
char *buffer;
int outlen;
if ((file = fopen(path, "rb")) == NULL)
return 0;
fseek(file, 0, SEEK_END);
length = ftell(file);
fseek(file, 0, SEEK_SET);
if (is_base64)
{
buffer = (char *)malloc(FILE_SIZE);
if (buffer == NULL)
return 0;
fread(buffer, length, 1, file);
base64_encoding(buffer, length, dst, &outlen);
}
else
fread(dst, length, 1, file);
fclose(file);
return 1;
}