Re: [Cbor] Self Described CBOR - Little Endian

Thiago Macieira <thiago.macieira@intel.com> Thu, 25 March 2021 15:52 UTC

Return-Path: <thiago.macieira@intel.com>
X-Original-To: cbor@ietfa.amsl.com
Delivered-To: cbor@ietfa.amsl.com
Received: from localhost (localhost [127.0.0.1]) by ietfa.amsl.com (Postfix) with ESMTP id BEABF3A260B for <cbor@ietfa.amsl.com>; Thu, 25 Mar 2021 08:52:00 -0700 (PDT)
X-Virus-Scanned: amavisd-new at amsl.com
X-Spam-Flag: NO
X-Spam-Score: -2.654
X-Spam-Level:
X-Spam-Status: No, score=-2.654 tagged_above=-999 required=5 tests=[BAYES_00=-1.9, DATE_IN_PAST_06_12=1.543, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_NONE=0.001, URIBL_BLOCKED=0.001] autolearn=ham autolearn_force=no
Received: from mail.ietf.org ([4.31.198.44]) by localhost (ietfa.amsl.com [127.0.0.1]) (amavisd-new, port 10024) with ESMTP id 7a8kHxn_62uJ for <cbor@ietfa.amsl.com>; Thu, 25 Mar 2021 08:51:56 -0700 (PDT)
Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ietfa.amsl.com (Postfix) with ESMTPS id 74B3C3A2607 for <cbor@ietf.org>; Thu, 25 Mar 2021 08:51:56 -0700 (PDT)
IronPort-SDR: GMI1ELGpv6bnSguMFwTyXGAmI6Yod2wnKfsLF9V8zyAH+wd0PYb75uataLbQ1mR0nG2kdnSUtc x69AHqD8Wh7g==
X-IronPort-AV: E=McAfee;i="6000,8403,9934"; a="276081684"
X-IronPort-AV: E=Sophos;i="5.81,277,1610438400"; d="scan'208";a="276081684"
Received: from fmsmga007.fm.intel.com ([10.253.24.52]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 25 Mar 2021 08:51:55 -0700
IronPort-SDR: EfbFOpx0s5+NwkHtShMY40OCL/cn7elh3dEj7UxE/6LfOW6Yq0sGr5FZ7nmmAUS3NP6g9c2Hzf GRHjCpIiSNuw==
X-ExtLoop1: 1
X-IronPort-AV: E=Sophos;i="5.81,277,1610438400"; d="scan'208";a="382259155"
Received: from irsmsx605.ger.corp.intel.com ([163.33.146.138]) by fmsmga007.fm.intel.com with ESMTP; 25 Mar 2021 08:51:55 -0700
Received: from tjmaciei-mobl1.localnet (10.251.10.23) by IRSMSX605.ger.corp.intel.com (163.33.146.138) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2106.2; Thu, 25 Mar 2021 15:51:53 +0000
From: Thiago Macieira <thiago.macieira@intel.com>
To: <cbor@ietf.org>
Date: Wed, 24 Mar 2021 22:38:21 -0700
Message-ID: <2139632.RHzpeVkrSA@tjmaciei-mobl1>
Organization: Intel Corporation
In-Reply-To: <5513CABE-CDD5-411B-9FA9-8E1D631BB173@tzi.org>
References: <BY5PR20MB2898CBA491A4C0AED983C81CD2649@BY5PR20MB2898.namprd20.prod.outlook.com> <ED83658C-0602-4845-AD5B-9850F803BDD7@cursive.net> <5513CABE-CDD5-411B-9FA9-8E1D631BB173@tzi.org>
MIME-Version: 1.0
Content-Transfer-Encoding: quoted-printable
Content-Type: text/plain; charset="UTF-8"
X-Originating-IP: [10.251.10.23]
X-ClientProxiedBy: orsmsx603.amr.corp.intel.com (10.22.229.16) To IRSMSX605.ger.corp.intel.com (163.33.146.138)
Archived-At: <https://mailarchive.ietf.org/arch/msg/cbor/XGmF_7YXHMa-Ol10I2sn800BEzA>
Subject: Re: [Cbor] Self Described CBOR - Little Endian
X-BeenThere: cbor@ietf.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: "Concise Binary Object Representation \(CBOR\)" <cbor.ietf.org>
List-Unsubscribe: <https://www.ietf.org/mailman/options/cbor>, <mailto:cbor-request@ietf.org?subject=unsubscribe>
List-Archive: <https://mailarchive.ietf.org/arch/browse/cbor/>
List-Post: <mailto:cbor@ietf.org>
List-Help: <mailto:cbor-request@ietf.org?subject=help>
List-Subscribe: <https://www.ietf.org/mailman/listinfo/cbor>, <mailto:cbor-request@ietf.org?subject=subscribe>
X-List-Received-Date: Thu, 25 Mar 2021 15:52:01 -0000

On Wednesday, 24 March 2021 13:27:01 PDT Carsten Bormann wrote:
> But to answer the original question, CBOR is big-endian because IETF
> protocols generally are.  Creating a little-endian fork of CBOR wouldn’t
> really help that much.  (Turns out my (endian-independent) code is usually
> shorter for big-endian transfer than it is for little-endian — remember
> that you'll have to byte-shift the numbers anyway to maintain alignment on
> the decoded side, so it’s not that much more effort to arrange them.)  And
> it would hurt a lot; even with a magic number in front of it (see [1] for
> our current view of how magic numbers for CBOR-based file encodings should
> look like).

On the dual side, my encoder would have been shorter on little-endian.

Current:
https://github.com/intel/tinycbor/blob/master/src/cborencoder.c#L279-L306

This code writes the 64-bit incoming value and then tries to find the position 
and size of the relevant data.

In little-endian, you don't need to find the position, since it's fixed. You 
just need to calculate the size.

    uint64_t buf[2];
    uint8_t *bufstart = (uint8_t *)buf[1];
    unsigned len = 1;

    buf[1] = ui;
    if (ui >= Value8Bit) {
        uint8_t more = 0;
        if (ui > 0xffU)
            ++more;
        if (ui > 0xffffU)
            ++more;
        if (ui > 0xffffffffU)
            ++more;
        len = 1U + (1U << more);
       --bufstart;
        *bufstart = Value8Bit + more;
    }
    *bufstart += shiftedMajorType;
    return append_to_buffer(encoder, bufstart, len)

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering