NAME=Search hex byte search - errors
FILE=bins/cmd/search/hex_bytes
CMDS=<<EOF
/x 00p
/x pp:ff
/x fff:ee
/x f:ee
/x f:
/x :f
/x f:ee:aa
# Things people do:
/x pp:f🍍
/x xff:xee
# Mask should not contain wildcards
/x 4141f1:f0f0f.
/x .2:ff
EOF
EXPECT_ERR=<<EOF
ERROR: Pattern contains forbitten characters. Allowed is only '0x', '0-9', 'a-f', 'A-F', '.' and ':'.
ERROR: Failed to parse given pattern.
ERROR: Pattern contains forbitten characters. Allowed is only '0x', '0-9', 'a-f', 'A-F', '.' and ':'.
ERROR: Failed to parse given pattern.
ERROR: Mask and bytes must have the same number of nibbles. But they mismatch: 2 != 3
ERROR: Failed to parse given pattern.
ERROR: Mask and bytes must have the same number of nibbles. But they mismatch: 2 != 1
ERROR: Failed to parse given pattern.
ERROR: Regex matching failed. Wrong group count.
ERROR: Failed to parse given pattern.
ERROR: Regex matching failed. Wrong group count.
ERROR: Failed to parse given pattern.
ERROR: More than one ':' is invalid.
ERROR: Failed to parse given pattern.
ERROR: Pattern contains forbitten characters. Allowed is only '0x', '0-9', 'a-f', 'A-F', '.' and ':'.
ERROR: Failed to parse given pattern.
ERROR: Regex matching failed. Wrong group count.
ERROR: Failed to parse given pattern.
ERROR: The mask cannot contain wildcards.
ERROR: Failed to parse given pattern.
ERROR: With a custom mask no wildcards are allowed.
ERROR: Failed to parse given pattern.
EOF
RUN

NAME=Search hex bytes regex - errors
FILE=bins/cmd/search/string_encodings/Chinese-Lipsum.utf32le
CMDS=<<EOF
/xr x8
/xr x8ylllasl
/xr yxxxx
EOF
EXPECT_ERR=<<EOF
ERROR: 'x' in the pattern must be followed by two hexadecimal nibbles (N = [a-fA-F0-9]): xNN.
ERROR: Bytes with non-hexadecimal nibbles are not allowed. Got: 'x8y'.
ERROR: Bytes with non-hexadecimal nibbles are not allowed. Got: 'xxx'.
EOF
RUN

NAME=Search hex bytes regex - matches
FILE=bins/cmd/search/string_encodings/Chinese-Lipsum.utf32le
CMDS=<<EOF
# Exact match
/xr x08x54x00x00x31
# None ASCII bytes
/xr x8bx00x00xd0x63
# Complex match
/xr x72.?x00{2}
EOF
EXPECT=<<EOF
0x00000000 5 hit.bytes.0
0x00000039 5 hit.bytes.0
0x0000000c 4 hit.bytes.0
0x0000007c 4 hit.bytes.1
0x00000195 3 hit.bytes.2
EOF
RUN

NAME=Search hex byte search - normal search
FILE=bins/cmd/search/hex_bytes
CMDS=<<EOF
e search.prefix="a"
/x 61
e search.prefix="b"
/x 62
# Pineapple
e search.prefix="🍍"
/x f09f8d8d
EOF
EXPECT=<<EOF
0x00000000 1 a.bytes.0
0x00000018 1 a.bytes.1
0x00000030 1 a.bytes.2
0x00000001 1 b.bytes.0
0x00000019 1 b.bytes.1
0x00000031 1 b.bytes.2
0x0000000e 4 🍍.bytes.0
0x00000026 4 🍍.bytes.1
0x0000003e 4 🍍.bytes.2
EOF
RUN

NAME=Search hex bytes - limits
FILE=bins/cmd/search/hex_bytes
CMDS=<<EOF
e search.prefix="all"
/x 6162636465666741424344454647f09f8d8d2092ff7211016162636465666741424344454647f09f8d8d2092ff7211016162636465666741424344454647f09f8d8d2092ff720a
e search.prefix="overlimit"
/x 6162636465666741424344454647f09f8d8d2092ff7211016162636465666741424344454647f09f8d8d2092ff7211016162636465666741424344454647f09f8d8d2092ff720aff
EOF
EXPECT=<<EOF
0x00000000 71 all.bytes.0
EOF
RUN

