| OLD | NEW |
| 1 // Copyright 2003, 2004 Colin Percival | 1 // Copyright 2003, 2004 Colin Percival |
| 2 // All rights reserved | 2 // All rights reserved |
| 3 // | 3 // |
| 4 // Redistribution and use in source and binary forms, with or without | 4 // Redistribution and use in source and binary forms, with or without |
| 5 // modification, are permitted providing that the following conditions | 5 // modification, are permitted providing that the following conditions |
| 6 // are met: | 6 // are met: |
| 7 // 1. Redistributions of source code must retain the above copyright | 7 // 1. Redistributions of source code must retain the above copyright |
| 8 // notice, this list of conditions and the following disclaimer. | 8 // notice, this list of conditions and the following disclaimer. |
| 9 // 2. Redistributions in binary form must reproduce the above copyright | 9 // 2. Redistributions in binary form must reproduce the above copyright |
| 10 // notice, this list of conditions and the following disclaimer in the | 10 // notice, this list of conditions and the following disclaimer in the |
| (...skipping 15 matching lines...) Expand all Loading... |
| 26 // the adjoining file "LICENSE". | 26 // the adjoining file "LICENSE". |
| 27 // | 27 // |
| 28 // ChangeLog: | 28 // ChangeLog: |
| 29 // 2005-05-05 - Use the modified header struct from bspatch.h; use 32-bit | 29 // 2005-05-05 - Use the modified header struct from bspatch.h; use 32-bit |
| 30 // values throughout. | 30 // values throughout. |
| 31 // --Benjamin Smedberg <benjamin@smedbergs.us> | 31 // --Benjamin Smedberg <benjamin@smedbergs.us> |
| 32 // 2015-08-03 - Change search() to template to allow PagedArray usage. | 32 // 2015-08-03 - Change search() to template to allow PagedArray usage. |
| 33 // --Samuel Huang <huangs@chromium.org> | 33 // --Samuel Huang <huangs@chromium.org> |
| 34 // 2015-08-19 - Optimized search() to be non-recursive. | 34 // 2015-08-19 - Optimized search() to be non-recursive. |
| 35 // --Samuel Huang <huangs@chromium.org> | 35 // --Samuel Huang <huangs@chromium.org> |
| 36 // 2016-06-17 - Moved matchlen() and search() to a new file; format; changed | 36 // 2016-06-28 - Moved matchlen() and search() to a new file; format; changed |
| 37 // search() use std::lexicographical_compare(). | 37 // search() use std::lexicographical_compare(). |
| 38 // 2016-06-30 - Changed matchlen() input; changed search() to return struct. |
| 38 // --Samuel Huang <huangs@chromium.org> | 39 // --Samuel Huang <huangs@chromium.org> |
| 39 | 40 |
| 40 // Copyright 2016 The Chromium Authors. All rights reserved. | 41 // Copyright 2016 The Chromium Authors. All rights reserved. |
| 41 // Use of this source code is governed by a BSD-style license that can be | 42 // Use of this source code is governed by a BSD-style license that can be |
| 42 // found in the LICENSE file. | 43 // found in the LICENSE file. |
| 43 | 44 |
| 44 #ifndef COURGETTE_THIRD_PARTY_BSDIFF_BSDIFF_SEARCH_H_ | 45 #ifndef COURGETTE_THIRD_PARTY_BSDIFF_BSDIFF_SEARCH_H_ |
| 45 #define COURGETTE_THIRD_PARTY_BSDIFF_BSDIFF_SEARCH_H_ | 46 #define COURGETTE_THIRD_PARTY_BSDIFF_BSDIFF_SEARCH_H_ |
| 46 | 47 |
| 47 #include <algorithm> | 48 #include <algorithm> |
| 48 #include <cstring> | |
| 49 | 49 |
| 50 namespace courgette { | 50 namespace bsdiff { |
| 51 | 51 |
| 52 // Similar to ::memcmp(), but returns match length instead. | 52 // Return values of search(). |
| 53 inline int matchlen(const unsigned char* old, | 53 struct SearchResult { |
| 54 int oldsize, | 54 SearchResult(int pos_in, int size_in) : pos(pos_in), size(size_in) {} |
| 55 const unsigned char* newbuf, | 55 int pos; |
| 56 int newsize) { | 56 int size; |
| 57 int i; | 57 }; |
| 58 | 58 |
| 59 for (i = 0; (i < oldsize) && (i < newsize); i++) | 59 // Similar to ::memcmp(), but assumes equal |size| and returns match length. |
| 60 if (old[i] != newbuf[i]) | 60 inline int matchlen(const unsigned char* buf1, |
| 61 break; | 61 const unsigned char* buf2, |
| 62 | 62 int size) { |
| 63 return i; | 63 for (int i = 0; i < size; ++i) |
| 64 if (buf1[i] != buf2[i]) |
| 65 return i; |
| 66 return size; |
| 64 } | 67 } |
| 65 | 68 |
| 66 // Finds a suffix in |old| that has the longest common prefix with |newbuf|, | 69 // Finds a suffix in |old| that has the longest common prefix with |keybuf|, |
| 67 // aided by suffix array |I| of |old|. Returns the match length, and writes to | 70 // aided by suffix array |I| of |old|. Returns the match length, and writes to |
| 68 // |pos| a position of best match in |old|. If multiple such positions exist, | 71 // |pos| a position of best match in |old|. If multiple such positions exist, |
| 69 // |pos| would take an arbitrary one. | 72 // |pos| would take an arbitrary one. |
| 70 template <class T> | 73 template <class T> |
| 71 int search(T I, | 74 SearchResult search(T I, |
| 72 const unsigned char* old, | 75 const unsigned char* srcbuf, |
| 73 int oldsize, | 76 int srcsize, |
| 74 const unsigned char* newbuf, | 77 const unsigned char* keybuf, |
| 75 int newsize, | 78 int keysize) { |
| 76 int* pos) { | |
| 77 int lo = 0; | 79 int lo = 0; |
| 78 int hi = oldsize; | 80 int hi = srcsize; |
| 79 while (hi - lo >= 2) { | 81 while (hi - lo >= 2) { |
| 80 int mid = (lo + hi) / 2; | 82 int mid = (lo + hi) / 2; |
| 81 if (std::lexicographical_compare(old + I[mid], old + oldsize, newbuf, | 83 if (std::lexicographical_compare( |
| 82 newbuf + newsize)) { | 84 srcbuf + I[mid], srcbuf + srcsize, keybuf, keybuf + keysize)) { |
| 83 lo = mid; | 85 lo = mid; |
| 84 } else { | 86 } else { |
| 85 hi = mid; | 87 hi = mid; |
| 86 } | 88 } |
| 87 } | 89 } |
| 88 | 90 int x = matchlen(srcbuf + I[lo], keybuf, std::min(srcsize - I[lo], keysize)); |
| 89 int x = matchlen(old + I[lo], oldsize - I[lo], newbuf, newsize); | 91 int y = matchlen(srcbuf + I[hi], keybuf, std::min(srcsize - I[hi], keysize)); |
| 90 int y = matchlen(old + I[hi], oldsize - I[hi], newbuf, newsize); | 92 return (x > y) ? SearchResult(I[lo], x) : SearchResult(I[hi], y); |
| 91 if (x > y) { | |
| 92 *pos = I[lo]; | |
| 93 return x; | |
| 94 } | |
| 95 *pos = I[hi]; | |
| 96 return y; | |
| 97 } | 93 } |
| 98 | 94 |
| 99 } // namespace courgette | 95 } // namespace bsdiff |
| 100 | 96 |
| 101 #endif // COURGETTE_THIRD_PARTY_BSDIFF_BSDIFF_SEARCH_H_ | 97 #endif // COURGETTE_THIRD_PARTY_BSDIFF_BSDIFF_SEARCH_H_ |
| OLD | NEW |