Path: blob/master/tools/testing/selftests/bpf/bpf_arena_strsearch.h
38226 views
/* SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause) */1/* Copyright (c) 2025 Meta Platforms, Inc. and affiliates. */2#pragma once3#include "bpf_arena_common.h"45__noinline int bpf_arena_strlen(const char __arena *s __arg_arena)6{7const char __arena *sc;89for (sc = s; *sc != '\0'; ++sc)10cond_break;11return sc - s;12}1314/**15* glob_match - Shell-style pattern matching, like !fnmatch(pat, str, 0)16* @pat: Shell-style pattern to match, e.g. "*.[ch]".17* @str: String to match. The pattern must match the entire string.18*19* Perform shell-style glob matching, returning true (1) if the match20* succeeds, or false (0) if it fails. Equivalent to !fnmatch(@pat, @str, 0).21*22* Pattern metacharacters are ?, *, [ and \.23* (And, inside character classes, !, - and ].)24*25* This is small and simple implementation intended for device blacklists26* where a string is matched against a number of patterns. Thus, it27* does not preprocess the patterns. It is non-recursive, and run-time28* is at most quadratic: strlen(@str)*strlen(@pat).29*30* An example of the worst case is glob_match("*aaaaa", "aaaaaaaaaa");31* it takes 6 passes over the pattern before matching the string.32*33* Like !fnmatch(@pat, @str, 0) and unlike the shell, this does NOT34* treat / or leading . specially; it isn't actually used for pathnames.35*36* Note that according to glob(7) (and unlike bash), character classes37* are complemented by a leading !; this does not support the regex-style38* [^a-z] syntax.39*40* An opening bracket without a matching close is matched literally.41*/42__noinline bool glob_match(char const __arena *pat __arg_arena, char const __arena *str __arg_arena)43{44/*45* Backtrack to previous * on mismatch and retry starting one46* character later in the string. Because * matches all characters47* (no exception for /), it can be easily proved that there's48* never a need to backtrack multiple levels.49*/50char const __arena *back_pat = NULL, *back_str;5152/*53* Loop over each token (character or class) in pat, matching54* it against the remaining unmatched tail of str. Return false55* on mismatch, or true after matching the trailing nul bytes.56*/57for (;;) {58unsigned char c = *str++;59unsigned char d = *pat++;6061switch (d) {62case '?': /* Wildcard: anything but nul */63if (c == '\0')64return false;65break;66case '*': /* Any-length wildcard */67if (*pat == '\0') /* Optimize trailing * case */68return true;69back_pat = pat;70back_str = --str; /* Allow zero-length match */71break;72case '[': { /* Character class */73bool match = false, inverted = (*pat == '!');74char const __arena *class = pat + inverted;75unsigned char a = *class++;7677/*78* Iterate over each span in the character class.79* A span is either a single character a, or a80* range a-b. The first span may begin with ']'.81*/82do {83unsigned char b = a;8485if (a == '\0') /* Malformed */86goto literal;8788if (class[0] == '-' && class[1] != ']') {89b = class[1];9091if (b == '\0')92goto literal;9394class += 2;95/* Any special action if a > b? */96}97match |= (a <= c && c <= b);98cond_break;99} while ((a = *class++) != ']');100101if (match == inverted)102goto backtrack;103pat = class;104}105break;106case '\\':107d = *pat++;108__attribute__((__fallthrough__));109default: /* Literal character */110literal:111if (c == d) {112if (d == '\0')113return true;114break;115}116backtrack:117if (c == '\0' || !back_pat)118return false; /* No point continuing */119/* Try again from last *, one character later in str. */120pat = back_pat;121str = ++back_str;122break;123}124cond_break;125}126return false;127}128129130