private void search(int value1, int value2, bool useFactors, string sFactors, bool mulByFactor, bool divByFactor) { int bMin = cbMinBytes.Text == "Auto" ? -1 : int.Parse(cbMinBytes.Text); int bMax = cbMaxBytes.Text == "Auto" ? -1 : int.Parse(cbMaxBytes.Text); int[] factors = useFactors ? parseFactorString(sFactors) : null; findToolRes.Direction dir = getSearchDirection(); foreach (var msg in DataList) { // do we need to check it? if (!Filter.Contains(msg.Id)) { // basic search with no factors List <findToolRes> res = new List <findToolRes>(); if (findTool.findValueInBuff(msg.Data, value1, value2, ref res, dir, bMin, bMax)) { addResult(msg, res); } // factors if (useFactors && factors != null && (mulByFactor || divByFactor)) { foreach (int f in factors) { int tmp1 = value1 * f; int tmp2 = value1 * f; if (tmp1 != 0 && tmp2 != 0 && mulByFactor) { // mul List <findToolRes> res1 = new List <findToolRes>(); if (findTool.findValueInBuff(msg.Data, value1 * f, value2 * f, ref res1, dir, bMin, bMax)) { addResult(msg, res1); } } tmp1 = value1 / f; tmp2 = value1 / f; if (tmp1 != 0 && tmp2 != 0 && divByFactor) { // div List <findToolRes> res2 = new List <findToolRes>(); if (findTool.findValueInBuff(msg.Data, value1 / f, value2 / f, ref res2, dir, bMin, bMax)) { addResult(msg, res2); } } } } } } }
static public bool findValueInBuff(byte[] data, int valFrom, int valTo, ref List <findToolRes> resList, findToolRes.Direction dir = findToolRes.Direction.All, int minBytes = -1, int maxBytes = -1) { if (data.Length == 0) { return(false); } bool found = false; // auto if (-1 == maxBytes) { // calc num of bytes int max = valFrom >= valTo ? valFrom : valTo; string s = max.ToString("X"); int len = s.Length; maxBytes = 0 == len % 2 ? len / 2 : len / 2 + 1; } // borders if (maxBytes < 1) { maxBytes = 1; } if (minBytes < 1) { minBytes = 1; } if (minBytes > maxBytes) { maxBytes = minBytes; } // swap if (valFrom > valTo) { int tmp = valFrom; valFrom = valTo; valTo = tmp; } for (int bytes = minBytes; bytes <= maxBytes; bytes++) { found |= find(data, valFrom, valTo, bytes, dir, ref resList); } return(found); }
static public bool findValueInBuff(byte[] data, int val, ref List <findToolRes> resList, findToolRes.Direction dir = findToolRes.Direction.All, int maxBytes = -1) { return(findValueInBuff(data, val, val, ref resList, dir, maxBytes)); }
// do find static private bool find(byte[] data, int valFrom, int valTo, int bytes, findToolRes.Direction dir, ref List <findToolRes> resList) { // borders if (0 == bytes || bytes > data.Length) { return(false); } int tmp = 0; bool found = false; bool dirBE = dir == findToolRes.Direction.LeftToRight || dir == findToolRes.Direction.All; bool dirLE = dir == findToolRes.Direction.RightToLeft || dir == findToolRes.Direction.All; // left -> right (big-endian) if (dirBE) { for (int attempt = 0; attempt < data.Length - bytes + 1; attempt++) { // get tmp = 0; for (int i = 0; i < bytes; i++) { tmp = (tmp << 8) | data[attempt + i]; } // check if (tmp >= valFrom && tmp <= valTo) { resList.Add( new findToolRes(tmp, attempt, bytes, findToolRes.Direction.LeftToRight) ); found = true; } } } // right -> left (little-endian) if (bytes > 1 || !dirBE) { if (dirLE) { for (int attempt = 0; attempt < data.Length - bytes + 1; attempt++) { // get tmp = 0; for (int i = bytes - 1; i >= 0; i--) { tmp = (tmp << 8) | data[attempt + i]; } // check if (tmp >= valFrom && tmp <= valTo) { resList.Add( new findToolRes(tmp, attempt, bytes, findToolRes.Direction.RightToLeft) ); found = true; } } } } return(found); }