From patchwork Thu Jul 12 17:13:56 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Elder X-Patchwork-Id: 1190101 Return-Path: X-Original-To: patchwork-ceph-devel@patchwork.kernel.org Delivered-To: patchwork-process-083081@patchwork2.kernel.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by patchwork2.kernel.org (Postfix) with ESMTP id 8BD71E0004 for ; Thu, 12 Jul 2012 17:14:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1161406Ab2GLROB (ORCPT ); Thu, 12 Jul 2012 13:14:01 -0400 Received: from mail-yw0-f46.google.com ([209.85.213.46]:63975 "EHLO mail-yw0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1161399Ab2GLROA (ORCPT ); Thu, 12 Jul 2012 13:14:00 -0400 Received: by yhmm54 with SMTP id m54so2950631yhm.19 for ; Thu, 12 Jul 2012 10:13:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type:content-transfer-encoding :x-gm-message-state; bh=CBJViOriz7PqZpOrgUzyf3mwm/btenIRMiP+vtuxruw=; b=OCt2KipMCNV+4OC7ZHSAXwZNA5vGz4BEcqeH+KMqRBX2P3CB04iSf9y+B0GAtVAI3s 3CEtUrd0iGDdiJBnyDoBM5pwNkpFz8hkzX3xHSRJ2sl44kw4R0QbF2//CbzlWeM7wJV7 9bGTAQrjfMTK8Pj9y1EV0YPVP03muRMj2AV31XwKPjBr0XVAqTepByxD4bzmM1qd2nNH klZrWbUPp97iElRc7uio1FMNJUkA5dDPXxhpeb3tFDzEhBm7HQq+Haar1a0vR4d32P9G 8x40hbwVxyjXcbPI6fU0TiAYH495FYxjryRE9/JNwX8KKfkj72ovgHJcmC9Ysjp6NeLk CAAQ== Received: by 10.100.239.2 with SMTP id m2mr16116877anh.33.1342113239847; Thu, 12 Jul 2012 10:13:59 -0700 (PDT) Received: from [172.22.22.4] (c-71-195-31-37.hsd1.mn.comcast.net. [71.195.31.37]) by mx.google.com with ESMTPS id y66sm9773960yhi.10.2012.07.12.10.13.56 (version=SSLv3 cipher=OTHER); Thu, 12 Jul 2012 10:13:58 -0700 (PDT) Message-ID: <4FFF05D4.9080006@inktank.com> Date: Thu, 12 Jul 2012 12:13:56 -0500 From: Alex Elder User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:13.0) Gecko/20120615 Thunderbird/13.0.1 MIME-Version: 1.0 To: ceph-devel@vger.kernel.org Subject: [PATCH v3 04/16] libceph: define ceph_extract_encoded_string() References: <4FFD847C.7070205@inktank.com> <4FFD8727.7050106@inktank.com> <4FFDF9B8.5040401@inktank.com> In-Reply-To: <4FFDF9B8.5040401@inktank.com> X-Gm-Message-State: ALoCoQmpsop2KjYeP2Pn4nqJtwNm/9Vjd11+BrTo61Z9rjGNxufLJyvqTgc7m+/BuSTcwUXrj4S7 Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org This adds a new utility routine which will return a dynamically- allocated buffer containing a string that has been decoded from ceph over-the-wire format. It also returns the length of the string if the address of a size variable is supplied to receive it. Signed-off-by: Alex Elder --- v3: Final version. Sage convinced me that there was no need for ceph_decode_string() other than its use in this function, so now this implements what that function had been doing directly. include/linux/ceph/decode.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) -- To unsubscribe from this list: send the line "unsubscribe ceph-devel" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html Index: b/include/linux/ceph/decode.h =================================================================== --- a/include/linux/ceph/decode.h +++ b/include/linux/ceph/decode.h @@ -85,6 +85,50 @@ static inline int ceph_has_room(void **p } while (0) /* + * Allocate a buffer big enough to hold the wire-encoded string, and + * decode the string into it. The resulting string will always be + * terminated with '\0'. If successful, *p will be advanced + * past the decoded data. Also, if lenp is not a null pointer, the + * length (not including the terminating '\0') will be recorded in + * *lenp. Note that a zero-length string is a valid return value. + * + * Returns a pointer to the newly-allocated string buffer, or a null + * pointer if an error occurs. Neither *p nor *lenp will have been + * updated if NULL is returned. + * + * There are two possible failures: + * - converting the string would require accessing memory at or + * beyond the "end" pointer provided + * - memory could not be allocated for the result + */ +static inline char *ceph_extract_encoded_string(void **p, void *end, + size_t *lenp, gfp_t gfp) +{ + u32 len; + void *sp = *p; + char *buf = NULL; + + ceph_decode_32_safe(&sp, end, len, out); + if (!ceph_has_room(&sp, end, len)) + return NULL; + + buf = kmalloc(len + 1, gfp); + if (!buf) + return NULL; + + if (len) + memcpy(buf, sp, len); + buf[len] = '\0'; + + *p = (char *) *p + sizeof (u32) + len; + + if (lenp) + *lenp = (size_t) len; +out: + return buf; +} + +/* * struct ceph_timespec <-> struct timespec */ static inline void ceph_decode_timespec(struct timespec *ts,