|
- public static string byteStr(ushort val)
- {
- byte num = (byte) ((val >> 8) & 0xff);
- byte num2 = (byte) (val & 0xff);
- return string.Format("{0:X2} {1:X2}", num, num2);
- }
- public static ulong HexToInt(string hexString)
- {
- string str = "0123456789abcdef";
- string str2 = hexString.ToLower();
- if ((str2.Length > 2) && str2.Substring(0, 2).Equals("0x"))
- {
- str2 = str2.Substring(2, str2.Length - 2);
- }
- string str3 = "";
- int length = str2.Length;
- for (int i = length - 1; i >= 0; i--)
- {
- str3 = str3 + str2[i];
- }
- ulong num3 = 0L;
- ulong num4 = 1L;
- for (int j = 0; j < length; j++)
- {
- uint index = (uint) str.IndexOf(str3[j]);
- num3 += (j == 0) ? ((ulong) index) : (num4 * index);
- num4 *= (ulong) 0x10L;
- }
- return num3;
- }
- public static bool ParseHexData(ArrayList rawList, byte[] FwBuf, ref ushort FwLen, ref ushort FwOff)
- {
- string str;
- for (int i = rawList.Count - 1; i >= 0; i--)
- {
- str = (string) rawList[i];
- if ((str.Length > 0) && (((int) HexToInt(str.Substring(7, 2))) != 0))
- {
- rawList.Remove(rawList[i]);
- }
- }
- FwLen = 0;
- FwOff = _MAX_FW_SIZE;
- for (int j = 0; j < _MAX_FW_SIZE; j++)
- {
- FwBuf[j] = 0xff;
- }
- for (int k = 0; k < rawList.Count; k++)
- {
- str = (string) rawList[k];
- int index = str.IndexOf("//");
- if (index > -1)
- {
- str = str.Substring(0, index - 1);
- }
- if (str.Length > 0)
- {
- ushort num5 = (ushort) HexToInt(str.Substring(3, 4));
- if (num5 >= _MAX_FW_SIZE)
- {
- return false;
- }
- if (num5 < FwOff)
- {
- FwOff = num5;
- }
- index = ((int) HexToInt(str.Substring(1, 2))) * 2;
- string str4 = str.Substring(9, index);
- int num6 = index / 2;
- int num7 = 0;
- while (num7 < num6)
- {
- FwBuf[num5] = (byte) HexToInt(str4.Substring(num7 * 2, 2));
- num7++;
- num5 = (ushort) (num5 + 1);
- }
- if (num5 > FwLen)
- {
- FwLen = num5;
- }
- }
- }
- return true;
- }
- public static bool ParseHexFile(string fName, byte[] FwBuf, ref ushort FwLen, ref ushort FwOff)
- {
- if (File.Exists(fName))
- {
- ArrayList rawList = new ArrayList();
- StreamReader reader = new StreamReader(fName);
- if (reader != null)
- {
- string str;
- while ((str = reader.ReadLine()) != null)
- {
- rawList.Add(str);
- }
- reader.Close();
- return ParseHexData(rawList, FwBuf, ref FwLen, ref FwOff);
- }
- }
- return false;
- }
- public static unsafe void ParseIICData(byte[] fData, byte[] FwBuf, ref ushort FwLen, ref ushort FwOff)
- {
- ushort xStart = 8;
- FwLen = 0;
- FwOff = _MAX_FW_SIZE;
- for (int i = 0; i < _MAX_FW_SIZE; i++)
- {
- FwBuf[i] = 0xff;
- }
- fixed (byte* numRef = fData)
- {
- ushort* numPtr;
- do
- {
- ReverseBytes(fData, xStart, 2);
- ReverseBytes(fData, xStart + 2, 2);
- numPtr = (ushort*) (numRef + xStart);
- ushort* numPtr2 = (ushort*) ((numRef + xStart) + 2);
- if (numPtr[0] != 0x8001)
- {
- Array.Copy(fData, xStart + 4, FwBuf, (int) numPtr2[0], (int) numPtr[0]);
- ushort num3 = (ushort) (numPtr2[0] + numPtr[0]);
- if (num3 > FwLen)
- {
- FwLen = num3;
- }
- if (numPtr2[0] < FwOff)
- {
- FwOff = numPtr2[0];
- }
- }
- xStart = (ushort) (xStart + ((ushort) (numPtr[0] + 4)));
- }
- while ((numPtr[0] != 0x8001) && (xStart < fData.Length));
- }
- }
- public static bool ParseIICFile(string fName, byte[] FwBuf, ref ushort FwLen, ref ushort FwOff)
- {
- if (!File.Exists(fName))
- {
- return false;
- }
- Stream stream = new FileStream(fName, FileMode.Open, FileAccess.Read);
- if (stream == null)
- {
- return false;
- }
- int length = (int) stream.Length;
- byte[] buffer = new byte[length];
- stream.Read(buffer, 0, length);
- stream.Close();
- if (length > _MAX_FW_SIZE)
- {
- return false;
- }
- ParseIICData(buffer, FwBuf, ref FwLen, ref FwOff);
- return true;
- }
- public static unsafe int ReverseBytes(byte* dta, int bytes)
- {
- if (bytes < 0)
- {
- return 0;
- }
- byte[] buffer = new byte[bytes];
- for (byte i = 0; i < bytes; i = (byte) (i + 1))
- {
- buffer[i] = *(((dta + bytes) - i) - 1);
- }
- for (byte j = 0; j < bytes; j = (byte) (j + 1))
- {
- dta[j] = buffer[j];
- }
- return ((bytes > 2) ? BitConverter.ToInt32(buffer, 0) : BitConverter.ToInt16(buffer, 0));
- }
- public static int ReverseBytes(byte[] dta, int xStart, int bytes)
- {
- if (bytes < 0)
- {
- return 0;
- }
- byte[] buffer = new byte[bytes];
- for (byte i = 0; i < bytes; i = (byte) (i + 1))
- {
- buffer[i] = dta[((xStart + bytes) - i) - 1];
- }
- for (byte j = 0; j < bytes; j = (byte) (j + 1))
- {
- dta[xStart + j] = buffer[j];
- }
- return ((bytes > 2) ? BitConverter.ToInt32(buffer, 0) : BitConverter.ToInt16(buffer, 0));
- }
复制代码
|
|