Ejemplo n.º 1
0
void CGUIStaticPicture::RemoveFromEngine()
{
	if(!rect)
	{
		throw CExc(eGUI, E_INTERNAL,"CGUIStaticPicture::RemoveFromEngine> rect not found");
	}

	CBltWindow *wnd = rect->GetParent();
	if(wnd)
		wnd->RemoveChild(rect);
	else
	{
		throw CExc(eGUI, E_INTERNAL,"CGUIStaticPicture::RemoveFromEngine> rect not found in window");
	}
}
Ejemplo n.º 2
0
CGUIStaticText::CGUIStaticText(float _x, float _y, char *_text, char *fontName, int size, bool pixelSize, int color)
	: CGUIWidget(_x,_y)
{
	HFONT font;
	
	if(!_text || !fontName)
	{
		throw CExc(eGUI, E_BAD_ARGUMENTS,"CGUIStaticText::CGUIStaticText> Text or Font not assigned");
	}

	originalText = KRKAL->CopyOutStrByLanguage(_text);
	int len = strlen(originalText);
	numChars = len;
	SIZE vel;

	if(len==0)	// text nulove delky
	{
		SetSize(0,0);
		throw_going=true;
		focusable=false;	// tento widget nelze focusovat (kliknutim mysi)
		texts=0;
		text=0;
		guiFont=0;
		numChars=0;
		numLines=0;
		AddToTrash();
		return;
	}

	guiFont = new CGUIFont();

	if(!pixelSize)
		guiFont->CreateFont(fontName,size);
	else
		guiFont->CreateFontPS(fontName,size);

	font = guiFont->font;

	SelectObject(mainGUI->dc,font);
	GetTextExtentPoint32(mainGUI->dc,originalText,len,&vel);

	SetSize((float)vel.cx,(float)vel.cy);
	text = new CBltText(x,y,(float)vel.cx,(float)vel.cy,originalText,len,font, color,0,0);
	throw_going=true;
	focusable=false;	// tento widget nelze focusovat (kliknutim mysi)
	texts = 0;

	maxX=maxY=linesSpaces=0;
	alignment=aLeft;
	maxNumLines=0;

	AddToTrash();
}
Ejemplo n.º 3
0
CGUIStaticPicture::CGUIStaticPicture(float _x, float _y, const char *texName, float _sx, float _sy, UINT color)
	: CGUIWidget(_x,_y), CGUIEventSupport(availableEvents, numEvents)
{
	UINT ssx, ssy;
	CTexture *texture = new CTexture;
	rect = 0;
	bool released = false;

	if ( !( texture->CreateFromFile(texName) ) )
	{
		texture->Release();
		throw CExc(eGUI, E_FILE_NOT_FOUND,"CGUIStaticPicture::CGUIStaticPicture> Cannot find texture: %s",texName);
	}

	try{
		texture->GetImageSize(&ssx,&ssy);
		if(_sx>0)
			ssx=(UINT)_sx;
		if(_sy>0)
			ssy=(UINT)_sy;

		rect = new CBltRect(_x, _y, (float)ssx, (float)ssy, color);
		rect->SetTexture(texture);
		texture->Release();
		released=true;

		SetSize((float)ssx,(float)ssy);
		SetPos(_x,_y);

		throw_going=true;
		focusable=false;
		MsgAcceptConsume(MsgMouseL);
	//	MsgSetConsume(MsgNone);
	//	MsgSetProduce(MsgNone);	// preposila elementum pod sebou
		AddToTrash();
	}catch(CExc)
	{
		if(!released)
			texture->Release();
		SAFE_DELETE(rect);
		throw;
	}
}
Ejemplo n.º 4
0
CSEdDesktop::CSEdDesktop()
	: CGUIDesktop()
{
	try{
		assert(!SEdDesktop);
		SEdDesktop=this;

		RemoveFromTrash();
		MsgAcceptConsume(MsgMouseL|MsgMouseM|MsgMouseR);
		// MsgSetProduce(MsgAll);

		CGUIWindowPanel* panel = new CGUIWindowPanel(0,sy-STD_PANEL_HEIGHT-STD_PANEL_SHIFT,styleSet->Get("Panel"),this,true,false,0,0);
													//0,-STD_PANEL_SHIFT (pridani nahoru)
		push=false;

		iconDefaultHandler = new CSEdDesktopIconHandler();
		iconDefaultHandler->icons = new typeID[4];		// zadat velikost pole dle poctu icon (polozek typeID)

		SEdOpenedFiles = new CSEdOpenedFiles();

		AddStartMenu();

		InitDefaultIcons();

		iconDefaultHandler->ShowFileBrowser();

		AddToTrash();
	}
	catch(CExc c)
	{
		mainGUI->LogError(c);

		// vypis informace o chybe na obrazovce jako DebugMessage (prozatimni reseni)
		__time64_t ltime;
		_time64( &ltime );
		DebugMessage(0,"%s : %2d:%2d - %s",_ctime64( &ltime ), c.errnum, c.param, c.errstr);

		desktop=0;
		throw CExc(eGUI, E_INTERNAL,"CSEdDesktop::CSEdDesktop> Fatal error - cannot creat DESKTOP");
	}
}
Ejemplo n.º 5
0
CMMDesktop::CMMDesktop()
	: CGUIDesktop()
{
	try{
		assert(!MMDesktop);
		MMDesktop=this;
		DefaultHandler = 0;
		levelIntro = 0;

		RemoveFromTrash();
		MsgAcceptConsume(MsgMouseL|MsgMouseM|MsgMouseR|MsgKeyboard);

		DefaultHandler = new CMMDesktopHandler();

		levBrowser=0;
		MMmode = 0;
		playingFile=0;
		quicksave=0;

		ShowMainMenu();

		AddToTrash();

	}

	catch(CExc c)
	{
		SAFE_DELETE(DefaultHandler);
		mainGUI->LogError(c);

		MMDesktop=0;
		desktop=0;
		throw CExc(eGUI, E_INTERNAL,"CMainMenuDesktop::CMainMenuDesktop> Fatal error - cannot create DESKTOP");
	}

}
Ejemplo n.º 6
0
CGUIStaticText::CGUIStaticText(float _x, float _y, float maxx, char *_text, char *fontName, int size, bool pixelSize, EGUIAlignment align, int color)
	: CGUIWidget(_x,_y)
{
	HFONT font;
	if(!_text || !fontName)
	{
		throw CExc(eGUI, E_BAD_ARGUMENTS,"CGUIStaticText::CGUIStaticText> Text or Font not assigned");
	}

	originalText = KRKAL->CopyOutStrByLanguage(_text);
	int len = strlen(originalText);

	SIZE vel;

	if(len==0)	// text nulove delky
	{
		SetSize(0,0);
		throw_going=true;
		focusable=false;	// tento widget nelze focusovat (kliknutim mysi)
		texts=0;
		text=0;
		guiFont=0;
		numLines=0;
		numChars=0;
		AddToTrash();
		return;
	}

	guiFont = new CGUIFont();

	if(!pixelSize)
		guiFont->CreateFont(fontName,size);
	else
		guiFont->CreateFontPS(fontName,size);

	font = guiFont->font;
	SelectObject(mainGUI->dc,font);

	// oriznuti textu na maximalni delku podle max. velikosti obdelniku
	while(len)
	{
		GetTextExtentPoint32(mainGUI->dc,originalText,len,&vel);
		if((float)vel.cx > maxx)
			len--;
		else
			break;
	}
	numChars = len;

    SetSize((float)vel.cx,(float)vel.cy);
	throw_going=true;
	focusable=false;	// tento widget nelze focusovat (kliknutim mysi)
	texts=0;

	switch(align)
	{
		case aLeft :
			break;
		case aRight :
			x += maxx - (float)vel.cx;
			break;
		case aCenter :
			x += floorf((maxx - (float)vel.cx)/2 + 0.5f);
			break;
	}
	text = new CBltText(x,y,(float)vel.cx,(float)vel.cy,originalText,len,font, color,0,0);

	maxX=maxx;
	maxY=linesSpaces=0;
	alignment=align;
	maxNumLines=0;

	AddToTrash();
}
Ejemplo n.º 7
0
void CGUIStaticText::CreateText(char *_text, CGUIFont* _font, float _x, float _y, int color, float maxx, float maxy, EGUIAlignment align, int maxLines, float LinesSpaces)
{
	float gsx=0, gsy=0;
	int numLinesNEW = 0;
	int i,len,l;
	HFONT font;

	char* startLine[256];
	int lengthLine[256];

	if(!_text || !_font || !_font->font)
	{
		throw CExc(eGUI, E_BAD_ARGUMENTS,"CGUIStaticText::CreateText> Text or Font not assigned");
	}

	if(guiFont != _font)
	{
		guiFont->Release();
		_font->AddRef();
	}
	guiFont = _font;
	font = guiFont->font;

	char *newText = KRKAL->CopyOutStrByLanguage(_text);
	int lengthText = strlen(newText);
	SIZE vel;

	SelectObject(mainGUI->dc,font);


	for( i=0;i<200;i++)
	{
		startLine[i]=0;
		lengthLine[i]=0;
	}

	startLine[0] = newText;

	while(lengthText>0)
	{
		len = 0;
		while(1)
		{
			GetTextExtentPoint32(mainGUI->dc,startLine[numLinesNEW],len,&vel);

			if(startLine[numLinesNEW][len] == '\n')
			{
				lengthLine[numLinesNEW] = len;
				startLine[numLinesNEW+1] = startLine[numLinesNEW] + lengthLine[numLinesNEW] + 1;
				lengthText -= lengthLine[numLinesNEW]+1;
				break;
			}

			if(maxx>0 && ((float)vel.cx > maxx))
			{
				l = len-1;
				while((startLine[numLinesNEW][l] != ' ') && (startLine[numLinesNEW] != &startLine[numLinesNEW][l]) )
					l--;
				if(startLine[numLinesNEW] == &startLine[numLinesNEW][l])
				{
					lengthLine[numLinesNEW] = len-1;
					startLine[numLinesNEW+1] = startLine[numLinesNEW] + lengthLine[numLinesNEW];
					lengthText -= lengthLine[numLinesNEW];
				}else{
					lengthLine[numLinesNEW] = l;
					startLine[numLinesNEW+1] = startLine[numLinesNEW] + lengthLine[numLinesNEW] + 1;
					lengthText -= lengthLine[numLinesNEW]+1;
				}
				break;
			}else
				len++;
			
			if(len>lengthText)
			{
				lengthLine[numLinesNEW] = lengthText;
				lengthText -= lengthLine[numLinesNEW];
				break;
			}
		}
		gsy += (float)vel.cy + LinesSpaces;
		if(maxy && gsy>maxy)
			break;	// pretekla mi max. vyska textu => dalsi text j*z nezpracovavam
		numLinesNEW++;
		if(maxLines && maxLines<=numLinesNEW)
			break;
	}

	if(numLinesNEW==0)
	{
		if(text){
			CBltWindow *wnd = text->GetParent();
			if(wnd){
				wnd->DeleteChild(text);
			}else{
				delete text;
			}
			text=0;
		}

		if(texts){
			for(int i=0;i<numLines;i++)
			{
				CBltWindow *wnd = texts[i]->GetParent();
				if(wnd){
					wnd->DeleteChild(texts[i]);
				}else{
					delete texts[i];
				}
			}
			SAFE_DELETE_ARRAY(texts);
		}

		SAFE_DELETE(originalText);

		if(guiFont)
			guiFont->Release();


		SetSize(0,0);
		guiFont=0;
		numChars=0;
		numLines=0;
		return;
	}

	CBltWindow *rootWnd=0;
	CBltElem* pos=0;

	if(texts)
	{
		rootWnd = texts[numLines-1]->GetParent();
		pos = texts[numLines-1];
	}

	numChars=0;
	CBltText **textsNEW = new CBltText*[numLinesNEW];
	for(i=0;i<numLinesNEW;i++)
	{
		GetTextExtentPoint32(mainGUI->dc,startLine[i],lengthLine[i],&vel);
		numChars+=lengthLine[i];
		if(gsx < (float)vel.cx)
			gsx = (float)vel.cx;
		if(i<numLines)
		{
			switch(align)
			{
				case aLeft :
					texts[i]->SetText(floorf(x),floorf(_y),(float)vel.cx,(float)vel.cy,startLine[i],lengthLine[i],font, color,0,0);
					break;
				case aRight :
					texts[i]->SetText(x+maxx - (float)vel.cx,_y,(float)vel.cx,(float)vel.cy,startLine[i],lengthLine[i],font, color,0,0);
					break;
				case aCenter :
					texts[i]->SetText(x+floorf((maxx - (float)vel.cx)/2 + 0.5f),_y,(float)vel.cx,(float)vel.cy,startLine[i],lengthLine[i],font, color,0,0);
					break;
			}
			textsNEW[i] = texts[i];
		}
		else
		{
			switch(align)
			{
				case aLeft :
					textsNEW[i] = new CBltText(floorf(x),floorf(_y),(float)vel.cx,(float)vel.cy,startLine[i],lengthLine[i],font, color,0,0);
					break;
				case aRight :
					textsNEW[i] = new CBltText(x+maxx - (float)vel.cx,_y,(float)vel.cx,(float)vel.cy,startLine[i],lengthLine[i],font, color,0,0);
					break;
				case aCenter :
					textsNEW[i] = new CBltText(x+floorf((maxx - (float)vel.cx)/2 + 0.5f),_y,(float)vel.cx,(float)vel.cy,startLine[i],lengthLine[i],font, color,0,0);
					break;
			}
			if(rootWnd)
			{
				rootWnd->AddChildToFrontOfEl(textsNEW[i],pos);
				pos=textsNEW[i];
			}
		}
		_y += (float)vel.cy + LinesSpaces;
	}

	if(numLinesNEW<=1)
	{
		SetSize((float)vel.cx,(float)vel.cy);
	}else{
		if((maxx > 0) && (align != aLeft))
			SetSize(maxx,gsy - LinesSpaces);
		else
			SetSize(gsx,gsy - LinesSpaces);	// pokud neni sirka radky nastavena, nastavi se na nejsirsi pouzity radek 
											//(odradkovani se provadi jen na '\n')
	}

	if(numLinesNEW<numLines)
	{
		for(int i=numLinesNEW;i<numLines;i++)
		{
			if(rootWnd)
				rootWnd->RemoveChild(texts[i]);

			delete texts[i];
		}
	}


	SAFE_DELETE_ARRAY(originalText);
	originalText=newText;

	SAFE_DELETE_ARRAY(texts);
	texts = textsNEW;
	numLines = numLinesNEW;
}
Ejemplo n.º 8
0
CGUIStaticText::CGUIStaticText(const char *_text, CGUIFont* _font, float _x, float _y, int color, float maxx, float maxy, EGUIAlignment align, int maxLines, float LinesSpaces, UINT dwDTFormat)
	: CGUIWidget(_x,_y)
{
	float gsx=0, gsy=0;
	numLines = 0;
	int i,len,l;
	HFONT font;

	char* startLine[256];
	int lengthLine[256];

	if(!_text || !_font || !_font->font)
	{
		throw CExc(eGUI, E_BAD_ARGUMENTS,"CGUIStaticText::CGUIStaticText> Text or Font not assigned");
	}

	guiFont = _font;
	font = guiFont->font;

	originalText = KRKAL->CopyOutStrByLanguage(_text);
	int lengthText = strlen(originalText);
	SIZE vel;

	SelectObject(mainGUI->dc,font);


	for( i=0;i<200;i++)
	{
		startLine[i]=0;
		lengthLine[i]=0;
	}

	startLine[0] = originalText;

	while(lengthText>0)
	{
		len = 0;
		while(1)
		{
			GetTextExtentPoint32(mainGUI->dc,startLine[numLines],len,&vel);

			if(startLine[numLines][len] == '\n')
			{
				lengthLine[numLines] = len;
				startLine[numLines+1] = startLine[numLines] + lengthLine[numLines] + 1;
				lengthText -= lengthLine[numLines]+1;
				break;
			}

			if(maxx>0 && ((float)vel.cx > maxx))
			{
				l = len-1;
				while((startLine[numLines][l] != ' ') && (startLine[numLines] != &startLine[numLines][l]) )
					l--;
				if(startLine[numLines] == &startLine[numLines][l])
				{
					lengthLine[numLines] = len-1;
					startLine[numLines+1] = startLine[numLines] + lengthLine[numLines];
					lengthText -= lengthLine[numLines];
				}else{
					lengthLine[numLines] = l;
					startLine[numLines+1] = startLine[numLines] + lengthLine[numLines] + 1;
					lengthText -= lengthLine[numLines]+1;
				}
				break;
			}else
				len++;
			
			if(len>lengthText)
			{
				lengthLine[numLines] = lengthText;
				lengthText -= lengthLine[numLines];
				break;
			}
		}
		gsy += (float)vel.cy + LinesSpaces;
		if(maxy && gsy>maxy)
			break;	// pretekla mi max. vyska textu => dalsi text j*z nezpracovavam
		numLines++;
		if(maxLines && maxLines<=numLines)
			break;
	}

	if(numLines==0)
	{
		text = 0;
		texts = 0;
		SetSize(0,0);
		throw_going=true;
		focusable=false;	// tento widget nelze focusovat (kliknutim mysi)
		guiFont=0;
		numChars=0;
		AddToTrash();
		return;
	}

	numChars=0;
	texts = new CBltText*[numLines];
	for(i=0;i<numLines;i++)
	{
		GetTextExtentPoint32(mainGUI->dc,startLine[i],lengthLine[i],&vel);
		numChars+=lengthLine[i];
		if(gsx < (float)vel.cx)
			gsx = (float)vel.cx;
		switch(align)
		{
			case aLeft :
				texts[i] = new CBltText(floorf(x),floorf(_y),(float)vel.cx,(float)vel.cy,startLine[i],lengthLine[i],font, color,dwDTFormat,0);
				break;
			case aRight :
				texts[i] = new CBltText(x + maxx - (float)vel.cx, _y, (float)vel.cx, (float)vel.cy, startLine[i], lengthLine[i], font, color, dwDTFormat, 0);
				break;
			case aCenter :
				texts[i] = new CBltText(x + floorf((maxx - (float)vel.cx) / 2 + 0.5f), _y, (float)vel.cx, (float)vel.cy, startLine[i], lengthLine[i], font, color, dwDTFormat, 0);
				break;
		}
		_y += (float)vel.cy + LinesSpaces;
	}

	if(numLines<=1)
	{
		SetSize((float)vel.cx,(float)vel.cy);
	}else{
		if((maxx > 0) && (align != aLeft))
			SetSize(maxx,gsy - LinesSpaces);
		else
			SetSize(gsx,gsy - LinesSpaces);	// pokud neni sirka radky nastavena, nastavi se na nejsirsi pouzity radek 
											//(odradkovani se provadi jen na '\n')
	}

	throw_going=true;
	focusable=false;	// tento widget nelze focusovat (kliknutim mysi)
	text = 0;
	guiFont->AddRef();

	maxX=maxx;
	maxY=maxy;
	linesSpaces=LinesSpaces;
	alignment=align;
	maxNumLines=maxLines;

	AddToTrash();
}
Ejemplo n.º 9
0
//intiace - vola se ze vsech konstruktoru
void CGUIEditWindow::InitEditWindow(float _x, float _y, float _sx, float _sy, CDXFont *dxfont, int textColor, int bgColor, int wordwrap,int noscrollbars, int _multiline, int xmargin, int ymargin, char *title_text, CGUIRectHost *_icon, UINT resizer_color)
{
	RemoveFromTrash(); //kvuli vyjimkam - viz. GUI

	highlight = 0; //bez highlitovani
	highlighter = 0;

	selectonfocus = 0;
	changed = 0;

	mousesel=0; //mysi se ted nic neoznacuje

	SetAvailableEvents(availableEvents,numEvents); //nastavi eventy

	multiline = _multiline; //viceradkovy editor?

	defaultfont=0; //defaultni font

	allowtab = 0; //zakazu psat tab

	readonly = 0; //neni readonly

	if(title_text)
	{	//pridam titulek, ikonu, resizery a scrolbary

		AddResizers(resizer_color,styleSet->Get("Resizers")); //pridam resizery
		AddTitle(title_text,_icon,STD_TITLE_HEIGHT); //pridam titulek

		//nastavi velikost viewportu
		CGUIStyle &style = *(styleSet->Get("Resizers"));
		SetViewPortBackWnd(style[1], style[0]+STD_TITLE_HEIGHT, _sx - 2*style[1], _sy - 2*style[0]-STD_TITLE_HEIGHT);

		//nastavi limity velikosti
		SetLimitSizes(4*STD_TITLE_HEIGHT,3*STD_SCROLLBAR_WIDTH+STD_TITLE_HEIGHT+2*style[0]);

		if(!noscrollbars)
		{
			//pridam vertikalni scrolbar
			CGUIScrollBar* dual = AddSimpleScrollbar(Vertical, sx-style[1]-STD_SCROLLBAR_WIDTH, style[0]+STD_TITLE_HEIGHT, STD_SCROLLBAR_WIDTH, sy - 2*style[0]-STD_TITLE_HEIGHT, STD_SCROLLBAR_SHIFT, 0, STD_SCROLLBAR_SLIDER_RESIZE,0,0,0);

			if(!wordwrap) //budu zalamovat radky?
			{
				//nebudu - pridam horizontalni scrolbar
				AddSimpleScrollbar(Horizontal, style[1], sy-style[0]-STD_SCROLLBAR_WIDTH, STD_SCROLLBAR_WIDTH, sx - 2*style[1], STD_SCROLLBAR_SHIFT, 0, STD_SCROLLBAR_SLIDER_RESIZE,dual,0,0);
			}
		}

	}else{
		//bez titulku, ramecku
		if(!noscrollbars)
		{
			//pridam vertikalni scrolbar
			CGUIScrollBar* dual = AddSimpleScrollbar(Vertical, sx-STD_SCROLLBAR_WIDTH, 0, STD_SCROLLBAR_WIDTH, sy, STD_SCROLLBAR_SHIFT, 0, STD_SCROLLBAR_SLIDER_RESIZE,0,0,0);

			if(!wordwrap) //budu zalamovat radky?
			{
				//nebudu - pridam horizontalni scrolbar
				AddSimpleScrollbar(Horizontal, 0, sy-STD_SCROLLBAR_WIDTH, STD_SCROLLBAR_WIDTH, sx, STD_SCROLLBAR_SHIFT, 0, STD_SCROLLBAR_SLIDER_RESIZE,dual,0,0);
			}
		}
	}

	//chci dostaveat zpravy od mysi a timeru
	MsgAcceptConsume(MsgMouseL|MsgMouseOver);
	MsgAcceptConsume(MsgTimer);

	text = NULL;
	blttext = NULL;

	focused=0; //nejsem focusovany

	if(!dxfont)
	{   //neni zadam defaultni font
		dxfont = (CDXFont*) RefMgr->Find(DEFAULT_EDITFONT); //najdu defaultfont
		if(!dxfont)
		{
			//chyba - font neexistuje
			throw CExc(eGUI,E_BAD_ARGUMENTS,"Font not exist!");
		}
	}

	dxfont->AddRef(); //zvysim pocet referenci
	defaultfont = dxfont;

	text = new CFormatedText(dxfont,textColor,wordwrap); //vytvori formatovany text

	CTextBlock *b;
	b = new CTextBlock("");
	text->AddTextBlockToEnd(b); //pridam jeden prazdny blok
	
	blttext = new CBltFormatedText(text,xmargin,ymargin); //vytvori zobrazovac formatovaneho textu

	back->AddChild(blttext); //pridam zobrazovac dovnitr okna

	text->CreateCursor(); //vytvori textovy kurzor
	blttext->SetCursor(1);

	if(_sy<0) //pokud _sy<0 -> spocitam vysku editboxu
	{
		_sy = (float)blttext->CmpEditBoxHeight();
		Resize(_sx,_sy);
	}

	float xx,yy;
	GetVPSize(xx,yy); //zjisti velikost viewportu
	blttext->SetWidth((int)xx); //nastavi sirku textu - pro zalamovani
	SetTextSize(); //opravi scrollbary

	ScrollToCursor();

	blttext->BlinkCursor(0); //zmizi kurzor

	cursorblinktimerID=0;
	mousescrolltimerID=0;
	mousescroll=0;

	AddToTrash(); //kvuli vyjimkam - viz. GUI

}
Ejemplo n.º 10
0
bool HeaderReader::parse(const unsigned char* in, size_t in_len)
{
	if(in == NULL)
		throw CExc(CExc::File::header, __LINE__);
	if (in_len < 9)
		return false;
	
	if (!cmpchars((const char*)in, "<nppcrypt", 9))
	{
		// header-version < 1010
		if (cmpchars((const char*)in, "nppcrypt", 8))
		{
			parse_old(in, in_len);
			return true;
		}
		else {
			return false;
		}
	}

	size_t					offset = 10;
	size_t					body_start;
	tinyxml2::XMLError		xml_err;
	tinyxml2::XMLDocument	xml_doc;
	crypt::Options::Crypt	t_options = options;

	// find header body start:
	while (offset < in_len - 11 && in[offset] != '\n')
		offset++;
	body_start = offset + 1;
	pContent = (const char*)in + body_start;

	// find header end:
	while (offset < in_len - 11 && !cmpchars((const char*)in + offset, "</nppcrypt>", 11))
		offset++;
	if (offset > in_len - 12) {
		throw CExc(CExc::File::header, __LINE__, CExc::Code::parse_header);
	}
	content_len = offset - body_start;

	// ------ parse header:
	xml_err = xml_doc.Parse((const char*)in, offset + 11);
	if (xml_err != tinyxml2::XMLError::XML_NO_ERROR)
		throw CExc(CExc::File::header, __LINE__, CExc::Code::parse_header);
	tinyxml2::XMLElement* xml_nppcrypt = xml_doc.FirstChildElement();
	if (!xml_nppcrypt)
		throw CExc(CExc::File::header, __LINE__, CExc::Code::parse_header);

	// ------ check version:
	xml_err = xml_nppcrypt->QueryIntAttribute("version", &version);
	if (xml_err != tinyxml2::XMLError::XML_NO_ERROR)
		throw CExc(CExc::Code::header_version);

	// ------ valid hmac present?
	const char* pHMAC = xml_nppcrypt->Attribute("hmac");
	if (pHMAC) {
		if (strlen(pHMAC) > 256)
			throw CExc(CExc::Code::header_hmac_data);
		s_hmac = std::string(pHMAC);
		const char* pHMAC_hash = xml_nppcrypt->Attribute("hmac-hash");
		if (!crypt::help::getHash(pHMAC_hash, t_options.hmac.hash) || t_options.hmac.hash == crypt::Hash::sha3_256
			|| t_options.hmac.hash == crypt::Hash::sha3_384 || t_options.hmac.hash == crypt::Hash::sha3_512)
		{
			throw CExc(CExc::Code::header_hmac_hash);
		}
		xml_err = xml_nppcrypt->QueryIntAttribute("auth-key", &t_options.hmac.key_id);
		if (xml_err != tinyxml2::XMLError::XML_NO_ERROR)
			t_options.hmac.key_id = -1;
		if (t_options.hmac.key_id >= (int)preferences.getKeyNum() || t_options.hmac.key_id < -1) {
			throw CExc(CExc::Code::header_hmac_key);
		}
		t_options.hmac.enable = true;
	}

	// ------- valid IV or Salt present?
	tinyxml2::XMLElement* xml_random = xml_nppcrypt->FirstChildElement("random");
	t_options.key.salt_bytes = 0;
	if (xml_random) {
		const char* pSalt = xml_random->Attribute("salt");
		if (pSalt) {
			if (strlen(pSalt) > 2 * crypt::Constants::salt_max)
				throw CExc(CExc::Code::header_salt);
			s_init.salt = std::string(pSalt);
			try {
				std::string tsalt;
				CryptoPP::StringSource(s_init.salt, true, new CryptoPP::Base64Decoder(new CryptoPP::StringSink(tsalt)));
				t_options.key.salt_bytes = tsalt.size();
			}
			catch (CryptoPP::Exception&)
			{
				throw CExc(CExc::Code::header_salt);
			}
			if (t_options.key.salt_bytes < 1 || t_options.key.salt_bytes > crypt::Constants::salt_max)
				throw CExc(CExc::Code::header_salt);
		}
		const char* pIV = xml_random->Attribute("iv");
		if (pIV) {
			if (strlen(pIV) > 1024)
				throw CExc(CExc::Code::header_iv);
			s_init.iv = std::string(pIV);
		}
	}

	// ------- valid Cipher information present?
	tinyxml2::XMLElement* xml_crypt = xml_nppcrypt->FirstChildElement("encryption");
	if (xml_crypt) {
		const char* t = xml_crypt->Attribute("cipher");
		if (!crypt::help::getCipher(t, t_options.cipher))
			throw CExc(CExc::Code::header_cipher);
		t = xml_crypt->Attribute("mode");
		if (!crypt::help::getCipherMode(t, t_options.mode))
			throw CExc(CExc::Code::header_mode);
		t = xml_crypt->Attribute("encoding");
		if (!crypt::help::getEncoding(t, t_options.encoding.enc))
			throw CExc(CExc::Code::header_mode);
		if ((t = xml_crypt->Attribute("tag")) != NULL) {
			if (strlen(t) != 24)
				throw CExc(CExc::Code::header_tag);
			s_init.tag = std::string(t);
		}
	}

	// ------- valid key derivation information present?
	tinyxml2::XMLElement* xml_key = xml_nppcrypt->FirstChildElement("key");
	if (xml_key) {
		const char* t = xml_key->Attribute("algorithm");
		if (!crypt::help::getKeyDerivation(t, t_options.key.algorithm))
			throw CExc(CExc::Code::header_keyderi);

		switch (t_options.key.algorithm) {
		case crypt::KeyDerivation::pbkdf2:
		{
			t = xml_key->Attribute("hash");
			crypt::Hash thash;
			if (!crypt::help::getHash(t, thash) || thash == crypt::Hash::sha3_256 || thash == crypt::Hash::sha3_384 || thash == crypt::Hash::sha3_512)
				throw CExc(CExc::Code::header_pbkdf2);
			t_options.key.option1 = static_cast<int>(thash);
			if (!(t = xml_key->Attribute("iterations")))
				throw CExc(CExc::Code::header_pbkdf2);
			t_options.key.option2 = std::atoi(t);
			if (t_options.key.option2 < crypt::Constants::pbkdf2_iter_min || t_options.key.option2 > crypt::Constants::pbkdf2_iter_max)
				throw CExc(CExc::Code::header_pbkdf2);
			break;
		}
		case crypt::KeyDerivation::bcrypt:
			if (!(t = xml_key->Attribute("iterations")))
				throw CExc(CExc::Code::header_bcrypt);
			t_options.key.option1 = std::atoi(t);
			if (!((t_options.key.option1 != 0) && !(t_options.key.option1 & (t_options.key.option1 - 1))))
				throw CExc(CExc::Code::header_bcrypt);
			t_options.key.option1 = static_cast<int>(std::log(t_options.key.option1) / std::log(2));
			if (t_options.key.option1 < crypt::Constants::bcrypt_iter_min || t_options.key.option1 > crypt::Constants::bcrypt_iter_max)
				throw CExc(CExc::Code::header_bcrypt);
			break;
		case crypt::KeyDerivation::scrypt:
			if (!(t = xml_key->Attribute("N")))
				throw CExc(CExc::Code::header_scrypt);
			t_options.key.option1 = std::atoi(t);
			if (!((t_options.key.option1 != 0) && !(t_options.key.option1 & (t_options.key.option1 - 1))))
				throw CExc(CExc::Code::header_scrypt);
			t_options.key.option1 = static_cast<int>(std::log(t_options.key.option1) / std::log(2));
			if (t_options.key.option1 < crypt::Constants::scrypt_N_min || t_options.key.option1 > crypt::Constants::scrypt_N_max)
				throw CExc(CExc::Code::header_scrypt);
			if (!(t = xml_key->Attribute("r")))
				throw CExc(CExc::Code::header_scrypt);
			t_options.key.option2 = std::atoi(t);
			if (t_options.key.option2 < crypt::Constants::scrypt_r_min || t_options.key.option2 > crypt::Constants::scrypt_r_max)
				throw CExc(CExc::Code::header_scrypt);
			if (!(t = xml_key->Attribute("p")))
				throw CExc(CExc::Code::header_scrypt);
			t_options.key.option3 = std::atoi(t);
			if (t_options.key.option3 < crypt::Constants::scrypt_p_min || t_options.key.option3 > crypt::Constants::scrypt_p_max)
				throw CExc(CExc::Code::header_scrypt);
			break;
		}
		t = xml_key->Attribute("generateIV");
		if (t != NULL && strlen(t) == 4 && strcmp(t, "true") == 0) {
			t_options.iv = crypt::IV::keyderivation;
		}
		else {
			if (s_init.iv.size() > 0)
				t_options.iv = crypt::IV::random;
			else
				t_options.iv = crypt::IV::zero;
		}
	}

	options = t_options;
	if (in[offset + 11] == '\r' && in[offset + 12] == '\n')
	{
		pCData = in + offset + 13;
		cdata_len = in_len - offset - 13;
	}
	else if (in[offset + 11] == '\n')
	{
		pCData = in + offset + 12;
		cdata_len = in_len - offset - 12;
	}
	else
	{
		pCData = in + offset + 11;
		cdata_len = in_len - offset - 11;
	}
	return true;
}
Ejemplo n.º 11
0
void HeaderReader::parse_old(const unsigned char* in, size_t in_len)
{
	if (in_len > 16 && in[8] == 1)
	{
		// -------------------------- 1008/9 ----------------------------------------------------------------------------------------------------------------------------

		crypt::Cipher old_ciphers[] = { crypt::Cipher::blowfish, crypt::Cipher::des, crypt::Cipher::rc2, crypt::Cipher::idea, crypt::Cipher::cast128, crypt::Cipher::rijndael128,
			crypt::Cipher::rijndael256, crypt::Cipher::des_ede, crypt::Cipher::des_ede3, crypt::Cipher::desx, crypt::Cipher::rc4 };
		crypt::Mode old_modes[] = { crypt::Mode::cbc, crypt::Mode::ecb, crypt::Mode::cfb, crypt::Mode::ofb, crypt::Mode::ctr };

		if (in[9] < 0 || in[9] > 10)
			throw CExc(CExc::File::header, __LINE__, CExc::Code::parse_header);
		if (in[10] < 0 || in[10] > 4)
			throw CExc(CExc::File::header, __LINE__, CExc::Code::parse_header);
		options.cipher = old_ciphers[in[9]];
		options.mode = old_modes[in[10]];
		options.encoding.enc = (in[13] == 1) ? crypt::Encoding::base16 : crypt::Encoding::ascii;

		if (in[12] == 0) {
			options.key.algorithm = crypt::KeyDerivation::pbkdf2;
			options.key.option1 = static_cast<int>(crypt::Hash::md5);
			options.key.option2 = 1000;
		}
		else {
			throw CExc(CExc::Code::nppfile1009);
		}

		// default: no salt
		size_t header_len = 16;
		pCData = in + 16;
		cdata_len = in_len - 16;
		options.key.salt_bytes = 0;

		try
		{
			if (options.encoding.enc == crypt::Encoding::ascii) {
				if (in_len > 32 && cmpchars((const char*)in + 16, "Salted__", 8))
				{
					CryptoPP::StringSource(in + 24, 8, true, new CryptoPP::Base64Encoder(new CryptoPP::StringSink(s_init.salt), false));
					header_len = 32;
					pCData = in + 32;
					cdata_len = in_len - 32;
					options.key.salt_bytes = 8;
				}
			}
			else {
				unsigned char t[8];
				if (in_len > 48 && cmpchars((const char*)in + 16, "53616C7465645F5F", 16))
				{
					CryptoPP::StringSource(in + 32, 16, true, new CryptoPP::HexDecoder(new CryptoPP::ArraySink(t, 8)));
					header_len = 48;
					pCData = in + 48;
					cdata_len = in_len - 48;
					options.key.salt_bytes = 8;
				}
				else if (in_len > 64 && cmpchars((const char*)in + 16, "53 61 6C 74 65 64 5F 5F ", 24))
				{
					CryptoPP::StringSource(in + 40, 24, true, new CryptoPP::HexDecoder(new CryptoPP::ArraySink(t, 8)));
					header_len = 64;
					pCData = in + 64;
					cdata_len = in_len - 64;
					options.key.salt_bytes = 8;
				}
				if (options.key.salt_bytes == 8)
				{
					CryptoPP::StringSource(t, 8, true, new CryptoPP::Base64Encoder(new CryptoPP::StringSink(s_init.salt), false));
				}
			}
		}
		catch (CryptoPP::Exception&)
		{
			throw CExc(CExc::Code::header_salt);
		}

		pContent = (const char*)in;
		content_len = header_len;

		options.hmac.enable = false;
		options.iv = crypt::IV::keyderivation;
	}
	else {
		// -------------------------- 1007 ---------------------------------------------------------------------------------------------------------------------------
		throw CExc(CExc::Code::nppfile1007);
	}
}