/** * Construct using a base64 string * @param value the string */ explicit CppIntegerData(const QString &string) { QByteArray data = FromBase64(string); _integer = CryptoPP::Integer( reinterpret_cast<const byte *>(data.constData()), data.size()); }
//================================================================== bool FileManagerAndroid::GrabFile(const char* pFileName, DVec<U8> &out_data, const char* pMode) { DFUNCTION(); DLOG("File: %s", pFileName); #if defined(ANDROID) bool prefs = isPrefsMode( pMode ); if (prefs) { // Read from the prefs storage DLOG("Grabbing file (prefs): %s", pFileName); char *contents = ::DoReadPrefs(pFileName); if (0 == contents) { DLOG("Failed to open the file"); return false; } // Decode into binary size_t binLength = 0; void *binData = FromBase64(contents, &binLength); free(contents); if (binData == 0) { DPRINT("!! File '%s' was not base64 format. Rejecting.", pFileName); return false; } // Copy into output array and free original // TODO: Could be sped up by pre-calculating length and not // copying. out_data.resize(binLength); memcpy(&out_data[0], binData, binLength); free(binData); DLOG("copied %d bytes", binLength); return true; } ::FileLoad(pFileName); int datasize; void *data; if (!FileGetData(pFileName, &data, &datasize)) { //DASSTHROW(0, ("failed loading file '%s'", pFileName)); return false; } out_data.resize(datasize); DVERBOSE("GrabFile: memcpy-ing '%s', %d bytes", pFileName, datasize); memcpy(&out_data[0], data, datasize); FileRemove(pFileName); DVERBOSE("GrabFile: released"); #endif return true; }
HRESULT CALLBACK bpcmds(PDEBUG_CLIENT4 Client, PCSTR Args) { HRESULT hRes=S_OK; char *msg, *decoded, *query; LPSTR pszString; int NbBytesRecvd; size_t cbBinary; INIT_API(); #if VERBOSE >= 2 dprintf("[sync] !bpcmds called\n"); #endif if(!g_Synchronized) { dprintf("[sync] please enable sync\n"); return E_FAIL; } if (!Args || !*Args){ msg = "query"; } else { msg = (char *)Args; } if ((strncmp("load", msg, 4)==0) || (strncmp("query", msg, 5)==0)) { dprintf("[sync] query idb for bpcmds\n"); hRes=TunnelSend("[sync]{\"type\":\"bps_get\"}\n"); } else if(strncmp("save", msg, 4)==0) { dprintf("[sync] dumping bpcmds to idb\n"); hRes=LocalCmd(Client, ".bpcmds"); // g_CommandBuffer first four bytes should contains // return value for command exec if (FAILED(hRes) || FAILED(*g_CommandBuffer)) { dprintf("[sync] failed to evaluate .bpcmds command\n"); return E_FAIL; } cbBinary = strlen(g_CommandBuffer+4); dprintf("%s\n", g_CommandBuffer); hRes = ToBase64((const byte *)g_CommandBuffer+4, (unsigned int)cbBinary, &pszString); if (SUCCEEDED(hRes)) { hRes = TunnelSend("[sync]{\"type\":\"bps_set\",\"msg\":\"%s\"}\n", pszString); free(pszString); } } else { dprintf("[sync] usage !bpcmds <||query|save|load|\n"); return E_FAIL; } // Check if we failed to query the idb client if (FAILED(hRes)){ dprintf("[sync] !bpcmds failed\n"); return hRes; } // Get result from idb client hRes=TunnelReceive(&NbBytesRecvd, &query); if (!(SUCCEEDED(hRes) & (NbBytesRecvd>0) & (query != NULL))) { dprintf("[sync] !bpcmds failed\n"); return hRes; } // Handle result if(strncmp("load", msg, 4)==0) { hRes = FromBase64(query, (BYTE **)(&decoded)); if (SUCCEEDED(hRes)) { hRes = ExecCmdList(decoded); free(decoded); } } else if(strncmp("query", msg, 4)==0) { hRes = FromBase64(query, (BYTE **)(&decoded)); if (SUCCEEDED(hRes)) { dprintf("[sync] idb's saved bpcmds:\n %s\n", decoded); free(decoded); } } else { dprintf("%s\n", query); } free(query); return hRes; }
bool XmlSimulationSettings::GetSettings(WorldSimulation& sim) { string globals="globals"; TiXmlElement* c = e->FirstChildElement(globals); if(c) { printf("Parsing timestep...\n"); //parse timestep double timestep; if(c->QueryValueAttribute("timestep",×tep)==TIXML_SUCCESS) sim.simStep = timestep; } printf("Parsing ODE...\n"); XmlODESettings ode(e); if(!ode.GetSettings(sim.odesim)) { return false; } printf("Parsing robot controllers / sensors\n"); c = e->FirstChildElement("robot"); while(c != NULL) { int index; if(c->QueryValueAttribute("index",&index)!=TIXML_SUCCESS) { fprintf(stderr,"Unable to read index of robot element\n"); continue; } Assert(index < (int)sim.robotControllers.size()); ControlledRobotSimulator& robotSim=sim.controlSimulators[index]; TiXmlElement* ec=c->FirstChildElement("controller"); if(ec) { RobotControllerFactory::RegisterDefault(*robotSim.robot); SmartPointer<RobotController> controller=RobotControllerFactory::Load(ec,*robotSim.robot); if(controller) sim.SetController(index,controller); else { fprintf(stderr,"Unable to load controller from xml file\n"); return false; } Real temp; if(ec->QueryValueAttribute("rate",&temp)==TIXML_SUCCESS){ if(!(temp > 0)) { fprintf(stderr,"Invalid rate %g\n",temp); continue; } sim.controlSimulators[index].controlTimeStep = 1.0/temp; } if(ec->QueryValueAttribute("timeStep",&temp)==TIXML_SUCCESS){ if(!(temp > 0)) { fprintf(stderr,"Invalid timestep %g\n",temp); continue; } sim.controlSimulators[index].controlTimeStep = temp; } } TiXmlElement*es=c->FirstChildElement("sensors"); if(es) { if(!sim.controlSimulators[index].sensors.LoadSettings(es)) { fprintf(stderr,"Unable to load sensors from xml file\n"); return false; } } /* TiXmlElement* es=c->FirstChildElement("sensors"); if(es) { printf("Parsing sensors...\n"); TiXmlElement* pos=es->FirstChildElement("position"); int ival; Real fval; if(pos) { if(pos->QueryValueAttribute("enabled",&ival)==TIXML_SUCCESS) { if(bodyIndex >= 0) fprintf(stderr,"Warning: cannot enable individual joint encoders yet\n"); robotSim.sensors.hasJointPosition=(ival!=0); } if(pos->QueryValueAttribute("variance",&fval)==TIXML_SUCCESS) { if(robotSim.sensors.qvariance.n==0) robotSim.sensors.qvariance.resize(robotSim.robot->q.n,Zero); if(bodyIndex >= 0) robotSim.sensors.qvariance(bodyIndex)=fval; else robotSim.sensors.qvariance.set(fval); } if(pos->QueryValueAttribute("resolution",&fval)==TIXML_SUCCESS) { if(robotSim.sensors.qresolution.n==0) robotSim.sensors.qresolution.resize(robotSim.robot->q.n,Zero); if(bodyIndex >= 0) robotSim.sensors.qresolution(bodyIndex)=fval; else robotSim.sensors.qresolution.set(fval); } } TiXmlElement* vel=c->FirstChildElement("velocity"); if(vel) { if(vel->QueryValueAttribute("enabled",&ival)==TIXML_SUCCESS) { if(bodyIndex >= 0) fprintf(stderr,"Warning: cannot enable individual joint encoders yet\n"); robotSim.sensors.hasJointVelocity=(ival!=0); } if(vel->QueryValueAttribute("variance",&fval)==TIXML_SUCCESS) { if(robotSim.sensors.dqvariance.n==0) robotSim.sensors.dqvariance.resize(robotSim.robot->q.n,Zero); if(bodyIndex >= 0) robotSim.sensors.dqvariance(bodyIndex)=fval; else robotSim.sensors.dqvariance.set(fval); } if(vel->QueryValueAttribute("resolution",&fval)==TIXML_SUCCESS) { if(robotSim.sensors.dqresolution.n==0) robotSim.sensors.dqresolution.resize(robotSim.robot->q.n,Zero); if(bodyIndex >= 0) robotSim.sensors.dqresolution(bodyIndex)=fval; else robotSim.sensors.dqresolution.set(fval); } } //TODO: other sensors? } */ /* TiXmlElement* ec=c->FirstChildElement("controller"); if(ec) { printf("Parsing controller...\n"); TiXmlAttribute* setting = ec->FirstAttribute(); while(setting) { bool res=robotSim.controller->SetSetting(setting->Name(),setting->Value()); if(!res) { printf("Setting %s not valid for current controller, or failed parsing\n",setting->Name()); } setting = setting->Next(); } } */ c = c->NextSiblingElement("robot"); } printf("Parsing state\n"); c = e->FirstChildElement("state"); if(c) { const char* data=c->Attribute("data"); if(!data) { fprintf(stderr,"No 'data' attribute in state\n"); return false; } string decoded=FromBase64(data); if(!sim.ReadState(decoded)) { fprintf(stderr,"Unable to read state from data\n"); return false; } } return true; }
/* Fills in a Bookmark structure based off of a line from the NcFTP * "bookmarks" file. */ int ParseHostLine(char *line, BookmarkPtr bmp) { char token[128]; char pass[128]; char *s, *d; char *tokenend; long L; int i; int result; int n, n1, n2; SetBookmarkDefaults(bmp); s = line; tokenend = token + sizeof(token) - 1; result = -1; for (i=1; ; i++) { if (*s == '\0') break; /* Some tokens may need to have a comma in them. Since this is a * field delimiter, these fields use \, to represent a comma, and * \\ for a backslash. This chunk gets the next token, paying * attention to the escaped stuff. */ for (d = token; *s != '\0'; ) { if ((*s == '\\') && (s[1] != '\0')) { if (d < tokenend) *d++ = s[1]; s += 2; } else if (*s == ',') { ++s; break; } else if ((*s == '$') && (s[1] != '\0') && (s[2] != '\0')) { n1 = HexCharToNibble(s[1]); n2 = HexCharToNibble(s[2]); if ((n1 >= 0) && (n2 >= 0)) { n = (n1 << 4) | n2; if (d < tokenend) *(unsigned char *)d++ = (unsigned int) n; } s += 3; } else { if (d < tokenend) *d++ = *s; ++s; } } *d = '\0'; switch(i) { case 1: (void) STRNCPY(bmp->bookmarkName, token); break; case 2: (void) STRNCPY(bmp->name, token); break; case 3: (void) STRNCPY(bmp->user, token); break; case 4: (void) STRNCPY(bmp->pass, token); break; case 5: (void) STRNCPY(bmp->acct, token); break; case 6: (void) STRNCPY(bmp->dir, token); result = 0; /* Good enough to have these fields. */ break; case 7: if (token[0] != '\0') bmp->xferType = (int) token[0]; break; case 8: /* Most of the time, we won't have a port. */ if (token[0] == '\0') bmp->port = (unsigned int) kDefaultFTPPort; else bmp->port = (unsigned int) atoi(token); break; case 9: (void) sscanf(token, "%lx", &L); bmp->lastCall = (time_t) L; break; case 10: bmp->hasSIZE = atoi(token); break; case 11: bmp->hasMDTM = atoi(token); break; case 12: bmp->hasPASV = atoi(token); break; case 13: bmp->isUnix = atoi(token); result = 3; /* Version 3 had all fields to here. */ break; case 14: (void) STRNCPY(bmp->lastIP, token); break; case 15: (void) STRNCPY(bmp->comment, token); break; case 16: case 17: case 18: case 19: break; case 20: bmp->xferMode = token[0]; result = 7; /* Version 7 has all fields to here. */ break; case 21: bmp->hasUTIME = atoi(token); break; case 22: (void) STRNCPY(bmp->ldir, token); result = 8; /* Version 8 has all fields to here. */ break; default: result = 99; /* Version >8 ? */ goto done; } } done: /* Decode password, if it was base-64 encoded. */ if (strncmp(bmp->pass, kPasswordMagic, kPasswordMagicLen) == 0) { FromBase64(pass, bmp->pass + kPasswordMagicLen, strlen(bmp->pass + kPasswordMagicLen), 1); (void) STRNCPY(bmp->pass, pass); } return (result); } /* ParseHostLine */