/* binary.c - The binary-related code for the replace program. (C) Richard K. Lloyd 2001-2004 */ #define Extern extern #include "replace.h" static int maxtimeswarn; static size_t minloop,totbuff; static LONG_LONG outpos; #ifdef __STDC__ char *alloc_mem(char *theptr,size_t *cursize,size_t newsize) #else char *alloc_mem(theptr,cursize,newsize) char *theptr; size_t *cursize; size_t newsize; #endif { /* Allocate newsize bytes of memory to a pointer that's already had cursize bytes allocated */ if (*cursize0 then minloop=C+1 for eachstr in 1..numstrs do O=length of X[eachstr] if T>=O then loop=0 while loop<=T-O do if B[loop...loop+O-1]==X[eachstr] then replace string at B[loop] with Y[eachstr] loop+=O else loop++ fi done if (loop0) fwrite L bytes at &B[0] */ #ifdef __STDC__ static void replace_chunk(size_t eachstr) #else static void replace_chunk(eachstr) size_t eachstr; #endif { /* Replace string number "eachstr" in the current binary chunk */ size_t olen=oldstrlen[eachstr],nlen=newstrlen[eachstr]; /* Note that we have to check the new string's len versus the old one here, cos auto-detect code may be calling the binary replace code without having compared the new length vs. the old length beforehand. Old string must also be >0 in length (auto-detect lets zero-length old strings through cos it's allowed for text files now) */ if (totbuff>=olen && nlen<=olen && olen>0) { size_t loop=0; while (loop<=totbuff-olen) { int binmatch; size_t bl; if (sensitive) binmatch=!memcmp(&binchunkptr[loop],oldstr[eachstr],olen); else { binmatch=1; for (bl=0;bl ",out_string(oldtmp,olen)); (void)fprintf(stderr, " %s (offset: " LONG_LONG_FORMAT ")\n", out_string(&binchunkptr[loop],nlen),coffset); } repcount++; loop+=olen; } else loop++; } } else loop++; } if (loop0 && !gotfail) { numread=fread((void *)&binchunkptr[loadoff],1,BIN_CHUNK-loadoff,fd); gotfail=ferror(fd); if (!gotfail && (totbuff=numread+loadoff)>0) { size_t estr; minloop=BIN_CHUNK+1; for (estr=1;estr<=numstrs;estr++) replace_chunk(estr); if (minloop==BIN_CHUNK+1 && totbuff0 && !gotfail) { outpos+=(LONG_LONG)loadoff; if (!fake) { (void)fwrite((void *)binchunkptr,loadoff,1,fdout); gotfail=ferror(fdout); } } return(gotfail); }