예제 #1
0
	TEST(Base32, StringToBinary)
	{
		std::string base = "JBQWY3DPEBLWK3DU";
		std::vector<uint8_t> res = Base32::toBinary(base);
		std::string res_str(res.begin(), res.end());
		ASSERT_EQ(res_str, std::string("Hallo Welt"));
	}
예제 #2
0
파일: cmds.cpp 프로젝트: makestuff/neopurgo
int process_command_line(const char* line)
{
    int argc = 0;
    char** argv = NULL;
    int i, rc = 0;
    const char* str_cmd;

    if (line == NULL)
        return 0;

	current_command_line = line;

    if (parse_cmdline_args(line, &argc, &argv) < 0)
	{
		rc = -1;
		goto cleanup;
	}

    if (argc == 0)
        goto cleanup;

    for (i = 0; commands[i].fn != NULL; i++)
    {
        str_cmd = res_str(commands[i].res_id);
        if (strcmp(str_cmd, argv[0]) == 0)
        {
            rc = commands[i].fn(argc, (const char**)argv);
            break;
        }
    }
    
    if (commands[i].fn == NULL)
    {
        msgf(STR_UNKNOWN_COMMAND);
        rc = 0;
    }
    else
    {
        switch (rc)
        {
        case CMDLINE_INVALID_PARAMETER:
            msgf(STR_INVALID_PARAMETERS);
			help(argv[0]);
            break;
        }
    }

cleanup:

    if (argv != NULL)
        free(argv);

	current_command_line = NULL;

    return rc;
}
예제 #3
0
std::vector<std::string> PyInterpreter::Complete(const std::string& cmd, int max_options)
{
    std::vector<std::string> ret;
    PyErr_Clear();

    if(pycomplete) {
        for(int i=0; i < max_options; ++i) {
#if PY_MAJOR_VERSION >= 3
            PyUniqueObj args = PyTuple_Pack( 2, PyUnicode_FromString(cmd.c_str()), PyLong_FromSize_t(i) );
            PyUniqueObj result = PyObject_CallObject(pycomplete, args);
            if (result && PyUnicode_Check(result)) {
                std::string res_str(PyUnicode_AsUTF8(result));
#else
            PyUniqueObj args = PyTuple_Pack(2, PyString_FromString(cmd.c_str()), PyInt_FromSize_t(i));
            PyUniqueObj result = PyObject_CallObject(pycomplete, args);
            if (result && PyString_Check(result)) {
                std::string res_str(PyString_AsString(result));
#endif
                if( res_str.find("__")==std::string::npos ||
                    cmd.find("__")!=std::string::npos ||
                    (cmd.size() > 0 && cmd[cmd.size()-1] == '_')
                ) {
                    ret.push_back( res_str );
                }
            }else{
                break;
            }
        }
    }

    return ret;
}

void PyInterpreter::PushCommand(const std::string& cmd)
{
    PyUniqueObj obj = EvalExec(cmd);
    if(obj && obj != Py_None) {
        const std::string output = ToString(obj);
        line_queue.push(
            ConsoleLine(output, ConsoleLineTypeOutput)
        );
    }
}
예제 #4
0
int msgf(unsigned int id, ...)
{
    va_list args;
    int rc;
    
    va_start(args, id);
    rc = vprintf(res_str(id), args);
    rc += printf("\n");
    va_end(args);

    return rc;
}
예제 #5
0
파일: cmds.cpp 프로젝트: makestuff/neopurgo
int help(const char* cmd)
{
    int i;
    const char* str_cmd = NULL;
    const char* str_help = NULL;
    const char* str_usage = NULL;

    if (cmd == NULL)
    {
        printf("%s:\n\n", res_str(STR_HELP));
        for (i = 0; commands[i].fn != NULL; i++)
        {
            str_cmd = res_str(commands[i].res_id);
            str_help = str_cmd + strlen(str_cmd) + 1;

            printf("%-20s %s\n", str_cmd, str_help);
        }
        printf("\n");
    }
    else
    {
        for (i = 0; commands[i].fn != NULL; i++)
        {
            str_cmd = res_str(commands[i].res_id);
            if (strcmp(str_cmd, cmd) == 0)
            {
                str_help = str_cmd + strlen(str_cmd) + 1;
                str_usage = str_help + strlen(str_help) + 1;
                break;
            }
        }

        if (commands[i].fn != NULL)
			printf("%s\n\n%s\n", str_help, str_usage);
        else
            msgf(STR_UNKNOWN_COMMAND);
    }
    return 0;
}
예제 #6
0
QuiddityCommand::ptr QuiddityCommand::make_command_from_tree(InfoTree::ptr tree) {
  QuiddityCommand::ptr command = std::make_shared<QuiddityCommand>();
  std::string command_str = tree->branch_get_value("command");
  command->set_id(QuiddityCommand::get_id_from_string(command_str.c_str()));
  command->time_ = tree->branch_get_value("calling time");
  {
    std::string args_str("arguments.");
    auto arguments = tree->get_child_keys(args_str);
    for (auto& it : arguments) {
      std::string arg = tree->branch_get_value(args_str + it);
      if (!arg.empty()) {
        command->add_arg(arg);
      } else {
        command->add_arg("null");
      }
    }
  }
  {
    std::string vect_str("vector argument.");
    auto arguments = tree->get_child_keys(vect_str);
    std::vector<std::string> string_vect_arg;
    for (auto& it : arguments) {
      std::string arg = tree->branch_get_value(vect_str + it);
      if (!arg.empty()) {
        string_vect_arg.push_back(arg);
      } else {
        string_vect_arg.push_back("null");
      }
    }
    command->set_vector_arg(string_vect_arg);
  }
  {
    std::string res_str("results.");
    auto results = tree->get_child_keys(res_str);
    std::vector<std::string> expected_result;
    for (auto& it : results) {
      std::string res = tree->branch_get_value(res_str + it);
      if (!res.empty()) expected_result.push_back(res);
    }
    command->expected_result_ = expected_result;
  }
  return command;
}
예제 #7
0
파일: cmds.cpp 프로젝트: makestuff/neopurgo
int cmd_chips(int argc, const char** argv)
{
    chip_database::iterator iter;
    chip_family* family;
    chip_family::iterator iter2;
    string desc;
    int all;
    char support[16];

    all = cmdline_has_opt(argc, argv, "all");

    printf("%s:\n\n", res_str(STR_SUPPORTED_FAMILIES));
        
    for (iter = g.chips.begin(); iter != g.chips.end(); iter++)
    {
        family = *iter;

        // Family description
        family->vars.get(strDESC, desc);

        // Supported operations
        strcpy(support, "[D   ]");
        if (family->vars.exists(strERASE) == 0)
            support[2] = 'E';
        if (family->vars.exists(strPROGRAM) == 0)
            support[3] = 'P';
        if (family->vars.exists(strREADBACK) == 0)
            support[4] = 'R';
                    
        printf("%-40s %s\n", desc.c_str(), support);

        // Show devices in family
        if (all)
            for (iter2 = family->begin(); iter2 != family->end(); iter2++)
                printf("    %s\n", (*iter2).name.c_str());
    }
    
    return 0;
}
예제 #8
0
//decompress a block of LLSD from provided istream
// not very efficient -- creats a copy of decompressed LLSD block in memory
// and deserializes from that copy using LLSDSerialize
bool unzip_llsd(LLSD& data, std::istream& is, S32 size)
{
	U8* result = NULL;
	U32 cur_size = 0;
	z_stream strm;
		
	const U32 CHUNK = 65536;

	U8 *in = new U8[size];
	is.read((char*) in, size); 

	U8 out[CHUNK];
		
	strm.zalloc = Z_NULL;
	strm.zfree = Z_NULL;
	strm.opaque = Z_NULL;
	strm.avail_in = size;
	strm.next_in = in;

	S32 ret = inflateInit(&strm);

	do
	{
		strm.avail_out = CHUNK;
		strm.next_out = out;
		ret = inflate(&strm, Z_NO_FLUSH);
		if (ret == Z_STREAM_ERROR)
		{
			inflateEnd(&strm);
			free(result);
			delete [] in;
			return false;
		}
		
		switch (ret)
		{
		case Z_NEED_DICT:
			ret = Z_DATA_ERROR;
		case Z_DATA_ERROR:
		case Z_MEM_ERROR:
			inflateEnd(&strm);
			free(result);
			delete [] in;
			return false;
			break;
		}

		U32 have = CHUNK-strm.avail_out;

		result = (U8*) realloc(result, cur_size + have);
		memcpy(result+cur_size, out, have);
		cur_size += have;

	} while (ret == Z_OK);

	inflateEnd(&strm);
	delete [] in;

	if (ret != Z_STREAM_END)
	{
		free(result);
		return false;
	}

	//result now points to the decompressed LLSD block
	{
		std::string res_str((char*) result, cur_size);

		std::string deprecated_header("<? LLSD/Binary ?>");

		if (res_str.substr(0, deprecated_header.size()) == deprecated_header)
		{
			res_str = res_str.substr(deprecated_header.size()+1, cur_size);
		}
		cur_size = res_str.size();

		std::istringstream istr(res_str);
		
		if (!LLSDSerialize::fromBinary(data, istr, cur_size))
		{
			llwarns << "Failed to unzip LLSD block" << llendl;
			free(result);
			return false;
		}
	}

	free(result);
	return true;
}
예제 #9
0
int ipmi_cmdraw_ms(uchar cmd, uchar netfn, uchar lun, uchar sa,
		uchar bus, uchar *pdata, int sdata, uchar *presp, int *sresp, 
		uchar *pcc, char fdebugcmd)
{
   int bRet;
   HRESULT hres;
   IWbemClassObject* pInParams = NULL; /*class definition*/
   IWbemClassObject* pInReq = NULL;    /*instance*/
   IWbemClassObject* pOutResp = NULL;
   VARIANT varCmd, varNetfn, varLun, varSa, varSize, varData;
   SAFEARRAY* psa = NULL;
   long i;
   uchar *p;

   fdebugms = fdebugcmd;
   if (!fmsopen) {
      bRet = ipmi_open_ms(fdebugcmd);
      if (bRet != 0) return(bRet);
   }
   bRet = -1;


   hres = pClass->GetMethod(L"RequestResponse",0,&pInParams,NULL);
   if (FAILED(hres)) {
        if (fdebugcmd) 
	     printf("ipmi_cmdraw_ms: Cannot get RequestResponse method\n");
        return (bRet);
   }

#ifdef WDM_FIXED
   /* see http://support.microsoft.com/kb/951242 for WDM bug info */
   hres = pInParams->SpawnInstance(0,&pInReq);
   if (FAILED(hres)) {
        if (fdebugcmd) 
	     printf("ipmi_cmdraw_ms: Cannot get RequestResponse instance\n");
        return (bRet);
   }
   // also substitute pInReq for pInParams below if this gets fixed.
#endif

   VariantInit(&varCmd);
   varCmd.vt = VT_UI1;
   varCmd.bVal = cmd;
   hres = pInParams->Put(_bstr_t(L"Command"), 0, &varCmd, 0);
   // VariantClear(&varCmd);
   if (FAILED(hres)) goto MSRET;

   VariantInit(&varNetfn);
   varNetfn.vt = VT_UI1;
   varNetfn.bVal = netfn;
   hres = pInParams->Put(_bstr_t(L"NetworkFunction"), 0, &varNetfn, 0);
   // VariantClear(&varNetfn);
   if (FAILED(hres)) goto MSRET;

   VariantInit(&varLun);
   varLun.vt = VT_UI1;
   varLun.bVal = lun;
   hres = pInParams->Put(_bstr_t(L"Lun"), 0, &varLun, 0);
   // VariantClear(&varLun);
   if (FAILED(hres)) goto MSRET;

   VariantInit(&varSa);
   varSa.vt = VT_UI1;
   varSa.bVal = sa;
   hres = pInParams->Put(_bstr_t(L"ResponderAddress"), 0, &varSa, 0);
   // VariantClear(&varSa);
   if (FAILED(hres)) goto MSRET;

   VariantInit(&varSize);
   varSize.vt = VT_I4;
   varSize.lVal = sdata;
   hres = pInParams->Put(_bstr_t(L"RequestDataSize"), 0, &varSize, 0);
   // VariantClear(&varSize);
   if (FAILED(hres)) goto MSRET;

   SAFEARRAYBOUND rgsabound[1];
   rgsabound[0].cElements = sdata;
   rgsabound[0].lLbound = 0;
   psa = SafeArrayCreate(VT_UI1,1,rgsabound);
   if(!psa) {
      printf("ipmi_cmdraw_ms: SafeArrayCreate failed\n");
      goto MSRET;
   }
#ifdef SHOULD_WORK_BUT_NO
   /* The SafeArrayPutElement does not put the data in the right 
    * place, so skip this and copy the raw data below. */
   VARIANT tvar;
   if (fdebugcmd && sdata > 0) 
	{ printf("psa1(%p):",psa); dumpbuf((uchar *)psa,42,1); }   

   for(i =0; i< sdata; i++)
   {
      VariantInit(&tvar);
      tvar.vt = VT_UI1;
      tvar.bVal = pdata[i];
      hres = SafeArrayPutElement(psa, &i, &tvar);
      // VariantClear(&tvar);
      if (FAILED(hres)) { 
         printf("ipmi_cmdraw_ms: SafeArrayPutElement(%d) failed\n",i);
         goto MSRET;
      }
   } /*end for*/
   if (fdebugcmd && sdata > 0) 
	{ printf("psa2(%p):",psa); dumpbuf((uchar *)psa,42,1); }  
#endif

   /* Copy the real RequestData into psa */
   memcpy(psa->pvData,pdata,sdata);

   VariantInit(&varData);
   varData.vt = VT_ARRAY | VT_UI1;
   varData.parray = psa;
   hres = pInParams->Put(_bstr_t(L"RequestData"), 0, &varData, 0);
   // VariantClear(&varData);
   if (FAILED(hres)) {
	printf("Put(RequestData) error %x\n",hres);
        goto MSRET;
   }

#ifdef TEST_METHODS
   IWbemClassObject* pOutSms = NULL;
   if (fdebugcmd) printf("ipmi_cmdraw_ms: calling SMS_Attention(%ls)\n",
			  V_BSTR(&varPath)); 
   hres = pSvc->ExecMethod( V_BSTR(&varPath), _bstr_t(L"SMS_Attention"), 
				0, NULL, NULL, &pOutSms, NULL);
   if (FAILED(hres)) {
	printf("ipmi_cmdraw_ms: SMS_Attention method error %x\n",hres);
        goto MSRET;
   }
   if (fdebugcmd) printf("ipmi_cmdraw_ms: SMS_Attention method ok\n"); 
   /* This does work, without input parameters */
   pOutSms->Release();
#endif

   hres = pSvc->ExecMethod( V_BSTR(&varPath), _bstr_t(L"RequestResponse"), 
				0, NULL, pInParams, &pOutResp, NULL);
   if (fdebugcmd) {
       printf("ipmi_cmdraw_ms(cmd=%x,netfn=%x,lun=%x,sa=%x,sdata=%d)"
	      " RequestResponse ret=%x\n", cmd,netfn,lun,sa,sdata,hres); 
       if (sdata > 0) {
	   printf("ipmi_cmdraw_ms: req data(%d):",sdata); 
	   dumpbuf(pdata,sdata,0); 
       }
   }
   if (FAILED(hres)) {
	printf("ipmi_cmdraw_ms: RequestResponse error %x %s\n",
		hres,res_str(hres));
#ifdef EXTRA_DESC
	/* This does not usually add any meaning for IPMI. */
	BSTR desc;
	IErrorInfo *pIErrorInfo;
	GetErrorInfo(0,&pIErrorInfo);
	pIErrorInfo->GetDescription(&desc);
	printf("ipmi_cmdraw_ms: ErrorInfoDescr: %ls\n",desc);
	SysFreeString(desc);
#endif
	bRet = -1; 
	/*fall through for cleanup and return*/
   }
   else {  /*successful, get ccode and response data */
	VARIANT varByte, varRSz, varRData;
        VariantInit(&varByte);
        VariantInit(&varRSz);
        VariantInit(&varRData);
	long rlen;

	hres = pOutResp->Get(_bstr_t(L"CompletionCode"),0, &varByte, NULL, 0);
	if (FAILED(hres)) goto MSRET;
	if (fdebugcmd) printf("ipmi_cmdraw_ms: CompletionCode %x returned\n",
				V_UI1(&varByte) );
	*pcc = V_UI1(&varByte);

	hres = pOutResp->Get(_bstr_t(L"ResponseDataSize"),0, &varRSz, NULL, 0);
	if (FAILED(hres)) goto MSRET;
        rlen = V_I4(&varRSz);
	if (rlen > 1) rlen--;   /*skip cc*/
	if (rlen > *sresp) {
	   if (fdebugcmd) printf("ResponseData truncated from %d to %d\n",
					rlen,*sresp);
	   rlen = *sresp; /*truncate*/
	}
	*sresp = (int)rlen;

	hres = pOutResp->Get(_bstr_t(L"ResponseData"),0, &varRData, NULL,0);
	if (FAILED(hres)) { /*ignore failure */ 
	   if (fdebugcmd) printf("Get ResponseData error %x\n",hres); 
	} else {  /* success */
#ifdef SHOULD_WORK_BUT_NO
	    uchar *pa;
	    p = (uchar*)varRData.parray->pvData;
	    pa = (uchar*)varRData.parray;
	    printf("pa=%p, pa+12=%p p=%p\n",pa,(pa+12),p);
	    if (fdebugcmd) {   
		 printf("Data.vt = %04x, Data.parray(%p):",
			varRData.vt, varRData.parray); 
	         // 0x2011 means VT_ARRAY | VT_UI1
		 dumpbuf((uchar *)varRData.parray,40,1);
	    }
	    /* The SafeArrayGetElement does not get the data from the right 
	     * place, so skip this and copy the raw data below. */
	    VARIANT rgvar[NVAR];
	    if (rlen > NVAR) *pcc = 0xEE; 
	    for (i = 0; i <= rlen; i++)
         	VariantInit(&rgvar[i]);
	    /* copy the response data from varRData to presp */
	    for( i = 0; i <= rlen; i++)
	    {
		hres = SafeArrayGetElement(varRData.parray, &i, &rgvar[i]);
		if (FAILED(hres)) { 
		   if (fdebugcmd)
		      printf("ipmi_cmdraw_ms: SafeArrayGetElement(%d) failed\n",i);
		   break;
		}
		if (fdebugcmd) {   
		     printf("Data[%d] vt=%02x val=%02x, rgvar(%p):",i,
				rgvar[i].vt, V_UI1(&rgvar[i]),&rgvar[i]);
		     dumpbuf((uchar *)&rgvar[i],12,0);
		}
	        /* skip the completion code */
	    	// if (i > 0) presp[i-1] = V_UI1(&rgvar[i]);
	    } /*end for*/
#endif
	    /* 
	     * parray from a GetDeviceId response:
	     * 0015CEE0: 01 00 80 00 01 00 00 00 00 00 00 00 00 cf 15 00
	     * 0015CEF0: 10 00 00 00 00 00 00 00 03 00 06 00 95 01 08 00
             *           ^- datalen=0x10
	     * 0015CF00: 00 20 01 00 19 02 9f 57 01 ...  
             *           ^- start of data (cc=00, ...)
	     */
	    /* Copy the real ResponseData into presp. */
	    p = (uchar*)varRData.parray->pvData;
	    for( i = 0; i <= rlen; i++) {
	        /* skip the completion code */
	    	if (i > 0) presp[i-1] = p[i];
	    }
	    if (fdebugcmd) {
		printf("ipmi_cmdraw_ms: resp data(%d):",rlen+1); 
		dumpbuf(p,rlen+1,0); 
	    }
	}
	bRet = 0;
   }

MSRET:
#define CLEAN_OK  1
#ifdef CLEAN_OK
   /* VariantClear(&var*) should be done by pInParams->Release() */
   if (psa != NULL) SafeArrayDestroy(psa);
   if (pInParams != NULL) pInParams->Release();
   if (pOutResp != NULL) pOutResp->Release();
#endif
   return(bRet);
}