Repository: mandiant/capa
File: capa/features/extractors/strings.py (Python)

Code before:
#
# Copyright (C) 2020 Mandiant, Inc. All Rights Reserved.
# Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
# You may obtain a copy of the License at: [package root]/LICENSE.txt
# Unless required by applicable law or agreed to in writing, software distributed under the License
#  is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and limitations under the License.

import re
import string
import contextlib
from dataclasses import dataclass
from collections.abc import Iterator

ASCII_BYTE = r" !\"#\$%&\'\(\)\*\+,-\./0123456789:;<=>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\]\^_`abcdefghijklmnopqrstuvwxyz\{\|\}\\\~\t".encode(
    "ascii"
)
ASCII_RE_4 = re.compile(b"([%s]{%d,})" % (ASCII_BYTE, 4))
UNICODE_RE_4 = re.compile(b"((?:[%s]\x00){%d,})" % (ASCII_BYTE, 4))
REPEATS = {ord("A"), 0x00, 0xFE, 0xFF}
SLICE_SIZE = 4096
PRINTABLE_CHAR_SET = set(string.printable)


@dataclass
class String:
    s: str
    offset: int


def buf_filled_with(buf: bytes, character: int) -> bool:
    """Check if the given buffer is filled with the given character, repeatedly.

    Args:
        buf: The bytes buffer to check
        character: The byte value (0-255) to check for

    Returns:
        bool: True if all bytes in the buffer match the character, False otherwise.
        The empty buffer contains no bytes, therefore always returns False.
    """
    if not buf:
        return False

    if len(buf) < SLICE_SIZE:
        return all(b == character for b in buf)

    # single big allocation, re-used each loop
    dupe_chunk = bytes(character) * SLICE_SIZE


Review comment (suggestion):
imo `4` here is not very descriptive, I'd suggest declaring it a constant:
```py
MIN_STRING_LENGTH = 4 

ASCII_RE_4 = re.compile(b"([%s]{%d,})" % (ASCII_BYTE, MIN_STRING_LENGTH))
UNICODE_RE_4 = re.compile(b"((?:[%s]\x00){%d,})" % (ASCII_BYTE, MIN_STRING_LENGTH))
```


Code after:
#
# Copyright 2020 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import re
import string
import contextlib
from dataclasses import dataclass
from collections.abc import Iterator

ASCII_BYTE = r" !\"#\$%&\'\(\)\*\+,-\./0123456789:;<=>\?@ABCDEFGHIJKLMNOPQRSTUVWXYZ\[\]\^_`abcdefghijklmnopqrstuvwxyz\{\|\}\\\~\t".encode(
    "ascii"
)
ASCII_RE_4 = re.compile(b"([%s]{%d,})" % (ASCII_BYTE, 4))
UNICODE_RE_4 = re.compile(b"((?:[%s]\x00){%d,})" % (ASCII_BYTE, 4))
REPEATS = {ord("A"), 0x00, 0xFE, 0xFF}
SLICE_SIZE = 4096
PRINTABLE_CHAR_SET = set(string.printable)


@dataclass
class String:
    s: str
    offset: int


def buf_filled_with(buf: bytes, character: int) -> bool:
    """Check if the given buffer is filled with the given character, repeatedly.

    Args:
        buf: The bytes buffer to check
        character: The byte value (0-255) to check for

    Returns:
        True if all bytes in the buffer match the character, False otherwise.
        The empty buffer contains no bytes, therefore always returns False.
    """
    if not buf:
        return False