From patchwork Wed Jul 11 14:00:59 2012 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alex Elder X-Patchwork-Id: 1182671 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 CD6BCDF25A for ; Wed, 11 Jul 2012 14:01:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1757783Ab2GKOBD (ORCPT ); Wed, 11 Jul 2012 10:01:03 -0400 Received: from mail-vb0-f46.google.com ([209.85.212.46]:57930 "EHLO mail-vb0-f46.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1757701Ab2GKOBC (ORCPT ); Wed, 11 Jul 2012 10:01:02 -0400 Received: by vbbff1 with SMTP id ff1so759843vbb.19 for ; Wed, 11 Jul 2012 07:01:01 -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=zTwTvoEo/pO8gubiDPVIE43k6atyL78qtouN+CWQ+mY=; b=mMaIMBjRuq9208NxxT9TQqJeNiiTnhCoF/kA3I9WADn2kYje/UdLRV1yzVx1Kh60nR +kTjOVWOQEtQap3kZ6+R2Wn44Id/yw7/MWTTk6XRoi7B/tmXreh9etxXN3EVAwawS57D LMR7QCwvDKEs/TTfLV9M6yoJ+APTiy293aI0MynqNHdihuhDkWn285PXpgzASKyFaTr7 gFyDi8dA8lHYMenDrUqnG8UHmtj7f9y31h9vraeXpddc/G2GlSH33HUCu5eI/Jg5iZg0 9u8k0EEltvlVLNv0Dor5d/Cs1z8N5DQs9Rpu8JABJDH4aI+Vw6z39lPWDv5JD162aDtR eDAw== Received: by 10.52.175.101 with SMTP id bz5mr19422530vdc.122.1342015261105; Wed, 11 Jul 2012 07:01:01 -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 cy18sm238882vdb.9.2012.07.11.07.01.00 (version=SSLv3 cipher=OTHER); Wed, 11 Jul 2012 07:01:00 -0700 (PDT) Message-ID: <4FFD871B.6020704@inktank.com> Date: Wed, 11 Jul 2012 09:00:59 -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 03/16] libceph: define ceph_decode_string() References: <4FFD847C.7070205@inktank.com> In-Reply-To: <4FFD847C.7070205@inktank.com> X-Gm-Message-State: ALoCoQmXLcdIEbktnUbgV3zAJCSWyAVLm0Uj1BUYJxkX6Uh+Xo1R13iQM2KpfOyn44B39TOi/RFj Sender: ceph-devel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: ceph-devel@vger.kernel.org There is no string decoding function defined in , so this defines one. This function is a little different from the others in that the length of the encoded string is not known a priori. So the interface is defined a bit like snprintf(), where the value returned indicates the space required--even if it's more than the space allotted. Signed-off-by: Alex Elder Reviewed-by: Yehuda Sadeh Reviewed-by: Josh Durgin --- include/linux/ceph/decode.h | 36 ++++++++++++++++++++++++++++++++++++ 1 files changed, 36 insertions(+), 0 deletions(-) static inline int ceph_has_room(void **p, void *end, size_t n) diff --git a/include/linux/ceph/decode.h b/include/linux/ceph/decode.h index bcbd66c..7ead11fc 100644 --- a/include/linux/ceph/decode.h +++ b/include/linux/ceph/decode.h @@ -44,6 +44,42 @@ static inline void ceph_decode_copy(void **p, void *pv, size_t n) } /* + * Decode the wire-encoded string at *p into the buffer "s" + * provided, whose size is indicated by "size". Note that "s" can + * be a null pointer if size is 0. If it fits, the resulting string + * will always be terminated with '\0'; otherwise the buffer will + * be unchanged. + * + * Returns the length of the encoded string (which may be greater + * than or equal to the buffer size). The return value does not + * include the terminating '\0'. + * + * If the the return value is less than the size provided, *p will + * be advanced past the decoded data; otherwise it is unchanged. + * This allows for a two call sequence to be used to allocate + * sufficient space for the string. + * + * NB It is assumed that *p refers to a block of valid memory + * sufficient to hold the length field followed by the number + * of bytes indicated by that field. + */ +static inline size_t ceph_decode_string(void **p, char *s, size_t size) +{ + size_t len; + + len = get_unaligned_le32(*p); + if (size < len + 1) + return len; + + if (len) + memcpy(s, (char *) *p + sizeof (u32), len); + *(s + len) = '\0'; + *p += sizeof (u32) + len; + + return len; +} + +/* * bounds check input. */