selectiveencode.c revision 04bdb234571448ed6194e1d4048e6512f2446f1c
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington#ifndef lint
3acf5eb97cebc2ba868e6ac4a4e01e6d1be0c892Automatic Updaterstatic char *rcsid = "$Id: selectiveencode.c,v 1.7 2000/09/20 02:47:32 ishisone Exp $";
75c0816e8295e180f4bc7f10db3d0d880383bc1cMark Andrews#endif
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
4a14ce5ba00ab7bc55c99ffdcf59c7a4ab902721Automatic Updater/*
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington * Copyright (c) 2000 Japan Network Information Center. All rights reserved.
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * By using this file, you agree to the terms and conditions set forth bellow.
9016767f4e15191b7c763b8a4ad36a57dc2705a2Mark Andrews *
9016767f4e15191b7c763b8a4ad36a57dc2705a2Mark Andrews * LICENSE TERMS AND CONDITIONS
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
9016767f4e15191b7c763b8a4ad36a57dc2705a2Mark Andrews * The following License Terms and Conditions apply, unless a different
9016767f4e15191b7c763b8a4ad36a57dc2705a2Mark Andrews * license is obtained from Japan Network Information Center ("JPNIC"),
9016767f4e15191b7c763b8a4ad36a57dc2705a2Mark Andrews * a Japanese association, Fuundo Bldg., 1-2 Kanda Ogawamachi, Chiyoda-ku,
9016767f4e15191b7c763b8a4ad36a57dc2705a2Mark Andrews * Tokyo, Japan.
0b062f4990db5cc6db2fe3398926f71b92a67407Brian Wellington *
3acf5eb97cebc2ba868e6ac4a4e01e6d1be0c892Automatic Updater * 1. Use, Modification and Redistribution (including distribution of any
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * modified or derived work) in source and/or binary forms is permitted
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * under this License Terms and Conditions.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * 2. Redistribution of source code must retain the copyright notices as they
ca67ebfe9eef0b8f04179f7e511a19e0337a5422Automatic Updater * appear in each source code file, this License Terms and Conditions.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * 3. Redistribution in binary form must reproduce the Copyright Notice,
5a4557e8de2951a2796676b5ec4b6a90caa5be14Mark Andrews * this License Terms and Conditions, in the documentation and/or other
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * materials provided with the distribution. For the purposes of binary
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * distribution the "Copyright Notice" refers to the following language:
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * "Copyright (c) Japan Network Information Center. All rights reserved."
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * 4. Neither the name of JPNIC may be used to endorse or promote products
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * derived from this Software without specific prior written approval of
3acf5eb97cebc2ba868e6ac4a4e01e6d1be0c892Automatic Updater * JPNIC.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein *
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * 5. Disclaimer/Limitation of Liability: THIS SOFTWARE IS PROVIDED BY JPNIC
3acf5eb97cebc2ba868e6ac4a4e01e6d1be0c892Automatic Updater * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL JPNIC BE LIABLE
d71e2e0c61df16ff37c9934c371a4a60c08974f7Mark Andrews * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
ad671240d635376dd8681550eebee799d2e3d1fdAutomatic Updater * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
ad671240d635376dd8681550eebee799d2e3d1fdAutomatic Updater * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
ad671240d635376dd8681550eebee799d2e3d1fdAutomatic Updater * ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
ad671240d635376dd8681550eebee799d2e3d1fdAutomatic Updater *
ad671240d635376dd8681550eebee799d2e3d1fdAutomatic Updater * 6. Indemnification by Licensee
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * Any person or entities using and/or redistributing this Software under
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * this License Terms and Conditions shall defend indemnify and hold
3acf5eb97cebc2ba868e6ac4a4e01e6d1be0c892Automatic Updater * harmless JPNIC from and against any and all judgements damages,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * expenses, settlement liabilities, cost and other liabilities of any
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * kind as a result of use and redistribution of this Software or any
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * claim, suite, action, litigation or proceeding by any third party
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * arising out of or relates to this License Terms and Conditions.
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater *
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater * 7. Governing Law, Jurisdiction and Venue
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater * This License Terms and Conditions shall be governed by and and
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater * construed in accordance with the law of Japan. Any person or entities
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater * using and/or redistributing this Software under this License Terms and
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater * Conditions hereby agrees and consent to the personal and exclusive
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater * jurisdiction and venue of Tokyo District Court of Japan.
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater */
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater#include <config.h>
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater#include <stddef.h>
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater#include <mdn/assert.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <mdn/logmacro.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <mdn/result.h>
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein#include <mdn/utf8.h>
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater#include <mdn/debug.h>
731cc132f22dbc9e0ecd7035dce314a61076d31bAutomatic Updater
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinstatic int is_domain_delimiter(char c);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinstatic char *find_nonascii(const char *s);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updatermdn_result_t
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austeinmdn_selectiveencode_findregion(const char *s,
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein char **startp, char **endp)
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein{
b272d38cc5d24f64c0647a9afb340c21c4b9aaf7Evan Hunt char *non_ascii;
b272d38cc5d24f64c0647a9afb340c21c4b9aaf7Evan Hunt char *start, *end;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater assert(s != NULL && startp != NULL && endp != NULL);
b272d38cc5d24f64c0647a9afb340c21c4b9aaf7Evan Hunt
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein TRACE(("mdn_selectiveencode_findregion(s=\"%s\")\n",
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater mdn_debug_xstring(s, 20)));
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
b272d38cc5d24f64c0647a9afb340c21c4b9aaf7Evan Hunt /*
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater * Scan the specified string looking for non-ascii character.
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater */
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater if ((non_ascii = find_nonascii(s)) == NULL)
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater return (mdn_notfound);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater /*
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater * Non-ascii character found.
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater * Determine the region to encode.
b272d38cc5d24f64c0647a9afb340c21c4b9aaf7Evan Hunt */
b272d38cc5d24f64c0647a9afb340c21c4b9aaf7Evan Hunt
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein /*
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * First, we scan backwards to find the beginning of the region
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein * that should be converted.
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein start = non_ascii;
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein while (start > s) {
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein char *prev = mdn_utf8_findfirstbyte(start - 1, s);
2f8d63983c297c62630044d28a6f66676b4d339dMark Andrews if (is_domain_delimiter(*prev))
2f8d63983c297c62630044d28a6f66676b4d339dMark Andrews break; /* Found */
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein start = prev;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater }
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater *startp = start;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater /*
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater * Next we scan forwards looking for the end of the region.
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater */
8e821eea5f57ac47a94305aa7ab0c3570d92a311Automatic Updater end = non_ascii + mdn_utf8_mblen(non_ascii);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater while (!is_domain_delimiter(*end))
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater end += mdn_utf8_mblen(end);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater *endp = end;
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater return (mdn_success);
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater}
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updaterstatic int
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updateris_domain_delimiter(char c) {
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater return ((unsigned char)c < 0x80 &&
0a7ed88633a680bb881868b75ded4d09a7bbbc50Automatic Updater !('A' <= c && c <= 'Z') &&
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein !('a' <= c && c <= 'z') &&
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein !('0' <= c && c <= '9') &&
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein c != '-' && c != '.');
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updaterstatic char *
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updaterfind_nonascii(const char *s) {
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater while (*s != '\0' && (unsigned char)*s < 0x80)
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater s++;
64affc54f96a2c71cbd10ed71e246ce0746259aaAutomatic Updater if (*s == '\0')
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater return (NULL);
8ec3c085233cedb22b05da36e2773c8f357a7e45Automatic Updater else
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein return ((char *)s);
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein}
60e5e10f8d2e2b0c41e8abad38cacd867caa6ab2Rob Austein