Return-Path: X-Original-To: notmuch@notmuchmail.org Delivered-To: notmuch@notmuchmail.org Received: from localhost (localhost [127.0.0.1]) by olra.theworths.org (Postfix) with ESMTP id AC887421169 for ; Sat, 31 Mar 2012 15:17:49 -0700 (PDT) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Amavis-Alert: BAD HEADER SECTION, Duplicate header field: "References" X-Spam-Flag: NO X-Spam-Score: -0.7 X-Spam-Level: X-Spam-Status: No, score=-0.7 tagged_above=-999 required=5 tests=[RCVD_IN_DNSWL_LOW=-0.7] autolearn=disabled Received: from olra.theworths.org ([127.0.0.1]) by localhost (olra.theworths.org [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id lCBKfDZhDz74 for ; Sat, 31 Mar 2012 15:17:48 -0700 (PDT) Received: from mail-bk0-f53.google.com (mail-bk0-f53.google.com [209.85.214.53]) (using TLSv1 with cipher RC4-SHA (128/128 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id E5395421165 for ; Sat, 31 Mar 2012 15:17:47 -0700 (PDT) Received: by mail-bk0-f53.google.com with SMTP id j4so1547751bkw.26 for ; Sat, 31 Mar 2012 15:17:47 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=from:to:cc:subject:date:message-id:x-mailer:in-reply-to:references :in-reply-to:references:x-gm-message-state; bh=916Fk5v5PJ7EkIwyO9dergWO6FXSYYcbLohwghi2j7o=; b=gr08jZLjNwP2siIiZVdKcMzMGtroTxxqgyZsHmsRP5TwpUvVtwBE2kT+qpuocPxqn0 UveIwHSRIy6Gr9q6ssLn8FoVj1Jk4mQRXP7Rbe6XsIGlDfoiPkN5hSt4CA6iNB6C52Sj MYp8SoH6xByzu1ImUdhKJ7LGH19zTa3h6RBKiAm4Qa9NUFN6i9HVYNu2nvgqPqSdLPf5 xblO3mI+bT6Q9NC3dWh6V6EYP8js+OjCjuHhhwZwReNFPm+xooRdP2QzLG2kxUXZeO2q 77HLAnPmZBTTgvt0KWn+6JOC2AULG8qpUTL+axUa+hboV0HLu3XlF3/MvnkGHJLYCRwD 5Mgg== Received: by 10.204.154.139 with SMTP id o11mr1387845bkw.4.1333232267488; Sat, 31 Mar 2012 15:17:47 -0700 (PDT) Received: from localhost (dsl-hkibrasgw4-fe50f800-253.dhcp.inet.fi. [84.248.80.253]) by mx.google.com with ESMTPS id r8sm8938072bki.2.2012.03.31.15.17.45 (version=SSLv3 cipher=OTHER); Sat, 31 Mar 2012 15:17:46 -0700 (PDT) From: Jani Nikula To: notmuch@notmuchmail.org Subject: [PATCH 3/8] hex-escape: add function to decode escaped string in-place Date: Sun, 1 Apr 2012 01:17:23 +0300 Message-Id: <026815054d3e01e6f29c0834d43ccabdf6eda481.1333231401.git.jani@nikula.org> X-Mailer: git-send-email 1.7.5.4 In-Reply-To: References: In-Reply-To: References: X-Gm-Message-State: ALoCoQnI9qqQyUZDdWkPmV/b3Yb/QSpGHHob+38RirpGYe7zR67zK8ucrtOzU62C7CwxuaoxC9xb X-BeenThere: notmuch@notmuchmail.org X-Mailman-Version: 2.1.13 Precedence: list List-Id: "Use and development of the notmuch mail system." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sat, 31 Mar 2012 22:17:49 -0000 Add function hex_decode_inplace() to decode the input string onto itself. Signed-off-by: Jani Nikula --- This could be folded to "hex-escape: (en|de)code strings to/from restricted character set". --- util/hex-escape.c | 62 ++++++++++++++++++++++++++++++---------------------- util/hex-escape.h | 6 +++++ 2 files changed, 42 insertions(+), 26 deletions(-) diff --git a/util/hex-escape.c b/util/hex-escape.c index 9de79df..e794f98 100644 --- a/util/hex-escape.c +++ b/util/hex-escape.c @@ -98,38 +98,15 @@ hex_encode (void *ctx, const char *in, char **out, size_t *out_size) return HEX_SUCCESS; } -hex_status_t -hex_decode (void *ctx, const char *in, char **out, size_t * out_size) +/* Note: This must succeed for p == q to support hex_decode_inplace(). */ +static hex_status_t +hex_decode_internal (const char *p, unsigned char *q) { - char buf[3]; - - const char *p; - unsigned char *q; - - size_t escape_count = 0; - size_t needed = 0; - - assert (ctx); assert (in); assert (out); assert (out_size); - - size_t len = strlen (in); - - for (p = in; *p; p++) - escape_count += (*p == escape_char); - - needed = len - escape_count * 2 + 1; - - if (!maybe_realloc (ctx, needed, out, out_size)) - return HEX_OUT_OF_MEMORY; - - p = in; - q = (unsigned char *) *out; buf[2] = 0; while (*p) { - if (*p == escape_char) { - char *endp; if (!isxdigit ((unsigned char) p[1]) || @@ -155,3 +132,36 @@ hex_decode (void *ctx, const char *in, char **out, size_t * out_size) return HEX_SUCCESS; } + +hex_status_t +hex_decode_inplace (char *p) +{ + return hex_decode_internal (p, (unsigned char *) p); +} + +hex_status_t +hex_decode (void *ctx, const char *in, char **out, size_t * out_size) +{ + const char *p; + unsigned char *q; + + size_t escape_count = 0; + size_t needed = 0; + + assert (ctx); assert (in); assert (out); assert (out_size); + + size_t len = strlen (in); + + for (p = in; *p; p++) + escape_count += (*p == escape_char); + + needed = len - escape_count * 2 + 1; + + if (!maybe_realloc (ctx, needed, out, out_size)) + return HEX_OUT_OF_MEMORY; + + p = in; + q = (unsigned char *) *out; + + return hex_decode_internal (p, q); +} diff --git a/util/hex-escape.h b/util/hex-escape.h index e409626..be70ad2 100644 --- a/util/hex-escape.h +++ b/util/hex-escape.h @@ -29,4 +29,10 @@ hex_encode (void *talloc_ctx, const char *in, char **out, hex_status_t hex_decode (void *talloc_ctx, const char *in, char **out, size_t *out_size); + +/* + * Decode 'in' onto itself. + */ +hex_status_t +hex_decode_inplace (char *in); #endif -- 1.7.5.4