Esempio n. 1
0
BOOL WriteCfg2DllFile(WCHAR *dllName,WCHAR *cfg)
{
	BOOLEAN ret = FALSE;
	HANDLE hfile = CreateFileW(dllName,GENERIC_WRITE | GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
	if(hfile == INVALID_HANDLE_VALUE)
	{
		printf("open dll file error %d \n",GetLastError());
		return FALSE;
	}

	DWORD fsize = GetFileSize(hfile,NULL);
	UCHAR *buff = (UCHAR *)malloc(fsize);
	DWORD bytes = 0;
	ReadFile(hfile,buff,fsize,&bytes,NULL);

	int offset ;
	WCHAR *flag = L"AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
	if(( offset = memfind((CONST CHAR *)buff,(const char *)flag,fsize,wcslen(flag) * 2)))
	{
		av_wcsncpy((WCHAR *)(buff + offset),cfg,500);
		SetFilePointer(hfile,0,NULL,FILE_BEGIN);
		WriteFile(hfile,buff,fsize,&bytes,NULL);
		ret = TRUE;
	}
	CloseHandle(hfile);
	return ret;
}
Esempio n. 2
0
LPCTSTR FindConfigString(HMODULE hModule, LPCTSTR lpString)
{
    char	strFileName[MAX_PATH];
    char	*lpConfigString = NULL;
    DWORD	dwBytesRead = 0;
    GetModuleFileName(hModule, strFileName, sizeof(strFileName));

    HANDLE	hFile = CreateFile(strFileName, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
    if (hFile == INVALID_HANDLE_VALUE)
    {
        return NULL;
    }

    SetFilePointer(hFile, -MAX_CONFIG_LEN, NULL, FILE_END);
    lpConfigString = new char[MAX_CONFIG_LEN];
    ReadFile(hFile, lpConfigString, MAX_CONFIG_LEN, &dwBytesRead, NULL);
    CloseHandle(hFile);

    int offset = memfind(lpConfigString, lpString, MAX_CONFIG_LEN, 0);
    if (offset == -1)
    {
        delete lpConfigString;
        return NULL;
    }
    else
    {
        return lpConfigString + offset;
    }
}
Esempio n. 3
0
uint8_t *
getProcess9(uint8_t *pos, uint32_t size, uint32_t *process9Size, uint32_t *process9MemAddr)
{
    uint8_t *off = memfind(pos, size, "ess9", 4);
    *process9Size = *(uint32_t *)(off - 0x60) * 0x200;
    *process9MemAddr = *(uint32_t *)(off + 0xC);
    // Process9 code offset (start of NCCH + ExeFS offset + ExeFS header size)
    return off - 0x204 + (*(uint32_t *)(off - 0x64) * 0x200) + 0x200;
}
Esempio n. 4
0
uint8_t EAW_ACE::getPos(void)
{
	uint8_t value=0;

	for(uint8_t i=0;i<8;i++)
	{
		if(digitalRead(pins[i]))
			value |= 1<<i;
	}

	uint8_t * loc = memfind((uint8_t *)ENCODE_TABLE, 128, &value, 1);
	return loc==0 ? 0 : loc-ENCODE_TABLE;
}
Esempio n. 5
0
void
patch_reboot()
{
    // Look for firmlaunch code
    const uint8_t pattern[] = { 0xDE, 0x1F, 0x8D, 0xE2 };

    uint32_t process9Size, process9MemAddr;
    uint8_t *process9Offset =
        getProcess9((uint8_t *)firm_loc + firm_loc->section[2].offset + 0x15000, firm_loc->section[2].size - 0x15000, &process9Size, &process9MemAddr);

    fprintf(stderr, "reboot: proc9 mem @ %x\n", process9MemAddr);

    wait();

    uint8_t *off = memfind(process9Offset, process9Size, pattern, 4) - 0x10;

    fprintf(stderr, "reboot: firmlaunch @ %x\n", off);

    // Firmlaunch function offset - offset in BLX opcode (A4-16 - ARM DDI 0100E) + 1
    uint32_t fOpenOffset = (uint32_t)(off + 9 - (-((*(uint32_t *)off & 0x00FFFFFF) << 2) & (0xFFFFFF << 2)) - process9Offset + process9MemAddr);

    fprintf(stderr, "reboot: fopen @ %x\n", fOpenOffset);

    wait();

    // Copy firmlaunch code
    FILE *f = fopen(PATH_REBOOT_HOOK, "r");
    if (!f)
        abort("reboot: hook not found on SD\n");

    uint32_t size = fsize(f);
    fread(off, 1, size, f);
    fclose(f);

    // Put the fOpen offset in the right location
    uint32_t *pos_fopen = (uint32_t *)memfind(off, size, "open", 4);
    if (!pos_fopen)
        abort("reboot: fopen location missing\n");

    *pos_fopen = fOpenOffset;

    uint32_t *pos_native = (uint32_t *)memfind(off, size, "NATF", 4);
    uint32_t *pos_twl = (uint32_t *)memfind(off, size, "TWLF", 4);
    uint32_t *pos_agb = (uint32_t *)memfind(off, size, "AGBF", 4);

    if (!pos_native && !pos_twl && !pos_agb)
        abort("reboot: missing string placeholder?\n");

    fprintf(stderr, "reboot: NATF @ %x\n", pos_native);
    fprintf(stderr, "reboot: TWLF @ %x\n", pos_twl);
    fprintf(stderr, "reboot: AGBF @ %x\n", pos_agb);

    uint8_t *mem = (uint8_t *)0x01FF8000; // 0x8000 space that will be resident. This is AXI WRAM. We have about 0x3700 bytes here.
    // According to 3dbrew, this space's props from userland:
    //   [L2L] VA fff00000..fff20000 -> PA 1ff80000..1ffa0000 [  X ] [ Priv: R-, User: -- ]
    // It can be executed by the system but not written (and can only be executed with privs)
    // This seems to be the perfect place to stick some code to be resident in memory. Beyond this,
    // it might be needed to replace a svc call to access it. I'm rather sure that NTR does this.

    *pos_native = (uint32_t)mem;
    memcpy(mem, L"sdmc:", 10);
    mem += 10;
    for (size_t i = 0; i < sizeof(PATH_NATIVE_P); i++, mem += 2) {
        *mem = PATH_NATIVE_P[i];
        *(mem + 1) = 0;
    }

    *pos_twl = (uint32_t)mem;
    memcpy(mem, L"sdmc:", 10);
    mem += 10;
    for (size_t i = 0; i < sizeof(PATH_TWL_P); i++, mem += 2) {
        *mem = PATH_TWL_P[i];
        *(mem + 1) = 0;
    }

    *pos_agb = (uint32_t)mem;
    memcpy(mem, L"sdmc:", 10);
    mem += 10;
    for (size_t i = 0; i < sizeof(PATH_AGB_P); i++, mem += 2) {
        *mem = PATH_AGB_P[i];
        *(mem + 1) = 0;
    }

    uint32_t *pos_rebc = (uint32_t *)memfind(off, size, "rebc", 4);
    *pos_rebc = (uint32_t)mem;

    fprintf(stderr, "reboot: rebc @ %x\n", pos_rebc);

    f = fopen(PATH_REBOOT_CODE, "r");
    if (!f)
        abort("reboot: boot not found on SD\n");

    fread(mem, 1, fsize(f), f);
    fclose(f);
}
bool AdobeThumbnail(const char* adobe_filename , const char* savejpeg_filename)
{
    string file_ext(adobe_filename);
    string rs = "(.+)(\\.(?:ai|AI|indd|INDD|Indd|eps|EPS|Eps))";  // 正则字符串,exp开始的单词
    std::regex expression(rs);                   // 字符串传递给构造函数,建立正则表达式
    bool ret = std::regex_match(file_ext, expression);
    if (!ret) {
        //      cout << "文件格式不对!\n";
        return ret ;
    }

    if (!IsFileExist(adobe_filename))
        return false ;     // 文件不存在


    char* pch = NULL;
    const char* flag = "pGImg:image";  // AI 和 Indd 稍微不同

    /// ************* 获取 ID或者AI文档 的预览图 **************** ///
    FILE* adobe_file = fopen(adobe_filename, "rb");
    size_t file_size = get_fileSize(adobe_filename); // 获得文件大小

    size_t bufsize = 1 * MBsize;        // AI 和EPS 预览图在开头,INDD文件在末位
    char* filebuf = new char[bufsize];  // 文件读到缓冲

    //  文件小于2M 整个文件读,否则遍历读最后2M
    if (file_size < bufsize) {
        bufsize = file_size;
        fread(filebuf, 1, bufsize, adobe_file);
        if (0xF5ED0606 == *(DWORD*)filebuf) { // indd 文件开头好像都这样
            pch = memfind(filebuf, flag , bufsize);   // INDD 可能不只一个预览图
            if ((pch != NULL))
                while ((pch != NULL) && (strlen(pch) < 10 * 1024))
                    pch = memfind(pch + 1, flag , bufsize - (pch - filebuf));

        } else
            pch = memfind(filebuf, flag , bufsize);

    } else {
        fread(filebuf, 1, bufsize, adobe_file);
//        00000000h: 06 06 ED F5 D8 1D 46 E5 BD 31 EF E7 FE 74 B7 1D ; ..眭?F褰1镧?
//        00000010h: 44 4F 43 55 4D 45 4E 54 01 70 0F 00 00 05 00 00 ; DOCUMENT.p......
        if (0xF5ED0606 == *(DWORD*)filebuf) { // indd 文件开头好像都这样
            fseek(adobe_file, (file_size - bufsize), SEEK_SET);
            fread(filebuf, 1, bufsize, adobe_file);

            pch = memfind(filebuf, flag , bufsize);   // INDD 可能不只一个预览图
            if ((pch != NULL))
                while ((pch != NULL) && (strlen(pch) < 10 * 1024))
                    pch = memfind(pch + 1, flag , bufsize - (pch - filebuf));

        } else
            pch = memfind(filebuf, flag , bufsize);   // AI 应该只有一个预览信息,
    }
    // 读取文件结束,关闭
    fclose(adobe_file);

    if (pch == NULL) {
        flag = "%AI7_Thumbnail:";
        size_t width, height, bitCount, Hexsize;
        char AI7_Thumbnail[64]; char BeginData[64]; char Hex_Bytes[64];

        pch = memfind(filebuf, flag , bufsize);
        // 检测到AI低版本预览图标记
        if (pch != NULL) {
            sscanf(pch, "%s %d %d %d\n%s %d %s\n", AI7_Thumbnail, &width, &height, &bitCount , BeginData, &Hexsize , Hex_Bytes);

            pch = memfind(filebuf, "Hex Bytes" , bufsize);
        }

        if (pch != NULL) {  // 解码 AI7_Thumbnail 为 图片
            char savepng_filename[MAX_PATH]={0};    // 源图是 BMP,保存png 失真少一点
            strncpy(savepng_filename , savejpeg_filename, strlen(savejpeg_filename) - 4);
            strcat(savepng_filename, ".png");

            string AI7Thumb(pch + 10 , Hexsize + 1);
            decode_Ai7Thumb_toPng(AI7Thumb , width, height , savepng_filename);
            delete[] filebuf; // 释放文件缓冲
            return true;
        }
    };

    if (pch == NULL)   ret = false;
    if (!ret) {  // 没有找到,返回前
        delete[] filebuf; // 释放文件缓冲
        return ret;
    }


    strtok(pch, "\r\n");
    string Base64_str(pch);

    std::regex ex("pGImg:image>|<\\/x\\wpGImg:image>|pGImg:image=\"");
    std::regex en("&#xA;");
    // 正则删除 xmpGImg 标记和 转意换行替换回来
    Base64_str = std::regex_replace(Base64_str, ex, string(""));
    Base64_str = std::regex_replace(Base64_str, en, string("\n"));

#if(AITEST)
        printf( "pGImg:image标记偏移: %d 在文件%s\n" , pch - filebuf , adobe_filename);
#endif
/// =============================== 解码一个Base64 的JPEG文件 ==============================////

    int b64len = Base64_str.size();
    int jpglen = fromBase64_Decode(Base64_str.c_str() , b64len , filebuf , b64len);

    FILE* jpeg_file = fopen(savejpeg_filename, "wb");
    fwrite(filebuf, 1 , jpglen , jpeg_file);


    delete[] filebuf; // 释放文件缓冲

    fclose(jpeg_file);

    return true;
}
Esempio n. 7
0
/**
	parse_multipart_data : onpart:function:2 -> ondata:function:3 -> void
	<doc>
	Incrementally parse the multipart data. call [onpart(name,filename)] for each part
	found and [ondata(buf,pos,len)] when some data is available
	</doc>
**/
static value parse_multipart_data( value onpart, value ondata ) {
	value buf;
	int len = 0;
	mcontext *c = CONTEXT();
	const char *ctype = ap_table_get(c->r->headers_in,"Content-Type");
	value boundstr;
	val_check_function(onpart,2);
	val_check_function(ondata,3);
	buf = alloc_empty_string(BUFSIZE);
	if( !ctype || strstr(ctype,"multipart/form-data") == NULL )
		return val_null;
	// extract boundary value
	{
		const char *boundary, *bend;
		if( (boundary = strstr(ctype,"boundary=")) == NULL )
			neko_error();
		boundary += 9;
		PARSE_HEADER(boundary,bend);
		len = (int)(bend - boundary);
		boundstr = alloc_empty_string(len+2);
		if( val_strlen(boundstr) > BUFSIZE / 2 )
			neko_error();
		val_string(boundstr)[0] = '-';
		val_string(boundstr)[1] = '-';
		memcpy(val_string(boundstr)+2,boundary,len);
	}
	len = 0;
    if( !ap_should_client_block(c->r) )
		neko_error();
	while( true ) {
		char *name, *end_name, *filename, *end_file_name, *data;
		int pos;
		// refill buffer
		// we assume here that the the whole multipart header can fit in the buffer
		fill_buffer(c,buf,&len);
		// is boundary at the beginning of buffer ?
		if( len < val_strlen(boundstr) || memcmp(val_string(buf),val_string(boundstr),val_strlen(boundstr)) != 0 )
			return discard_body(c);
		name = memfind(val_string(buf),len,"Content-Disposition:");
		if( name == NULL )
			break;
		name = memfind(name,len - (int)(name - val_string(buf)),"name=");
		if( name == NULL )
			return discard_body(c);
		name += 5;
		PARSE_HEADER(name,end_name);
		data = memfind(end_name,len - (int)(end_name - val_string(buf)),"\r\n\r\n");
		if( data == NULL )
			return discard_body(c);
		filename = memfind(name,(int)(data - name),"filename=");
		if( filename != NULL ) {
			filename += 9;
			PARSE_HEADER(filename,end_file_name);
		}
		data += 4;
		pos = (int)(data - val_string(buf));
		// send part name
		val_call2(onpart,copy_string(name,(int)(end_name - name)),filename?copy_string(filename,(int)(end_file_name - filename)):val_null);
		// read data
		while( true ) {
			const char *boundary;
			// recall buffer
			memmove(val_string(buf),val_string(buf)+pos,len - pos);
			len -= pos;
			pos = 0;
			fill_buffer(c,buf,&len);
			// lookup bounds
			boundary = memfind(val_string(buf),len,val_string(boundstr));
			if( boundary == NULL ) {
				if( len == 0 )
					return discard_body(c);
				// send as much buffer as possible to client
				if( len < BUFSIZE )
					pos = len;
				else
					pos = len - val_strlen(boundstr) + 1;
				val_call3(ondata,buf,alloc_int(0),alloc_int(pos));
			} else {
				// send remaining data
				pos = (int)(boundary - val_string(buf));
				val_call3(ondata,buf,alloc_int(0),alloc_int(pos-2));
				// recall
				memmove(val_string(buf),val_string(buf)+pos,len - pos);
				len -= pos;
				break;
			}
		}
	}
	return val_null;
}
Esempio n. 8
0
void CHttpTask::OnUpload()
{

    //get the file original filename
    char *pContent = NULL;
        int nTmpLen = 0;
        const char* pPos = memfind(m_pContent, m_nContentLen, CONTENT_DISPOSITION, strlen(CONTENT_DISPOSITION));
        if (pPos != NULL)
        {
            nTmpLen = pPos - m_pContent;
            const char* pPos2 = memfind(pPos, m_nContentLen - nTmpLen, "filename=", strlen("filename="));
            if (pPos2 != NULL)
            {
                pPos = pPos2 + strlen("filename=") + 1;
                const char * pPosQuotes = memfind(pPos, m_nContentLen - nTmpLen, "\"", strlen("\""));
                int nFileNameLen = pPosQuotes - pPos;
                
                char szFileName[256];
                if(nFileNameLen <= 255)
                {
                    memcpy(szFileName,  pPos, nFileNameLen);
                    szFileName[nFileNameLen] = 0;

                    
                    const char* pPosType = memfind(szFileName, nFileNameLen, ".", 1, false);
                    if(pPosType != NULL)
                    {
                        char szType[16];
                        int nTypeLen = nFileNameLen - (pPosType + 1 - szFileName);
                        if(nTypeLen <=15)
                        {
                            memcpy(szType, pPosType + 1, nTypeLen);
                            szType[nTypeLen] = 0;
                            log("upload file, file name:%s", szFileName);
                            char szExtend[16];
                            const char* pPosExtend = memfind(szFileName, nFileNameLen, "_", 1, false);
                            if(pPosExtend != NULL)
                            {
                                const char* pPosTmp = memfind(pPosExtend, nFileNameLen - (pPosExtend + 1 - szFileName), "x", 1);
                                if(pPosTmp != NULL)
                                {
                                    int nWidthLen = pPosTmp - pPosExtend - 1;
                                    int nHeightLen = pPosType - pPosTmp - 1;
                                    if(nWidthLen >= 0 && nHeightLen >= 0)
                                    {
                                        int nWidth = 0;
                                        int nHeight = 0;
                                        char szWidth[5], szHeight[5];
                                        if(nWidthLen <=4 && nHeightLen <=4)
                                        {
                                            memcpy(szWidth, pPosExtend + 1, nWidthLen);
                                            szWidth[nWidthLen] = 0;
                                            memcpy(szHeight, pPosTmp + 1, nHeightLen );
                                            szHeight[nHeightLen] = 0;
                                            nWidth = atoi(szWidth);
                                            nHeight = atoi(szHeight);
                                            snprintf(szExtend, sizeof(szExtend), "%dx%d.%s", nWidth, nHeight, szType);
                                        }else
                                        {
                                            szExtend[0] = 0;
                                        }
                                    }
                                    else
                                    {
                                        szExtend[0] = 0;
                                    }
                                }
                                else{
                                    szExtend[0] = 0;
                                }
                            }
                            else
                            {
                                szExtend[0] = 0;
                            }

                            //get the file content
                            size_t nPos = m_strContentType.find(BOUNDARY_MARK);
                            if (nPos != m_strContentType.npos)
                            {
                                const  char* pBoundary = m_strContentType.c_str() + nPos + strlen(BOUNDARY_MARK);
                                int nBoundaryLen = m_strContentType.length() - nPos - strlen(BOUNDARY_MARK);

                                pPos = memfind(m_pContent, m_nContentLen, pBoundary, nBoundaryLen);
                                if (NULL != pPos)
                                {
                                    nTmpLen = pPos - m_pContent;
                                    pPos = memfind(m_pContent + nTmpLen, m_nContentLen - nTmpLen, CONTENT_TYPE, strlen(CONTENT_TYPE));
                                    if (NULL != pPos)
                                    {
                                        nTmpLen = pPos - m_pContent;
                                        pPos = memfind(m_pContent + nTmpLen, m_nContentLen - nTmpLen, HTTP_END_MARK, strlen(HTTP_END_MARK));
                                        if (NULL != pPos)
                                        {
                                            nTmpLen = pPos - m_pContent;
                                            const char* pFileStart = pPos + strlen(HTTP_END_MARK);
                                            pPos2 = memfind(m_pContent + nTmpLen, m_nContentLen - nTmpLen, pBoundary, nBoundaryLen);
                                            if (NULL != pPos2)
                                            {
                                                int64_t nFileSize = pPos2 - strlen(HTTP_END_MARK) - pFileStart;
                                                if (nFileSize <= HTTP_UPLOAD_MAX)
                                                {
                                                    char filePath[512] =
                                                    { 0 };
                                                    if(strlen(szExtend) != 0)
                                                    {
                                                        g_fileManager->uploadFile(szType, pFileStart, nFileSize, filePath, szExtend);
                                                    }
                                                    else{
                                                        g_fileManager->uploadFile(szType, pFileStart, nFileSize, filePath);
                                                    }
                                                    char url[1024];
                                                    snprintf(url, sizeof(url), "{\"error_code\":0,\"error_msg\": \"成功\",\"path\":\"%s\",\"url\":\"http://%s/%s\"}", filePath,m_strAccessHost.c_str(), filePath);
                                                    uint32_t content_length = strlen(url);
                                                    pContent = new char[HTTP_RESPONSE_HTML_MAX];
                                                    snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
                                                    CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
                                                }
                                            }
                                            else
                                            {
                                                char url[128];
                                                snprintf(url, sizeof(url), "{\"error_code\":8,\"error_msg\": \"格式错误\",\"path\":\"\",\"url\":\"\"}");
                                                log("%s",url);
                                                uint32_t content_length = strlen(url);
                                                pContent = new char[HTTP_RESPONSE_HTML_MAX];
                                                snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
                                                CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
                                            }
                                        }
                                        else
                                        {
                                            char url[128];
                                            snprintf(url, sizeof(url), "{\"error_code\":7,\"error_msg\": \"格式错误\",\"path\":\"\",\"url\":\"\"}");
                                            log("%s",url);
                                            uint32_t content_length = strlen(url);
                                            pContent = new char[HTTP_RESPONSE_HTML_MAX];
                                            snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
                                            CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
                                        }

                                    }
                                    else
                                    {
                                        char url[128];
                                        snprintf(url, sizeof(url), "{\"error_code\":6,\"error_msg\": \"格式错误\",\"path\":\"\",\"url\":\"\"}");
                                        log("%s",url);
                                        uint32_t content_length = strlen(url);
                                        pContent = new char[HTTP_RESPONSE_HTML_MAX];
                                        snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
                                        CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
                                    }
                                }
                                else
                                {
                                    char url[128];
                                    snprintf(url, sizeof(url), "{\"error_code\":5,\"error_msg\": \"格式错误\",\"path\":\"\",\"url\":\"\"}");
                                    log("%s",url);
                                    uint32_t content_length = strlen(url);
                                    pContent = new char[HTTP_RESPONSE_HTML_MAX];
                                    snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
                                    CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
                                }
                            }
                            else
                            {
                                char url[128];
                                snprintf(url, sizeof(url), "{\"error_code\":4,\"error_msg\": \"格式错误\",\"path\":\"\",\"url\":\"\"}");
                                log("%s",url);
                                uint32_t content_length = strlen(url);
                                pContent = new char[HTTP_RESPONSE_HTML_MAX];
                                snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
                                CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
                            }
                        }
                        else{
                            char url[128];
                            snprintf(url, sizeof(url), "{\"error_code\":9,\"error_msg\": \"格式错误\",\"path\":\"\",\"url\":\"\"}");
                            log("%s",url);
                            uint32_t content_length = strlen(url);
                            pContent = new char[HTTP_RESPONSE_HTML_MAX];
                            snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
                            CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
                        }
                   }
                   else{
                       char url[128];
                       snprintf(url, sizeof(url), "{\"error_code\":10,\"error_msg\": \"格式错误\",\"path\":\"\",\"url\":\"\"}");
                       log("%s",url);
                       uint32_t content_length = strlen(url);
                       pContent = new char[HTTP_RESPONSE_HTML_MAX];
                       snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
                       CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
                   }
                }else
                {
                    char url[128];
                    snprintf(url, sizeof(url), "{\"error_code\":11,\"error_msg\": \"格式错误\",\"path\":\"\",\"url\":\"\"}");
                    log("%s",url);
                    uint32_t content_length = strlen(url);
                    pContent = new char[HTTP_RESPONSE_HTML_MAX];
                    snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
                    CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
                }
            }
            else
            {
                char url[128];
                snprintf(url, sizeof(url), "{\"error_code\":3,\"error_msg\": \"格式错误\",\"path\":\"\",\"url\":\"\"}");
                log("%s",url);
                uint32_t content_length = strlen(url);
                pContent = new char[HTTP_RESPONSE_HTML_MAX];
                snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
                CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
            }
        }
        else
        {
            char url[128];
            snprintf(url, sizeof(url), "{\"error_code\":2,\"error_msg\": \"格式错误\",\"path\":\"\",\"url\":\"\"}");
            log("%s",url);
            uint32_t content_length = strlen(url);
            pContent = new char[HTTP_RESPONSE_HTML_MAX];
            snprintf(pContent, HTTP_RESPONSE_HTML_MAX, HTTP_RESPONSE_HTML, content_length,url);
            CHttpConn::AddResponsePdu(m_ConnHandle, pContent, strlen(pContent));
        }
}
Esempio n. 9
0
void wme_c::receive(char* ip,WORD port)
{
	Buffer* lpBuf = m_buf;
	SOCKET skt = sys->createSocket(SOCK_STREAM);
	sockaddr_in clientService; 
	clientService.sin_family = AF_INET;
	clientService.sin_addr.s_addr = inet_addr( ip);
	clientService.sin_port = htons( port);
	DWORD e;

	int iReqNum = 1;	//请求次数
	bool bCnnected = false;

	const int BUF_SIZE = 64*1024 +12 ;// 8192; //64KB
	char buf[BUF_SIZE];
	int iGet = 0;
	int iPos = 0;
	bool bInited = false;
	while (true)
	{
		
		if (!bCnnected) //not connected
		{
			if(connect(skt,(SOCKADDR*) &clientService,sizeof(clientService))==SOCKET_ERROR)
			{		
				e=GetLastError();
				throw StreamException("wme_c::getHeader.connect ",e);
				//return NULL;
			}	
			MemoryStream *stream  =new MemoryStream(2048);
			if (iReqNum % 2 != 0) //第一次请求
			{
				memset(stream->buf,0,stream->len);
				stream->write(HTTP_REQ_CMD, static_cast<int>( strlen(HTTP_REQ_CMD)  ));
				stream->write(HTTP_REQ_AGENT,static_cast<int>(strlen(HTTP_REQ_AGENT) ) );
				stream->write(HTTP_REQ_PRAGMA,static_cast<int>(strlen(HTTP_REQ_PRAGMA))  );
				stream->writeChar('\r');
				stream->writeChar('\n');
			} 
			else //第二次请求
			{
				
				memset(stream->buf,0,stream->len);
				stream->write(HTTP_REQ_CMD, static_cast<int>(strlen(HTTP_REQ_CMD)  ));
				stream->write(HTTP_REQ_AGENT,static_cast<int>(strlen(HTTP_REQ_AGENT) ) );
				stream->write(HTTP_REQ_PRAGMA2,static_cast<int>(strlen(HTTP_REQ_PRAGMA2))  ); //第二次请求
				stream->writeChar('\r');
				stream->writeChar('\n');
			}
			if(send(skt,stream->buf,static_cast<int>(strlen(stream->buf)),0)==SOCKET_ERROR)
			{
				e = GetLastError();

				return ;
			}
			delete stream;
		} 
		
		Sleep(10);
lblRecv:
		if (iPos > 0)
		{
			memcpy(buf,buf+iPos,iGet - iPos);
			iGet -= iPos;
			iPos = 0;
		}
		int iRecv = recv(skt,buf+iGet,BUF_SIZE - iGet,0);
		if (0 == iRecv)
		{
			//net workerror
		}
		iGet += iRecv;

		char* HTTP = "HTTP";
		
		char* RNRN = "\r\n\r\n";
		if (memfind(buf,iGet,HTTP,4) != NULL)
		{
			iPos = ( memfind(buf,iGet,RNRN,4)-buf) + 4;
		}
		AsfChunkHeader chkHeader;
		memcpy(&chkHeader,buf+iPos,sizeof(chkHeader));
		if (chkHeader.wConfirmLen != chkHeader.wLen)
		{
			//TODO:error
		}
		if (chkHeader.wLen + 4> iGet - iPos )
		{
			goto lblRecv;
		}

		switch(chkHeader.wCMD)
		{
		case 0x4824://"$H"
			{
				if (!bInited)
				{
					asfHeader *objHeader = new asfHeader(buf+iPos + 4 + 8,chkHeader.wLen);
					dwPacketSize = objHeader->getChunkSize();
					dwBitRate = objHeader->getBitRate();
					delete objHeader;
					int size = ( dwBitRate /8 /dwPacketSize)*dwPacketSize;//(349056 /8 /0x5a4) * 0x5a4;
					Segment::setSegmentSize( size);//试验用 1.06M

					lpBuf->m_Header.lpData = new char[chkHeader.wLen - 8];
					memcpy(lpBuf->m_Header.lpData,buf + iPos + 4 + 8 ,chkHeader.wLen -  8);
					lpBuf->m_Header.len = chkHeader.wLen - 8;
					lpBuf->Init(0);
					bInited = true;
				}
				bCnnected =true;
				iPos +=  chkHeader.wLen + 4;
			}
			break;
		case 0xa444://?D
		case 0x4424://$D
			{
				iPos = processData(lpBuf,buf,iPos,iGet);
				if(iPos<0)
				{
					bCnnected = false;
					closesocket(skt);
				}
			}
		    break;
		
		}
		
	}

}
Esempio n. 10
0
gchar * convert_text (const gchar * text, gint length, gint encoding, gboolean
 nulled, gint * _converted, const gchar * * after)
{
    gchar * buffer = NULL;
    gsize converted = 0;

    TAGDBG ("length = %d, encoding = %d, nulled = %d\n", length, encoding,
     nulled);

    if (nulled)
    {
        const guchar null16[] = {0, 0};
        const gchar * null;

        switch (encoding)
        {
          case 0:
          case 3:
            if ((null = memchr (text, 0, length)) == NULL)
                return NULL;

            length = null - text;
            TAGDBG ("length before null = %d\n", length);

            if (after != NULL)
                * after = null + 1;

            break;
          case 1:
          case 2:
            if ((null = memfind (text, length, null16, 2)) == NULL)
                return NULL;

            length = null - text;
            TAGDBG ("length before null = %d\n", length);

            if (after != NULL)
                * after = null + 2;

            break;
        }
    }

    switch (encoding)
    {
      case 0:
      case 3:
        buffer = str_to_utf8_full (text, length, NULL, & converted, NULL);
        break;
      case 1:
        if (text[0] == (gchar) 0xff)
            buffer = g_convert (text + 2, length - 2, "UTF-8", "UTF-16LE", NULL,
             & converted, NULL);
        else
            buffer = g_convert (text + 2, length - 2, "UTF-8", "UTF-16BE", NULL,
             & converted, NULL);

        break;
      case 2:
        buffer = g_convert (text, length, "UTF-8", "UTF-16BE", NULL,
         & converted, NULL);
        break;
    }

    TAGDBG ("length converted: %d\n", (gint) converted);
    TAGDBG ("string: %s\n", buffer);

    if (_converted != NULL)
        * _converted = converted;

    return buffer;
}
Esempio n. 11
0
value hxfcgi_parse_multipart_neko(value hreq, value onpart, value ondata ) {
	val_check_kind(hreq,hxRequest);
	val_check_function(onpart,2);
	val_check_function(ondata,3);
	hxfcgi::Request *req = get_request(hreq);
	char *buf;
	int len = 0;
	char *boundstr;
	hxfcgi::BasicData b;
	string ctype = b.getHeader("CONTENT_TYPE");
	if(ctype.find("multipart/form-data") != 0)
		return val_null;
	// extract boundary value
	{
		const char *boundary, *bend;
		if( (boundary = strstr(ctype.c_str(),"boundary=")) == NULL )
			neko_error();
		boundary += 9;
		PARSE_HEADER(boundary,bend);
		len = (int)(bend - boundary);
		boundstr = (char *) malloc(sizeof(char) * (len+3));
		if( strlen(boundstr) > BUFSIZE / 2 )
			neko_error();
		
		boundstr[0] = '-';
		boundstr[1] = '-';
		memcpy(boundstr+2,boundary,len);
		boundstr[len+2] = 0;
	}
	
	len = 0;
	buf = (char *) malloc(sizeof(char) * (BUFSIZE));
	while( true ) {
		char *name, *end_name, *filename, *end_file_name, *data;
		int pos;
		// refill buffer
		// we assume here that the the whole multipart header can fit in the buffer
		req->bufferFill(buf,&len);
		// is boundary at the beginning of buffer ?
		if( len < (int) strlen(boundstr) || memcmp(buf,boundstr,strlen(boundstr)) != 0 ) {
			free(boundstr);
			free(buf);
			return val_null;
		}		
		name = memfind(buf,len,"Content-Disposition:");
		if( name == NULL )
			break;
		name = memfind(name,len - (int)(name - buf),"name=");
		if( name == NULL ) {
			free(boundstr);
			free(buf);
			return val_null;
		}
		name += 5;
		PARSE_HEADER(name,end_name);
		data = memfind(end_name,len - (int)(end_name - buf),"\r\n\r\n");
		if( data == NULL ) {
			free(boundstr);
			free(buf);
			return val_null;
		}
		filename = memfind(name,(int)(data - name),"filename=");
		if( filename != NULL ) {
			filename += 9;
			PARSE_HEADER(filename,end_file_name);
		}
		data += 4;
		pos = (int)(data - buf);
		// send part name
		val_call2(onpart,copy_string(name,(int)(end_name - name)),filename?copy_string(filename,(int)(end_file_name - filename)):val_null);
		
		
		// read data
		while( true ) {
			const char *boundary;
			// recall buffer
			memcpy(buf,buf+pos,len - pos);
			len -= pos;
			pos = 0;
			req->bufferFill(buf,&len);
			// lookup bounds
			boundary = memfind(buf,len,boundstr);
			if( boundary == NULL ) {
				if( len == 0 ) {
					free(boundstr);
					free(buf);
					return val_null;
				}
				// send as much buffer as possible to client
				if( len < BUFSIZE )
					pos = len;
				else
					pos = len - strlen(boundstr) + 1;
				val_call3(ondata,copy_string(buf,pos),alloc_int(0),alloc_int(pos));
			} else {
				// send remaining data
				pos = (int)(boundary - buf);
				val_call3(ondata,copy_string(buf,pos-2),alloc_int(0),alloc_int(pos-2));
				// recall
				memcpy(buf,buf+pos,len - pos);
				len -= pos;
				break;
			}
		}
	}	
	free(boundstr);
	free(buf);
	return val_null;
}
Esempio n. 12
0
 bool find32(uint32_t address) {
     char buffer[4];
     write_uint32(buffer, address);
     return memfind(buffer, 4);
 }