From f4b17f68913be2bac29e3962f59a5b53887d9ad2 Mon Sep 17 00:00:00 2001 From: Richard Hansen Date: Wed, 15 Jun 2016 00:48:03 -0400 Subject: [PATCH] eliminate redundant code get_cert_sigval() and get_roa_sigval() are nearly identical, so combine them. Similarly, set_cert_sigval() and set_roa_sigval() are nearly identical, so combine them. --- lib/rpki/sqhl.c | 144 +++++++++++++----------------------------------- 1 file changed, 38 insertions(+), 106 deletions(-) diff --git a/lib/rpki/sqhl.c b/lib/rpki/sqhl.c index 311b31ac..ce80a810 100644 --- a/lib/rpki/sqhl.c +++ b/lib/rpki/sqhl.c @@ -541,11 +541,13 @@ add_crl_internal( * One of the SIGVAL_ constants indicating what happened. */ static sigval_state -get_cert_sigval( +get_sigval( scmcon *conp, + object_type typ, const char *ski, const char *subj) { + scmtab *table; unsigned int val; scmsrch search_cols[] = { { @@ -557,45 +559,21 @@ get_cert_sigval( }, }; char where[WHERESTR_SIZE]; - xsnprintf(where, sizeof(where), - "ski=\"%s\" and subject=\"%s\"", ski, subj); - scmsrcha sigsrch = { - .vec = search_cols, - .ntot = ELTS(search_cols), - .nused = ELTS(search_cols), - .wherestr = where, - }; - err_code sta = 0; - - if (theSCMP != NULL) - initTables(theSCMP); - sta = searchscm(conp, theCertTable, &sigsrch, NULL, &ok, - SCM_SRCH_DOVALUE_ALWAYS, NULL); - if (sta < 0) - return SIGVAL_UNKNOWN; - sigval_state sval = val; - if (sval < SIGVAL_UNKNOWN || sval > SIGVAL_INVALID) + if (OT_CER == typ) + { + xsnprintf(where, sizeof(where), + "ski=\"%s\" and subject=\"%s\"", ski, subj); + table = theCertTable; + } + else if (OT_ROA == typ) + { + xsnprintf(where, sizeof(where), "ski=\"%s\"", ski); + table = theROATable; + } + else + { return SIGVAL_UNKNOWN; - return sval; -} - -static sigval_state -get_roa_sigval( - scmcon *conp, - const char *ski) -{ - unsigned int val; - scmsrch search_cols[] = { - { - .colno = 1, - .sqltype = SQL_C_ULONG, - .colname = "sigval", - .valptr = &val, - .valsize = sizeof(val), - }, - }; - char where[WHERESTR_SIZE]; - xsnprintf(where, sizeof(where), "ski=\"%s\"", ski); + } scmsrcha sigsrch = { .vec = search_cols, .ntot = ELTS(search_cols), @@ -606,7 +584,7 @@ get_roa_sigval( if (theSCMP != NULL) initTables(theSCMP); - sta = searchscm(conp, theROATable, &sigsrch, NULL, &ok, + sta = searchscm(conp, table, &sigsrch, NULL, &ok, SCM_SRCH_DOVALUE_ALWAYS, NULL); if (sta < 0) return SIGVAL_UNKNOWN; @@ -616,34 +594,15 @@ get_roa_sigval( return sval; } -static sigval_state -get_sigval( - scmcon *conp, - object_type typ, - const char *ski, - const char *subj) -{ - switch (typ) - { - case OT_CER: - return get_cert_sigval(conp, ski, subj); - case OT_ROA: - return get_roa_sigval(conp, ski); - // other cases not handled yet - default: - break; - } - return SIGVAL_UNKNOWN; -} - /** * @brief * This function attempts to set the sigval parameter in a table * based on the type. */ static err_code -set_cert_sigval( +set_sigval( scmcon *conp, + object_type typ, const char *ski, const char *subj, sigval_state valu) @@ -651,63 +610,36 @@ set_cert_sigval( /** @bug magic number */ char stmt[520]; err_code sta; + scmtab *table; + char where[sizeof(stmt)]; if (theSCMP != NULL) initTables(theSCMP); - if (theCertTable == NULL) - return ERR_SCM_NOSUCHTAB; - char escaped_subj[2 * strlen(subj) + 1]; - mysql_escape_string(escaped_subj, subj, strlen(subj)); - xsnprintf(stmt, sizeof(stmt), - "update %s set sigval=%d where ski=\"%s\" and subject=\"%s\";", - theCertTable->tabname, valu, ski, escaped_subj); - sta = statementscm_no_data(conp, stmt); - return sta; -} - -static err_code -set_roa_sigval( - scmcon *conp, - const char *ski, - sigval_state valu) -{ - /** @bug magic number */ - char stmt[520]; - err_code sta; - - if (theSCMP != NULL) - initTables(theSCMP); - if (theROATable == NULL) - return ERR_SCM_NOSUCHTAB; - xsnprintf(stmt, sizeof(stmt), - "update %s set sigval=%d where ski=\"%s\";", - theROATable->tabname, valu, ski); - sta = statementscm_no_data(conp, stmt); - return sta; -} - -static err_code -set_sigval( - scmcon *conp, - object_type typ, - const char *ski, - const char *subject, - sigval_state valu) -{ - err_code sta = ERR_SCM_UNSPECIFIED; - switch (typ) { case OT_CER: - sta = set_cert_sigval(conp, ski, subject, valu); + { + table = theCertTable; + size_t subj_len = strlen(subj); + char escaped_subj[2 * subj_len + 1]; + mysql_escape_string(escaped_subj, subj, subj_len); + xsnprintf(where, sizeof(where), "ski=\"%s\" and subject=\"%s\"", + ski, escaped_subj); break; + } case OT_ROA: - sta = set_roa_sigval(conp, ski, valu); + table = theROATable; + xsnprintf(where, sizeof(where), "ski=\"%s\"", ski); break; default: // other cases not handled yet - break; + return ERR_SCM_UNSPECIFIED; } + if (table == NULL) + return ERR_SCM_NOSUCHTAB; + xsnprintf(stmt, sizeof(stmt), "update %s set sigval=%d where %s;", + table->tabname, valu, where); + sta = statementscm_no_data(conp, stmt); return sta; }