Guest User

Untitled

a guest
Apr 22nd, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.45 KB | None | 0 0
  1. #include <boost/algorithm/string/find.hpp>
  2. #include <boost/iostreams/device/mapped_file.hpp>
  3. #include <cassert>
  4. #include <iostream>
  5. using namespace boost;
  6. using namespace boost::algorithm;
  7. using namespace boost::iostreams;
  8. using namespace std;
  9.  
  10. int main(int argc, char **argv)
  11. {
  12. if (argc != 3) {
  13. cerr << "Call: " << argv[0] << " PATTERN_FILE SRC_FILEn";
  14. return 3;
  15. }
  16. mapped_file_source pattern(argv[1]);
  17. mapped_file_source src(argv[2]);
  18. iterator_range<const char*> p_range(pattern.data(),
  19. pattern.data() + pattern.size());
  20. iterator_range<const char*> s_range(src.data(), src.data() + src.size());
  21. iterator_range<const char*> result = find_first(s_range, p_range);
  22. if (result) {
  23. size_t pos = result.begin()-s_range.begin();
  24. cout << pos << 'n';
  25. return 0;
  26. }
  27. return 1;
  28. }
  29.  
  30. $ make CXXFLAGS="-Wall -g" LDLIBS="-lboost_iostreams" searchb
  31.  
  32. $ dd if=WTF_-_EPISODE_277_RACHAEL_HARRIS.mp3 of=t skip=232323 bs=1 count=4K
  33. $ ls -l t
  34. -rw-r--r-- 1 juser users 4096 2012-05-31 15:24 t
  35. $ ./searchb t WTF_-_EPISODE_277_RACHAEL_HARRIS.mp3
  36. 232323
  37.  
  38. #!/usr/bin/env python
  39. import locale
  40. import os
  41. import sys
  42. import urllib2
  43.  
  44. def boyermoore_horspool(fd, needle):
  45. nlen = len(needle)
  46. nlast = nlen - 1
  47.  
  48. skip = []
  49. for k in range(256):
  50. skip.append(nlen)
  51. for k in range(nlast):
  52. skip[ord(needle[k])] = nlast - k
  53. skip = tuple(skip)
  54.  
  55. pos = 0
  56. consumed = 0
  57. haystack = bytes()
  58. while True:
  59. more = nlen - (consumed - pos)
  60. morebytes = fd.read(more)
  61. haystack = haystack[more:] + morebytes
  62.  
  63. if len(morebytes) < more:
  64. return -1
  65. consumed = consumed + more
  66.  
  67. i = nlast
  68. while i >= 0 and haystack[i] == needle[i]:
  69. i = i - 1
  70. if i == -1:
  71. return pos
  72.  
  73. pos = pos + skip[ord(haystack[nlast])]
  74.  
  75. return -1
  76.  
  77. if __name__ == "__main__":
  78. if len(sys.argv) < 2:
  79. sys.stderr.write("""Usage: horspool.py NEEDLE_FILE [URL]
  80. Search for the contents of NEEDLE_FILE inside the content at URL.
  81. If URL is omitted, search standard input.
  82. If the content is found, print the offset of the first occurrence and return 0.
  83. Otherwise, return 1.""")
  84. sys.exit(2)
  85. needle_file = open(sys.argv[1])
  86. needle = needle_file.read()
  87. needle_file.close
  88. fd = urllib2.urlopen(sys.argv[2]) if len(sys.argv) > 2 else sys.stdin
  89. offset = boyermoore_horspool(fd, needle)
  90. if offset >= 0: print offset
  91. else: sys.exit(1)
  92. fd.close()
Add Comment
Please, Sign In to add comment