Поиск по subbytes
This commit is contained in:
@@ -4,16 +4,18 @@ import lombok.extern.slf4j.Slf4j;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.context.annotation.Lazy;
|
||||
import org.springframework.security.crypto.codec.Hex;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import ru.serega6531.packmate.model.FoundPattern;
|
||||
import ru.serega6531.packmate.model.Pattern;
|
||||
import ru.serega6531.packmate.model.enums.PatternDirectionType;
|
||||
import ru.serega6531.packmate.model.Stream;
|
||||
import ru.serega6531.packmate.model.enums.PatternDirectionType;
|
||||
import ru.serega6531.packmate.model.enums.PatternSearchType;
|
||||
import ru.serega6531.packmate.model.enums.SubscriptionMessageType;
|
||||
import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
|
||||
import ru.serega6531.packmate.repository.PatternRepository;
|
||||
import ru.serega6531.packmate.utils.Bytes;
|
||||
|
||||
import java.util.*;
|
||||
import java.util.regex.Matcher;
|
||||
@@ -51,20 +53,19 @@ public class PatternService {
|
||||
}
|
||||
|
||||
public Set<FoundPattern> findMatches(byte[] bytes, boolean incoming) {
|
||||
String content = new String(bytes);
|
||||
|
||||
return patterns.values().stream()
|
||||
.filter(p -> p.getDirectionType() == (incoming ? PatternDirectionType.INPUT : PatternDirectionType.OUTPUT)
|
||||
|| p.getDirectionType() == PatternDirectionType.BOTH)
|
||||
.map(pattern -> match(pattern, content))
|
||||
.map(pattern -> match(pattern, bytes))
|
||||
.flatMap(List::stream)
|
||||
.collect(Collectors.toSet());
|
||||
}
|
||||
|
||||
private List<FoundPattern> match(Pattern pattern, String content) {
|
||||
private List<FoundPattern> match(Pattern pattern, byte[] bytes) {
|
||||
List<FoundPattern> found = new ArrayList<>();
|
||||
|
||||
if (pattern.getSearchType() == PatternSearchType.REGEX) {
|
||||
String content = new String(bytes);
|
||||
final java.util.regex.Pattern regex = compilePattern(pattern);
|
||||
final Matcher matcher = regex.matcher(content);
|
||||
|
||||
@@ -78,9 +79,10 @@ public class PatternService {
|
||||
|
||||
return found;
|
||||
} else if (pattern.getSearchType() == PatternSearchType.SUBSTRING) {
|
||||
String content = new String(bytes);
|
||||
int startSearch = 0;
|
||||
|
||||
final String value = pattern.getValue();
|
||||
|
||||
while (true) {
|
||||
int start = StringUtils.indexOfIgnoreCase(content, value, startSearch);
|
||||
|
||||
@@ -98,8 +100,25 @@ public class PatternService {
|
||||
startSearch = end + 1;
|
||||
}
|
||||
} else { // SUBBYTES
|
||||
// TODO
|
||||
return found;
|
||||
int startSearch = 0;
|
||||
final byte[] value = Hex.decode(pattern.getValue());
|
||||
|
||||
while (true) {
|
||||
int start = Bytes.indexOf(bytes, value, startSearch, bytes.length);
|
||||
|
||||
if (start == -1) {
|
||||
return found;
|
||||
}
|
||||
|
||||
int end = start + value.length - 1;
|
||||
found.add(FoundPattern.builder()
|
||||
.patternId(pattern.getId())
|
||||
.startPosition(start)
|
||||
.endPosition(end)
|
||||
.build());
|
||||
|
||||
startSearch = end + 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user