/** * @brief Return a string representation of the RPM tag type. * * @param header A librpm header. * @param tag A librpm rpmTag_t name. * @param td A librpm rpmtd. * * Given a librpm iterator header and a requested tag name: * 1. Determine the type of the tag (the class of value). * 2. Request a const pointer or cast of numerate to that class. * 3. Lexical-cast the value for SQL. * * @return The string representation of the tag type. */ static std::string getRpmAttribute(const Header& header, rpmTag tag, const rpmtd& td) { std::string result; if (headerGet(header, tag, td, HEADERGET_DEFAULT) == 0) { // Intentional check for a 0 = failure. TLOG << "Could not get RPM header flag."; return result; } if (rpmTagGetClass(tag) == RPM_NUMERIC_CLASS) { long long int attr = rpmtdGetNumber(td); result = BIGINT(attr); } else if (rpmTagGetClass(tag) == RPM_STRING_CLASS) { const char* attr = rpmtdGetString(td); if (attr != nullptr) { result = TEXT(attr); } } return result; }
static int validData(rpmTagVal tag, rpmTagType type, rpmTagReturnType retype, PyObject *value) { rpmTagClass tclass = rpmTagGetClass(tag); int valid = 1; if (retype == RPM_SCALAR_RETURN_TYPE) { valid = validItem(tclass, value); } else if (retype == RPM_ARRAY_RETURN_TYPE && PyList_Check(value)) { /* python lists can contain arbitrary objects, validate each item */ Py_ssize_t len = PyList_Size(value); for (Py_ssize_t i = 0; i < len; i++) { PyObject *item = PyList_GetItem(value, i); if (!validItem(tclass, item)) { valid = 0; break; } } } else { valid = 0; } return valid; }
rpmRC checkForEncoding(Header h, int addtag) { rpmRC rc = RPMRC_OK; #if HAVE_ICONV const char *encoding = "utf-8"; rpmTagVal tag; iconv_t ic = (iconv_t) -1; char *dest = NULL; size_t destlen = 0; int strict = rpmExpandNumeric("%{_invalid_encoding_terminates_build}"); HeaderIterator hi = headerInitIterator(h); ic = iconv_open(encoding, encoding); if (ic == (iconv_t) -1) { rpmlog(RPMLOG_WARNING, _("encoding %s not supported by system\n"), encoding); goto exit; } while ((tag = headerNextTag(hi)) != RPMTAG_NOT_FOUND) { struct rpmtd_s td; const char *src = NULL; if (rpmTagGetClass(tag) != RPM_STRING_CLASS) continue; headerGet(h, tag, &td, (HEADERGET_RAW|HEADERGET_MINMEM)); while ((src = rpmtdNextString(&td)) != NULL) { size_t srclen = strlen(src); size_t outlen, inlen = srclen; char *out, *in = (char *) src; if (destlen < srclen) { destlen = srclen * 2; dest = xrealloc(dest, destlen); } out = dest; outlen = destlen; /* reset conversion state */ iconv(ic, NULL, &inlen, &out, &outlen); if (iconv(ic, &in, &inlen, &out, &outlen) == (size_t) -1) { rpmlog(strict ? RPMLOG_ERR : RPMLOG_WARNING, _("Package %s: invalid %s encoding in %s: %s - %s\n"), headerGetString(h, RPMTAG_NAME), encoding, rpmTagGetName(tag), src, strerror(errno)); rc = RPMRC_FAIL; } } rpmtdFreeData(&td); } /* Stomp "known good utf" mark in header if requested */ if (rc == RPMRC_OK && addtag) headerPutString(h, RPMTAG_ENCODING, encoding); if (!strict) rc = RPMRC_OK; exit: if (ic != (iconv_t) -1) iconv_close(ic); headerFreeIterator(hi); free(dest); #endif /* HAVE_ICONV */ return rc; }