From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-oo1-xc32.google.com (mail-oo1-xc32.google.com [IPv6:2607:f8b0:4864:20::c32]) by sourceware.org (Postfix) with ESMTPS id 840CA3858C83 for ; Mon, 21 Mar 2022 12:24:09 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 840CA3858C83 Received: by mail-oo1-xc32.google.com with SMTP id p10-20020a056820044a00b00320d7d4af22so18873001oou.4 for ; Mon, 21 Mar 2022 05:24:09 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:message-id:date:mime-version:user-agent:subject :content-language:to:references:from:in-reply-to :content-transfer-encoding; bh=9+1SnjQqrcVHdjyApaU4TEI72cSwygIKucKyDRLXfAw=; b=NgrWOKZ8LvpCUTkmS/0/xZkTkwlvIvmz5ZoSbnuu9sR/NsX5rX8DTRtHg3BphD0hz8 92xW6JsZQqYp2sYNPccoNiqAF1eKju8IYszZdbsiowVvNJGZ4hKjJ4zIPitMhc7+h48Q yVxHyp29ry8IjydunUVgcwVMZcDqBDpIpRbLnfBGRAKziB+04rSLxKTksvBqoREvukV3 kwGMER6S2GNGHFp9EtV16y5lljiJZM/CeX1rEKZxVIa4RiYnsMjyIRBY/7szSzXcN0Eu dKuri5VA3ka3QohNkTwg1GpPUZYVL3zjb4DecVhvbS1cfA6LdRJ3n+T4sySO9Bbo+R5q NvwA== X-Gm-Message-State: AOAM530Z/Go8I1svI8uUtzaYBIkIq7/qPQjWAeWZFhFaaLusUTFx8FaK 4YTM/xpGSohzy4tU+O+nGCDdfuGk4xL/RQ== X-Google-Smtp-Source: ABdhPJz/QKlS0MyST3kL5+T++ooPhShoL/U0KFI2VozLCajdEJG84Uw7FaIJJRpcR1lhmfU88DRw4g== X-Received: by 2002:a05:6870:45a4:b0:dd:b08e:fa49 with SMTP id y36-20020a05687045a400b000ddb08efa49mr8524338oao.270.1647865447198; Mon, 21 Mar 2022 05:24:07 -0700 (PDT) Received: from ?IPV6:2804:431:c7ca:2d55:f04a:67c7:cbf3:571d? ([2804:431:c7ca:2d55:f04a:67c7:cbf3:571d]) by smtp.gmail.com with ESMTPSA id bb15-20020a056820160f00b0032404d7a88esm7208554oob.27.2022.03.21.05.24.05 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Mon, 21 Mar 2022 05:24:06 -0700 (PDT) Message-ID: Date: Mon, 21 Mar 2022 09:24:04 -0300 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.7.0 Subject: Re: [PATCH v5 3/4] iconv: make utf-7.c able to use variants Content-Language: en-US To: Max Gautier , libc-alpha@sourceware.org References: <87blcw9ptq.fsf@oldenburg.str.redhat.com> <20211209093152.313872-1-mg@max.gautier.name> <20211209093152.313872-4-mg@max.gautier.name> From: Adhemerval Zanella In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, NICE_REPLY_A, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP, T_FILL_THIS_FORM_SHORT, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 21 Mar 2022 12:24:12 -0000 On 20/03/2022 13:42, Max Gautier via Libc-alpha wrote: > Add infrastructure in utf-7.c to handle variants. The approach comes from > iso646.c > The variant is defined at gconv_init time and is passed as a > supplementary variable. > > Signed-off-by: Max Gautier Patch looks ok, although it should be refactor to add 'enum variant' argument on isdirect and isxdirect instead of relying on previous patch (to keep the patch consistent). > --- > iconvdata/utf-7.c | 230 ++++++++++++++++++++++++++++++++++------------ > 1 file changed, 170 insertions(+), 60 deletions(-) > > diff --git a/iconvdata/utf-7.c b/iconvdata/utf-7.c > index 15f3669ac8..b639d8ff3e 100644 > --- a/iconvdata/utf-7.c > +++ b/iconvdata/utf-7.c > @@ -29,6 +29,24 @@ > #include > > > +enum variant > +{ > + UTF7, > +}; > + > +/* Must be in the same order as enum variant above. */ > +static const char names[] = > + "UTF-7//\0" > + "\0"; > + > +static uint32_t > +shift_character (enum variant const var) > +{ > + if (var == UTF7) > + return '+'; > + else > + abort (); > +} > > static bool > between (uint32_t const ch, > @@ -38,23 +56,27 @@ between (uint32_t const ch, > } > > /* The set of "direct characters": > + FOR UTF-7 > A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr > */ > > static bool > isdirect (uint32_t ch, enum variant var) > { > - return (between (ch, 'A', 'Z') > - || between (ch, 'a', 'z') > - || between (ch, '0', '9') > - || ch == '\'' || ch == '(' || ch == ')' > - || between (ch, ',', '/') > - || ch == ':' || ch == '?' > - || ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); > + if (var == UTF7) > + return (between (ch, 'A', 'Z') > + || between (ch, 'a', 'z') > + || between (ch, '0', '9') > + || ch == '\'' || ch == '(' || ch == ')' > + || between (ch, ',', '/') > + || ch == ':' || ch == '?' > + || ch == ' ' || ch == '\t' || ch == '\n' || ch == '\r'); > + abort (); > } > > > /* The set of "direct and optional direct characters": > + (UTF-7 only) > A-Z a-z 0-9 ' ( ) , - . / : ? space tab lf cr > ! " # $ % & * ; < = > @ [ ] ^ _ ` { | } > */ > @@ -62,10 +84,15 @@ isdirect (uint32_t ch, enum variant var) > static bool > isxdirect (uint32_t ch, enum variant var) > { > - return (ch == '\t' > - || ch == '\n' > - || ch == '\r' > - || (between (ch, ' ', '}') && ch != '+' && ch != '\\')); > + if (isdirect (ch, var)) > + return true; > + if (var != UTF7) > + return false; > + return between (ch, '!', '&') > + || ch == '*' > + || between (ch, ';', '@') > + || (between (ch, '[', '`') && ch != '\\') > + || between (ch, '{', '}'); > } > > > @@ -85,7 +112,7 @@ needs_explicit_shift (uint32_t ch) > > /* Converts a value in the range 0..63 to a base64 encoded char. */ > static unsigned char > -base64 (unsigned int i) > +base64 (unsigned int i, enum variant var) > { > if (i < 26) > return i + 'A'; > @@ -95,7 +122,7 @@ base64 (unsigned int i) > return i - 52 + '0'; > else if (i == 62) > return '+'; > - else if (i == 63) > + else if (i == 63 && var == UTF7) > return '/'; > else > abort (); > @@ -103,9 +130,8 @@ base64 (unsigned int i) > > > /* Definitions used in the body of the `gconv' function. */ > -#define CHARSET_NAME "UTF-7//" > -#define DEFINE_INIT 1 > -#define DEFINE_FINI 1 > +#define DEFINE_INIT 0 > +#define DEFINE_FINI 0 > #define FROM_LOOP from_utf7_loop > #define TO_LOOP to_utf7_loop > #define MIN_NEEDED_FROM 1 > @@ -113,11 +139,27 @@ base64 (unsigned int i) > #define MIN_NEEDED_TO 4 > #define MAX_NEEDED_TO 4 > #define ONE_DIRECTION 0 > +#define FROM_DIRECTION (dir == from_utf7) > #define PREPARE_LOOP \ > mbstate_t saved_state; \ > - mbstate_t *statep = data->__statep; > -#define EXTRA_LOOP_ARGS , statep > + mbstate_t *statep = data->__statep; \ > + enum direction dir = ((struct utf7_data *) step->__data)->dir; \ > + enum direction var = ((struct utf7_data *) step->__data)->var; > +#define EXTRA_LOOP_ARGS , statep, var > + > > +enum direction > +{ > + illegal_dir, > + from_utf7, > + to_utf7 > +}; > + > +struct utf7_data > +{ > + enum direction dir; > + enum variant var; > +}; > > /* Since we might have to reset input pointer we must be able to save > and restore the state. */ > @@ -127,6 +169,70 @@ base64 (unsigned int i) > else \ > *statep = saved_state > > +int > +gconv_init (struct __gconv_step *step) > +{ > + /* Determine which direction. */ > + struct utf7_data *new_data; > + enum direction dir = illegal_dir; > + > + enum variant var = 0; > + for (const char *name = names; *name != '\0'; > + name = __rawmemchr (name, '\0') + 1) > + { > + if (__strcasecmp (step->__from_name, name) == 0) > + { > + dir = from_utf7; > + break; > + } > + else if (__strcasecmp (step->__to_name, name) == 0) > + { > + dir = to_utf7; > + break; > + } > + ++var; > + } > + > + if (__glibc_likely (dir != illegal_dir)) > + { > + new_data = malloc (sizeof (*new_data)); > + if (new_data == NULL) > + return __GCONV_NOMEM; > + > + new_data->dir = dir; > + new_data->var = var; > + step->__data = new_data; > + > + if (dir == from_utf7) > + { > + step->__min_needed_from = MIN_NEEDED_FROM; > + step->__max_needed_from = MAX_NEEDED_FROM; > + step->__min_needed_to = MIN_NEEDED_TO; > + step->__max_needed_to = MAX_NEEDED_TO; > + } > + else > + { > + step->__min_needed_from = MIN_NEEDED_TO; > + step->__max_needed_from = MAX_NEEDED_TO; > + step->__min_needed_to = MIN_NEEDED_FROM; > + step->__max_needed_to = MAX_NEEDED_FROM; > + } > + } > + else > + return __GCONV_NOCONV; > + > + step->__stateful = 1; > + > + return __GCONV_OK; > +} > + > +void > +gconv_end (struct __gconv_step *data) > +{ > + free (data->__data); > +} > + > + > > /* First define the conversion function from UTF-7 to UCS4. > The state is structured as follows: > @@ -154,13 +260,13 @@ base64 (unsigned int i) > if ((statep->__count >> 3) == 0) \ > { \ > /* base64 encoding inactive. */ \ > - if (isxdirect (ch)) \ > + if (isxdirect (ch, var)) \ > { \ > inptr++; \ > put32 (outptr, ch); \ > outptr += 4; \ > } \ > - else if (__glibc_likely (ch == '+')) \ > + else if (__glibc_likely (ch == shift_character (var))) \ > { \ > if (__glibc_unlikely (inptr + 2 > inend)) \ > { \ > @@ -285,7 +391,7 @@ base64 (unsigned int i) > } \ > } > #define LOOP_NEED_FLAGS > -#define EXTRA_LOOP_DECLS , mbstate_t *statep > +#define EXTRA_LOOP_DECLS , mbstate_t *statep, enum variant var > #include > > > @@ -316,7 +422,7 @@ base64 (unsigned int i) > if ((statep->__count & 0x18) == 0) \ > { \ > /* base64 encoding inactive */ \ > - if (isdirect (ch)) \ > + if (isdirect (ch, var)) \ > { \ > *outptr++ = (unsigned char) ch; \ > } \ > @@ -324,7 +430,7 @@ base64 (unsigned int i) > { \ > size_t count; \ > \ > - if (ch == '+') \ > + if (ch == shift_character (var)) \ > count = 2; \ > else if (ch < 0x10000) \ > count = 3; \ > @@ -339,13 +445,13 @@ base64 (unsigned int i) > break; \ > } \ > \ > - *outptr++ = '+'; \ > - if (ch == '+') \ > + *outptr++ = shift_character (var); \ > + if (ch == shift_character (var)) \ > *outptr++ = '-'; \ > else if (ch < 0x10000) \ > { \ > - *outptr++ = base64 (ch >> 10); \ > - *outptr++ = base64 ((ch >> 4) & 0x3f); \ > + *outptr++ = base64 (ch >> 10, var); \ > + *outptr++ = base64 ((ch >> 4) & 0x3f, var); \ > statep->__count = ((ch & 15) << 5) | (3 << 3); \ > } \ > else if (ch < 0x110000) \ > @@ -354,11 +460,11 @@ base64 (unsigned int i) > uint32_t ch2 = 0xdc00 + ((ch - 0x10000) & 0x3ff); \ > \ > ch = (ch1 << 16) | ch2; \ > - *outptr++ = base64 (ch >> 26); \ > - *outptr++ = base64 ((ch >> 20) & 0x3f); \ > - *outptr++ = base64 ((ch >> 14) & 0x3f); \ > - *outptr++ = base64 ((ch >> 8) & 0x3f); \ > - *outptr++ = base64 ((ch >> 2) & 0x3f); \ > + *outptr++ = base64 (ch >> 26, var); \ > + *outptr++ = base64 ((ch >> 20) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 14) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 8) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 2) & 0x3f, var); \ > statep->__count = ((ch & 3) << 7) | (2 << 3); \ > } \ > else \ > @@ -368,7 +474,7 @@ base64 (unsigned int i) > else \ > { \ > /* base64 encoding active */ \ > - if (isdirect (ch)) \ > + if (isdirect (ch, var)) \ > { \ > /* deactivate base64 encoding */ \ > size_t count; \ > @@ -382,7 +488,7 @@ base64 (unsigned int i) > } \ > \ > if ((statep->__count & 0x18) >= 0x10) \ > - *outptr++ = base64 ((statep->__count >> 3) & ~3); \ > + *outptr++ = base64 ((statep->__count >> 3) & ~3, var); \ > if (needs_explicit_shift (ch)) \ > *outptr++ = '-'; \ > *outptr++ = (unsigned char) ch; \ > @@ -410,22 +516,24 @@ base64 (unsigned int i) > switch ((statep->__count >> 3) & 3) \ > { \ > case 1: \ > - *outptr++ = base64 (ch >> 10); \ > - *outptr++ = base64 ((ch >> 4) & 0x3f); \ > + *outptr++ = base64 (ch >> 10, var); \ > + *outptr++ = base64 ((ch >> 4) & 0x3f, var); \ > statep->__count = ((ch & 15) << 5) | (3 << 3); \ > break; \ > case 2: \ > *outptr++ = \ > - base64 (((statep->__count >> 3) & ~3) | (ch >> 12)); \ > - *outptr++ = base64 ((ch >> 6) & 0x3f); \ > - *outptr++ = base64 (ch & 0x3f); \ > + base64 (((statep->__count >> 3) & ~3) | (ch >> 12), \ > + var); \ > + *outptr++ = base64 ((ch >> 6) & 0x3f, var); \ > + *outptr++ = base64 (ch & 0x3f, var); \ > statep->__count = (1 << 3); \ > break; \ > case 3: \ > *outptr++ = \ > - base64 (((statep->__count >> 3) & ~3) | (ch >> 14)); \ > - *outptr++ = base64 ((ch >> 8) & 0x3f); \ > - *outptr++ = base64 ((ch >> 2) & 0x3f); \ > + base64 (((statep->__count >> 3) & ~3) | (ch >> 14), \ > + var); \ > + *outptr++ = base64 ((ch >> 8) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 2) & 0x3f, var); \ > statep->__count = ((ch & 3) << 7) | (2 << 3); \ > break; \ > default: \ > @@ -441,30 +549,32 @@ base64 (unsigned int i) > switch ((statep->__count >> 3) & 3) \ > { \ > case 1: \ > - *outptr++ = base64 (ch >> 26); \ > - *outptr++ = base64 ((ch >> 20) & 0x3f); \ > - *outptr++ = base64 ((ch >> 14) & 0x3f); \ > - *outptr++ = base64 ((ch >> 8) & 0x3f); \ > - *outptr++ = base64 ((ch >> 2) & 0x3f); \ > + *outptr++ = base64 (ch >> 26, var); \ > + *outptr++ = base64 ((ch >> 20) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 14) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 8) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 2) & 0x3f, var); \ > statep->__count = ((ch & 3) << 7) | (2 << 3); \ > break; \ > case 2: \ > *outptr++ = \ > - base64 (((statep->__count >> 3) & ~3) | (ch >> 28)); \ > - *outptr++ = base64 ((ch >> 22) & 0x3f); \ > - *outptr++ = base64 ((ch >> 16) & 0x3f); \ > - *outptr++ = base64 ((ch >> 10) & 0x3f); \ > - *outptr++ = base64 ((ch >> 4) & 0x3f); \ > + base64 (((statep->__count >> 3) & ~3) | (ch >> 28), \ > + var); \ > + *outptr++ = base64 ((ch >> 22) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 16) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 10) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 4) & 0x3f, var); \ > statep->__count = ((ch & 15) << 5) | (3 << 3); \ > break; \ > case 3: \ > *outptr++ = \ > - base64 (((statep->__count >> 3) & ~3) | (ch >> 30)); \ > - *outptr++ = base64 ((ch >> 24) & 0x3f); \ > - *outptr++ = base64 ((ch >> 18) & 0x3f); \ > - *outptr++ = base64 ((ch >> 12) & 0x3f); \ > - *outptr++ = base64 ((ch >> 6) & 0x3f); \ > - *outptr++ = base64 (ch & 0x3f); \ > + base64 (((statep->__count >> 3) & ~3) | (ch >> 30), \ > + var); \ > + *outptr++ = base64 ((ch >> 24) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 18) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 12) & 0x3f, var); \ > + *outptr++ = base64 ((ch >> 6) & 0x3f, var); \ > + *outptr++ = base64 (ch & 0x3f, var); \ > statep->__count = (1 << 3); \ > break; \ > default: \ > @@ -480,7 +590,7 @@ base64 (unsigned int i) > inptr += 4; \ > } > #define LOOP_NEED_FLAGS > -#define EXTRA_LOOP_DECLS , mbstate_t *statep > +#define EXTRA_LOOP_DECLS , mbstate_t *statep, enum variant var > #include > > > @@ -510,7 +620,7 @@ base64 (unsigned int i) > { \ > /* Write out the shift sequence. */ \ > if ((state & 0x18) >= 0x10) \ > - *outbuf++ = base64 ((state >> 3) & ~3); \ > + *outbuf++ = base64 ((state >> 3) & ~3, var); \ > *outbuf++ = '-'; \ > \ > data->__statep->__count = 0; \