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 A2202431FC9 for ; Fri, 14 Dec 2012 05:34:55 -0800 (PST) X-Virus-Scanned: Debian amavisd-new at olra.theworths.org X-Spam-Flag: NO X-Spam-Score: 0 X-Spam-Level: X-Spam-Status: No, score=0 tagged_above=-999 required=5 tests=[none] 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 SBHk3L9TygaG for ; Fri, 14 Dec 2012 05:34:54 -0800 (PST) Received: from tesseract.cs.unb.ca (tesseract.cs.unb.ca [131.202.240.238]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by olra.theworths.org (Postfix) with ESMTPS id 8482F431FD2 for ; Fri, 14 Dec 2012 05:34:52 -0800 (PST) Received: from fctnnbsc30w-142167090129.dhcp-dynamic.fibreop.nb.bellaliant.net ([142.167.90.129] helo=zancas.localnet) by tesseract.cs.unb.ca with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) (Exim 4.72) (envelope-from ) id 1TjVPf-0004yk-KX; Fri, 14 Dec 2012 09:34:52 -0400 Received: from bremner by zancas.localnet with local (Exim 4.80) (envelope-from ) id 1TjVPa-00022D-42; Fri, 14 Dec 2012 09:34:46 -0400 From: david@tethera.net To: notmuch@notmuchmail.org Subject: [Patch v7 05/14] quote_and_decode_query: new function to quote hex-decoded queries Date: Fri, 14 Dec 2012 09:34:13 -0400 Message-Id: <1355492062-7546-6-git-send-email-david@tethera.net> X-Mailer: git-send-email 1.7.10.4 In-Reply-To: <1355492062-7546-1-git-send-email-david@tethera.net> References: <1355492062-7546-1-git-send-email-david@tethera.net> X-Spam_bar: - Cc: David Bremner 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: Fri, 14 Dec 2012 13:34:56 -0000 From: David Bremner The query is split into tokens, with ' ' and ':' as delimiters. Any token containing some hex-escaped character is quoted according to Xapian rules. This maps id:foo%20%22bar to id:"foo ""bar". This intentionally does not quote prefixes, so they still work as prefixes. --- tag-util.c | 50 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) diff --git a/tag-util.c b/tag-util.c index f89669a..e1181f8 100644 --- a/tag-util.c +++ b/tag-util.c @@ -56,6 +56,56 @@ illegal_tag (const char *tag, notmuch_bool_t remove) return NULL; } +static tag_parse_status_t +quote_and_decode_query (void *ctx, char *encoded, const char *line_for_error, + char **query_string) +{ + char *tok = encoded; + size_t tok_len = 0; + char *buf = NULL; + size_t buf_len = 0; + tag_parse_status_t ret = TAG_PARSE_SUCCESS; + + *query_string = talloc_strdup (ctx, ""); + + while (*query_string && + (tok = strtok_len (tok + tok_len, ": ", &tok_len)) != NULL) { + char delim = tok[tok_len]; + + *(tok + tok_len++) = '\0'; + + if (strcspn (tok, "%") < tok_len - 1) { + /* something to decode */ + if (hex_decode_inplace (tok) != HEX_SUCCESS) { + ret = line_error (TAG_PARSE_INVALID, line_for_error, + "hex decoding of token '%s' failed", tok); + goto DONE; + } + + if (double_quote_str (ctx, tok, &buf, &buf_len)) { + ret = line_error (TAG_PARSE_OUT_OF_MEMORY, + line_for_error, "aborting"); + goto DONE; + } + *query_string = talloc_asprintf_append_buffer ( + *query_string, "%s%c", buf, delim); + + } else { + /* This is not just an optimization, but used to preserve + * prefixes like id:, which cannot be quoted. + */ + *query_string = talloc_asprintf_append_buffer ( + *query_string, "%s%c", tok, delim); + } + + } + + DONE: + if (ret != TAG_PARSE_SUCCESS && *query_string) + talloc_free (*query_string); + return ret; +} + tag_parse_status_t parse_tag_line (void *ctx, char *line, tag_op_flag_t flags, -- 1.7.10.4