#ifndef lint
static char *rcsid = "$Id: normalizer.c,v 1.1 2003/06/04 00:26:05 marka Exp $";
#endif
#include <config.h>
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <idn/assert.h>
#include <idn/logmacro.h>
#include <idn/result.h>
#include <idn/normalizer.h>
#include <idn/strhash.h>
#include <idn/unormalize.h>
#include <idn/unicode.h>
#include <idn/ucs4.h>
#include <idn/debug.h>
#include <idn/util.h>
#define MAX_LOCAL_SCHEME 3
#define INITIALIZED (scheme_hash != NULL)
typedef struct {
char *name;
idn_normalizer_proc_t proc;
} normalize_scheme_t;
struct idn_normalizer {
int nschemes;
int scheme_size;
normalize_scheme_t **schemes;
normalize_scheme_t *local_buf[MAX_LOCAL_SCHEME];
int reference_count;
};
static idn__strhash_t scheme_hash;
static idn__unicode_version_t vcur = NULL;
static idn__unicode_version_t v320 = NULL;
#define INIT_VERSION(version, var) \
if (var == NULL) { \
idn_result_t r = idn__unicode_create(version, &var); \
if (r != idn_success) \
return (r); \
}
static idn_result_t expand_schemes(idn_normalizer_t ctx);
static idn_result_t register_standard_normalizers(void);
static idn_result_t normalizer_formkc(const unsigned long *from,
unsigned long *to, size_t tolen);
static idn_result_t normalizer_formkc_v320(const unsigned long *from,
unsigned long *to,
size_t tolen);
static struct standard_normalizer {
char *name;
idn_normalizer_proc_t proc;
} standard_normalizer[] = {
{ "unicode-form-kc", normalizer_formkc },
{ "unicode-form-kc/3.2.0", normalizer_formkc_v320 },
{ "RFC3491", normalizer_formkc_v320 },
{ NULL, NULL },
};
idn_result_t
idn_normalizer_initialize(void) {
idn__strhash_t hash;
idn_result_t r;
TRACE(("idn_normalizer_initialize()\n"));
if (scheme_hash != NULL) {
r = idn_success;
goto ret;
}
if ((r = idn__strhash_create(&hash)) != idn_success)
goto ret;
scheme_hash = hash;
r = register_standard_normalizers();
ret:
TRACE(("idn_normalizer_initialize(): %s\n", idn_result_tostring(r)));
return (r);
}
idn_result_t
idn_normalizer_create(idn_normalizer_t *ctxp) {
idn_normalizer_t ctx;
idn_result_t r;
assert(ctxp != NULL);
TRACE(("idn_normalizer_create()\n"));
if ((ctx = malloc(sizeof(struct idn_normalizer))) == NULL) {
r = idn_nomemory;
goto ret;
}
ctx->nschemes = 0;
ctx->scheme_size = MAX_LOCAL_SCHEME;
ctx->schemes = ctx->local_buf;
ctx->reference_count = 1;
*ctxp = ctx;
r = idn_success;
ret:
TRACE(("idn_normalizer_create(): %s\n", idn_result_tostring(r)));
return (r);
}
void
idn_normalizer_destroy(idn_normalizer_t ctx) {
assert(ctx != NULL);
TRACE(("idn_normalizer_destroy()\n"));
ctx->reference_count--;
if (ctx->reference_count <= 0) {
TRACE(("idn_normalizer_destroy(): the object is destroyed\n"));
if (ctx->schemes != ctx->local_buf)
free(ctx->schemes);
free(ctx);
} else {
TRACE(("idn_normalizer_destroy(): "
"update reference count (%d->%d)\n",
ctx->reference_count + 1, ctx->reference_count));
}
}
void
idn_normalizer_incrref(idn_normalizer_t ctx) {
assert(ctx != NULL);
TRACE(("idn_normalizer_incrref()\n"));
TRACE(("idn_normalizer_incrref: update reference count (%d->%d)\n",
ctx->reference_count, ctx->reference_count + 1));
ctx->reference_count++;
}
idn_result_t
idn_normalizer_add(idn_normalizer_t ctx, const char *scheme_name) {
idn_result_t r;
void *v;
normalize_scheme_t *scheme;
assert(ctx != NULL && scheme_name != NULL);
TRACE(("idn_normalizer_add(scheme_name=%s)\n", scheme_name));
assert(INITIALIZED);
if (idn__strhash_get(scheme_hash, scheme_name, &v) != idn_success) {
ERROR(("idn_normalizer_add(): invalid scheme \"%-.30s\"\n",
scheme_name));
r = idn_invalid_name;
goto ret;
}
scheme = v;
assert(ctx->nschemes <= ctx->scheme_size);
if (ctx->nschemes == ctx->scheme_size &&
(r = expand_schemes(ctx)) != idn_success) {
goto ret;
}
ctx->schemes[ctx->nschemes++] = scheme;
r = idn_success;
ret:
TRACE(("idn_normalizer_add(): %s\n", idn_result_tostring(r)));
return (r);
}
idn_result_t
idn_normalizer_addall(idn_normalizer_t ctx, const char **scheme_names,
int nschemes) {
idn_result_t r;
int i;
assert(ctx != NULL && scheme_names != NULL);
TRACE(("idn_normalizer_addall(nschemes=%d)\n", nschemes));
for (i = 0; i < nschemes; i++) {
r = idn_normalizer_add(ctx, (const char *)*scheme_names);
if (r != idn_success)
goto ret;
scheme_names++;
}
r = idn_success;
ret:
TRACE(("idn_normalizer_addall(): %s\n", idn_result_tostring(r)));
return (r);
}
idn_result_t
idn_normalizer_normalize(idn_normalizer_t ctx, const unsigned long *from,
unsigned long *to, size_t tolen) {
idn_result_t r;
unsigned long *src, *dst;
unsigned long *buffers[2] = {NULL, NULL};
size_t buflen[2] = {0, 0};
size_t dstlen;
int idx;
int i;
assert(scheme_hash != NULL);
assert(ctx != NULL && from != NULL && to != NULL);
TRACE(("idn_normalizer_normalize(from=\"%s\", tolen=%d)\n",
idn__debug_ucs4xstring(from, 50), (int)tolen));
if (ctx->nschemes <= 0) {
if (tolen < idn_ucs4_strlen(from) + 1) {
r = idn_buffer_overflow;
goto ret;
}
idn_ucs4_strcpy(to, from);
r = idn_success;
goto ret;
}
src = (void *)from;
dstlen = idn_ucs4_strlen(from) + 1;
i = 0;
while (i < ctx->nschemes) {
TRACE(("idn_normalizer_normalize(): normalize %s\n",
ctx->schemes[i]->name));
if (i + 1 == ctx->nschemes) {
dst = to;
dstlen = tolen;
} else {
if (src == buffers[0])
idx = 1;
else
idx = 0;
if (buflen[idx] < dstlen) {
void *newbuf;
newbuf = realloc(buffers[idx],
sizeof(long) * dstlen);
if (newbuf == NULL) {
r = idn_nomemory;
goto ret;
}
buffers[idx] = (unsigned long *)newbuf;
buflen[idx] = dstlen;
}
dst = buffers[idx];
dstlen = buflen[idx];
}
r = (ctx->schemes[i]->proc)(src, dst, dstlen);
if (r == idn_buffer_overflow && dst != to) {
dstlen *= 2;
continue;
}
if (r != idn_success)
goto ret;
src = dst;
i++;
}
r = idn_success;
ret:
free(buffers[0]);
free(buffers[1]);
if (r == idn_success) {
TRACE(("idn_normalizer_normalize(): success (to=\"%s\")\n",
idn__debug_ucs4xstring(to, 50)));
} else {
TRACE(("idn_normalizer_normalize(): %s\n",
idn_result_tostring(r)));
}
return (r);
}
idn_result_t
idn_normalizer_register(const char *scheme_name, idn_normalizer_proc_t proc) {
idn_result_t r;
normalize_scheme_t *scheme;
assert(scheme_name != NULL && proc != NULL);
TRACE(("idn_normalizer_register(scheme_name=%s)\n", scheme_name));
assert(INITIALIZED);
scheme = malloc(sizeof(*scheme) + strlen(scheme_name) + 1);
if (scheme == NULL) {
r = idn_nomemory;
goto ret;
}
scheme->name = (char *)(scheme + 1);
(void)strcpy(scheme->name, scheme_name);
scheme->proc = proc;
r = idn__strhash_put(scheme_hash, scheme_name, scheme);
if (r != idn_success)
goto ret;
r = idn_success;
ret:
TRACE(("idn_normalizer_register(): %s\n", idn_result_tostring(r)));
return (r);
}
static idn_result_t
expand_schemes(idn_normalizer_t ctx) {
normalize_scheme_t **new_schemes;
int new_size = ctx->scheme_size * 2;
if (ctx->schemes == ctx->local_buf) {
new_schemes = malloc(sizeof(normalize_scheme_t) * new_size);
} else {
new_schemes = realloc(ctx->schemes,
sizeof(normalize_scheme_t) * new_size);
}
if (new_schemes == NULL)
return (idn_nomemory);
if (ctx->schemes == ctx->local_buf)
memcpy(new_schemes, ctx->local_buf, sizeof(ctx->local_buf));
ctx->schemes = new_schemes;
ctx->scheme_size = new_size;
return (idn_success);
}
static idn_result_t
register_standard_normalizers(void) {
int i;
int failed = 0;
for (i = 0; standard_normalizer[i].name != NULL; i++) {
idn_result_t r;
r = idn_normalizer_register(standard_normalizer[i].name,
standard_normalizer[i].proc);
if (r != idn_success) {
WARNING(("idn_normalizer_initialize(): "
"failed to register \"%-.100s\"\n",
standard_normalizer[i].name));
failed++;
}
}
if (failed > 0)
return (idn_failure);
else
return (idn_success);
}
static idn_result_t
normalizer_formkc(const unsigned long *from, unsigned long *to, size_t tolen) {
INIT_VERSION(NULL, vcur);
return (idn__unormalize_formkc(vcur, from, to, tolen));
}
static idn_result_t
normalizer_formkc_v320(const unsigned long *from, unsigned long *to,
size_t tolen) {
INIT_VERSION("3.2.0", v320);
return (idn__unormalize_formkc(v320, from, to, tolen));
}