NAME=Search hex byte search - wildcard search - no overlap
FILE=bins/cmd/search/hex_bytes
CMDS=<<EOF
e search.overlap=false
e search.prefix="3wild"
/x ...1
e search.prefix="complex"
/x 4..54647.09f...d2092.f72
EOF
EXPECT=<<EOF
0x00000006 2 3wild.bytes.0
0x00000015 2 3wild.bytes.1
0x00000017 2 3wild.bytes.2
0x0000001e 2 3wild.bytes.3
0x0000002d 2 3wild.bytes.4
0x0000002f 2 3wild.bytes.5
0x00000036 2 3wild.bytes.6
0x0000000a 12 complex.bytes.0
0x00000022 12 complex.bytes.1
0x0000003a 12 complex.bytes.2
EOF
RUN


NAME=Search hex byte search - wildcard search - overlap
FILE=bins/cmd/search/hex_bytes
CMDS=<<EOF
e search.overlap=true
e search.prefix="4wild2"
/x 4.4.
EOF
EXPECT=<<EOF
0x00000007 2 4wild2.bytes.0
0x00000008 2 4wild2.bytes.1
0x00000009 2 4wild2.bytes.2
0x0000000a 2 4wild2.bytes.3
0x0000000b 2 4wild2.bytes.4
0x0000000c 2 4wild2.bytes.5
0x0000001f 2 4wild2.bytes.6
0x00000020 2 4wild2.bytes.7
0x00000021 2 4wild2.bytes.8
0x00000022 2 4wild2.bytes.9
0x00000023 2 4wild2.bytes.10
0x00000024 2 4wild2.bytes.11
0x00000037 2 4wild2.bytes.12
0x00000038 2 4wild2.bytes.13
0x00000039 2 4wild2.bytes.14
0x0000003a 2 4wild2.bytes.15
0x0000003b 2 4wild2.bytes.16
0x0000003c 2 4wild2.bytes.17
EOF
RUN

NAME=Search hex byte search - odd number of nibbles
FILE=bins/cmd/search/hex_bytes_1024
CMDS=<<EOF
# Should be equivalent to .140
e search.prefix="odd14f"
/x 14f
# Should be equivalent to .0424a41424b
e search.prefix="odd1424"
/x 1424a41424b
EOF
EXPECT=<<EOF
0x00000029 2 odd14f.bytes.0
0x000000d0 6 odd1424.bytes.0
EOF
RUN

NAME=Search hex byte search - custom flag
FILE=bins/cmd/search/hex_bytes
CMDS=<<EOF
e search.prefix="1"
/x 4040f0:f0f0f0
# Invalid flag value combination. Mask dominates.
e search.prefix="2"
/x 4141f1:f0f0f0
e search.prefix="all_f"
/x 4647f0:ffffff
e search.prefix="odd_mask"
/x 647f0:f0f0f
# Custom flag with wildcard.
e search.overlap=false
e search.prefix="4142"
/x 4142:4142
EOF
EXPECT=<<EOF
0x0000000c 3 1.bytes.0
0x00000024 3 1.bytes.1
0x0000003c 3 1.bytes.2
0x0000000c 3 2.bytes.0
0x00000024 3 2.bytes.1
0x0000003c 3 2.bytes.2
0x0000000c 3 all_f.bytes.0
0x00000024 3 all_f.bytes.1
0x0000003c 3 all_f.bytes.2
0x0000000c 3 odd_mask.bytes.0
0x00000024 3 odd_mask.bytes.1
0x0000003c 3 odd_mask.bytes.2
0x00000000 2 4142.bytes.0
0x00000004 2 4142.bytes.1
0x00000007 2 4142.bytes.2
0x0000000b 2 4142.bytes.3
0x00000014 2 4142.bytes.4
0x00000018 2 4142.bytes.5
0x0000001c 2 4142.bytes.6
0x0000001f 2 4142.bytes.7
0x00000023 2 4142.bytes.8
0x0000002c 2 4142.bytes.9
0x00000030 2 4142.bytes.10
0x00000034 2 4142.bytes.11
0x00000037 2 4142.bytes.12
0x0000003b 2 4142.bytes.13
0x00000044 2 4142.bytes.14
EOF
RUN

