File: fuzzing.hpp

package info (click to toggle)
rapidfuzz-cpp 3.3.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,480 kB
  • sloc: cpp: 30,893; python: 63; makefile: 26; sh: 8
file content (55 lines) | stat: -rw-r--r-- 1,320 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#pragma once
#include <iostream>
#include <rapidfuzz/distance/Levenshtein.hpp>
#include <vector>

static inline bool extract_strings(const uint8_t* data, size_t size, std::vector<uint8_t>& s1,
                                   std::vector<uint8_t>& s2)
{
    if (size <= sizeof(uint32_t)) {
        return false;
    }
    uint32_t len1 = *(uint32_t*)data;

    if (len1 > size - sizeof(len1)) {
        return false;
    }

    data += sizeof(len1);
    size -= sizeof(len1);
    s1 = std::vector<uint8_t>(data, data + len1);
    s2 = std::vector<uint8_t>(data + len1, data + size);
    return true;
}

template <typename T>
static inline T pow(T x, unsigned int p)
{
    if (p == 0) return 1;
    if (p == 1) return x;

    T tmp = pow(x, p / 2);
    if (p % 2 == 0)
        return tmp * tmp;
    else
        return x * tmp * tmp;
}

template <typename T>
std::vector<T> vec_multiply(const std::vector<T>& a, size_t b)
{
    std::vector<T> output;
    while (b--)
        output.insert(output.end(), a.begin(), a.end());

    return output;
}

template <typename T>
void print_seq(const std::string& name, const std::vector<T>& seq)
{
    std::cout << name << " len: " << seq.size() << " content: ";
    for (const auto& ch : seq)
        std::cout << static_cast<uint64_t>(ch) << " ";
    std::cout << std::endl;
}