1#!/bin/bash
2# SPDX-License-Identifier: BSD-2-Clause
3
4SHOW_DETAILS=1
5ERRORS=""
6basepath="$(dirname $(dirname $(readlink -f "$0")))"
7
8details () [[ ${SHOW_DETAILS} -ne 0 ]]
9
10verify_enum () {
11  PREFIX=$1
12  SYMBOL_LIST="$(grep PKCS11\_$PREFIX\_ ${basepath}/include/pkcs11_ta.h | cut -f2 | cut '-d ' -f1)"
13  details && echo -e "\e[4m${PREFIX}\e[0m"
14  details && echo ""
15  details && echo -e "symbol\tuses\tstatus"
16  for symbol in $SYMBOL_LIST ; do
17    details && echo -n "$symbol"
18    COUNT="$(grep ${symbol} ${basepath}/src/* | wc -l)"
19    details && echo -n -e "\t${COUNT}\t"
20    PRESENT="$(grep PKCS11_ID.*\($symbol.*\) ${basepath}/src/pkcs11_helpers.c | wc -l)"
21    if [ ${PRESENT} -ne 0 ] ; then
22      details && echo -e "\e[32mOK\e[0m"
23    elif [ $COUNT -eq 0 ] ; then
24      details && echo -e "\e[33mmissing but unused\e[0m"
25    else
26      details && echo -e "\e[31mMISSING!\e[0m"
27      ERRORS+=" ${symbol}"
28    fi
29  done
30  details && echo ""
31}
32
33verify_define () {
34  PREFIX=$1
35  SYMBOL_LIST="$(grep PKCS11\_$PREFIX\_ ${basepath}/include/pkcs11_ta.h | grep ^#define | cut '-d ' -f2 | cut -f1)"
36  details && echo -e "\e[4m${PREFIX}\e[0m"
37  details && echo ""
38  details && echo -e "symbol\tuses\tstatus"
39  for symbol in $SYMBOL_LIST ; do
40    details && echo -n "$symbol"
41    COUNT="$(grep ${symbol} ${basepath}/src/* | wc -l)"
42    details && echo -n -e "\t${COUNT}\t"
43    PRESENT="$(grep PKCS11_ID.*\($symbol.*\) ${basepath}/src/pkcs11_helpers.c | wc -l)"
44    if [ ${PRESENT} -ne 0 ] ; then
45      details && echo -e "\e[32mOK\e[0m"
46    elif [ $COUNT -eq 0 ] ; then
47      details && echo -e "\e[33mmissing but unused\e[0m"
48    else
49      details && echo -e "\e[31mMISSING!\e[0m"
50      ERRORS+=" ${symbol}"
51    fi
52  done
53  details && echo ""
54}
55
56usage() {
57  SCR=$(basename "$0")
58  echo "Usage: $SCR		Verify that helpers are up to date"
59  echo "       $SCR --quiet	Only print errors"
60  echo "       $SCR --help		This help"
61  echo ""
62  echo "Verification checks that all PKCS11_* enums or defines from \
63include/pkcs11_ta.h are either present in src/pkcs11_helpers.c or not used at \
64all."
65  exit 1
66}
67
68while [[ $# -gt 0 ]]; do
69  arg="$1"
70  shift
71
72  case $arg in
73    -q|--quiet)
74      SHOW_DETAILS=0
75      ;;
76    -h|--help)
77      usage
78      ;;
79    *)
80      echo "error: invalid argument: ${arg}" 1>&2
81      exit 1
82  esac
83done
84
85# check that symbols exists
86verify_enum CKA
87verify_define CKFS
88verify_define CKFT
89verify_define CKFSS
90verify_enum CKS
91verify_enum CKR
92verify_enum CKO
93verify_enum CKK
94
95if [ -n "${ERRORS}" ] ; then
96  SCR=$(basename "$0")
97  for symbol in $ERRORS ; do
98    echo "${SCR}: error: missing symbol ${symbol} in ${basepath}/src/pkcs11_helpers.c" 1>&2
99  done
100  exit 1
101fi
102
103exit 0
104