NAME=Search hex byte search - search over boundary
FILE=bins/cmd/search/hex_bytes_128K
CMDS=<<EOF
# The default chunk size (RZ_SEARCH_DEFAULT_CHUNK_SIZE) is 4096 bytes.
#
# Current elements per chunk are 64 (see: RZ_SEARCH_MIN_ELEMENTS_PER_CHUNK).
# So at a minimum we need 6 * 64 = 256 bytes per chunk.
# This is way smaller than the default chunk size. Hence we keep the default one.
#
# This test searches over the boundary of the chunks.
# Checking if results are found there as well.
# Boundary at from 0x0ffff/0x10000.

e search.overlap=true
e search.prefix="chunk_32_overlap_0_1fffa"
/x 343735383464
e search.prefix="chunk_32_overlap_1_1fffb"
/x 373538346434
e search.prefix="chunk_32_overlap_2_1fffc"
/x 353834643437
e search.prefix="chunk_32_overlap_3_1fffd"
/x 383464343735
e search.prefix="chunk_32_overlap_4_1fffe"
/x 346434373538
e search.prefix="chunk_32_overlap_5_1ffff"
/x 643437353834
e search.prefix="chunk_33_20000"
/x 343735383465
EOF
EXPECT=<<EOF
0x0001fffa 6 chunk_32_overlap_0_1fffa.bytes.0
0x0001fffb 6 chunk_32_overlap_1_1fffb.bytes.0
0x0001fffc 6 chunk_32_overlap_2_1fffc.bytes.0
0x0001fffd 6 chunk_32_overlap_3_1fffd.bytes.0
0x0001fffe 6 chunk_32_overlap_4_1fffe.bytes.0
0x0001ffff 6 chunk_32_overlap_5_1ffff.bytes.0
0x00020000 6 chunk_33_20000.bytes.0
EOF
RUN

