Electroneum
Loading...
Searching...
No Matches
slow_memmem.cpp
Go to the documentation of this file.
1// Copyrights(c) 2017-2021, The Electroneum Project
2// Copyrights(c) 2014-2019, The Monero Project
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without modification, are
7// permitted provided that the following conditions are met:
8//
9// 1. Redistributions of source code must retain the above copyright notice, this list of
10// conditions and the following disclaimer.
11//
12// 2. Redistributions in binary form must reproduce the above copyright notice, this list
13// of conditions and the following disclaimer in the documentation and/or other
14// materials provided with the distribution.
15//
16// 3. Neither the name of the copyright holder nor the names of its contributors may be
17// used to endorse or promote products derived from this software without specific
18// prior written permission.
19//
20// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29//
30// Parts of this file are originally copyright (c) 2012-2013 The Cryptonote developers
31
32#include <stdio.h>
33#include <stdlib.h>
34#include <unistd.h>
35#include <string.h>
36#include <stdint.h>
37#include "gtest/gtest.h"
38
39// OS X, FreeBSD, and OpenBSD don't need malloc.h
40#if !defined(__APPLE__) && !defined(__FreeBSD__) && !defined(__OpenBSD__) && \
41 !defined(__DragonFly__)
42 #include <malloc.h>
43#endif
44
45//#define TEST_ORIGINAL
46//#define VERBOSE
47
48#ifdef TEST_ORIGINAL
49size_t slow_memmem_original(void* start_buff, size_t buflen,void* pat,size_t patlen)
50{
51 void* buf = start_buff;
52 void* end=(char*)buf+buflen-patlen;
53 while((buf=memchr(buf,((char*)pat)[0],buflen)))
54 {
55 if(buf>end)
56 return 0;
57 if(memcmp(buf,pat,patlen)==0)
58 return (char*)buf - (char*)start_buff;
59 buf=(char*)buf+1;
60 }
61 return 0;
62}
63
64#define slow_memmem slow_memmem_original
65#else
66namespace cryptonote {
67 size_t slow_memmem(const void* start_buff, size_t buflen,const void* pat,size_t patlen);
68}
69using namespace cryptonote;
70#endif
71
72static const struct {
73 size_t buflen;
74 const char *buf;
75 size_t patlen;
76 const char *pat;
77 size_t res;
78} T[]={
79 {0,"",0,"",0},
80 {1,"",0,"",0},
81 {0,"",1,"",0},
82 {1,"x",1,"x",0},
83 {2,"x",1,"",1},
84 {1,"x",1,"",0},
85 {1,"x",2,"x",0},
86 {2,"ax",2,"x",0},
87 {1,"xx",2,"xx",0},
88 {4,"abcd",3,"abc",0},
89 {4,"abcd",3,"bcd",1},
90 {4,"abcd",4,"abcd",0},
91 {4,"abcd",1,"d",3},
92 {4,"abcd",1,"c",2},
93 {4,"abcd",1,"bc",1},
94 {4,"abcd",1,"",0},
95 {3,"abcd",1,"d",0},
96 {5,"aaaab",2,"ab",3},
97 {7,"aaaabab",2,"ab",3},
98 {7,"aaaabab",3,"abc",0},
99 {4,"abcd",2,"cd",2},
100 {3,"abcd",2,"cd",0},
101 {3,"a\0b",1,"",1},
102 {3,"a\0b",2,"\0b",1},
103 {8,"xxxxxxab",3,"xyz",0},
104 {8,"xxxxxxab",6,"abcdef",0},
105 {9,"\0xxxxxab",3,"ab",6},
106 {4,"\0\0a",3,"\0a",1}, //
107};
108
109TEST(slowmem,Success)
110{
111 size_t n;
112 for (n=0;n<sizeof(T)/sizeof(T[0]);++n) {
113#ifdef VERBOSE
114 printf("%3zu: ",n);
115 fflush(stdout);
116#endif
117 void *buf=malloc(T[n].buflen);
118 memcpy(buf,T[n].buf,T[n].buflen);
119 void *pat=malloc(T[n].patlen);
120 memcpy(pat,T[n].pat,T[n].patlen);
121 size_t res=slow_memmem(buf,T[n].buflen,pat,T[n].patlen);
122 free(pat);
123 free(buf);
124 ASSERT_EQ(res,T[n].res);
125#ifdef VERBOSE
126 if (res!=T[n].res) printf("failed (got %zu, expected %zu)",res,T[n].res); else printf("ok");
127 printf("\n");
128#endif
129 }
130}
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1956
#define TEST(test_case_name, test_name)
Definition gtest.h:2187
void * memcpy(void *a, const void *b, size_t c)
const char * res
Holds cryptonote related classes and helpers.
Definition ban.cpp:40
size_t slow_memmem(const void *start_buff, size_t buflen, const void *pat, size_t patlen)
size_t patlen
const char * pat
size_t buflen
const char * buf
#define T(x)