From 5e83546a0b02acd88d205e4cd99b14a4fc4636e0 Mon Sep 17 00:00:00 2001 From: rsiddharth Date: Tue, 12 Jan 2021 19:22:07 -0500 Subject: lib: add HasConsecutiveMatchingBlocks --- lib/blocks.go | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/lib/blocks.go b/lib/blocks.go index a82feb2..76f39d6 100644 --- a/lib/blocks.go +++ b/lib/blocks.go @@ -72,6 +72,20 @@ func CipherUsesECB(bs []byte) []byte { return nil } +// Returns (index, found); where `index` is the starting index of the +// consecutive matching blocks; `index` is -1 when consective matching +// blocks are not found. +func HasConsecutiveMatchingBlocks(bs []byte, bsize int) (int, bool) { + blocks := BreakIntoBlocks(bs, bsize) + + for i := 0; i < len(blocks)-1; i++ { + if BlocksEqual(blocks[i], blocks[i+1]) { + return i * bsize, true + } + } + return -1, false +} + func hasMatchingBlock(id int, blocks [][]byte) bool { for i := 0; i < len(blocks); i++ { if i == id { -- cgit v1.2.3