void Trace::process(bool doBasicBlocks) { // Decide what kind of trace file it is FILE *fin = fopen(this->tracefile, "r"); if (fin == 0) { throw wxString(wxT("Could not open trace file")); } wchar_t linew[LINELEN] = {0}; char line[LINELEN] = {0}; fgetws(linew, sizeof(linew), fin); fseek(fin, 0, 0); fgets(line, sizeof(line), fin); fclose(fin); // The first line of the file determines the type of trace // Validate it before proceeding if ( (wcsstr(linew, L"vera_trace_version=0.1") == linew) ) processVeraPin(doBasicBlocks); else if ( wcsstr(linew, L"After init") == linew ) processEther(doBasicBlocks); else { // Check to make sure it isn't an Ether trace file without the header. char *colPos = strchr(line, ':'); size_t colLen = ((size_t) colPos) - ((size_t) line); if (colPos && colLen > 0 && isHexString(line, colLen) ) { processEther(doBasicBlocks); } else { throw wxString(wxT("Unrecognized trace file %s (unrecognized format)")); } } if (bblMap.size() <= 1 || edgeMap.size() <= 1) // Error { throw wxString(wxT("Could not parse trace file (bad format)")); } }
static void parseOldX11(char const colorname[], pixval const maxval, bool const closeOk, pixel * const colorP) { /*---------------------------------------------------------------------------- Return as *colorP the color specified by the old X11 style color specififier colorname[] (e.g. #554055). -----------------------------------------------------------------------------*/ int hexit[256]; long r,g,b; pixval rNorm, gNorm, bNorm; computeHexTable(hexit); if (!isHexString(&colorname[1], hexit)) pm_error("Non-hexadecimal characters in #-type color specification"); switch (strlen(colorname) - 1 /* (Number of hex digits) */) { case 3: r = hexit[(int)colorname[1]]; g = hexit[(int)colorname[2]]; b = hexit[(int)colorname[3]]; rNorm = rgbnorm(r, maxval, 1, closeOk, colorname); gNorm = rgbnorm(g, maxval, 1, closeOk, colorname); bNorm = rgbnorm(b, maxval, 1, closeOk, colorname); break; case 6: r = (hexit[(int)colorname[1]] << 4 ) + hexit[(int)colorname[2]]; g = (hexit[(int)colorname[3]] << 4 ) + hexit[(int)colorname[4]]; b = (hexit[(int)colorname[5]] << 4 ) + hexit[(int)colorname[6]]; rNorm = rgbnorm(r, maxval, 2, closeOk, colorname); gNorm = rgbnorm(g, maxval, 2, closeOk, colorname); bNorm = rgbnorm(b, maxval, 2, closeOk, colorname); break; case 9: r = (hexit[(int)colorname[1]] << 8) + (hexit[(int)colorname[2]] << 4) + (hexit[(int)colorname[3]] << 0); g = (hexit[(int)colorname[4]] << 8) + (hexit[(int)colorname[5]] << 4) + (hexit[(int)colorname[6]] << 0); b = (hexit[(int)colorname[7]] << 8) + (hexit[(int)colorname[8]] << 4) + (hexit[(int)colorname[9]] << 0); rNorm = rgbnorm(r, maxval, 3, closeOk, colorname); gNorm = rgbnorm(g, maxval, 3, closeOk, colorname); bNorm = rgbnorm(b, maxval, 3, closeOk, colorname); break; case 12: r = (hexit[(int)colorname[1]] << 12) + (hexit[(int)colorname[2]] << 8) + (hexit[(int)colorname[3]] << 4) + hexit[(int)colorname[4]]; g = (hexit[(int)colorname[5]] << 12) + (hexit[(int)colorname[6]] << 8) + (hexit[(int)colorname[7]] << 4) + hexit[(int)colorname[8]]; b = (hexit[(int)colorname[9]] << 12) + (hexit[(int)colorname[10]] << 8) + (hexit[(int)colorname[11]] << 4) + hexit[(int)colorname[12]]; rNorm = rgbnorm(r, maxval, 4, closeOk, colorname); gNorm = rgbnorm(g, maxval, 4, closeOk, colorname); bNorm = rgbnorm(b, maxval, 4, closeOk, colorname); break; default: pm_error("invalid color specifier '%s'", colorname); } PPM_ASSIGN(*colorP, rNorm, gNorm, bNorm); }
void handleToken(FILE *inStream, FILE *outStream, char *token) { int n; char name[200]; int starting_index; int hasElse = 0; int endif = 0; if(strcmp(token, "+") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get values off top of stack fputs("\tLDR R1 R6 #1\n", outStream); fputs("\tADD R0 R0 R1\n", outStream); // add and store in R0 fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tSTR R0 R6 #0\n", outStream); // push return value onto top of stack } else if(strcmp(token, "-") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get values off top of stack fputs("\tLDR R1 R6 #1\n", outStream); fputs("\tSUB R0 R0 R1\n", outStream); // subtract and store in R0 fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tSTR R0 R6 #0\n", outStream); // push return value onto top of stack } else if(strcmp(token, "*") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get values off top of stack fputs("\tLDR R1 R6 #1\n", outStream); fputs("\tMUL R0 R0 R1\n", outStream); // multiply and store in R0 fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tSTR R0 R6 #0\n", outStream); // push return value onto top of stack } else if(strcmp(token, "/") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get values off top of stack fputs("\tLDR R1 R6 #1\n", outStream); fputs("\tDIV R0 R0 R1\n", outStream); // divide and store in R0 fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tSTR R0 R6 #0\n", outStream); // push return value onto top of stack } else if(strcmp(token, "%") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get values off top of stack fputs("\tLDR R1 R6 #1\n", outStream); fputs("\tMOD R0 R0 R1\n", outStream); // mod and store in R0 fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tSTR R0 R6 #0\n", outStream); // push return value onto top of stack } else if(strcmp(token, "lt") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get a fputs("\tLDR R1 R6 #1\n", outStream); // get b fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tCMP R0 R1\n", outStream); // a - b fprintf(outStream, "\tBRn %s_comp_%d\n", fileName, comp_index); // if negative, skip next 2 lines fputs("\tCONST R0 #0\n", outStream); // false, so want to return 0 fprintf(outStream, "\tBRnzp %s_comp_%d\n", fileName, comp_index + 1); // skip next line fprintf(outStream, "%s_comp_%d", fileName, comp_index); fputs("\tCONST R0 #1\n", outStream); // true, so want to return 1 fprintf(outStream, "%s_comp_%d\n", fileName, comp_index + 1); comp_index++; fputs("\tSTR R0 R6 #0\n", outStream); // push return value on top of stack } else if(strcmp(token, "le") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get a fputs("\tLDR R1 R6 #1\n", outStream); // get b fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tCMP R0 R1\n", outStream); // a - b fprintf(outStream, "\tBRnz %s_comp_%d\n", fileName, comp_index); // if negative or 0, skip next 2 lines fputs("\tCONST R0 #0\n", outStream); // false, so want to return 0 fprintf(outStream, "\tBRnzp %s_comp_%d\n", fileName, comp_index + 1); // skip next line fprintf(outStream, "%s_comp_%d", fileName, comp_index); fputs("\tCONST R0 #1\n", outStream); // true, so want to return 1 fprintf(outStream, "%s_comp_%d\n", fileName, comp_index + 1); comp_index++; fputs("\tSTR R0 R6 #0\n", outStream); // push return value on top of stack } else if(strcmp(token, "eq") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get a fputs("\tLDR R1 R6 #1\n", outStream); // get b fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tCMP R0 R1\n", outStream); // a - b fprintf(outStream, "\tBRz %s_comp_%d\n", fileName, comp_index); // if negative or 0, skip next 2 lines fputs("\tCONST R0 #0\n", outStream); // false, so want to return 0 fprintf(outStream, "\tBRnzp %s_comp_%d\n", fileName, comp_index + 1); // skip next line fprintf(outStream, "%s_comp_%d", fileName, comp_index); fputs("\tCONST R0 #1\n", outStream); // true, so want to return 1 fprintf(outStream, "%s_comp_%d\n", fileName, comp_index + 1); comp_index++; fputs("\tSTR R0 R6 #0\n", outStream); // push return value on top of stack } else if(strcmp(token, "gt") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get a fputs("\tLDR R1 R6 #1\n", outStream); // get b fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tCMP R0 R1\n", outStream); // a - b fprintf(outStream, "\tBRp %s_comp_%d\n", fileName, comp_index); // if negative or 0, skip next 2 lines fputs("\tCONST R0 #0\n", outStream); // false, so want to return 0 fprintf(outStream, "\tBRnzp %s_comp_%d\n", fileName, comp_index + 1); // skip next line fprintf(outStream, "%s_comp_%d", fileName, comp_index); fputs("\tCONST R0 #1\n", outStream); // true, so want to return 1 fprintf(outStream, "%s_comp_%d\n", fileName, comp_index + 1); comp_index++; fputs("\tSTR R0 R6 #0\n", outStream); // push return value on top of stack } else if(strcmp(token, "ge") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get a fputs("\tLDR R1 R6 #1\n", outStream); // get b fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tCMP R0 R1\n", outStream); // a - b fprintf(outStream, "\tBRzp %s_comp_%d\n", fileName, comp_index); // if negative or 0, skip next 2 lines fputs("\tCONST R0 #0\n", outStream); // false, so want to return 0 fprintf(outStream, "\tBRnzp %s_comp_%d\n", fileName, comp_index + 1); // skip next line fprintf(outStream, "%s_comp_%d", fileName, comp_index); fputs("\tCONST R0 #1\n", outStream); // true, so want to return 1 fprintf(outStream, "%s_comp_%d\n", fileName, comp_index + 1); comp_index++; fputs("\tSTR R0 R6 #0\n", outStream); // push return value on top of stack } else if(strcmp(token, "and") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get top two values from stack fputs("\tLDR R1 R6 #1\n", outStream); fputs("\tAND R0 R0 R1\n", outStream); // bitwise and them fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tSTR R0 R6 #0\n", outStream); // push return value on top of stack } else if(strcmp(token, "or") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get top two values from stack fputs("\tLDR R1 R6 #1\n", outStream); fputs("\tOR R0 R0 R1\n", outStream); // bitwise or them fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tSTR R0 R6 #0\n", outStream); // push return value on top of stack } else if(strcmp(token, "not") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get top value from stack fputs("\tNOT R0 R0\n", outStream); // bitwise not fputs("\tSTR R0 R6 #0\n", outStream); // push return value on top of stack } else if(strcmp(token, "drop") == 0) { fputs("\tADD R6 R6 #1\n", outStream); } else if(strcmp(token, "dup") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // get value at top of stack fputs("\tADD R6 R6 #-1\n", outStream); // update SP fputs("\tSTR R0 R6 #0\n", outStream); // push value to new stack top } else if(strcmp(token, "swap") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // load top two values fputs("\tLDR R1 R6 #1\n", outStream); fputs("\tSTR R1 R6 #0\n", outStream); // swap and push back to stack fputs("\tSTR R0 R6 #1\n", outStream); } else if(strcmp(token, "rot") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // load top three values fputs("\tLDR R1 R6 #1\n", outStream); fputs("\tLDR R2 R6 #2\n", outStream); fputs("\tSTR R0 R6 #1\n", outStream); // rotate them and push back to stack fputs("\tSTR R1 R6 #2\n", outStream); fputs("\tSTR R2 R6 #0\n", outStream); } else if(strcmp(token, "pick") == 0) { fputs("\tLDR R0 R6 #0\n", outStream); // pop index val off top of stack fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tADD R0 R0 R6\n", outStream); // offset R6 by index value in R0 fputs("\tLDR R0 R0 #0\n", outStream); // load value at offset value fputs("\tADD R6 R6 #-1\n", outStream); // update SP fputs("\tSTR R0 R6 #0\n", outStream); // push return value to stack top } else if(strcmp(token, "if") == 0) { ind+=2; starting_index = ind; fputs("\tLDR R0 R6 #0\n", outStream); // pop value off top of stack fputs("\tADD R6 R6 #1\n", outStream); // update SP fputs("\tCMPI R0 #0\n", outStream); // check if top of stack was true fprintf(outStream, "\tBRz %s_%d\n", fileName, ind); // if false, break to assigned label while(!endif) { readToken(inStream, token); if(strcmp(token, "if") == 0) { handleToken(inStream, outStream, token); } else if(strcmp(token, "else") == 0) { fprintf(outStream, "\tBRnzp %s_%d\n", fileName, starting_index + 1); fprintf(outStream, "%s_%d\n", fileName, starting_index); hasElse = 1; } else if(strcmp(token, "endif") == 0) { fprintf(outStream, "%s_%d\n", fileName, starting_index + hasElse); endif = 1; } else { handleToken(inStream, outStream, token); } } } else if(strcmp(token, "defun") == 0) { readToken(inStream, name); if(!isAlnumString(name)) { printf("Error: %s is not a valid function name. All characters in a function name must be alphanumeric.\n", name); exit(1); } fputs("\t.CODE\n", outStream); fputs("\t.FALIGN\n", outStream); //writes name of function to file fprintf(outStream, "%s\n", name); //prologue fputs("\tADD R6 R6 #-3\n", outStream); // allocate 3 slots for FP, RA, RV fputs("\tSTR R7 R6 #1\n", outStream); // save RA that was stored in R7 fputs("\tSTR R5 R6 #0\n", outStream); // save caller FP that is the value of R5 fputs("\tADD R5 R6 #0\n", outStream); // set up local FP to begin at current stack top } else if(strcmp(token, "return") == 0) { //epilogue fputs("\tLDR R0 R6 #0\n", outStream); // load value from top of stack fputs("\tSTR R0 R5 #2\n", outStream); // put this value into RV address fputs("\tADD R6 R5 #0\n", outStream); // move top of stack back to local frame fputs("\tLDR R5 R6 #0\n", outStream); // restore caller FP fputs("\tLDR R7 R6 #1\n", outStream); // restore RA fputs("\tADD R6 R6 #2\n", outStream); // free RA, FP; top of stack is RV fputs("\tRET\n", outStream); // return } else if(isDecimalString(token)) { fprintf(outStream, "\tCONST R0 #%s\n", token); fputs("\tSTR R0 R6 #-1\n", outStream); fputs("\tADD R6 R6 #-1\n", outStream); } else if(isHexString(token)) { fprintf(outStream, "\tCONST R0 %s\n", token); fputs("\tSTR R0 R6 #-1\n", outStream); fputs("\tADD R6 R6 #-1\n", outStream); } else if(isAlnumString(token)) { fprintf(outStream, "\tJSR %s\n", token); // Jump to function name } else { printf("Error: %s is not a valid token in J.\n", token); exit(1); } printf("%s\n", token); }
AnonPrimitive* AnonModule::createPrimitive(AnonMethod::Method m, const std::string& parameter, std::vector<map_info> mapping) { AnonPrimitive* ret = 0; char buffer[32]; char c; bool isHex = isHexString(parameter); switch (m) { case AnonMethod::HashSha1: ret = new AnonHashSha1(); break; case AnonMethod::BytewiseHashSha1: ret = new AnonBytewiseHashSha1(); break; case AnonMethod::ContinuousChar: ret = new AnonContinuousChar(); break; case AnonMethod::Randomize: ret = new AnonRandomize(); break; case AnonMethod::Shuffle: ret = new AnonShuffle(); break; case AnonMethod::Whitenoise: ret = new AnonWhitenoise(atoi(parameter.c_str())); break; case AnonMethod::HashHmacSha1: ret = new AnonHashHmacSha1(parameter); break; case AnonMethod::BytewiseHashHmacSha1: ret = new AnonBytewiseHashHmacSha1(parameter); break; case AnonMethod::ConstOverwrite: if (parameter.size() != 1 || (isHex && parameter.size() != 4)) { THROWEXCEPTION("AnonConstOverwrite only uses one character as key"); } c = parameter.c_str()[0]; if (isHex) { if (convHexToBinary(parameter, &c, 1)!=1) { THROWEXCEPTION("Failed to convert hexadecimal key parameter '%s' to binary (one byte required)!", parameter.c_str()); } } ret = new AnonConstOverwrite(parameter.c_str()[0]); break; case AnonMethod::CryptoPan: if (parameter.length()!=32) if (isHex && parameter.length() != 66) THROWEXCEPTION("CryptoPAN key *MUST* have exactly 32 characters!"); if (isHex) { if (convHexToBinary(parameter, buffer, 32)!=32) { THROWEXCEPTION("Failed to convert hexadecimal key parameter '%s' to binary (32 bytes required)!", parameter.c_str()); } } else { memcpy(buffer, parameter.c_str(), 32); } ret = new AnonCryptoPan(buffer); break; case AnonMethod::CryptoPanPrefix: if (parameter.length()!=32) if (isHex && parameter.length() != 66) THROWEXCEPTION("CryptoPAN key *MUST* have exactly 32 characters!"); if (isHex) { if (convHexToBinary(parameter, buffer, 32)!=32) { THROWEXCEPTION("Failed to convert hexadecimal key parameter '%s' to binary (32 bytes required)!", parameter.c_str()); } } else { memcpy(buffer, parameter.c_str(), 32); } ret = new AnonCryptoPanPrefix(buffer, mapping); break; default: msg(MSG_FATAL, "AnonPrimitive number %i is unknown", m); THROWEXCEPTION("AnonPrimitive number %i is unknown", m); } return ret; }