NAME=Search hex byte search - search larger than default chunk
FILE=bins/cmd/search/hex_bytes_128K
CMDS=<<EOF
# The default chunk size (RZ_SEARCH_DEFAULT_CHUNK_SIZE) is 4096 bytes.
# So each search element is allowed to be 4096 / 64 = 64 bytes
e search.overlap=true
e search.prefix="larger_than_default_chunk"
/x 343134313431343234313431343334313431343434313431343534313431343634313431343734313431343834313431343934313431346134313431346234313431346334313431346434313431346534313431346634313431353034313431353134313431353234313431353334313431353434313431353534313431353634313431353734313431353834313431353934313431356134313431363134313431363234313431363334313431363434313431363534313431363634313431363734313431363834313431363934313431366134313431366234313431366334313431366434313431366534313431366634313431373034313431373134313431373234313431373334313431373434313431373534313431373634313431373734313431373834313431373934313431376134313431333134313431333234313431333334313431333434313431333534313431333634313431333734313431333834313431333934313431333034313432343234313432343334313432343434313432343534313432343634313432343734313432343834313432343934313432346134313432346234313432346334313432346434313432346534313432346634313432353034313432353134313432353234313432353334313432353434313432353534313432353634313432353734313432353834313432353934313432356134313432363134313432363234313432363334313432363434313432363534313432363634313432363734313432363834313432363934313432366134313432366234313432366334313432366434313432366534313432366634313432373034313432373134313432373234313432373334313432373434313432373534313432373634313432373734313432373834313432373934313432376134313432333134313432333234313432333334313432333434313432333534313432333634313432333734313432333834313432333934313432333034313433343234313433343334313433343434313433343534313433343634313433343734313433343834313433343934313433346134313433346234313433346334313433346434313433346534313433346634313433353034313433353134313433353234313433353334313433353434313433353534313433353634313433353734313433353834313433353934313433356134313433363134313433363234313433363334313433363434313433363534313433363634313433363734313433363834313433363934313433366134313433366234313433366334313433366434313433366534313433366634313433373034313433373134313433373234313433373334313433373434313433373534313433373634313433373734313433373834313433373934313433376134313433333134313433333234313433333334313433333434313433333534313433333634313433333734313433333834313433333934313433333034313434343234313434343334313434343434313434343534313434343634313434343734313434343834313434343934313434346134313434346234313434346334313434346434313434346534313434346634313434353034313434353134313434353234313434353334313434353434313434353534313434353634313434353734313434353834313434353934313434356134313434363134313434363234313434363334313434363434313434363534313434363634313434363734313434363834313434363934313434366134313434366234313434366334313434366434313434366534313434366634313434373034313434373134313434373234313434373334313434373434313434373534313434373634313434373734313434373834313434373934313434376134313434333134313434333234313434333334313434333434313434333534313434333634313434333734313434333834313434333934313434333034313435343234313435343334313435343434313435343534313435343634313435343734313435343834313435343934313435346134313435346234313435346334313435346434313435346534313435346634313435353034313435353134313435353234313435353334313435353434313435353534313435353634313435353734313435353834313435353934313435356134313435363134313435363234313435363334313435363434313435363534313435363634313435363734313435363834313435363934313435366134313435366234313435366334313435366434313435366534313435366634313435373034313435373134313435373234313435373334313435373434313435373534313435373634313435373734313435373834313435373934313435376134313435333134313435333234313435333334313435333434313435333534313435333634313435333734313435333834313435333934313435333034313436343234313436343334313436343434313436343534313436343634313436343734313436343834313436343934313436346134313436346234313436346334313436346434313436346534313436346634313436353034313436353134313436353234313436353334313436353434313436353534313436353634313436353734313436353834313436353934313436356134313436363134313436363234313436363334313436363434313436363534313436363634313436363734313436363834313436363934313436366134313436366234313436366334313436366434313436366534313436366634313436373034313436373134313436373234313436373334313436373434313436373534313436373634313436373734313436373834313436373934313436376134313436333134313436333234313436333334313436333434313436333534313436333634313436333734313436333834313436333934313436333034313437343234313437343334313437343434313437343534313437343634313437343734313437343834313437343934313437346134313437346234313437346334313437346434313437346534313437346634313437353034313437353134313437353234313437353334313437353434313437353534313437353634313437353734313437353834313437353934313437356134313437363134313437363234313437363334313437363434313437363534313437363634313437363734313437363834313437363934313437366134313437366234313437366334313437366434313437366534313437366634313437373034313437373134313437373234313437373334313437373434313437373534313437373634313437373734313437373834313437373934313437376134313437333134313437333234313437333334313437333434313437333534313437333634313437333734313437333834313437333934313437333034313438343234313438343334313438343434313438343534313438343634313438343734313438343834313438343934313438346134313438346234313438346334313438346434313438346534313438346634313438353034313438353134313438353234313438353334313438353434313438353534313438353634313438353734313438353834313438353934313438356134313438363134313438363234313438363334313438363434313438363534313438363634313438363734313438363834313438363934313438366134313438366234313438366334313438366434313438366534313438366634313438373034313438373134313438373234313438373334313438373434313438373534313438373634313438373734313438373834313438373934313438376134313438333134313438333234313438333334313438333434313438333534313438333634313438333734313438333834313438333934313438333034313439343234313439343334313439343434313439343534313439343634313439343734313439343834313439343934313439346134313439346234313439346334313439346434313439346534313439346634313439353034313439353134313439353234313439353334313439353434313439353534313439353634313439353734313439353834313439353934313439356134313439363134313439363234313439363334313439363434313439363534313439363634313439363734313439363834313439363934313439366134313439366234313439366334313439366434313439366534313439366634313439373034313439373134313439373234313439373334313439373434313439373534313439373634313439373734313439373834313439373934313439376134313439333134313439333234313439333334313439333434313439333534313439333634313439333734313439333834313439333934313439333034313461343234313461343334313461343434313461343534313461343634313461343734313461343834313461343934313461346134313461346234313461346334313461346434313461346534313461346634313461353034313461353134313461353234313461353334313461353434313461353534313461353634313461353734313461353834313461353934313461356134313461363134313461363234313461363334313461363434313461363534313461363634313461363734313461363834313461363934313461366134313461366234313461366334313461366434313461366534313461366634313461373034313461373134313461373234313461373334313461373434313461373534313461373634313461373734313461373834313461373934313461376134313461333134313461333234313461333334313461333434313461333534313461333634313461333734313461333834313461333934313461333034313462343234313462343334313462343434313462343534313462343634313462343734313462343834313462343934313462346134313462346234313462346334313462346434313462346534313462346634313462353034313462353134313462353234313462353334313462353434313462353534313462353634313462353734313462353834313462353934313462356134313462363134313462363234313462363334313462363434313462363534313462363634313462363734313462363834313462363934313462366134313462366234313462366334313462366434313462366534313462366634313462373034313462373134313462373234313462373334313462373434313462373534313462373634313462373734313462373834313462373934313462376134313462333134313462333234313462333334313462333434313462333534313462333634313462333734313462333834313462333934313462333034313463343234313463343334313463343434313463343534313463343634313463343734313463343834313463343934313463346134313463346234313463346334313463346434313463346534313463346634313463353034313463353134
e search.prefix="just_fit_in_one_chunk"
/x 34313431343134323431343134333431343134343431343134353431343134363431343134373431343134383431343134393431343134613431343134623431
EOF
EXPECT=<<EOF
0x00000000 4125 larger_than_default_chunk.bytes.0
0x00000000 64 just_fit_in_one_chunk.bytes.0
EOF
RUN

