<commit_before>#ifndef LIB_RPKI_SQHL_H
#define LIB_RPKI_SQHL_H

#include <stdio.h>
#include "db_constants.h"
#include "scm.h"
#include "scmf.h"

#include "rpki-object/certificate.h"

/**
 * @brief
 *     Object types
 */

#define OT_UNKNOWN      0
/** @brief DER encoded certificate */
#define OT_CER          1
/** @brief DER encoded CRL */
#define OT_CRL          2
/** @brief DER encoded ROA */
#define OT_ROA          3
/** @brief manifests are only DER for now */
#define OT_MAN          4
#define OT_GBR          5
/** @brief highest-valued DER type */
#define OT_MAXBASIC     5

/**
 * @brief
 *     difference between DER and PEM equivalent types
 *
 * The types that are less than ::OT_PEM_OFFSET are DER types and
 * the types that are greater than or equal to ::OT_PEM_OFFSET are
 * PEM types.  The PEM types are exactly ::OT_PEM_OFFSET greater
 * than their corresponding DER types (e.g., `OT_CRL_PEM == OT_CRL
 * + OT_PEM_OFFSET`).
 */
#define OT_PEM_OFFSET   128

/** @brief PEM encoded certificate */
#define OT_CER_PEM      (OT_CER+OT_PEM_OFFSET)
/** @brief PEM encoded CRL */
#define OT_CRL_PEM      (OT_CRL+OT_PEM_OFFSET)
/** @brief PEM encoded ROA */
#define OT_ROA_PEM      (OT_ROA+OT_PEM_OFFSET)
/** @brief PEM encoded manifest */
#define OT_MAN_PEM      (OT_MAN+OT_PEM_OFFSET)

/*
 * Certificate types
 */

#define CA_CERT       1
#define EE_CERT       2
#define TA_CERT       3
#define UN_CERT       4


/*
 * certain fields need to have "rsync URIs". The only test we perform for now
 * is to verify that the field starts with this text
 */
#define RSYNC_PREFIX "rsync://"
#define RSYNC_PREFIX_LEN (sizeof(RSYNC_PREFIX) - 1)

/*
 * Data types
 */

typedef int (
    *crlfunc)(
    scm *scmp,
    scmcon *conp,
    char *issuer,
    char *aki,
    uint8_t *sn);

typedef struct _crlinfo {
    scm *scmp;
    scmcon *conp;
    scmtab *tabp;
    crlfunc cfunc;
} crlinfo;

struct goodoid {
    int lth;
    unsigned char *oid;
};

/*
 * Prototypes
 */

/*
 * Find a directory in the directory table, or create it if it is not found.
 * Return the id in idp. The function returns 0 on success and a negative
 * error code on failure.
 *
 * It is assumed that the existence of the putative directory has already been
 * verified.
 */
extern int findorcreatedir(
    scm *scmp,
    scmcon *conp,
    char *dirname,
    unsigned int *idp);

/*
 * Add the indicated object to the DB. If "trusted" is set then verify that
 * the object is self-signed. Note that this add operation may result in the
 * directory also being added.
 *
 * Symlinks and files that are not regular files are not processed.
 *
 * This function returns 0 on success and a negative error code on failure.
 */
extern int add_object(
    scm *scmp,
    scmcon *conp,
    char *outfile,
    char *outdir,
    char *outfull,
    int utrust);

/**
 * @brief
 *     Delete an object.
 *
 * First find the object's directory.  If it is not found then we are
 * done.  If it is found, then find the corresponding (filename,
 * dir_id) combination in the appropriate table and issue the delete
 * SQL call.
 */
extern int delete_object(
    scm *scmp,
    scmcon *conp,
    char *outfile,
    char *outdir,
    char *outfull,
    unsigned int dir_id);

/**
 * @brief
 *     Infer the object type based on which file extensions are present.
 *
 * @param[in] fname
 *     File pathname.  This MUST NOT be NULL.
 *
 * @return
 *     An object type code depending on the filename suffix:
 *       - `.cer`: ::OT_CER
 *       - `.crl`: ::OT_CRL
 *       - `.roa`: ::OT_ROA
 *       - `.man`, `.mft`, `.mnf`: ::OT_MAN
 *       - `.gbr`: ::OT_GBR
 *       - `.cer.pem`: ::OT_CER_PEM
 *       - `.crl.pem`: ::OT_CRL_PEM
 *       - `.roa.pem`: ::OT_ROA_PEM
 *       - `.man.pem`, `.mft.pem`, `.mnf.pem`: ::OT_MAN_PEM
 *       - all others: ::OT_UNKNOWN.
 */<commit_msg>use an enum for object types<commit_after>#ifndef LIB_RPKI_SQHL_H
