Exemplo n.º 1
0
void TestShow::miniPrintWriteUnicode(int fd,char *pwchello)
{
    //know the detail of DWORD and UCHAR
    DWORD dwWrote = 0;
    char szPrint[500] = {0};
    if(fd==-1) printf("error!\n");
    char szTmp[500] = {0};
    memset(szPrint, 0, sizeof(szPrint));
    szPrint[0] = 0x1c;
    szPrint[1] = 0x26;
    write(fd,szPrint,2);
    dwWrote = 0;

    memset(szPrint, 0, sizeof(szPrint));
    szPrint[0] = 0x1b;
    szPrint[1] = 0x38;
    szPrint[2] = 0x3;
    write(fd,szPrint,3);

    memset(szPrint, 0, sizeof(szPrint));
    memset(szTmp, 0, sizeof(szTmp));
    int i=1;
    i=u2g(pwchello,500,szTmp,sizeof(szTmp));//Here DID not work

    sprintf(szPrint, "%s", szTmp);
    write(fd,szPrint,sizeof(szPrint));
    memset(szPrint, 0, sizeof(szPrint));
    szPrint[0] = 0x1c;
    szPrint[1] = 0x2e;
    write(fd,szPrint,2);
}
Exemplo n.º 2
0
/***********************************************
 *  生成XML的字符串                            *
 *  成功返回XML的字符串                        *
 *  失败则返回NULL                             *
 *  doc用完已自动清空                          *
 ***********************************************/
byte * generateXmlBuffer(xmlDocPtr doc)
{
	static byte szBuf[2048];
	byte * buffer;
	xmlBufferPtr buf = NULL;
    	xmlSaveCtxtPtr ctxt = NULL;
	LIBXML_TEST_VERSION;
	if(doc == NULL) {
		ErrorLog(ERROR, "doc is null");
		printf("doc is null");
		return NULL;
	}

	buf = xmlBufferCreate();
    	ctxt = xmlSaveToBuffer(buf, "UTF-8", 1);

    	xmlSaveDoc(ctxt, doc);
    	xmlSaveClose(ctxt);
	
	memset(szBuf, 0x00, sizeof(szBuf));
	buffer = u2g(buf->content);
	sprintf(szBuf, "%s",buffer );
	
	buf->content = NULL;
    	xmlBufferFree(buf);

    	xmlFreeDoc(doc);
    	xmlCleanupParser();
	
	free(buffer);
	return szBuf;
}
Exemplo n.º 3
0
int sendDir(int fSock, const char* fullpath, int fileSize, int fileType)
{
  char strtmp[FILENAME], strformat[50], strdata[RECFRG], fileName[FILENAME];
  int tmp, headLen, packetNo;
  FILE *sf;

  if (getFileName(strtmp, fullpath, sizeof(strtmp)) < 0)
  {
    printf("\nFilename is too long.\n");
    return -1;
  }

  addColon(strtmp, sizeof(strtmp));
  if (utf8)
    u2g(strtmp, sizeof(strtmp),
        fileName, sizeof(fileName));
  else strncpy(fileName, strtmp, sizeof(fileName));

  headLen = (strlen(fileName)+1) + (HL_HEADERSIZE+1) +
    (HL_FILETYPE+1) + (HL_FILESIZE+1) + 2*(HL_1416+1);
  packetNo = (unsigned int)time(NULL); //简化了,属性值并不准确
  snprintf(strformat, sizeof(strformat), "%%0%dx:%%s:%%0%dx:%%0%dx:14=%%0%dx:16=%%0%dx:",
           HL_HEADERSIZE, HL_FILESIZE, HL_FILETYPE, HL_1416-3, HL_1416-3);

  tmp = snprintf(strdata, sizeof(strdata), strformat,
           headLen, fileName, fileSize, fileType, packetNo, packetNo);

  switch (fileType)
  {
  case 1:
    if ((sf = fopen(fullpath, "r")) == NULL)
    {
      printf("file open error.\n");
      return -1;
    }
    if (writen(fSock, strdata, tmp)<0)
      return -1;
    while ((tmp = fread(strdata, 1, RECFRG, sf))>0)
    {
      if (writen(fSock, strdata, tmp)<0)
        return -1;
    }
    fclose(sf);
    break;
  case 2:

    //break;
  case 3:
    if (writen(fSock, strdata, tmp)<0)
      return -1;
    break;
  default:
    break;
  }
  return 0;
}
Exemplo n.º 4
0
/***********************************************
 *  从doc中查找和keyName匹配的结点             *
 *  成功返回匹配结点的值                       *
 *  失败则返回空字符串                         *
 *  doc用完后请手动free                        *
 ***********************************************/
