1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
|
#ifndef lint
static char *rcsid = "$Id: util.c,v 1.21 2001/10/31 08:51:30 m-kasahr Exp $";
#endif
/*
* Copyright (c) 2000 Japan Network Information Center. All rights reserved.
*
* By using this file, you agree to the terms and conditions set forth bellow.
*
* LICENSE TERMS AND CONDITIONS
*
* The following License Terms and Conditions apply, unless a different
* license is obtained from Japan Network Information Center ("JPNIC"),
* a Japanese association, Kokusai-Kougyou-Kanda Bldg 6F, 2-3-4 Uchi-Kanda,
* Chiyoda-ku, Tokyo 101-0047, Japan.
*
* 1. Use, Modification and Redistribution (including distribution of any
* modified or derived work) in source and/or binary forms is permitted
* under this License Terms and Conditions.
*
* 2. Redistribution of source code must retain the copyright notices as they
* appear in each source code file, this License Terms and Conditions.
*
* 3. Redistribution in binary form must reproduce the Copyright Notice,
* this License Terms and Conditions, in the documentation and/or other
* materials provided with the distribution. For the purposes of binary
* distribution the "Copyright Notice" refers to the following language:
* "Copyright (c) Japan Network Information Center. All rights reserved."
*
* 4. Neither the name of JPNIC may be used to endorse or promote products
* derived from this Software without specific prior written approval of
* JPNIC.
*
* 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* 6. Indemnification by Licensee
* Any person or entities using and/or redistributing this Software under
* this License Terms and Conditions shall defend indemnify and hold
* harmless JPNIC from and against any and all judgements damages,
* expenses, settlement liabilities, cost and other liabilities of any
* kind as a result of use and redistribution of this Software or any
* claim, suite, action, litigation or proceeding by any third party
* arising out of or relates to this License Terms and Conditions.
*
* 7. Governing Law, Jurisdiction and Venue
* This License Terms and Conditions shall be governed by and and
* construed in accordance with the law of Japan. Any person or entities
* using and/or redistributing this Software under this License Terms and
* Conditions hereby agrees and consent to the personal and exclusive
* jurisdiction and venue of Tokyo District Court of Japan.
*/
#include <config.h>
#include <stdio.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <ctype.h>
#include <errno.h>
#include <mdn/resconf.h>
#include <mdn/res.h>
#include <mdn/utf8.h>
#include <mdn/selectiveencode.h>
#include "util.h"
extern int line_number;
mdn_result_t
selective_encode(mdn_resconf_t conf, char *insn,
char *from, char *to, int tolen)
{
for (;;) {
int len;
char *region_start, *region_end;
mdn_result_t r;
char save;
/*
* Find the region that needs conversion.
*/
r = mdn_selectiveencode_findregion(from, ®ion_start,
®ion_end);
if (r == mdn_notfound) {
/*
* Not found. Just copy the whole thing.
*/
if (tolen <= strlen(from))
return (mdn_buffer_overflow);
(void)strcpy(to, from);
return (mdn_success);
} else if (r != mdn_success) {
/* This should not happen.. */
errormsg("internal error at line %d: %s\n",
line_number, mdn_result_tostring(r));
return (r);
}
/*
* We have found a region to convert.
* First, copy the prefix part verbatim.
*/
len = region_start - from;
if (tolen < len) {
errormsg("internal buffer overflow at line %d\n",
line_number);
return (mdn_buffer_overflow);
}
(void)memcpy(to, from, len);
to += len;
tolen -= len;
/*
* Terminate the region with NUL.
*/
save = *region_end;
*region_end = '\0';
/*
* Encode the region.
*/
r = mdn_res_nameconv(conf, insn, region_start, to, tolen);
/*
* Restore character.
*/
*region_end = save;
if (r != mdn_success)
return (r);
len = strlen(to);
to += len;
tolen -= len;
from = region_end;
}
}
mdn_result_t
selective_decode(mdn_resconf_t conf, char *insn,
char *from, char *to, int tolen)
{
char *domain_name;
char *ignored_chunk;
char save;
int len;
mdn_result_t r;
/*
* While `*from' points to a character in a string which may be
* a domain name, `domain_name' refers to the beginning of the
* domain name.
*/
domain_name = NULL;
/*
* We ignore chunks matching to the regular expression:
* [\-\.][0-9A-Za-z\-\.]*
*
* While `*from' points to a character in such a chunk,
* `ignored_chunk' refers to the beginning of the chunk.
*/
ignored_chunk = NULL;
for (;;) {
if (*from == '-') {
/*
* We don't recognize `.-' as a part of domain name.
*/
if (domain_name != NULL) {
if (*(from - 1) == '.') {
ignored_chunk = domain_name;
domain_name = NULL;
}
} else if (ignored_chunk == NULL) {
ignored_chunk = from;
}
} else if (*from == '.') {
/*
* We don't recognize `-.' nor `..' as a part of
* domain name.
*/
if (domain_name != NULL) {
if (*(from - 1) == '-' || *(from - 1) == '.') {
ignored_chunk = domain_name;
domain_name = NULL;
}
} else if (ignored_chunk == NULL) {
ignored_chunk = from;
}
} else if (('a' <= *from && *from <= 'z') ||
('A' <= *from && *from <= 'Z') ||
('0' <= *from && *from <= '9')) {
if (ignored_chunk == NULL && domain_name == NULL)
domain_name = from;
} else {
if (ignored_chunk != NULL) {
/*
* `from' reaches the end of the ignored chunk.
* Copy the chunk to `to'.
*/
len = from - ignored_chunk;
if (tolen < len)
return (mdn_buffer_overflow);
(void)memcpy(to, ignored_chunk, len);
to += len;
tolen -= len;
} else if (domain_name != NULL) {
/*
* `from' reaches the end of the domain name.
* Decode the domain name, and copy the result
* to `to'.
*/
save = *from;
*from = '\0';
r = mdn_res_nameconv(conf, insn,
domain_name, to, tolen);
*from = save;
if (r == mdn_success) {
len = strlen(to);
} else if (r == mdn_invalid_encoding) {
len = from - domain_name;
if (tolen < len)
return (mdn_buffer_overflow);
(void)memcpy(to, domain_name, len);
} else {
return (r);
}
to += len;
tolen -= len;
}
/*
* Copy a character `*from' to `to'.
*/
if (tolen < 1)
return (mdn_buffer_overflow);
*to = *from;
to++;
tolen--;
domain_name = NULL;
ignored_chunk = NULL;
if (*from == '\0')
break;
}
from++;
}
return (mdn_success);
}
void
set_encoding_alias(const char *encoding_alias) {
mdn_result_t r;
if ((r = mdn_converter_aliasfile(encoding_alias)) != mdn_success) {
errormsg("cannot read alias file %s: %s\n",
encoding_alias, mdn_result_tostring(r));
exit(1);
}
}
void
set_localcode(mdn_resconf_t conf, const char *code) {
mdn_result_t r;
r = mdn_resconf_setlocalconvertername(conf, code, 0);
if (r != mdn_success) {
errormsg("cannot create converter for codeset %s: %s\n",
code, mdn_result_tostring(r));
exit(1);
}
}
void
set_idncode(mdn_resconf_t conf, const char *code) {
mdn_result_t r;
r = mdn_resconf_setidnconvertername(conf, code, 0);
if (r != mdn_success) {
errormsg("cannot create converter for codeset %s: %s\n",
code, mdn_result_tostring(r));
exit(1);
}
}
void
set_delimitermapper(mdn_resconf_t conf, unsigned long *delimiters,
int ndelimiters) {
mdn_result_t r;
r = mdn_resconf_addalldelimitermapucs(conf, delimiters, ndelimiters);
if (r != mdn_success) {
errormsg("cannot add delimiter: %s\n",
mdn_result_tostring(r));
exit(1);
}
}
void
set_localmapper(mdn_resconf_t conf, char **mappers, int nmappers) {
mdn_result_t r;
/* Add mapping. */
r = mdn_resconf_addalllocalmapselectornames(conf,
MDN_MAPSELECTOR_DEFAULT,
(const char **)mappers,
nmappers);
if (r != mdn_success) {
errormsg("cannot add local map: %s\n",
mdn_result_tostring(r));
exit(1);
}
}
void
set_nameprep(mdn_resconf_t conf, char *version) {
mdn_result_t r;
r = mdn_resconf_setnameprepversion(conf, version);
if (r != mdn_success) {
errormsg("error setting nameprep %s: %s\n",
version, mdn_result_tostring(r));
exit(1);
}
}
void
set_mapper(mdn_resconf_t conf, char **mappers, int nmappers) {
mdn_result_t r;
/* Configure mapper. */
r = mdn_resconf_addallmappernames(conf, (const char **)mappers,
nmappers);
if (r != mdn_success) {
errormsg("cannot add nameprep map: %s\n",
mdn_result_tostring(r));
exit(1);
}
}
void
set_normalizer(mdn_resconf_t conf, char **normalizers, int nnormalizer) {
mdn_result_t r;
r = mdn_resconf_addallnormalizernames(conf,
(const char **)normalizers,
nnormalizer);
if (r != mdn_success) {
errormsg("cannot add normalizer: %s\n",
mdn_result_tostring(r));
exit(1);
}
}
void
set_prohibit_checkers(mdn_resconf_t conf, char **prohibits, int nprohibits) {
mdn_result_t r;
r = mdn_resconf_addallprohibitcheckernames(conf,
(const char **)prohibits,
nprohibits);
if (r != mdn_success) {
errormsg("cannot add prohibit checker: %s\n",
mdn_result_tostring(r));
exit(1);
}
}
void
set_unassigned_checkers(mdn_resconf_t conf, char **unassigns, int nunassigns) {
mdn_result_t r;
r = mdn_resconf_addallunassignedcheckernames(conf,
(const char **)unassigns,
nunassigns);
if (r != mdn_success) {
errormsg("cannot add unassigned checker: %s\n",
mdn_result_tostring(r));
exit(1);
}
}
void
check_defaultlocalcode(mdn_resconf_t conf, const char *opt) {
mdn_converter_t conv = mdn_resconf_getlocalconverter(conf);
if (conv == NULL) {
errormsg("cannot get the default local encoding.\n"
"please specify an appropriate one "
"with `%s' option.\n", opt);
exit(1);
} else
mdn_converter_destroy(conv);
}
void
check_defaultidncode(mdn_resconf_t conf, const char *opt) {
mdn_converter_t conv = mdn_resconf_getidnconverter(conf);
if (conv == NULL) {
errormsg("cannot get the default IDN encoding.\n"
"please specify an appropriate one "
"with `%s' option.\n", opt);
exit(1);
} else
mdn_converter_destroy(conv);
}
void
errormsg(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
}
|