1/*-
2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3 *
4 * Copyright (c) 2004 Tim J. Robbins.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 *    notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 *    notice, this list of conditions and the following disclaimer in the
14 *    documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28/*
29 * "Character map" ADT. Stores mappings between pairs of characters in a
30 * splay tree, with a lookup table cache to simplify looking up the first
31 * bunch of characters (which are presumably more common than others).
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD$");
36
37#include <assert.h>
38#include <limits.h>
39#include <stdbool.h>
40#include <stdlib.h>
41#include <wchar.h>
42#include "cmap.h"
43
44static struct cmapnode *cmap_splay(struct cmapnode *, wint_t);
45
46/*
47 * cmap_alloc --
48 *	Allocate a character map.
49 */
50struct cmap *
51cmap_alloc(void)
52{
53	struct cmap *cm;
54
55	cm = malloc(sizeof(*cm));
56	if (cm == NULL)
57		return (NULL);
58	cm->cm_root = NULL;
59	cm->cm_def = CM_DEF_SELF;
60	cm->cm_havecache = false;
61	cm->cm_min = cm->cm_max = 0;
62	return (cm);
63}
64
65/*
66 * cmap_add --
67 *	Add a mapping from "from" to "to" to the map.
68 */
69bool
70cmap_add(struct cmap *cm, wint_t from, wint_t to)
71{
72	struct cmapnode *cmn, *ncmn;
73
74	cm->cm_havecache = false;
75
76	if (cm->cm_root == NULL) {
77		cmn = malloc(sizeof(*cmn));
78		if (cmn == NULL)
79			return (false);
80		cmn->cmn_from = from;
81		cmn->cmn_to = to;
82		cmn->cmn_left = cmn->cmn_right = NULL;
83		cm->cm_root = cmn;
84		cm->cm_min = cm->cm_max = from;
85		return (true);
86	}
87
88	cmn = cm->cm_root = cmap_splay(cm->cm_root, from);
89
90	if (cmn->cmn_from == from) {
91		cmn->cmn_to = to;
92		return (true);
93	}
94
95	ncmn = malloc(sizeof(*ncmn));
96	if (ncmn == NULL)
97		return (false);
98	ncmn->cmn_from = from;
99	ncmn->cmn_to = to;
100	if (from < cmn->cmn_from) {
101		ncmn->cmn_left = cmn->cmn_left;
102		ncmn->cmn_right = cmn;
103		cmn->cmn_left = NULL;
104	} else {
105		ncmn->cmn_right = cmn->cmn_right;
106		ncmn->cmn_left = cmn;
107		cmn->cmn_right = NULL;
108	}
109	if (from < cm->cm_min)
110		cm->cm_min = from;
111	if (from > cm->cm_max)
112		cm->cm_max = from;
113        cm->cm_root = ncmn;
114
115	return (true);
116}
117
118/*
119 * cmap_lookup_hard --
120 *	Look up the mapping for a character without using the cache.
121 */
122wint_t
123cmap_lookup_hard(struct cmap *cm, wint_t ch)
124{
125
126	if (cm->cm_root != NULL) {
127		cm->cm_root = cmap_splay(cm->cm_root, ch);
128		if (cm->cm_root->cmn_from == ch)
129			return (cm->cm_root->cmn_to);
130	}
131	return (cm->cm_def == CM_DEF_SELF ? ch : cm->cm_def);
132}
133
134/*
135 * cmap_cache --
136 *	Update the cache.
137 */
138void
139cmap_cache(struct cmap *cm)
140{
141	wint_t ch;
142
143	for (ch = 0; ch < CM_CACHE_SIZE; ch++)
144		cm->cm_cache[ch] = cmap_lookup_hard(cm, ch);
145
146	cm->cm_havecache = true;
147}
148
149/*
150 * cmap_default --
151 *	Change the value that characters without mappings map to, and
152 *	return the old value. The special character value CM_MAP_SELF
153 *	means characters map to themselves.
154 */
155wint_t
156cmap_default(struct cmap *cm, wint_t def)
157{
158	wint_t old;
159
160	old = cm->cm_def;
161	cm->cm_def = def;
162	cm->cm_havecache = false;
163	return (old);
164}
165
166static struct cmapnode *
167cmap_splay(struct cmapnode *t, wint_t ch)
168{
169	struct cmapnode N, *l, *r, *y;
170
171	/*
172	 * Based on public domain code from Sleator.
173	 */
174
175	assert(t != NULL);
176
177	N.cmn_left = N.cmn_right = NULL;
178	l = r = &N;
179	for (;;) {
180		if (ch < t->cmn_from) {
181			if (t->cmn_left != NULL &&
182			    ch < t->cmn_left->cmn_from) {
183				y = t->cmn_left;
184				t->cmn_left = y->cmn_right;
185				y->cmn_right = t;
186				t = y;
187			}
188			if (t->cmn_left == NULL)
189				break;
190			r->cmn_left = t;
191			r = t;
192			t = t->cmn_left;
193		} else if (ch > t->cmn_from) {
194			if (t->cmn_right != NULL &&
195			    ch > t->cmn_right->cmn_from) {
196				y = t->cmn_right;
197				t->cmn_right = y->cmn_left;
198				y->cmn_left = t;
199				t = y;
200			}
201			if (t->cmn_right == NULL)
202				break;
203			l->cmn_right = t;
204			l = t;
205			t = t->cmn_right;
206		} else
207			break;
208	}
209	l->cmn_right = t->cmn_left;
210	r->cmn_left = t->cmn_right;
211	t->cmn_left = N.cmn_right;
212	t->cmn_right = N.cmn_left;
213	return (t);
214}
215