FPoke - File Poke
File PokePuritanulo
Magyar Esperanto British

Magyar
Program letöltése (147k, Ver:1.2, )

Az FPoke parancssori program, ami bináris bájtsorozatot képes beíni egy fájl tetszőleges címére.

    Használat: fpoke.exe [--debug] [--create] filePath [--offset:[+-]hex] hexAddr hexByte1[ ]hexByte2 ... hexByteN
    Pl. fpoke --create crlf.txt 00 0D 0A    - törli vagy újra írja a crlf.txt nevű fájlt
                                              és CR/LF karaktereket ír bele a 0. bájttól.
        fpoke --create empty.txt            - üres fájl hoz létre
    


Esperanto
Elŝutu programon (147k, Ver:1.2, exe+C programo)

FPoke estas komandprogramo, kiu skribas serion da okbitoj en dozieron ĉe definita pozicio.

    Uzo: fpoke.exe [--debug] [--create] filePath [--offset:[+-]hex] hexAddr hexByte1[ ]hexByte2 ... hexByteN
    Ekzampoj:
       fpoke --create crlf.txt 00 0D 0A     - purgas aŭ kreas dozieron crlf.txt kaj skribas CR/LF en la dozieron.
       fpoke --create empty.txt             - kreas malplenan dozieron
    


British
Download the program (147k, Ver:1.2, exe+c source)

FPoke is a command line program that writes a serial of binary bytes into a file at a position.

    Usage: fpoke.exe [--debug] [--create] filePath [--offset:[+-]hex] hexAddr hexByte1[ ]hexByte2 ... hexByteN
    E.g. fpoke --create crlf.txt 00 0D 0A   - clears or creates file crlf.txt and puts CR/LF into it.
         fpoke --create empty.txt           - creates an empty file
    


Source

//---------------------------------------------------------------------------------
// fpoke.exe file addr byte1 byte2 ... byteN  - put bytes into file at a position 
//---------------------------------------------------------------------------------
// Sar, 2006-04-04 V1.2  
// Compiles: gcc -mno-cygwin fpoke.c -o fpoke.exe

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char* argv[])
{
   FILE *file;
   int i, debug;
   unsigned long addr;
   unsigned long offset = 0;
   unsigned int byte;
   char *bp;
   char dummy[3];

   // Check the minimal number of parameters
   if( argc==1 ){
      printf("fpoke - puts a number of bytes into a file at a position\n");
      printf("Usage: fpoke.exe [--debug] [--create] filePath [--offset:[+-]hex] hexAddr hexByte1[ ]hexByte2 ... hexByteN\n" );
      printf("(Sar, 2006-04-04 V1.2)\n");
      exit(1);
   }

   // init 
   debug = 0;
   i=1;

   // Check if debug mode
   if( 0==strcmp(argv[i], "--debug" ) ){
     debug = 1;
     ++i;
     if( i>=argc ){
       printf("Warning: No file name\n" );
       exit(1);
     } 
   }
   if( debug ) printf("fpoke.exe started!\n" );


   // Check file creation needed
   if( 0==strcmp(argv[i], "--create" ) ){
     ++i;

     // Check if file name exists
     if( i>=argc ){
       printf("Warning: No file name\n" );
       exit(1);
     } 
     
     if( debug ) printf("(Re)creating file \"%s\"\n", argv[i] );
     file = fopen(argv[i], "wb");
     if( file == NULL) {
        printf("Error: Can not (re)create file \"%s\"\n", argv[i] );
        exit(1);
     }
     fclose(file);
   }



   file = fopen(argv[i], "rb+");
   if( file == NULL) {
      printf("Error: Can not find file \"%s\"\n", argv[i] );
      exit(1);
   }
   ++i;


   // Check offset
   if( 0==memcmp(argv[i], "--offset:+", 10 ) ){
     if( 1 != sscanf(argv[i]+10, "%lx", &offset) ){
        printf("Error: Can not read hexa offset \"%s\"\n", argv[i] );
        fclose(file);
        exit(1);
     }
     if( debug ) printf("Offset: +%08lX\n", offset );
     ++i;
   }

   if( 0==memcmp(argv[i], "--offset:-", 10 ) ){

     if( 1 != sscanf(argv[i]+10, "%lx", &offset) ){
        printf("Error: Can not read hexa offset \"%s\"\n", argv[i] );
        fclose(file);
        exit(1);
     }
     offset = - offset;
     if( debug ) printf("Offset: -%08lX (%08lX)\n", -offset, offset );
     ++i;
   }
   if( 0==memcmp(argv[i], "--offset:", 9 ) ){
     if( 1 != sscanf(argv[i]+9, "%lx", &offset) ){
        printf("Error: Can not read hexa offset \"%s\"\n", argv[i] );
        fclose(file);
        exit(1);
     }
     if( debug ) printf("Offset: +%08lX\n", offset );
     ++i;
   }

   // Check address
   if( i>=argc ){
     printf("Warning: No hexAddr\n" );
     fclose(file);
     exit(0);
   } 
   if( 1 != sscanf(argv[i], "%lx", &addr) ){
      printf("Error: Can not read hexa address \"%s\"\n", argv[i] );
      fclose(file);
      exit(1);
   }
   ++i;
   if( debug ) printf("hexAddr: %08lX (%08lX)\n", addr, addr+offset );


   // poke all bytes  
   if( i>=argc ){
     printf("Warning: No hexByte\n" );
     fclose(file);
     exit(0);
   } 
   for( ; i<argc; i++, addr++ ){

     bp = argv[i];

     while( *bp ){

       dummy[0] = *bp++;
       dummy[1] = 0;
       if( *bp ) dummy[1] = *bp++;
       dummy[2] = 0;

       if( 1 != sscanf(dummy, "%x", &byte) ){
          printf("Error: Can not read hexa byte \"%s\"\n", dummy);
          fclose(file);
          exit(1);
       }
    
       if( fseek( file, addr + offset, SEEK_SET) ){
          printf("Error: Can not seek file position\n" );
          fclose(file);
          exit(1);
       }
       if( debug ) printf("Poke hexAddr: %08lX (%08lX) -> hexByte: %02X\n", addr, addr+offset, byte&0xFF );
       fputc( (char) byte, file);
     }
   }

   fclose(file);

   if( debug ) printf("fpoke.exe ended!\n" );
   exit(0);
}
// End of fpoke.c