@@ -33,6 +33,7 @@ const (
3333// otherwise we let the readers swarm.
3434type Scanner struct {
3535 db * detection.SignatureDatabase
36+ sigMap map [string ]int
3637 matchThreshold float64
3738 entropyTolerance float64
3839 mu sync.RWMutex
@@ -44,6 +45,7 @@ type Scanner struct {
4445func NewScanner () * Scanner {
4546 return & Scanner {
4647 db : & detection.SignatureDatabase {},
48+ sigMap : make (map [string ]int ),
4749 matchThreshold : 0.75 , // 75% minimum confidence keeps the false positives manageable
4850 entropyTolerance : 0.5 ,
4951 }
@@ -94,6 +96,13 @@ func (s *Scanner) LoadDatabase(path string) error {
9496 }
9597
9698 s .db = & db
99+
100+ // Rebuild the map index.
101+ s .sigMap = make (map [string ]int , len (s .db .Signatures ))
102+ for i , sig := range s .db .Signatures {
103+ s .sigMap [sig .ID ] = i
104+ }
105+
97106 return nil
98107}
99108
@@ -213,6 +222,13 @@ func (s *Scanner) AddSignature(sig *detection.Signature) error {
213222
214223 // Append copies the struct value.
215224 s .db .Signatures = append (s .db .Signatures , * sig )
225+
226+ // Update the map index.
227+ if s .sigMap == nil {
228+ s .sigMap = make (map [string ]int )
229+ }
230+ s .sigMap [sig .ID ] = len (s .db .Signatures ) - 1
231+
216232 return nil
217233}
218234
@@ -255,11 +271,14 @@ func (s *Scanner) GetSignature(id string) (*detection.Signature, error) {
255271 if s .db == nil {
256272 return nil , fmt .Errorf ("database not loaded" )
257273 }
258- for i := range s .db .Signatures {
259- if s .db .Signatures [i ].ID == id {
260- return s .deepCopySignature (& s .db .Signatures [i ]), nil
274+
275+ if idx , ok := s .sigMap [id ]; ok {
276+ // Bounds check just in case of state drift, though locks should prevent it.
277+ if idx >= 0 && idx < len (s .db .Signatures ) {
278+ return s .deepCopySignature (& s .db .Signatures [idx ]), nil
261279 }
262280 }
281+
263282 return nil , fmt .Errorf ("signature %q not found" , id )
264283}
265284
0 commit comments