c# - How Can I Fix My Method To Scan large array of bytes for smaller arrays -


i want create method scan arrays of bytes specific array have done this, there else want, want extend can scan arrays indexes being unknowns replacing them 256(0xff) e.g if have array [1,2,3,4,5,6,7,8,9,20,30,50] normally, can search [5,6,7] , index [4] because 5 begins want able search [5,256,7] means if dont know actual value of 256, still correct answer

my solution grab chunk of 256/512 bytes,use loop read chunk of x bytes(in case, 3 bytes), , replace 256 corresponding value of corresponding index in bytes being read

here code have tried, isnt working properly

p.s, method meant scan big arrays why there large values

public long arraysearch(byte[] scanfor, long startindex, long endindex) {     if (scanfor.length == 0 || startindex == 0 || endindex == 0 || endindex  <= startindex - scanfor.length) return 0;      long tempstart = startaddress;     long foundindex = 0;     long tmpad = 0;      long maxchunk = endindex - startindex;     float parts = maxchunk / 256;      int x = 0;     byte[] tmp = new byte[scanfor.length];     byte[] buf = new byte[256];      (x = 0; x < parts; x++)      { //the 'byte[] readarray(int64 index,int length)' method returns byte array  (byte[]) @ given index of specified size         buf = readarray(tempstart, 256);          (int = 0; < 256; i++)         {              if (buf[i] == scanfor[0])             {                 tmp = readarray(tempstart + i, scanfor.length);                     (int iii = 0; iii < scanfor.length; iii++)                     {                         if (scanfor[iii] == 0xff) scanfor[iii] = tmp[iii];                     }                  tmpad = tempstart + i; //the 'bytearraycompare' method compares elements of array ,  returns true if equal                 if (bytearraycompare(tmp, scanfor))                 {                     foundindex = tmpad;                     break;                 }             }         }         tempstart += 256;     }     return foundindex; } 

i find code confusing, can't tell what's wrong it, can try explain working solution, inspired attempt, you.

first of created few helper stuff, simulate environment - tell me if understood wrong (the parts there simplify things me):

private static readonly byte [] firstpart = enumerable.repeat (1, 250).select (i => (byte) i).toarray (); private static readonly byte [] secondpart = enumerable.range (1, 250).select (i => (byte) i).toarray (); private static readonly byte [] thirdpart = enumerable.range (0, 250).select (i => (byte) i).toarray (); private static readonly byte [] fourthpart = enumerable.repeat (1, 500).select (i => (byte) i).toarray ();  private static readonly byte [] wholearray =     new [] {firstpart, secondpart, thirdpart, fourthpart}.selectmany (bytes => bytes).toarray ();  private static byte [] readarray (long index, int length) =>     wholearray.where ((b, i) => >= index && < index + length).toarray (); 

then implementation of arraysearch came - explained in comments, if unclear, feel free ask:

public static long arraysearch (byte [] scanfor, long startindex, long endindex) {     if (startindex < 0 ||         endindex < startindex ||         scanfor.length > endindex - startindex) //check wether parameters valid         return -1;      if (scanfor.length == 0) //if scanfor empty, must return 0 directly,          //as indexoutofrange exception caused otherwise         return 0;      var partscount = (endindex - startindex) / 256; //the number of parts scan      long tempfoundindex = -1; //the found index, later going returned      (var = 0; < partscount; i++) //we iterate through parts          //to find item in parts matches first item of array we're searching     {         var start = * 256 + startindex; //this startindex of current part         if (start > endindex) //if start index bigger endindex, can directly break,              //because won't find array in specified range anymore              break;         var part = readarray (start, 256); //this part we're scanning          (var j = 0; j < part.length && start + j <= endindex; j++) //now we're iterating part             //to find item matches first item of array we're searching             if (scanfor [0] == 0xff || part [j] == scanfor [0]) //if found such item,                  //we want check following items, wether match search array             {                 var arraycheckfailed = false; //this indicates wether failed,                  //so doesn't continue search                 var tempscanforindex = 0; //this index of item scanfor we're checking                 tempfoundindex = start + j; //this starting index found                  //and later want return, if items match                 (var i2 = i; i2 < partscount && !arraycheckfailed; i2++)                      //so we're once again iterating through parts (beginning current part)                     //to check items parts                 {                     var start2 = i2 * 256 + startindex; //this once again startindex of current part                     if (start2 > endindex) //see above                         break;                     var part2 = readarray (start2, 256);//todo rename                     (var j2 = i2 == ? j : 0; j2 < part2.length && start2 + j2 <= endindex; j2++)                         if (scanfor [tempscanforindex] == 0xff || part2 [j2] == scanfor [tempscanforindex])                         {                             tempscanforindex++; //if current item matched, want check next item                             if (scanfor.length <= tempscanforindex) //and if checked enough items,                                  //we know array matches, can return found index                                 return tempfoundindex;                         }                         else                         {                             arraycheckfailed = true;                                  //if didn't match continue search first item matches                             tempfoundindex = -1;                              break;                         }                 }             }     }     return tempfoundindex; } 

i tested this method:

private static void main () {     console.write (arraysearch (thirdpart.select ((b, i) => == 0 ? (byte) 0xff : b).toarray (), 0, 1100));     console.readline (); } 

the output 249, seemd correct me.

i couldn't test in environment, please inform me, if isn't working should.


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -