00001 //--------------------------------------------------------------------------
00002 // Copyright (C) 2014-2017 Cisco and/or its affiliates. All rights reserved.
00003 //
00004 // This program is free software; you can redistribute it and/or modify it
00005 // under the terms of the GNU General Public License Version 2 as published
00006 // by the Free Software Foundation. You may not use, modify or distribute
00007 // this program under any other version of the GNU General Public License.
00008 //
00009 // This program is distributed in the hope that it will be useful, but
00010 // WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License along
00015 // with this program; if not, write to the Free Software Foundation, Inc.,
00016 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00017 //--------------------------------------------------------------------------
00018 // magic.h author Russ Combs <rucombs@cisco.com>
00019
00020 #ifndef MAGIC_H
00021 #define MAGIC_H
00022
00023 #include <string>
00024 #include <vector>
00025
00026 class MagicBook;
00027
00028 struct MagicPage
00029 {
00030 std::string key;
00031 std::string value;
00032
00033 MagicPage* next[256];
00034 MagicPage* any;
00035
00036 const MagicBook& book;
00037
00038 MagicPage(const MagicBook&);
00039 ~MagicPage();
00040 };
00041
00042 typedef std::vector<uint16_t> HexVector;
00043
00044 // MagicBook is a set of MagicPages implementing a trie
00045
00046 class MagicBook
00047 {
00048 public:
00049 virtual ~MagicBook();
00050
00051 virtual bool add_spell(const char* key, const char* val) = 0;
00052 virtual const char* find_spell(const uint8_t*, unsigned len, const MagicPage*&) const = 0;
00053
00054 const MagicPage* page1()
00055 { return root; }
00056
00057 protected:
00058 MagicBook();
00059 MagicPage* root;
00060 };
00061
00062 //-------------------------------------------------------------------------
00063 // spells - a sequence of case insensitive text strings with wild cards
00064 // designated by * (indicating any number of arbitrary bytes)
00065 //-------------------------------------------------------------------------
00066
00067 class SpellBook : public MagicBook
00068 {
00069 public:
00070 SpellBook();
00071
00072 bool add_spell(const char*, const char*) override;
00073 const char* find_spell(const uint8_t*, unsigned len, const MagicPage*&) const override;
00074
00075 private:
00076 bool translate(const char*, HexVector&);
00077 void add_spell(const char*, const char*, HexVector&, unsigned, MagicPage*);
00078 const MagicPage* find_spell(const uint8_t*, unsigned, const MagicPage*, unsigned) const;
00079 };
00080
00081 //-------------------------------------------------------------------------
00082 // hexes - a sequence of pipe delimited hex, text literals, and wild chars
00083 // designated by '?' (indicating one arbitrary byte)
00084 //-------------------------------------------------------------------------
00085
00086 class HexBook : public MagicBook
00087 {
00088 public:
00089 HexBook() = default;
00090
00091 bool add_spell(const char*, const char*) override;
00092 const char* find_spell(const uint8_t*, unsigned len, const MagicPage*&) const override;
00093
00094 private:
00095 bool translate(const char*, HexVector&);
00096 void add_spell(const char*, const char*, HexVector&, unsigned, MagicPage*);
00097 const MagicPage* find_spell(const uint8_t*, unsigned, const MagicPage*, unsigned) const;
00098 };
00099
00100 #endif
00101
END OF CODE