From 4efea7e314b49df660799e71ede713dff0cd1230 Mon Sep 17 00:00:00 2001
From: Witold Filipczyk <witekfl@poczta.onet.pl>
Date: Sun, 7 Feb 2016 11:51:17 +0100
Subject: [PATCH] Added support for label_key in references of dumps.

Added base parameter to dec2qwerty and qwerty2dec to avoid strlen calls
im dumps.
---
 src/document/html/renderer.c | 11 +++++------
 src/document/html/renderer.h |  3 ++-
 src/viewer/dump/dump.c       | 15 +++++++++++----
 src/viewer/text/link.c       |  3 ++-
 4 files changed, 20 insertions(+), 12 deletions(-)

diff --git a/src/document/html/renderer.c b/src/document/html/renderer.c
index fd85bc710..e566ea5e4 100644
--- a/src/document/html/renderer.c
+++ b/src/document/html/renderer.c
@@ -1505,10 +1505,9 @@ put_chars_conv(struct html_context *html_context,
  * represented by key. I the trivial case, key="0123456789". A more homerow
  * friendly key="gfdsahjkl;trewqyuiopvcxznm". Returns the length of link_sym.
  */
-static int
-dec2qwerty(int num, char *link_sym, const char *key)
+int
+dec2qwerty(int num, unsigned char *link_sym, const unsigned char *key, int base)
 {
-	int base = strlen(key);
 	int newlen, i, pow;
 
 	if (base < 2) return 0;
@@ -1528,10 +1527,9 @@ dec2qwerty(int num, char *link_sym, const char *key)
  * Returns the value of link_sym in decimal according to key.
  */
 int
-qwerty2dec(const char *link_sym, const char *key)
+qwerty2dec(const unsigned char *link_sym, const unsigned char *key, int base)
 {
 	int z = 0;
-	int base = strlen(key);
 	int symlen = strlen(link_sym);
 	int i;
 	int pow;
@@ -1555,12 +1553,13 @@ put_link_number(struct html_context *html_context)
 	unsigned char *fi = format.image;
 	struct form_control *ff = format.form;
 	int slen = 0;
+	int base = strlen(symkey);
 
 	format.link = format.target = format.image = NULL;
 	format.form = NULL;
 
 	s[slen++] = '[';
-	slen += dec2qwerty(part->link_num, s + 1, symkey);
+	slen += dec2qwerty(part->link_num, s + 1, symkey, base);
 	s[slen++] = ']';
 	s[slen] = '\0';
 
diff --git a/src/document/html/renderer.h b/src/document/html/renderer.h
index ffae4cb07..54cbecb0d 100644
--- a/src/document/html/renderer.h
+++ b/src/document/html/renderer.h
@@ -68,5 +68,6 @@ void free_table_cache(void);
 
 struct part *format_html_part(struct html_context *html_context, unsigned char *, unsigned char *, int, int, int, struct document *, int, int, unsigned char *, int);
 
-int qwerty2dec(const char *link_sym, const char *key);
+int dec2qwerty(int num, unsigned char *link_sym, const unsigned char *key, int base);
+int qwerty2dec(const unsigned char *link_sym, const unsigned char *key, int base);
 #endif
diff --git a/src/viewer/dump/dump.c b/src/viewer/dump/dump.c
index cb9558ff5..0958f070b 100644
--- a/src/viewer/dump/dump.c
+++ b/src/viewer/dump/dump.c
@@ -23,6 +23,7 @@
 #include "cache/cache.h"
 #include "config/options.h"
 #include "document/document.h"
+#include "document/html/renderer.h"
 #include "document/options.h"
 #include "document/renderer.h"
 #include "document/view.h"
@@ -326,9 +327,12 @@ dump_references(struct document *document, int fd, unsigned char buf[D_BUF])
 {
 	if (document->nlinks
 	    && get_opt_bool("document.dump.references", NULL)) {
+		unsigned char key_sym[64] = {0};
 		int x;
 		unsigned char *header = "\nReferences\n\n   Visible links\n";
+		const unsigned char *label_key = get_opt_str("document.browse.links.label_key", NULL);
 		int headlen = strlen(header);
+		int base = strlen(label_key);
 
 		if (hard_write(fd, header, headlen) != headlen)
 			return -1;
@@ -341,12 +345,15 @@ dump_references(struct document *document, int fd, unsigned char buf[D_BUF])
 			if (!where) continue;
 
 			if (document->options.links_numbering) {
+
+				dec2qwerty(x + 1, key_sym, label_key, base);
+
 				if (link->title && *link->title)
-					snprintf(buf, D_BUF, "%4d. %s\n\t%s\n",
-						 x + 1, link->title, where);
+					snprintf(buf, D_BUF, "%4s. %s\n\t%s\n",
+						 key_sym, link->title, where);
 				else
-					snprintf(buf, D_BUF, "%4d. %s\n",
-						 x + 1, where);
+					snprintf(buf, D_BUF, "%4s. %s\n",
+						 key_sym, where);
 			} else {
 				if (link->title && *link->title)
 					snprintf(buf, D_BUF, "   . %s\n\t%s\n",
diff --git a/src/viewer/text/link.c b/src/viewer/text/link.c
index cb299a0af..b129e4bd9 100644
--- a/src/viewer/text/link.c
+++ b/src/viewer/text/link.c
@@ -1223,13 +1223,14 @@ goto_link_symbol(struct session *ses, unsigned char *sym)
 	char *symkey = get_opt_str("document.browse.links.label_key", ses);
 	struct document_view *doc_view;
 	int num;
+	int base = strlen(symkey);
 
 	assert(ses && sym);
 	if_assert_failed return;
 	doc_view = current_frame(ses);
 	assert(doc_view);
 	if_assert_failed return;
-	num = qwerty2dec(sym, symkey);
+	num = qwerty2dec(sym, symkey, base);
 	goto_link_number_do(ses, doc_view, num - 1);
 }