byte * fetchValueByKey(xmlDocPtr doc, byte * keyName)
{
	xmlNodePtr curNode = NULL;	/* node pointer */
	xmlNodePtr saveNode = NULL;
	xmlChar *szValue;
	byte * buf = NULL;
	static byte outValue[256];
	
	curNode = xmlDocGetRootElement(doc);
	if (curNode == NULL) {
                //xmlFreeDoc(doc);
		ErrorLog(ERROR, "open doc fail, doc content is empty");
		printf("open doc fail, doc content is empty");
		return NULL;
	}

	curNode = curNode->xmlChildrenNode;
	if (curNode == NULL) {
                //xmlFreeDoc(doc);
		ErrorLog(ERROR, "can not find child node");
		printf("can not find child node");
		return NULL;
	}

	/* if has child node, do while */
	while (curNode != NULL) {
		if(curNode->type == XML_ELEMENT_NODE) {
			saveNode = curNode;
			curNode = curNode->xmlChildrenNode;
			while(curNode != NULL) {
				if(!xmlStrcmp(curNode->name, BAD_CAST keyName)) {
					szValue = xmlNodeGetContent(curNode);
                                        if ( strlen(szValue) > 1 )
					    buf = u2g(szValue);
                                        else
                                            buf = (byte *)szValue;
					memset(outValue, 0x00, sizeof(outValue));
					strcpy(outValue, buf);
					xmlFree(szValue);
					goto BREAK_ALL;
				}
				curNode = curNode->next;
			}
			curNode = saveNode;
		} 
		curNode = curNode->next;
	}
BREAK_ALL:
	
	//if(buf != NULL)
	//	free(buf);
	//xmlFreeDoc(doc);	
	//xmlCleanupParser();
	return outValue;
}
Exemplo n.º 5
0
// write it to file
void write_all_notice_to_xmlfile(char *filename, p_notice_element head)
{
#ifdef WIN32
	char gbk_filename[BUFFER_MAX_SIZE];
	u2g(filename,strlen(filename), gbk_filename, BUFFER_MAX_SIZE);
	msdosify(gbk_filename);
#endif
	xmlDocPtr doc = NULL;
	xmlNodePtr root_node = NULL, notice = NULL;
	doc = xmlNewDoc(BAD_CAST "1.0");
	root_node = xmlNewNode(NULL, BAD_CAST "root");
	xmlDocSetRootElement(doc, root_node);

	char number_buf[8];
	p_notice_element p = head->next;
	while(p)
	{
		xmlNodePtr notice_id = NULL, notice_publisher = NULL, notice_time = NULL,notice_title = NULL ,notice_content = NULL;
		sprintf(number_buf,"%d",p->notice_id);
		notice = xmlNewChild(root_node, NULL, BAD_CAST "notice", BAD_CAST "");
		xmlNewProp(notice, BAD_CAST "isHighLight", p->is_highlight == TRUE? BAD_CAST "true":BAD_CAST "false");
		notice_id = xmlNewChild(notice, NULL ,BAD_CAST "id" , BAD_CAST number_buf);
		notice_publisher = xmlNewChild(notice, NULL ,BAD_CAST "publisher" , BAD_CAST p->notice_publisher);
		notice_time = xmlNewChild(notice, NULL ,BAD_CAST "time" , BAD_CAST p->notice_time);
		notice_title = xmlNewChild(notice, NULL ,BAD_CAST "title" , BAD_CAST p->notice_title);
		notice_content = xmlNewChild(notice, NULL ,BAD_CAST "content" , BAD_CAST p->notice_body);
		p = p->next;
	}
#ifdef WIN32
	xmlSaveFormatFileEnc(gbk_filename, doc, "UTF-8", 1);
#endif
#ifndef WIN32
	xmlSaveFormatFileEnc(filename, doc, "UTF-8", 1);
#endif

	/*free the document */
	xmlFreeDoc(doc);

	xmlCleanupParser();

	xmlMemoryDump();//debug memory for regression tests*/
}
Exemplo n.º 6
0
int UTF8ToGBK(unsigned char * lpUTF8Str,unsigned char ** lpGBKStr)
{
#ifdef _WIN32
	int nRetLen = 0;
	nRetLen = WTC(lpUTF8Str,NULL,0);
	(* lpGBKStr) = (unsigned char *)malloc((nRetLen + 1)*sizeof(char));
	if((* lpGBKStr) == NULL)
		return 0;
	nRetLen = WTC(lpUTF8Str,(* lpGBKStr),nRetLen);
	return nRetLen;
#endif


#ifdef __LINUX__
	int nRetLen = strlen((char *)lpUTF8Str) + 1;
	* lpGBKStr = (unsigned char *)malloc(nRetLen);
	return u2g((char *)lpUTF8Str,strlen((char *)lpUTF8Str),(char *)*lpGBKStr,nRetLen);
#endif
	return 0;
}
Exemplo n.º 7
0
int main(int argc, char **argv)
{

	if(argc<3)
	{
		printf("conv3 1/0 xxx\n");
		printf("argv[2] 0 = u2g \n");
		printf("argv[2] 1 = g2u \n");
		return -1;
	}
	//char *in_utf8[6] =   {"01 26涓","02 瀹跺涵鎴愬憳", "03 姘存灉","04 鏁板瓧1鍒","05 浜斿畼" , "06 中文"};
	
	char out[OUTLEN];
	int rec ;

	if(atoi(argv[1])==0)
	{
		//unicode码转为gb2312码
		for(int i=2;i<argc;i++)
		{
			//rec = u2g(in_utf8[i],strlen(in_utf8[i]),out,OUTLEN);
			rec = u2g(argv[i],255,out,OUTLEN);			
			printf("unicode-->gb2312 in=%s out=%s\n",argv[i],out);
		}
	}
	else if(atoi(argv[1])==1)
	{
		//gb2312码转为unicode码
		for(int i=2;i<argc;i++)
		{
			//rec = u2g(in_utf8[i],strlen(in_utf8[i]),out,OUTLEN);
	
			rec = g2u(argv[i],255,out,OUTLEN);
			printf("gb2312-->unicode in=%s out=%s\n",argv[i],out);
		}
	}
	
	return 0;
}
Exemplo n.º 8
0
static int api_mail_do_post(ONION_FUNC_PROTO_STR, int mode)
{
	const char * userid = onion_request_get_query(req, "userid");
	const char * appkey = onion_request_get_query(req, "appkey");
	const char * sessid = onion_request_get_query(req, "sessid");
	const char * token = onion_request_get_query(req, "token");
	const char * to_userid = onion_request_get_query(req, "to_userid");
	const char * title = onion_request_get_query(req, "title");
	const char * backup = onion_request_get_query(req, "backup");

	if(!userid || !appkey || !sessid || !title || !to_userid || !token)
		return api_error(p, req, res, API_RT_WRONGPARAM);

	struct userec *ue = getuser(userid);
	if(!ue)
		return api_error(p, req, res, API_RT_NOSUCHUSER);

	struct userec currentuser;
	memcpy(&currentuser, ue, sizeof(currentuser));
	free(ue);

	int r = check_user_session(&currentuser, sessid, appkey);
	if(r != API_RT_SUCCESSFUL) {
		return api_error(p, req, res, r);
	}

	if(HAS_PERM(PERM_DENYMAIL)) {
		return api_error(p, req, res, API_RT_MAILNOPPERM);
	}

	int uent_index = get_user_utmp_index(sessid);
	struct user_info *ui = &(shm_utmp->uinfo[uent_index]);
	if(strcmp(ui->token, token) != 0) {
		return api_error(p, req, res, API_RT_WRONGTOKEN);
	}

	// 更新 token 和来源 IP
	getrandomstr_r(ui->token, TOKENLENGTH+1);
	const char * fromhost = onion_request_get_header(req, "X-Real-IP");
	memset(ui->from, 0, 20);
	strncpy(ui->from, fromhost, 20);

	if(check_user_maxmail(currentuser)) {
		return api_error(p, req, res, API_RT_MAILFULL);
	}

	struct userec *to_user = getuser(to_userid);
	if(!to_user) {
		return api_error(p, req, res, API_RT_NOSUCHUSER);
	}

	if(inoverride(currentuser.userid, to_user->userid, "rejects")) {
		free(to_user);
		return api_error(p, req, res, API_RT_INUSERBLIST);
	}

	const char * data = onion_request_get_post(req, "content");

	char filename[80];
	sprintf(filename, "bbstmpfs/tmp/%s_%s.tmp", currentuser.userid, ui->token);

	char * data2 = strdup(data);
	while(strstr(data2, "[ESC]") != NULL)
		data2 = string_replace(data2, "[ESC]", "\033");

	char * data_gbk = (char *)malloc(strlen(data2)*2);
	u2g(data2, strlen(data2), data_gbk, strlen(data2)*2);

	f_write(filename, data_gbk);
	free(data2);

	int mark=0;		// 文件标记
	//if(insertattachments(filename, data_gbk, currentuser->userid)>0)
		//mark |= FH_ATTACHED;

	free(data_gbk);

	char * title_tmp = (char *)malloc(strlen(title)*2);
	u2g(title, strlen(title), title_tmp, strlen(title)*2);
	char title_gbk[80], title_tmp2[80];
	strncpy(title_gbk, title_tmp[0]==0 ? "No Subject" : title_tmp, 80);
	snprintf(title_tmp2, 80, "{%s} %s", to_user->userid, title);
	free(title_tmp);

	r = do_mail_post(to_user->userid, title, filename, currentuser.userid,
			currentuser.username, fromhost, 0, mark);
	if(backup && strcasecmp(backup, "true")==0) {
		do_mail_post_to_sent_box(currentuser.userid, title_tmp2, filename, currentuser.userid,
			currentuser.username, fromhost, 0, mark);
	}

	unlink(filename);
	free(to_user);

	if(r<0) {
		return api_error(p, req, res, API_RT_MAILINNERR);
	}

	api_set_json_header(res);
	onion_response_printf(res, "{ \"errcode\":0, \"token\":\"%s\" }", ui->token);

	return OCS_PROCESSED;
}
Exemplo n.º 9
0
/***********************************************
 *  获取Rowset集                               *
 *  成功返回SUCC和Rowset集                     *
 *  失败则返回FAIL和NULL                       *
 *  resultset用完后请手动free                  *
 ***********************************************/