NAME=Search hex byte search - search larger than file
FILE=bins/cmd/search/hex_bytes
CMDS=<<EOF
e search.overlap=true
/x 34313431343134323431343134333431343134343431343134353431343134363431343134373431343134383431343134393431343134613431343134623431
EOF
EXPECT=<<EOF
EOF
RUN

NAME=Search library maps.
FILE=bins/elf/arch-x86_64-ls
CMDS=<<EOF
e search.in=io.maps
# This equals the string: "ISO-10646/UCS4/".
# It should only be present if libc is loaded.
/x 49534f2d31303634362f554353342f~?
EOF
EXPECT=<<EOF
0
EOF
RUN

NAME=/xr on virtual address
FILE=bins/cmd/search/alignment_test
ARGS=-m 0x80000
CMDS=<<EOF
e search.overlap=true
om 3 0x400000
echo Alignment=None
/xr x62x62.x62x62~$[0]
e search.align=4
echo Alignment=4
/xr x62x62.x62x62~$[0]
EOF
EXPECT=<<EOF
Alignment=None
0x00080032
0x00080040
0x00080041
0x00080052
0x00080057
0x0008005a
0x00400032
0x00400040
0x00400041
0x00400052
0x00400057
0x0040005a
Alignment=4
0x00080040
0x00400040
EOF
RUN

NAME=/x on virtual address
FILE=bins/cmd/search/alignment_test
ARGS=-m 0x80000
CMDS=<<EOF
e search.overlap=true
echo Alignment=None
om 3 0x400000
/x 62626.6262~$[0]
e search.align=4
echo Alignment=4
/x 62626.6262~$[0]
EOF
EXPECT=<<EOF
Alignment=None
0x00080032
0x00080040
0x00080041
0x00080052
0x0008005a
0x00400032
0x00400040
0x00400041
0x00400052
0x0040005a
Alignment=4
0x00080040
0x00400040
EOF
RUN

NAME=/xr alignment
FILE=bins/cmd/search/alignment_test
ARGS=
CMDS=<<EOF
e search.overlap=true
echo Alignment=None
/xr x62x62.x62x62~$[0]
e search.align=2
echo Alignment=2
/xr x62x62.x62x62~$[0]
e search.align=4
echo Alignment=4
/xr x62x62.x62x62~$[0]
e search.align=16
echo Alignment=16
/xr x62x62.x62x62~$[0]
EOF
EXPECT=<<EOF
Alignment=None
0x00000032
0x00000040
0x00000041
0x00000052
0x00000057
0x0000005a
Alignment=2
0x00000032
0x00000040
0x00000052
0x0000005a
Alignment=4
0x00000040
Alignment=16
0x00000040
EOF
RUN

NAME=/x alignment
FILE=bins/cmd/search/alignment_test
ARGS=
CMDS=<<EOF
e search.overlap=true
echo Alignment=None
/x 62626.6262~$[0]
e search.align=2
echo Alignment=2
/x 62626.6262~$[0]
e search.align=4
echo Alignment=4
/x 62626.6262~$[0]
e search.align=16
echo Alignment=16
/x 62626.6262~$[0]
EOF
EXPECT=<<EOF
Alignment=None
0x00000032
0x00000040
0x00000041
0x00000052
0x0000005a
Alignment=2
0x00000032
0x00000040
0x00000052
0x0000005a
Alignment=4
0x00000040
Alignment=16
0x00000040
EOF
RUN
