/// <inheritdoc /> internal override HandleResult OnReceive <TSender>(TSender sender, IntPtr connId, byte[] data, ParseRequestBody <TSender, TRequestBodyType> parseRequestBody) { var cache = _dataReceiveAdapterCache.Get(connId); if (cache == null) { return(HandleResult.Error); } cache.Data.AddRange(data); if (cache.Data.Count > data.Length) { data = cache.Data.ToArray(); } // 在data中搜索terminator var findList = _boyerMoore.SearchAll(data); if (findList.Count == 0) { // 没找到等下次 return(HandleResult.Ok); } // 终止符长度 var terminatorLength = _boyerMoore.PatternLength; // 最后位置 var lastPosition = 0; foreach (var index in findList) { // 数量 var bodyLength = index - lastPosition; // 得到一条完整数据包 var bodyData = cache.Data.GetRange(lastPosition, bodyLength).ToArray(); // 记录最后位置 lastPosition += bodyLength + terminatorLength; // 包体解析对象从适配器子类中获取 var obj = ParseRequestBody(bodyData); // 调用解析请求包体事件 if (parseRequestBody.Invoke(sender, connId, obj) == HandleResult.Error) { return(HandleResult.Error); } } if (lastPosition > 0) { // 清除已使用缓存 cache.Data.RemoveRange(0, lastPosition); } return(HandleResult.Ignore); }
private static int GetEndOfJpeg(byte[] fileBytes, int indexOfMp4, out BoyerMoore bm) { var endOfJpegBytes = MovingPhotoExtraction.ToBytes("FF D9".Split(' ')); bm = new BoyerMoore(endOfJpegBytes); var subBytes = fileBytes.Take(indexOfMp4).ToArray(); var endOfJpegList = bm.SearchAll(subBytes); return(endOfJpegList.LastOrDefault()); }
static void Main() { string _keyWord = "hey"; byte[] _byteKeyWord = Encoding.ASCII.GetBytes(_keyWord); string _context = "Lo hey hey rem ipsum dolor sit amet, consectetur adipiscing elit. Fusce et varius nisi"; byte[] _byteContext = Encoding.ASCII.GetBytes(_context); BoyerMoore boyerMoore = new BoyerMoore() .SetPattern(_byteKeyWord) .SetSearchArray(_byteContext); List <int> _findList = boyerMoore.SearchAll(); Console.WriteLine("Points of entitites:"); foreach (var item in _findList) { Console.WriteLine(item); } Console.WriteLine("Number of find words: " + boyerMoore.count()); Console.WriteLine("Long random prime: " + longRandomPrime()); }
public static bool Read(string file, List <byte[]> byteSearch) { //var r1 = new ExifLib.ExifReader(file); //var m1 = MetadataExtractor.ImageMetadataReader.ReadMetadata(file); Console.Write("Searching " + file); var fileBytes = System.IO.File.ReadAllBytes(file); int indexOfMp4 = -1; foreach (var search in byteSearch) { indexOfMp4 = new BoyerMoore(search).Search(fileBytes); if (indexOfMp4 >= 0) { break; } } if (indexOfMp4 >= 0) { Console.WriteLine(" Found the video"); WriteVideo(file, fileBytes, indexOfMp4); return(true); } else { Console.Write(" Did not find known video, trying generic approach"); // http://dev.exiv2.org/projects/exiv2/wiki/The_Metadata_in_JPEG_files says that a JPEG start of image with 0xFFD8 and ends with 0xFFD9. var endOfJpeg = MovingPhotoExtraction.ToBytes("FF D9".Split(' ')); var indexOfMp4_Part1 = MovingPhotoExtraction.ToBytes("00 00 00".Split(' ')); var indexOfMp4_Part2 = MovingPhotoExtraction.ToBytes("66 74 79 70".Split(' ')); var indexOfMp4_Part3 = MovingPhotoExtraction.ToBytes("6D 70 34 32".Split(' ')); var bm = new BoyerMoore(endOfJpeg); var endOf = bm.Search(fileBytes); bm.SetPattern(indexOfMp4_Part2); var part2s = bm.SearchAll(fileBytes); bm.SetPattern(indexOfMp4_Part3); foreach (var part2 in part2s) { if (part2 < endOf) { Console.Write("Not yet at end of jpeg"); } var part3 = bm.SearchAll(fileBytes).FirstOrDefault(a => a > part2); // part 3 is just a bit further than part2 if (part3 > part2 && part2 + 20 > part3) { var minus4 = fileBytes[part2 - 4]; var minus3 = fileBytes[part2 - 3]; var minus2 = fileBytes[part2 - 2]; var minus1 = fileBytes[part2 - 1]; if (minus4 == 0 && minus3 == 0 && minus2 == 0) { Console.WriteLine("... Found video via pattern search"); WriteVideo(file, fileBytes, part2 - 4); return(true); } Console.WriteLine($"... Found part2 and 3, not 1: {minus4} {minus3} {minus2} {minus1}"); } } Console.WriteLine($"Failed to find the video out of {part2s.Count} possibilites"); Console.WriteLine("Please report this output to https://github.com/cliveontoast/GoMoPho/issues and attach your moving image jpeg file"); } return(false); }