binoffset.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. /***************************************************************************
  2. * binoffset.c
  3. * (C) 2002 Randy Dunlap <rdunlap@xenotime.net>
  4. # This program is free software; you can redistribute it and/or modify
  5. # it under the terms of the GNU General Public License as published by
  6. # the Free Software Foundation; either version 2 of the License, or
  7. # (at your option) any later version.
  8. #
  9. # This program is distributed in the hope that it will be useful,
  10. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. # GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License
  15. # along with this program; if not, write to the Free Software
  16. # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. # binoffset.c:
  18. # - searches a (binary) file for a specified (binary) pattern
  19. # - returns the offset of the located pattern or ~0 if not found
  20. # - exits with exit status 0 normally or non-0 if pattern is not found
  21. # or any other error occurs.
  22. ****************************************************************/
  23. #include <stdio.h>
  24. #include <stdlib.h>
  25. #include <string.h>
  26. #include <errno.h>
  27. #include <unistd.h>
  28. #include <fcntl.h>
  29. #include <sys/types.h>
  30. #include <sys/stat.h>
  31. #include <sys/mman.h>
  32. #define VERSION "0.1"
  33. #define BUF_SIZE (16 * 1024)
  34. #define PAT_SIZE 100
  35. char *progname;
  36. char *inputname;
  37. int inputfd;
  38. unsigned int bix; /* buf index */
  39. unsigned char patterns [PAT_SIZE] = {0}; /* byte-sized pattern array */
  40. int pat_len; /* actual number of pattern bytes */
  41. unsigned char *madr; /* mmap address */
  42. size_t filesize;
  43. int num_matches = 0;
  44. off_t firstloc = 0;
  45. void usage (void)
  46. {
  47. fprintf (stderr, "%s ver. %s\n", progname, VERSION);
  48. fprintf (stderr, "usage: %s filename pattern_bytes\n",
  49. progname);
  50. fprintf (stderr, " [prints location of pattern_bytes in file]\n");
  51. exit (1);
  52. }
  53. void get_pattern (int pat_count, char *pats [])
  54. {
  55. int ix, err, tmp;
  56. #ifdef DEBUG
  57. fprintf (stderr,"get_pattern: count = %d\n", pat_count);
  58. for (ix = 0; ix < pat_count; ix++)
  59. fprintf (stderr, " pat # %d: [%s]\n", ix, pats[ix]);
  60. #endif
  61. for (ix = 0; ix < pat_count; ix++) {
  62. tmp = 0;
  63. err = sscanf (pats[ix], "%5i", &tmp);
  64. if (err != 1 || tmp > 0xff) {
  65. fprintf (stderr, "pattern or value error in pattern # %d [%s]\n",
  66. ix, pats[ix]);
  67. usage ();
  68. }
  69. patterns [ix] = tmp;
  70. }
  71. pat_len = pat_count;
  72. }
  73. void search_pattern (void)
  74. {
  75. for (bix = 0; bix < filesize; bix++) {
  76. if (madr[bix] == patterns[0]) {
  77. if (memcmp (&madr[bix], patterns, pat_len) == 0) {
  78. if (num_matches == 0)
  79. firstloc = bix;
  80. num_matches++;
  81. }
  82. }
  83. }
  84. }
  85. #ifdef NOTDEF
  86. size_t get_filesize (int fd)
  87. {
  88. off_t end_off = lseek (fd, 0, SEEK_END);
  89. lseek (fd, 0, SEEK_SET);
  90. return (size_t) end_off;
  91. }
  92. #endif
  93. size_t get_filesize (int fd)
  94. {
  95. int err;
  96. struct stat stat;
  97. err = fstat (fd, &stat);
  98. fprintf (stderr, "filesize: %ld\n", err < 0 ? (long)err : stat.st_size);
  99. if (err < 0)
  100. return err;
  101. return (size_t) stat.st_size;
  102. }
  103. int main (int argc, char *argv [])
  104. {
  105. progname = argv[0];
  106. if (argc < 3)
  107. usage ();
  108. get_pattern (argc - 2, argv + 2);
  109. inputname = argv[1];
  110. inputfd = open (inputname, O_RDONLY);
  111. if (inputfd == -1) {
  112. fprintf (stderr, "%s: cannot open '%s'\n",
  113. progname, inputname);
  114. exit (3);
  115. }
  116. filesize = get_filesize (inputfd);
  117. madr = mmap (0, filesize, PROT_READ, MAP_PRIVATE, inputfd, 0);
  118. if (madr == MAP_FAILED) {
  119. fprintf (stderr, "mmap error = %d\n", errno);
  120. close (inputfd);
  121. exit (4);
  122. }
  123. search_pattern ();
  124. if (munmap (madr, filesize))
  125. fprintf (stderr, "munmap error = %d\n", errno);
  126. if (close (inputfd))
  127. fprintf (stderr, "%s: error %d closing '%s'\n",
  128. progname, errno, inputname);
  129. fprintf (stderr, "number of pattern matches = %d\n", num_matches);
  130. if (num_matches == 0)
  131. firstloc = ~0;
  132. printf ("%ld\n", firstloc);
  133. fprintf (stderr, "%ld\n", firstloc);
  134. exit (num_matches ? 0 : 2);
  135. }
  136. /* end binoffset.c */