#include #include #include "genUtils.h" #include "genFxn.h" /************************************************************************************** * writeBuffer: write input buffer to a binary file. * **************************************************************************************/ int writeBuffer(char *buffer, int len, char *name) { char outputFile[300]; FILE *outFilePtr; strcpy(outputFile,name); strcat(outputFile,".bin"); outFilePtr = fopen(outputFile, "wb"); if (outFilePtr == NULL) return(-1); fwrite(buffer, sizeof(char), len, outFilePtr); fclose(outFilePtr); return 0; } /************************************************************************************** * getFileSize: retrieve the file size of the requested file, in bytes. * **************************************************************************************/ int getFileSize(FILE *file, int *size) { int status, xsize; *size= 0; status= fseek(file, 0, SEEK_END); if (!status) { xsize= ftell(file); if (xsize >= 0) *size= xsize; else status= xsize; //ftell returns -1 if unsuccessful. } rewind(file); if (status) return 0; else return 1; } /****************************************************************************** * * formatConvert: translate a binary file (little endian format) into the * format needed to insert it into the EVM memory (ascii hex * word values with a header), and the converse: translate from a text * file into a binary. * * author: Douglas Ferguson. ******************************************************************************/ int formatConvert(char *inputFile) { char outputFile[300], lineStr[25], charStr[3]; FILE *inFilePtr, *outFilePtr; long inFileSize, outFileSize, i, j, k; int status, pow16, word, line, nLines; unsigned char *buffer; unsigned int *wbuffer; memset(outputFile, 0, 300); strncpy(outputFile, inputFile, strlen(inputFile)-4); /* chop off .bin */ if (strstr(inputFile,".txt")) { inFilePtr = fopen(inputFile, "r"); /* read-only */ if (inFilePtr == NULL) return(-1); fgets(lineStr, 25, inFilePtr); //discard 1st line nLines= 0; while (fgets(lineStr, 25, inFilePtr)) { ++nLines; } fclose(inFilePtr); wbuffer= calloc(nLines, sizeof(int)); inFilePtr = fopen(inputFile, "r"); /* read-only */ fgets(lineStr, 25, inFilePtr); //discard 1st line strcat(outputFile,".bin"); outFilePtr = fopen(outputFile, "wb"); if (outFilePtr == NULL) return(-1); for (word= line= 0; line < nLines; ++line) { fgets(lineStr, 25, inFilePtr); if (strlen(lineStr) < 10) continue; if ( (lineStr[0] == '0') &&((lineStr[1] == 'X')||(lineStr[1] == 'x')) &&(isxdigit(lineStr[2]))&&(isxdigit(lineStr[3])) &&(isxdigit(lineStr[4]))&&(isxdigit(lineStr[5])) &&(isxdigit(lineStr[6]))&&(isxdigit(lineStr[7])) &&(isxdigit(lineStr[8]))&&(isxdigit(lineStr[9])) ) { pow16= 1; for (j= 9; j >= 0; --j, pow16*= 16) { if ((lineStr[j]>= '0') && (lineStr[j] <= '9')) wbuffer[word]+= (lineStr[j] - '0')*pow16; else if ((lineStr[j]>= 'a') && (lineStr[j] <= 'f')) wbuffer[word]+= (lineStr[j] - 'a' +10)*pow16; else if ((lineStr[j]>= 'A') && (lineStr[j] <= 'F')) wbuffer[word]+= (lineStr[j] - 'A' +10)*pow16; } ++word; } } fclose(inFilePtr); fwrite(wbuffer, sizeof(int), word, outFilePtr); } else if (strstr(inputFile,".bin")) { inFilePtr = fopen(inputFile, "rb"); /* rb= read-only, binary */ if (inFilePtr == NULL) return(-1); status= getFileSize (inFilePtr, &inFileSize); if ((status == 0) || (inFileSize%4 != 0)) return(-1); /* eof= word boundary */ buffer= calloc(inFileSize, sizeof(char)); fread(buffer, sizeof(char), inFileSize, inFilePtr); fclose(inFilePtr); strcat(outputFile,".txt"); outFilePtr = fopen(outputFile, "w"); if (outFilePtr == NULL) return(-1); /* outFileSize is the size of the data in words, to be tranferred into the EVM memory; it is inserted into the header together with other data: the 1651, 1, and 2 can be left constant, the memory location for the transfer is overwritten by whatever the actual location should be. */ outFileSize= inFileSize/4; fprintf(outFilePtr,"1651 1 80008000 2 %x \n",outFileSize); /* write the ascii file */ for (k= i= 0; i< outFileSize; ++i, k+= 4) { strcpy(lineStr,"0xXXXXXXXX\n"); for (j= 0; j< 4; ++j) { sprintf(charStr,"%02x",buffer[k+j]); lineStr[8-2*j]= charStr[0]; lineStr[9-2*j]= charStr[1]; /* buffer is in little endian format*/ } fputs(lineStr, outFilePtr); } } free(buffer); fclose(outFilePtr); return 0; } /****************************************************************************** * utility routines: stripWhiteSpace, removeQuotes, leftShift, paramCnt, * paramParse, parse, getWord, addChecksum ******************************************************************************/ void stripWhiteSpace(char *line) { int i= 0; char c, nQuotes=0; while (c=line[i]) { if (c=='"') ++nQuotes; if ((c==' ')||(c=='\t')||(c=='\n')||(c=='\r')) { if (nQuotes%2==0) {leftShift(&line[i],1); } else {++i;} continue; } else {++i;} } } /***********************************************/ void removeQuotes(char *line) { int i= 0; char c, nQuotes=0; while (c=line[i]) { if (c=='"') { leftShift(&line[i],1); continue; } else {++i;} } } /***********************************************/ void leftShift(char *line, char nShiftIn) { int j= 0; char nShift; nShift= nShiftIn; if (nShift > strlen(line)) nShift= strlen(line); for (j= nShift; j <= strlen(line); ++j) line[j-nShift]= line[j]; } /***********************************************/ void parse(char *line, char *word, char *ws) { int i, j, k, wsi; if (!line[0]) {word[0] = 0; return;} i= 0; do { wsi= false; for (k= 0; k < strlen(ws); ++k) if ((line[i])&&(line[i] == ws[k])) wsi= true; if (wsi) ++i; } while (wsi); j= i; do { wsi= false; for (k= 0; k < strlen(ws); ++k) if ((line[j])&&(line[j] == ws[k])) wsi= true; if (!wsi) ++j; } while ((!wsi) && (line[j])); strncpy(word, &line[i], j-i); word[j-i]=0; //Skip past any surrounding white space. do { wsi= false; for (k= 0; k < strlen(ws); ++k) if ((line[j])&&(line[j] == ws[k])) wsi= true; if (wsi) ++j; } while (wsi); leftShift(line,j); //line[0]= 0 at the end of parsing. } /***********************************************/ unsigned int getWord(char *word, char *hexStr, char base) { unsigned int w; double x; float xf; char *ptr; if ( (strspn(word, "1234567890.-") == strlen(word)) &&(strchr(word,'.')) ){ /* floating point */ x= strtod(word, &ptr); xf= (float) x; w= *((unsigned int *) &xf); } else { w= (unsigned int) strtoul(word, &ptr, base); /* nice fxn! */ } sprintf(hexStr,"0x%08x\n",w); return w; }