int __fetchRowset(xmlXPathObjectPtr resultset, Rowset * rowset)
{
        int nRet;
	int i;
	xmlNodeSetPtr nodeset;
	xmlNodeSetPtr childnodeset;
	xmlNodePtr curnode;
	xmlNodePtr childnode;
	xmlChar * szValue;
	Column _column;
	Column * column;
        Row _row;
	Row * row;

        nRet = FAIL;
        row = &_row;
        column = &_column;
	
	if(resultset == NULL) {
                //xmlFreeDoc(doc);
                ErrorLog(ERROR, "xmlXPathObjectPtr resultset is NULL");
                printf("xmlXPathObjectPtr resultset is NULL");
                return FAIL;
	}
	
	nodeset = resultset->nodesetval;
	// print_xpath_nodes(nodeset, stdout);
	
        if(nodeset == NULL || nodeset->nodeNr == 0) {
		xmlXPathFreeObject(resultset);
                ErrorLog(ERROR, "nodeset is empty");
                printf("nodeset is empty");
                return FAIL;
        }

        memset(column, 0, sizeof(Column));
        memset(row, 0, sizeof(Row));
	rowset->row_num = nodeset->nodeNr;
	//printf("rowset->row_num : %d\n", rowset->row_num);
	for(i=0; i<nodeset->nodeNr; i++) {
		curnode = nodeset->nodeTab[i];
		childnode = curnode->xmlChildrenNode;
		row->column_num = 0;
		while(childnode != NULL) {
			//printf("node type is:%d\n", childnode->type);
			if(childnode->type == XML_ELEMENT_NODE) {
				//printf("is [%d] ok\n", i);
                                column->attrib = (byte *)childnode->name;
				szValue = xmlNodeGetContent(childnode);
                                if ( strlen(szValue) > 1 )
                                	column->value = u2g(szValue);
                                else
                                        column->value = (byte *)szValue;

                                //printf("column[%s]:%s\n",column->attrib,column->value);
                                memcpy(&(row->column[row->column_num]), column, sizeof(Column));
                                memset(column, 0, sizeof(Column));
                                row->column_num++;

                                childnode = childnode->next;
			}
			else {
				xmlXPathFreeObject(resultset);
                        	ErrorLog(ERROR, "current node is not xml element node");
                        	printf("current node is not xml element node\n");
                        	return FAIL;
			}
		}
		row->key = (byte *)curnode->name;
                if ( strlen(xmlNodeGetContent(curnode)) > 1 )
                        row->value = u2g((byte *)xmlNodeGetContent(curnode));
                else
                        row->value = (byte *)xmlNodeGetContent(curnode);
                memcpy(&(rowset->row[i]), row, sizeof(Row));
                memset(row, 0, sizeof(Row));
	}

	nRet = SUCC;	
	xmlXPathFreeObject (resultset);
	return nRet;
}