#!/bin/bash

## Copyright (C) 2022 - 2025 ENCRYPTED SUPPORT LLC <adrelanos@whonix.org>
## See the file COPYING for copying conditions.

## --binary-files=text - required to find backspace and null character
grep_args="\
   --files-with-matches \
   --line-number \
   --binary-files=text"

one="$(LC_ALL=C grep $grep_args --perl-regexp '[^\x00-\x7F]' "$@")"

two="$(LC_ALL=C grep $grep_args --perl-regexp "[^[:ascii:]]" "$@")"

## https://access.redhat.com/security/vulnerabilities/RHSB-2021-007
## https://lintian.debian.org/tags/unicode-trojan
##
## '--perl-regexp':
## Not using 'grep's '--perl-regexp' option. because not mentioned above and
## can lead to the following error message:
# grep: PCRE2 does not support \F, \L, \l, \N{name}, \U, or \u
three="$(LC_ALL=C grep $grep_args $'[\u061C\u200E\u200F\u202A\u202B\u202C\u202D\u202E\u2066\u2067\u2068\u2069]' "$@")"

## ASCII control characters.
four="$(LC_ALL=C grep $grep_args --perl-regexp '[\x00-\x08\x0B\x0C\x0E-\x1F\x7F]' "$@")"

result="\
$one
$two
$three
$four"

output_message="$(printf "%s\n" "$result" | sort --unique)"

if [ "$output_message" = "" ]; then
   exit 1
else
   printf "%s\n" "$output_message"
fi

exit 0
