Esempio n. 1
0
PCS_API char *pcs_http_build_post_data_v(PcsHttp handle, va_list args)
{
	struct pcs_http *http = (struct pcs_http *)handle;
	char *name, *val, 
		*escapval, *res = NULL, *p;
	int ressz;

	while((name = va_arg(args, char *)) != NULL) {
		val = va_arg(args, char *);
		if (name[0] == '\0') continue;
		escapval = curl_easy_escape(http->curl, val, 0);

		if (!res) {
			ressz = strlen(name) + strlen(escapval) + 1;
			res = (char *)pcs_malloc(ressz + 1);
			if (!res) {
				return NULL;
			}
			strcpy(res, name);
			strcat(res, "=");
			strcat(res, escapval);
		}
		else {
			ressz += strlen(name) + strlen(escapval) + 2;
			p = (char *)pcs_malloc(ressz + 1);
			if (!p) {
				pcs_free(res);
				return NULL;
			}
			strcpy(p, res);
			pcs_free(res);
			res = p;
			strcat(res, "&");
			strcat(res, name);
			strcat(res, "=");
			strcat(res, escapval);
		}
		curl_free((void *)escapval);
	}

	return res;
}
Esempio n. 2
0
PCS_API char *pcs_utils_vsprintf(const char *fmt, va_list ap)
{
    int cnt, sz=0;
    char *buf;
	va_list ap_try;

    sz = 128;
    buf = (char*)pcs_malloc(sz);
	va_copy(ap_try, ap);
	cnt = vsnprintf(buf, sz, fmt, ap_try);
	va_end(ap_try);
    if (cnt >= sz) {
		pcs_free(buf);
        buf = (char*)pcs_malloc(cnt + 1);
        sz = cnt + 1;
		cnt = vsnprintf(buf, sz, fmt, ap);
    }
	buf[cnt] = '\0';
	return buf;
}
Esempio n. 3
0
static HashtableNode *node_create(const char *key, int key_size, void *value, unsigned int hashA, unsigned int hashB)
{
	HashtableNode *node;
	node = (HashtableNode *) pcs_malloc(sizeof(HashtableNode));
	if (!node)
		return NULL;
	memset (node, 0, sizeof(HashtableNode));
	if (key_size == -1) key_size = strlen(key);
	node->key = (char *)pcs_malloc(key_size + 1);
	if (!node->key) {
		pcs_free(node);
		return NULL;
	}
	memcpy(node->key, key, key_size);
	node->key[key_size] = '\0';
	node->value = value;
	node->hashA = hashA;
	node->hashB = hashB;
	return node;
}
Esempio n. 4
0
HashtableIterater *ht_it_create(Hashtable *ht)
{
	HashtableIterater *iterater;
	iterater = (HashtableIterater *) pcs_malloc(sizeof(HashtableIterater));
	if (!iterater)
		return NULL;
	memset(iterater, 0, sizeof(HashtableIterater));
	iterater->ht = ht;
	iterater->index = -1;
	return iterater;
}
Esempio n. 5
0
PCS_API char *pcs_utils_strdup(const char *str)
{
	char *res = 0;
	if (str) {
		res = (char *)pcs_malloc(strlen(str) + 1);
		if (!res)
			return 0;
		strcpy(res, str);
	}
	return res;
}
Esempio n. 6
0
File: dir.c Progetto: yaoey/baidupcs
my_dirent *create_dirent(const char *basepath, const char *filename, int is_dir, time_t mtime)
{
	my_dirent *ent;
	ent = (my_dirent *)pcs_malloc(sizeof(my_dirent));
	if (!ent)
		return NULL;
	ent->path = (char *)pcs_malloc(strlen(basepath) + strlen(filename) + 2);
	if (!ent->path) {
		pcs_free(ent);
		return NULL;
	}
	strcpy(ent->path, basepath);
	strcat(ent->path, "\\");
	strcat(ent->path, filename);
	ent->filename = ent->path + strlen(basepath) + 1;
	ent->is_dir = is_dir;
	ent->mtime = mtime;
	ent->user_flag = 0;
	ent->next = NULL;
	return ent;
}
Esempio n. 7
0
Hashtable *ht_create(int capacity, int ignore_case, void (*free_value)(void *))
{
	Hashtable *ht;
	ht = (Hashtable *) pcs_malloc(sizeof(Hashtable));
	if (!ht)
		return NULL;
	memset (ht, 0, sizeof(Hashtable));
	if (capacity < 17) capacity = 17;
	ht->capacity = capacity;
	ht->real_capacity = (int)(ht->capacity * HASH_EXTEND_MULTIPLIER);
	ht->count = 0;
	ht->free_value = free_value;
	ht->table = (HashtableNode **)pcs_malloc(ht->real_capacity * sizeof(HashtableNode *));
	if (!ht->table) {
		pcs_free(ht);
		return NULL;
	}
	memset (ht->table, 0, ht->real_capacity * sizeof(HashtableNode *));
	ht->ignore_case = ignore_case;
	return ht;
}
Esempio n. 8
0
static inline char *i_strdup(const char *str, int len)
{
	char *res = 0;
	if (str) {
		if (len == -1) len = strlen(str);
		res = (char *)pcs_malloc(len + 1);
		if (!res) return 0;
		memcpy(res, str, len);
		res[len] = '\0';
	}
	return res;
}
Esempio n. 9
0
PCS_API char *pcs_utils_vsprintf(const char *fmt, va_list ap)
{
    int cnt, sz=0;
    char *buf;
	va_list ap_try;

    sz = 1024;
    buf = (char*)pcs_malloc(sz);
try_print:
	va_copy(ap_try, ap);
	cnt = vsnprintf(buf, sz - 1, fmt, ap_try);
	va_end(ap_try);
	if (cnt > sz) {
		pcs_free(buf);
        sz *= 2;
		buf = (char*)pcs_malloc(sz);
		goto try_print;
    }
	if (cnt < 0) return NULL;
	buf[cnt] = '\0';
	return buf;
}
Esempio n. 10
0
/*
* 合并unix格式的路径,如果filename传入的是绝对路径,则直接返回filename的拷贝。
* 使用完后,需调用pcs_free来释放返回值
*/
char *combin_net_disk_path(const char *base, const char *filename)
{
	char *result;
	int basesz = 0, filenamesz = 0, sz;

	if (base) basesz = strlen(base);
	if (filename) filenamesz = strlen(filename);
	sz = basesz + filenamesz + 2;
	result = (char *)pcs_malloc(sz + 1);
	result[0] = '\0';
	fill_unix_true_path_to_buf(result, base);
	fill_unix_true_path_to_buf(result, filename);
	return result;
}
Esempio n. 11
0
File: pcs.c Progetto: imzjy/baidupcs
PCS_API Pcs pcs_create(const char *cookie_file)
{
	struct pcs *pcs;

	pcs = (struct pcs *) pcs_malloc(sizeof(struct pcs));
	if (!pcs)
		return NULL;
	memset(pcs, 0, sizeof(struct pcs));
	pcs->http = pcs_http_create(cookie_file);
	if (!pcs->http) {
		pcs_free(pcs);
		return NULL;
	}
	return pcs;
}
Esempio n. 12
0
static inline char *pcs_http_append_bytes(char *dest, int sz, char *src, int srcsz)
{
	char *p;

	p = (char *) pcs_malloc(sz + srcsz + 1);
	if (!p)
		return NULL;
	if (dest) {
		memcpy(p, dest, sz);
		pcs_free(dest);
	}
	memcpy(&p[sz], src, srcsz);
	p[sz + srcsz] = '\0';
	return p;
}
Esempio n. 13
0
static inline char *read_cookie_attr(char **p)
{
	char *start = *p,
		*res = 0;
	while (**p != '\t' && **p != '\0') 
		(*p)++;
	if (*p > start) {
		res = (char *)pcs_malloc(*p - start + 1);
		if (res) {
			memcpy(res, start, *p - start);
			res[*p - start] = '\0';
		}
		if (**p) (*p)++;
	}
	return res;
}
Esempio n. 14
0
PCS_API PcsSList *pcs_slist_create_ex(const char *src, int len)
{
	PcsSList *res = pcs_slist_create();
	if (res) {
		if (len == -1) len = strlen(src);
		res->string = (char *)pcs_malloc(len + 1);
		if (res->string) {
			memcpy(res->string, src, len);
			res->string[len] = '\0';
		}
		else {
			pcs_free(res);
			res = 0;
		}
	}
	return res;
}
Esempio n. 15
0
/*
* 读取全部文件内容
* file    - 待读取的文件
* pBuffer - 文件的内容所在的内存指针将存入pBuffer指定的内存中
* 返回读取到的字节大小。使用完成后,需调用pcs_free(*pBuffer)
*/
int read_file(const char *file, char **pBuffer)
{
	FILE *fp;
	long size_of_file, sz;
	char *content;

	fp = fopen(file, "rb");
	if (!fp) {
		//printf("Open file fail: %s\n", file);
		return -1;
	}
	fseek(fp, 0L, SEEK_END);
	size_of_file = ftell(fp);
	if (size_of_file < 3) {
		printf("Wrong file size: Size=%ld, %s\n", size_of_file, file);
		fclose(fp);
		return -1;
	}
	fseek(fp, 0L, SEEK_SET);
	content = (char *)pcs_malloc(size_of_file + 1);
	sz = fread(content, 1, size_of_file, fp);
	fclose(fp);
	if (sz != size_of_file) {
		printf("Read file error: %s\n", file);
		pcs_free(content);
		return -1;
	}
	content[size_of_file] = '\0';
	if ((((int)content[0]) & 0xEF) == 0xEF) {
		if ((((int)content[1]) & 0xBB) == 0xBB) {
			if ((((int)content[2]) & 0xBF) == 0xBF) {
				content[0] = content[1] = content[2] = ' ';
			}
			else {
				printf("Wrong UTF-8 BOM: BOM=0x%2X%2X%2X %s\n", content[0] & 0xFF, content[1] & 0xFF, content[2] & 0xFF, file);
				return -1;
			}
		}
		else {
			printf("Wrong UTF-8 BOM: BOM=0x%2X%2X%2X %s\n", content[0] & 0xFF, content[1] & 0xFF, content[2] & 0xFF, file);
			return -1;
		}
	}
	*pBuffer = content;
	return size_of_file;
}
Esempio n. 16
0
static char *combin_remote_path(const char *base, const char *filename)
{
	char *p;
	size_t basesz, filenamesz, sz;

	basesz = strlen(base);
	filenamesz = strlen(filename);
	sz = basesz + filenamesz + 1;
	p = (char *)pcs_malloc(sz + 1);
	if (!p)
		return NULL;
	memset(p, 0, sz + 1);
	strcpy(p, base);
	if (base[basesz - 1] != '/' && filename[0] != '/')
		strcat(p, "/");
	strcat(p, filename);
	return p;
}
Esempio n. 17
0
static char *combin_path(const char *base, const char *filename, const char **pName)
{
	char *p;
	int sz_base;

	sz_base = strlen(base);
	p = (char *)pcs_malloc(sz_base + strlen(filename) + 2);
	strcpy(p, base);
#ifdef WIN32
	p[sz_base] = '\\';
#else
	p[sz_base] = '/';
#endif
	sz_base++;
	p[sz_base] = '\0';
	if (pName) (*pName) = &p[sz_base];
	strcat(p, filename);
	return p;
}
Esempio n. 18
0
File: pcs.c Progetto: imzjy/baidupcs
/*根据slist字符串链表,构造成数组格式的json字符串,并把数组元素数量写入到count指定的内存中*/
static char *pcs_build_filelist_2(Pcs handle, PcsSList2 *slist, int *count)
{
	PcsSList2 *item;
	char *file_list_string, *p;
	size_t sz;
	int cnt = 0;

	if (count) *count = cnt;
	if (!slist)
		return NULL;

	sz = 0;
	item = slist;
	while(item) {
		sz += strlen(item->string1);
		sz += strlen(item->string2);
		sz += 25;
		item = item->next;
		cnt++;
	}
	sz += 2;
	if (count) *count = cnt;

	file_list_string = (char *) pcs_malloc(sz);
	if (!file_list_string) {
		pcs_set_errmsg(handle, "Can't alloc memory: size=0x%x", sz);
		return NULL;
	}

	strcpy(file_list_string, "[");
	p = file_list_string;
	item = slist;
	while(item) {
		while(*p) p++;
		sprintf(p, "{\"path\":\"%s\",\"newname\":\"%s\"},", item->string1, item->string2);
		p += 25;
		item = item->next;
	}
	file_list_string[sz - 2] = ']';
	file_list_string[sz - 1] = '\0';

	return file_list_string;
}
Esempio n. 19
0
/*
* 获取路径的父路径,如果没有父路径则返回NULL。
*   path  - 当前路径
*   len   - path的字节长度,如果传入-1,则内部使用strlen()获取其长度
* 返回值需要调用pcs_free()
*/
char *base_dir(const char *path, int len)
{
	char *dir, *p;
	if (!path) return NULL;
	if (len == -1) len = strlen(path);
	if (len == 0) return NULL;
	dir = (char *)pcs_malloc(len + 1);
	strcpy(dir, path);
	p = dir + len - 1;
	while (p > dir && *p != '/' && *p != '\\') p--;
	if (p == dir) {
		if (*p != '/' && *p != '\\') {
			pcs_free(dir);
			return NULL;
		}
		p[1] = '\0';
	}
	*p = '\0';
	return dir;
}
Esempio n. 20
0
PCS_API PcsFileInfo *pcs_fileinfo_clone(PcsFileInfo *fi)
{
	PcsFileInfo *res = pcs_fileinfo_create();
	if (!res) return 0;
	res->fs_id = fi->fs_id;
	res->path = pcs_utils_strdup(fi->path);
	res->server_filename = pcs_utils_strdup(fi->server_filename);
	res->server_ctime = fi->server_ctime;
	res->server_mtime = fi->server_mtime;
	res->local_ctime = fi->local_ctime;
	res->local_mtime = fi->local_mtime;
	res->size = fi->size;
	res->category = fi->category;
	res->isdir = fi->isdir;
	res->dir_empty = fi->dir_empty;
	res->empty = fi->empty;
	res->md5 = pcs_utils_strdup(fi->md5);
	res->dlink = pcs_utils_strdup(fi->dlink);
	if (fi->block_list) {
		char **p = fi->block_list;
		int i = 0;
		while(*p) {
			i++;
			p++;
		}
		res->block_list = (char **)pcs_malloc((i + 1) * sizeof(char *));
		if (res->block_list) {
			i = 0;
			p = fi->block_list;
			while(*p) {
				res->block_list[i++] = pcs_utils_strdup(*p);
				p++;
			}
		}
	}
	res->ifhassubdir = fi->ifhassubdir;
	if (fi->thumbs) {
        res->thumbs = pcs_slist2_clone(fi->thumbs);
    }
    return res;
}
Esempio n. 21
0
PCS_API char *pcs_utils_filename(const char *path)
{
	char *name;
	const char *p;
	int i;
	i = strlen(path);
	p = path + i - 1;
	while(p > path) {
		if (*p == '/' || *p == '\\')
			break;
		p--;
	}
	if (*p == '/' || *p == '\\')
		p++;
	i = strlen(p);
	name = (char *) pcs_malloc(i + 1);
	if (!name)
		return NULL;
	strcpy(name, p);
	return name;
}
Esempio n. 22
0
File: pcs.c Progetto: imzjy/baidupcs
void pcs_cat_errmsg(Pcs handle, const char *fmt, ...)
{
	struct pcs *pcs = (struct pcs *)handle;
	char *p, *errmsg;
	size_t sz = 0;
	va_list args;
	va_start(args, fmt);
	errmsg = pcs_utils_vsprintf(fmt, args);
	va_end(args);
	if (pcs->errmsg) {
		sz = strlen(pcs->errmsg) + strlen(errmsg);
		p = (char *)pcs_malloc(sz + 1);
		strcpy(p, pcs->errmsg);
		pcs_free(pcs->errmsg);
		pcs->errmsg = p;
		strcat(p, errmsg);
	}
	else {
		pcs->errmsg = errmsg;
	}
}
Esempio n. 23
0
File: pcs.c Progetto: imzjy/baidupcs
/*根据slist字符串链表,构造成数组格式的json字符串,并把数组元素数量写入到count指定的内存中*/
static char *pcs_build_filelist_1(Pcs handle, PcsSList *slist, int *count)
{
	PcsSList *item;
	char *file_list_string;
	size_t sz;
	int cnt = 0;

	if (count) *count = cnt;
	if (!slist)
		return NULL;

	sz = 0;
	item = slist;
	while(item) {
		sz += strlen(item->string);
		sz += 3;
		item = item->next;
		cnt++;
	}
	sz += 2;
	if (count) *count = cnt;

	file_list_string = (char *) pcs_malloc(sz);
	if (!file_list_string) {
		pcs_set_errmsg(handle, "Can't alloc memory: size=0x%x", sz);
		return NULL;
	}

	strcpy(file_list_string, "[");
	item = slist;
	while(item) {
		strcat(file_list_string, "\"");
		strcat(file_list_string, item->string);
		strcat(file_list_string, "\",");
		item = item->next;
	}
	file_list_string[sz - 2] = ']';
	file_list_string[sz - 1] = '\0';
	return file_list_string;
}
Esempio n. 24
0
static error_t add_arg(struct params *params, char *arg)
{
	char **p;
	int i;
	p = (char **) pcs_malloc((params->args_count + 1) * sizeof(char *));
	if (!p) {
		print_arg_err("alloc memory failed\n");
		params->is_fail = PcsTrue;
		return EINVAL;
	}
	if (params->args_count > 0) {
		for (i = 0; i < params->args_count; i++) {
			p[i] = params->args[i];
		}
		pcs_free(params->args);
	}
	params->args = p;
	p = &params->args[params->args_count];
	params->args_count++;
	*p = pcs_utils_strdup(arg);
	return 0;
}
Esempio n. 25
0
static char *combin_local_path(const char *base, const char *filename)
{
	char *p;
	size_t basesz, filenamesz, sz;

	basesz = strlen(base);
	filenamesz = strlen(filename);
	sz = basesz + filenamesz + 1;
	p = (char *)pcs_malloc(sz + 1);
	if (!p)
		return NULL;
	memset(p, 0, sz + 1);
	strcpy(p, base);
#if defined(WIN32)
	if (base[basesz - 1] != '\\' && filename[0] != '\\')
		strcat(p, "\\");
#else
	if (base[basesz - 1] != '/' && filename[0] != '/')
		strcat(p, "/");
#endif
	strcat(p, filename);
	return p;
}
Esempio n. 26
0
static LocalFileInfo *CreateLocalFileInfo(const char *parentPath, const char *filename, int isdir, time_t mtime, size_t size, LocalFileInfo *parent)
{
	LocalFileInfo *info;
	info = (LocalFileInfo *)pcs_malloc(sizeof(LocalFileInfo));
	if (!parentPath || !parentPath[0]) {
		if (filename && filename[0])
			info->path = filename_dup(filename, &info->filename);
		else 
			info->filename = info->path = NULL;
	}
	else {
		if (filename && filename[0])
			info->path = combin_path(parentPath, filename, &info->filename);
		else
			info->path = path_dup(parentPath, &info->filename);
	}
	info->isdir = isdir;
	info->mtime = mtime;
	info->size = size;
	info->parent = parent;
	info->next = NULL;
	info->userdata = NULL;
	return info;
}
Esempio n. 27
0
static inline char *pcs_http_get_charset_from_header(const char *header, int size)
{
	char *res = NULL;
	const char *p = header, *key = "charset",
		*end = NULL,
		*tmp;
	int i = 7;//strlen(key) = 7;
	while (*p) {
		if (*p == key[0] && pcs_utils_streq(p, key, i)) {
			tmp = p + i;
			if (*tmp != '=') continue; tmp++;
			end = tmp;
			while (*end && PCS_IS_TOKEN_CHAR(*end)) end++;
			if (end > tmp) {
				res = (char *)pcs_malloc(end - tmp + 1);
				memcpy(res, tmp, end - tmp);
				res[end - tmp] = '\0';
				break;
			}
		}
		p++;
	}
	return res;
}
Esempio n. 28
0
PCS_API char *pcs_utils_basedir(const char *path)
{
	char *dir, *p;
	int i;
	i = strlen(path);
	dir = (char *) pcs_malloc(i + 1);
	if (!dir)
		return NULL;
	strcpy(dir, path);
	p = dir + i - 1;
	while(p > dir) {
		if (*p == '/' || *p == '\\')
			break;
		p--;
	}
	if (p == dir) {
		*p = '/';
		p++;
		*p = '\0';
	} else {
		*p = '\0';
	}
	return dir;
}
Esempio n. 29
0
File: pcs.c Progetto: imzjy/baidupcs
/*从URL地址的QueryString中解析出类似于 &token=abc_ef-g&a= 的值。此例中,key传入"&token",将返回abc_ef-g*/
static char *pcs_get_embed_query_token_by_key(const char *html, const char *key)
{
	char *val = NULL;
	const char *p = html,
		*end = NULL,
		*tmp;
	int i = strlen(key);// \f\n\r\t\v
	while (*p) {
		if (*p == key[0] && pcs_utils_streq(p, key, i)) {
			tmp = p + i;
			if (*tmp != '=') continue; tmp++;
			end = tmp;
			while (*end && PCS_IS_TOKEN_CHAR(*end)) end++;
			if (end > tmp) {
				val = (char *)pcs_malloc(end - tmp + 1);
				memcpy(val, tmp, end - tmp);
				val[end - tmp] = '\0';
				return val;
			}
		}
		p++;
	}
	return val;
}
Esempio n. 30
0
PCS_API char *pcs_http_build_url_v(PcsHttp handle, const char *url, va_list args)
{
	struct pcs_http *http = (struct pcs_http *)handle;
	char *name, *val,
		*escapval, 
		*res = NULL, *p;
	int ressz, 
		ampersand = 0;

	ressz = strlen(url) + 1;
	res = (char *)pcs_malloc(ressz + 1);
	if (!res) return NULL;
	strcpy(res, url);

	p = res;
	while(*p) {
		if (*p == '?')
			break;
		p++;
	}
	if (*p) { // find the '?'
		ressz--;
		p++;
	}
	else { // not find the '?', so add '?' at end
		*p = '?';
		p++;
		*p = '\0';
	}
	if (*p) { // have char after '?'
		while(*p) p++; // find the end of url
		p--; // *p is the last char
		if (*p != '&') { // last char is not '&', so the next param need add '&' in the front
			ampersand = 1;
		}
	}

	while((name = va_arg(args, char *)) != NULL) {
		val = va_arg(args, char *);
		if (name[0] == '\0') continue;
		if (!val) {
			ressz += strlen(name) + 1;
			p = (char *)pcs_malloc(ressz + 1);
			if (!p) {
				pcs_free(res);
				return NULL;
			}
			strcpy(p, res);
			pcs_free(res);
			res = p;
			if (ampersand) {
				strcat(res, "&");
			}
			else {
				ampersand = 1;
				ressz--;
			}
			strcat(res, name);
			continue;
		}
		escapval = curl_easy_escape(http->curl, val, 0);

		ressz += strlen(name) + strlen(escapval) + 2;
		p = (char *)pcs_malloc(ressz + 1);
		if (!p) {
			pcs_free(res);
			return NULL;
		}
		strcpy(p, res);
		pcs_free(res);
		res = p;
		if (ampersand) {
			strcat(res, "&");
		}
		else {
			ampersand = 1;
			ressz--;
		}
		strcat(res, name);
		strcat(res, "=");
		strcat(res, escapval);

		curl_free((void *)escapval);
	}

	return res;
}