+ int result = 3;
+ int j;
+ /*
+ * ncurses' quickdump writes only RFC 4648 "url/filename-safe" encoding,
+ * but accepts RFC-3548
+ */
+ for (j = 0; j < 4; ++j) {
+ int ch = UChar(**source);
+ *source += 1;
+ if (ch >= 'A' && ch <= 'Z') {
+ target[j] = (ch - 'A');
+ } else if (ch >= 'a' && ch <= 'z') {
+ target[j] = 26 + (ch - 'a');
+ } else if (ch >= '0' && ch <= '9') {
+ target[j] = 52 + (ch - '0');
+ } else if (ch == '-' || ch == '+') {
+ target[j] = 62;
+ } else if (ch == '_' || ch == '/') {
+ target[j] = 63;
+ } else if (ch == '=') {
+ target[j] = 64;
+ result--;
+ } else {
+ result = -1;
+ break;
+ }
+ }
+ return result;
+}
+
+static int
+decode_hex(const char **source)
+{
+ int result = 0;
+ int nibble;
+ int ch;
+
+ for (nibble = 0; nibble < 2; ++nibble) {
+ result <<= 4;
+ ch = UChar(**source);
+ *source += 1;
+ if (ch >= '0' && ch <= '9') {
+ ch -= '0';
+ } else if (ch >= 'A' && ch <= 'F') {
+ ch -= 'A';
+ ch += 10;
+ } else if (ch >= 'a' && ch <= 'f') {
+ ch -= 'a';
+ ch += 10;
+ } else {
+ result = -1;
+ break;
+ }
+ result |= ch;
+ }
+ return result;
+}
+
+static int
+decode_quickdump(char *target, const char *source)
+{
+ char *base = target;
+ int result = 0;
+
+ if (!strncmp(source, "b64:", 4)) {
+ source += 4;
+ while (*source != '\0') {
+ int bits[4];
+ int ch = lookup_b64(bits, &source);
+ if (ch < 0 || (ch + target - base) >= MAX_ENTRY_SIZE) {
+ result = 0;
+ break;
+ }
+ result += ch;
+ *target++ = (char) ((bits[0] << 2) | (bits[1] >> 4));
+ if (bits[2] < 64) {
+ *target++ = (char) ((bits[1] << 4) | (bits[2] >> 2));
+ if (bits[3] < 64) {
+ *target++ = (char) ((bits[2] << 6) | bits[3]);
+ }
+ }
+ }
+ } else if (!strncmp(source, "hex:", 4)) {
+ source += 4;
+ while (*source != '\0') {
+ int ch = decode_hex(&source);
+ if (ch < 0 || (target - base) >= MAX_ENTRY_SIZE) {
+ result = 0;
+ break;
+ }
+ *target++ = (char) ch;
+ ++result;
+ }
+ }
+ return result;
+}
+
+/*
+ * Build a terminfo pathname and try to read the data. Returns TGETENT_YES on
+ * success, TGETENT_NO on failure.
+ */
+static int
+_nc_read_tic_entry(char *filename,
+ unsigned limit,
+ const char *const path,
+ const char *name,
+ TERMTYPE *const tp)
+{
+ int code = TGETENT_NO;
+#if USE_HASHED_DB
+ DB *capdbp;
+#endif
+ char buffer[MAX_ENTRY_SIZE + 1];
+ int used;
+
+ TR(TRACE_DATABASE,
+ (T_CALLED("_nc_read_tic_entry(file=%p, path=%s, name=%s)"),
+ filename, path, name));
+
+ if ((used = decode_quickdump(buffer, path)) != 0
+ && (code = _nc_read_termtype(tp, buffer, used)) == TGETENT_YES
+ && _nc_name_match(tp->term_names, name, "|")) {
+ TR(TRACE_DATABASE, ("loaded quick-dump for %s", name));
+ } else
+#if USE_HASHED_DB
+ if (make_db_filename(filename, limit, path)
+ && (capdbp = _nc_db_open(filename, FALSE)) != 0) {
+
+ DBT key, data;
+ int reccnt = 0;
+ char *save = strdup(name);
+
+ memset(&key, 0, sizeof(key));
+ key.data = save;
+ key.size = strlen(save);
+
+ /*
+ * This lookup could return termcap data, which we do not want. We are
+ * looking for compiled (binary) terminfo data.
+ *
+ * cgetent uses a two-level lookup. On the first it uses the given
+ * name to return a record containing only the aliases for an entry.
+ * On the second (using that list of aliases as a key), it returns the
+ * content of the terminal description. We expect second lookup to
+ * return data beginning with the same set of aliases.
+ *
+ * For compiled terminfo, the list of aliases in the second case will
+ * be null-terminated. A termcap entry will not be, and will run on
+ * into the description. So we can easily distinguish between the two
+ * (source/binary) by checking the lengths.
+ */
+ while (_nc_db_get(capdbp, &key, &data) == 0) {
+ char *have = (char *) data.data;
+ used = (int) data.size - 1;
+
+ if (*have++ == 0) {
+ if (data.size > key.size
+ && IS_TIC_MAGIC(have)) {
+ code = _nc_read_termtype(tp, have, used);
+ if (code == TGETENT_NO) {
+ _nc_free_termtype(tp);
+ }