#
#	ITSV GmbH
#	CCDB - Command and Control Database
#
#	FILE:		dquermfile_base64code.txt
#	DESCRIPTION:	DQUERY definition for CCDB DQUERY "base64code"
#
@querytitle	base64-Coding
@querydescription	
@group	SVCLEARCF
@querytype	jsexpr
@qexpression	that.result = new aux.Result({resulttype: 'dbresult',
		                               metaData: [{name:'base64'},{name:'cleartext'},{name:'encode'},{name:'decode'},{name:'decode_hex'},{name:'hexdump'},{name:'base64_hexdump'}],
		                               rows: [['((base64))','((cleartext))','encbutton','decbutton','dhxbutton','((hexdump))','bhxbutton']]});
@decoration.decorate_colcell.0	$TEXTAREA
@decoration.decorate_colcell.1	$TEXTAREA
@decoration.decorate_colcell.2	$BUTTON:encode:encbuttonoptions
@decoration.decorate_colcell.3	$BUTTON:decode:decbuttonoptions
@decoration.decorate_colcell.4	$BUTTON:decode_hex:dhxbuttonoptions
@decoration.decorate_colcell.5	$TEXTAREA
@decoration.decorate_colcell.6	$BUTTON:dump_hex:bhxbuttonoptions
@encbuttonoptions.action					jsexpr
@encbuttonoptions.expression				btoa(ps.cleartext)
@encbuttonoptions.parameters				cleartext
@encbuttonoptions.cleartext_parametername	cleartext
@encbuttonoptions.cleartext_fromcol			1
@encbuttonoptions.resulttocol				0
@decbuttonoptions.action					jsexpr
@decbuttonoptions.expression				atob(ps.base64)
@decbuttonoptions.parameters				base64
@decbuttonoptions.base64_parametername		base64
@decbuttonoptions.base64_fromcol			0
@decbuttonoptions.resulttocol				1
@dhxbuttonoptions.action					jsexpr
@dhxbuttonoptions.expression	
	var bb;
	try { 
		bb = atob(ps.base64);
	} catch (e) {
		bb = 'Error in atob: '+e.message;
	}
	console.log(bb);
	var bi = 0; 
	var ci = 0; 
	var res = ''; 
	var ra = ''; 
	var cc;
	while (bi<bb.length) {
		cc = bb.charCodeAt(bi);
		if (ci>15) {
		    ci = 0;
		    if (ra) res += ': ' + ra + '\n';
		    ra = '';
		}
		if (ci==0) {
			res += HEX(bi,4,4)+' : ';
		}
		res += HEX(cc,2,2)+' ';
		ra += ((cc>=32)&&(cc<127))?String.fromCharCode(cc):'.';
		bi++;
		ci++;
	}
	if (ra) res += ': ' + ra;
	res
@dhxbuttonoptions.parameters			base64
@dhxbuttonoptions.base64_parametername	base64
@dhxbuttonoptions.base64_fromcol		0
@dhxbuttonoptions.resulttocol			5
@bhxbuttonoptions.action				jsexpr
@bhxbuttonoptions.expression	
	var bb;
	bb = ps.base64;
	console.log(bb);
	var bi = 0; 
	var ci = 0; 
	var res = ''; 
	var ra = ''; 
	var cc;
	while (bi<bb.length) {
		cc = bb.charCodeAt(bi);
		if (ci>15) {
		    ci = 0;
		    if (ra) res += ': ' + ra + '\n';
		    ra = '';
		}
		if (ci==0) {
		  res += HEX(bi,4,4)+' : ';
		}
		res += HEX(cc,2,2)+' ';
		ra += ((cc>=32)&&(cc<127))?String.fromCharCode(cc):'.';
		bi++;
		ci++;
	}
	if (ra) res += ': ' + ra;
	res
@bhxbuttonoptions.parameters			base64
@bhxbuttonoptions.base64_parametername	base64
@bhxbuttonoptions.base64_fromcol		0
@bhxbuttonoptions.resulttocol			5
