Убраны пересекающиеся матчи

This commit is contained in:
serega6531
2020-03-09 23:00:40 +03:00
parent 38dd736542
commit 9317e6626c
2 changed files with 110 additions and 78 deletions

View File

@@ -0,0 +1,107 @@
package ru.serega6531.packmate.service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.crypto.codec.Hex;
import ru.serega6531.packmate.model.FoundPattern;
import ru.serega6531.packmate.model.Pattern;
import ru.serega6531.packmate.model.enums.PatternSearchType;
import ru.serega6531.packmate.utils.Bytes;
import java.util.*;
import java.util.regex.Matcher;
public class PatternMatcher {
private static final Map<String, java.util.regex.Pattern> compiledPatterns = new HashMap<>();
private final byte[] contentBytes;
private final String content;
private final List<Pattern> patterns;
private final Set<FoundPattern> result = new HashSet<>();
public PatternMatcher(byte[] contentBytes, List<Pattern> patterns) {
this.contentBytes = contentBytes;
this.content = new String(contentBytes);
this.patterns = patterns;
}
public Set<FoundPattern> findMatches() {
patterns.forEach(this::match);
return result;
}
private void match(Pattern pattern) {
if (pattern.getSearchType() == PatternSearchType.REGEX) {
final java.util.regex.Pattern regex = compilePattern(pattern);
final Matcher matcher = regex.matcher(content);
int startPos = 0;
while (matcher.find(startPos)) {
addIfPossible(FoundPattern.builder()
.patternId(pattern.getId())
.startPosition(matcher.start())
.endPosition(matcher.end() - 1)
.build());
startPos = matcher.end();
}
} else if (pattern.getSearchType() == PatternSearchType.SUBSTRING) {
int startSearch = 0;
final String value = pattern.getValue();
while (true) {
int start = StringUtils.indexOfIgnoreCase(content, value, startSearch);
if (start == -1) {
return;
}
int end = start + value.length() - 1;
addIfPossible(FoundPattern.builder()
.patternId(pattern.getId())
.startPosition(start)
.endPosition(end)
.build());
startSearch = end + 1;
}
} else { // SUBBYTES
int startSearch = 0;
final byte[] value = Hex.decode(pattern.getValue());
while (true) {
int start = Bytes.indexOf(contentBytes, value, startSearch, contentBytes.length);
if (start == -1) {
return;
}
int end = start + value.length - 1;
addIfPossible(FoundPattern.builder()
.patternId(pattern.getId())
.startPosition(start)
.endPosition(end)
.build());
startSearch = end + 1;
}
}
}
private void addIfPossible(FoundPattern found) {
if (result.stream().noneMatch(match -> between(match.getStartPosition(), match.getEndPosition(), found.getStartPosition()) ||
between(match.getStartPosition(), match.getEndPosition(), found.getEndPosition()))) {
result.add(found);
}
}
private boolean between(int a, int b, int x) {
return a <= x && x <= b;
}
private java.util.regex.Pattern compilePattern(Pattern pattern) {
return compiledPatterns.computeIfAbsent(pattern.getValue(), java.util.regex.Pattern::compile);
}
}

View File

@@ -1,24 +1,19 @@
package ru.serega6531.packmate.service; package ru.serega6531.packmate.service;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.security.crypto.codec.Hex;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import ru.serega6531.packmate.model.FoundPattern; import ru.serega6531.packmate.model.FoundPattern;
import ru.serega6531.packmate.model.Pattern; import ru.serega6531.packmate.model.Pattern;
import ru.serega6531.packmate.model.Stream; import ru.serega6531.packmate.model.Stream;
import ru.serega6531.packmate.model.enums.PatternDirectionType; 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.enums.SubscriptionMessageType;
import ru.serega6531.packmate.model.pojo.SubscriptionMessage; import ru.serega6531.packmate.model.pojo.SubscriptionMessage;
import ru.serega6531.packmate.repository.PatternRepository; import ru.serega6531.packmate.repository.PatternRepository;
import ru.serega6531.packmate.utils.Bytes;
import java.util.*; import java.util.*;
import java.util.regex.Matcher;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@Service @Service
@@ -30,7 +25,6 @@ public class PatternService {
private final StreamSubscriptionService subscriptionService; private final StreamSubscriptionService subscriptionService;
private final Map<Integer, Pattern> patterns = new HashMap<>(); private final Map<Integer, Pattern> patterns = new HashMap<>();
private final Map<String, java.util.regex.Pattern> compiledPatterns = new HashMap<>();
@Autowired @Autowired
public PatternService(PatternRepository repository, public PatternService(PatternRepository repository,
@@ -53,75 +47,11 @@ public class PatternService {
} }
public Set<FoundPattern> findMatches(byte[] bytes, boolean incoming) { public Set<FoundPattern> findMatches(byte[] bytes, boolean incoming) {
return patterns.values().stream() final List<Pattern> list = patterns.values().stream()
.filter(p -> p.getDirectionType() == (incoming ? PatternDirectionType.INPUT : PatternDirectionType.OUTPUT) .filter(p -> p.getDirectionType() == (incoming ? PatternDirectionType.INPUT : PatternDirectionType.OUTPUT)
|| p.getDirectionType() == PatternDirectionType.BOTH) || p.getDirectionType() == PatternDirectionType.BOTH)
.map(pattern -> match(pattern, bytes)) .collect(Collectors.toList());
.flatMap(List::stream) return new PatternMatcher(bytes, list).findMatches();
.collect(Collectors.toSet());
}
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);
int startPos = 0;
while (matcher.find(startPos)) {
found.add(FoundPattern.builder()
.patternId(pattern.getId())
.startPosition(matcher.start())
.endPosition(matcher.end() - 1)
.build());
startPos = matcher.end();
}
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);
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;
}
} else { // SUBBYTES
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;
}
}
} }
@Transactional @Transactional
@@ -138,7 +68,6 @@ public class PatternService {
pattern.getMatchedStreams().clear(); pattern.getMatchedStreams().clear();
patterns.remove(id); patterns.remove(id);
compiledPatterns.remove(pattern.getValue());
repository.delete(pattern); repository.delete(pattern);
subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.DELETE_PATTERN, id)); subscriptionService.broadcast(new SubscriptionMessage(SubscriptionMessageType.DELETE_PATTERN, id));
} }
@@ -152,8 +81,4 @@ public class PatternService {
return saved; return saved;
} }
private java.util.regex.Pattern compilePattern(Pattern pattern) {
return compiledPatterns.computeIfAbsent(pattern.getValue(), java.util.regex.Pattern::compile);
}
} }