#define LIB_RPKI_SQHL_H

#include <stdio.h>
#include "db_constants.h"
#include "scm.h"
#include "scmf.h"

#include "rpki-object/certificate.h"

/**
 * @brief
 *     Object types
 */
typedef enum {
    OT_UNKNOWN = 0,

    /** @brief DER encoded certificate */
    OT_CER,
    /** @brief DER encoded CRL */
    OT_CRL,
    /** @brief DER encoded ROA */
    OT_ROA,
    /** @brief manifests are only DER for now */
    OT_MAN,
    OT_GBR,

    OT_MAXBASIC_PLUS_ONE,
    /** @brief highest-valued DER type */
    OT_MAXBASIC = OT_MAXBASIC_PLUS_ONE - 1,
    /**
     * @brief
     *     difference between DER and PEM equivalent types
     *
     * The types that are less than ::OT_PEM_OFFSET are DER types and
     * the types that are greater than or equal to ::OT_PEM_OFFSET are
     * PEM types.  The PEM types are exactly ::OT_PEM_OFFSET greater
     * than their corresponding DER types (e.g., `OT_CRL_PEM == OT_CRL
     * + OT_PEM_OFFSET`).
     */
    OT_PEM_OFFSET,

    /** @brief PEM encoded certificate */
    OT_CER_PEM,
    /** @brief PEM encoded CRL */
    OT_CRL_PEM,
    /** @brief PEM encoded ROA */
    OT_ROA_PEM,
    /** @brief PEM encoded manifest */
    OT_MAN_PEM,

} object_type;

/*
 * Certificate types
 */

#define CA_CERT       1
#define EE_CERT       2
#define TA_CERT       3
#define UN_CERT       4


/*
 * certain fields need to have "rsync URIs". The only test we perform for now
 * is to verify that the field starts with this text
 */
#define RSYNC_PREFIX "rsync://"
#define RSYNC_PREFIX_LEN (sizeof(RSYNC_PREFIX) - 1)

/*
 * Data types
 */

typedef int (
    *crlfunc)(
    scm *scmp,
    scmcon *conp,
    char *issuer,
    char *aki,
    uint8_t *sn);

typedef struct _crlinfo {
    scm *scmp;
    scmcon *conp;
    scmtab *tabp;
    crlfunc cfunc;
} crlinfo;

struct goodoid {
    int lth;
    unsigned char *oid;
};

/*
 * Prototypes
 */

/*
 * Find a directory in the directory table, or create it if it is not found.
 * Return the id in idp. The function returns 0 on success and a negative
 * error code on failure.
 *
 * It is assumed that the existence of the putative directory has already been
 * verified.
 */
extern int findorcreatedir(
    scm *scmp,
    scmcon *conp,
    char *dirname,
    unsigned int *idp);

/*
 * Add the indicated object to the DB. If "trusted" is set then verify that
 * the object is self-signed. Note that this add operation may result in the
 * directory also being added.
 *
 * Symlinks and files that are not regular files are not processed.
 *
 * This function returns 0 on success and a negative error code on failure.
 */
extern int add_object(
    scm *scmp,
    scmcon *conp,
    char *outfile,
    char *outdir,
    char *outfull,
    int utrust);

/**
 * @brief
 *     Delete an object.
 *
 * First find the object's directory.  If it is not found then we are
 * done.  If it is found, then find the corresponding (filename,
 * dir_id) combination in the appropriate table and issue the delete
 * SQL call.
 */
extern int delete_object(
    scm *scmp,
    scmcon *conp,
    char *outfile,
    char *outdir,
    char *outfull,
    unsigned int dir_id);

/**
 * @brief
 *     Infer the object type based on which file extensions are present.
 *
 * @param[in] fname
 *     File pathname.  This MUST NOT be NULL.
 *
 * @return
 *     A value from ::object_type depending on the filename suffix:
 *       - `.cer`: ::OT_CER
 *       - `.crl`: ::OT_CRL
 *       - `.roa`: ::OT_ROA
 *       - `.man`, `.mft`, `.mnf`: ::OT_MAN
 *       - `.gbr`: ::OT_GBR
 *       - `.cer.pem`: ::OT_CER_PEM
 *       - `.crl.pem`: ::OT_CRL_PEM
 *       - `.roa.pem`: ::OT_ROA_PEM
 *       - `.man.pem`, `.mft.pem`, `.mnf.pem`: ::OT_MAN_PEM
 *       - all others: ::OT_UNKNOWN.
 */
{'fraction_non_alphanumeric': 0.11234632487575202, 'fraction_numerical': 0.0036620455139942452, 'mean_word_length': 2.5567441860465117, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3342312', 'n_tokens_mistral': 2814, 'n_tokens_neox': 2712, 'n_words': 908}