LEFT | RIGHT |
1 /* | 1 /* |
2 * Copyright (c) 2007 Todd C. Miller <Todd.Miller@courtesan.com> | 2 * Copyright (C) 2008 The Android Open Source Project |
| 3 * All rights reserved. |
3 * | 4 * |
4 * Permission to use, copy, modify, and distribute this software for any | 5 * Redistribution and use in source and binary forms, with or without |
5 * purpose with or without fee is hereby granted, provided that the above | 6 * modification, are permitted provided that the following conditions |
6 * copyright notice and this permission notice appear in all copies. | 7 * are met: |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above copyright |
| 11 * notice, this list of conditions and the following disclaimer in |
| 12 * the documentation and/or other materials provided with the |
| 13 * distribution. |
7 * | 14 * |
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | 16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | 17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | 18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | 19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, |
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | 21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS |
15 * | 22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED |
| 23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT |
| 25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF |
| 26 * SUCH DAMAGE. |
16 */ | 27 */ |
17 | 28 |
18 #include <string.h> | 29 #include <string> |
19 | |
20 #include "common/memrchr.h" | 30 #include "common/memrchr.h" |
21 | 31 |
22 namespace google_breakpad { | 32 namespace google_breakpad { |
23 | 33 |
24 /* | 34 void *memrchr(const void *s, int c, size_t n) |
25 * Reverse memchr() | |
26 * Find the last occurrence of 'c' in the buffer 's' of size 'n'. | |
27 */ | |
28 void * | |
29 memrchr(const void *s, int c, size_t n) | |
30 { | 35 { |
31 const unsigned char *cp; | 36 if (n > 0) { |
| 37 const char* p = (const char*) s; |
| 38 const char* q = p + n; |
32 | 39 |
33 if (n != 0) { | 40 while (1) { |
34 cp = (unsigned char *)s + n; | 41 q--; if (q < p || q[0] == (char) c) break; |
35 do { | 42 q--; if (q < p || q[0] == (char) c) break; |
36 if (*(--cp) == (unsigned char)c) | 43 q--; if (q < p || q[0] == (char) c) break; |
37 return((void *)cp); | 44 q--; if (q < p || q[0] == (char) c) break; |
38 } while (--n != 0); | 45 } |
| 46 if (q >= p) |
| 47 return (void*)q; |
39 } | 48 } |
40 return(NULL); | 49 return NULL; |
41 } | 50 } |
42 | 51 |
43 } // namespace google_breakpad | 52 } |
LEFT | RIGHT |