From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Kangas Newsgroups: gmane.emacs.bugs Subject: bug#58669: 28.2; bindat: str and strz not operating on vectors Date: Thu, 20 Oct 2022 13:57:46 -0700 Message-ID: References: <87tu3ymm51.fsf@cern.ch> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="26694"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Stefan Monnier To: Nacho Barrientos , 58669@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Thu Oct 20 23:03:50 2022 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1olchh-0006ew-V2 for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 20 Oct 2022 23:03:50 +0200 Original-Received: from localhost ([::1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olchg-0005jN-Or for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 20 Oct 2022 17:03:48 -0400 Original-Received: from [::1] (helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1olcc8-0002Op-PS for geb-bug-gnu-emacs@m.gmane-mx.org; Thu, 20 Oct 2022 16:58:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1olcc6-0002OP-Q0 for bug-gnu-emacs@gnu.org; Thu, 20 Oct 2022 16:58:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1olcc6-0006TE-Il for bug-gnu-emacs@gnu.org; Thu, 20 Oct 2022 16:58:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1olcc6-0005oL-5g for bug-gnu-emacs@gnu.org; Thu, 20 Oct 2022 16:58:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Kangas Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Thu, 20 Oct 2022 20:58:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 58669 X-GNU-PR-Package: emacs Original-Received: via spool by 58669-submit@debbugs.gnu.org id=B58669.166629947522325 (code B ref 58669); Thu, 20 Oct 2022 20:58:02 +0000 Original-Received: (at 58669) by debbugs.gnu.org; 20 Oct 2022 20:57:55 +0000 Original-Received: from localhost ([127.0.0.1]:36257 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1olcby-0005o0-T1 for submit@debbugs.gnu.org; Thu, 20 Oct 2022 16:57:55 -0400 Original-Received: from mail-ot1-f49.google.com ([209.85.210.49]:35718) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1olcbx-0005no-23 for 58669@debbugs.gnu.org; Thu, 20 Oct 2022 16:57:53 -0400 Original-Received: by mail-ot1-f49.google.com with SMTP id cy15-20020a056830698f00b0065c530585afso643717otb.2 for <58669@debbugs.gnu.org>; Thu, 20 Oct 2022 13:57:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date :mime-version:references:in-reply-to:from:from:to:cc:subject:date :message-id:reply-to; bh=ob+qa8r/hvIK0eEIinNmplvoCSwZiMuA7wATIqzRvxA=; b=WuwMrzubdXejDX174gIk3za5SbwVvP3OLgs77cWJReXqfTKyY7mP68db07cyvbEQu2 YUq8dkQsNX9aL+TcUomgrVFieiSVJSfwnOhTg7EY/in/cpYLI5YWZJndlO/yE9ifVdrh U3WTeqM7lszT58RqrHyjSy851pPkz+ihYDC1WxW9rbTNh3x9AkxAOM+4pe8jFCZvADpu VqWF5Q9KlLq/LcgQxY+8lNzdEwhxGNUVBTYSKcz7ZWxt05UHSgTARtkmkNkXltkzm70v Nj2csFGUBhopDg1r/Q6XoJ1HHm8nq+jM0FYtaSE5ankhrj8I/KLhDCv9RcnKwUd2bkPM r+pA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=content-transfer-encoding:cc:to:subject:message-id:date :mime-version:references:in-reply-to:from:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=ob+qa8r/hvIK0eEIinNmplvoCSwZiMuA7wATIqzRvxA=; b=dXjVvhIRwtnWrCEFOiCEAgvX3ynsSYJlC6CVOFZ54xKhUZa9nOrEpAC5p38G5v/V07 D8m/8qJ0Psc/KTiiIzmDz7UhETZQaIqpiFxL5ry5qfHhZL+7nb3j+R1UbG3lFqWCM7If aaM3aXKYw6TLJMA4mVYG+nINrPo83hh2M2ecdo1RWQZOUNLE0ekEZ4y4nt1wcP7hpchv 65qEW5Y7bgf4S0w+MdX458AtEsko1sPLYZ/c3LpZrewgTszNi61Oa3U5CdfFHSb68eVT yISk6GRshuaz+ewimkAp7raVY6KRQ6pZTITtCdmhoCZqw+E5Zh8ppVxpunfcw5XVlaGU B4zQ== X-Gm-Message-State: ACrzQf2eIJHouK5bnEZWxPqJoUi+leX6S8Cw9dbCPGpj+3te1cZaD0Qs KgjtFVv3321xkVMgfNkA6uSYvYrp1/0fZvXaTMw= X-Google-Smtp-Source: AMsMyM63WE4HGukaYB2phq0xEWwJ53d7GDmxErP/GVyChMybtXqGV6sHZghEiye2l+prFLcNP+feSvA+rbHi9PoTGuY= X-Received: by 2002:a05:6830:3c1:b0:637:28be:7920 with SMTP id p1-20020a05683003c100b0063728be7920mr7815491otc.105.1666299467207; Thu, 20 Oct 2022 13:57:47 -0700 (PDT) Original-Received: from 753933720722 named unknown by gmailapi.google.com with HTTPREST; Thu, 20 Oct 2022 13:57:46 -0700 In-Reply-To: <87tu3ymm51.fsf@cern.ch> X-Hashcash: 1:20:221020:monnier@iro.umontreal.ca::1IVnaspq0PJLuLYb:0F/E X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:245952 Archived-At: Nacho Barrientos writes: > These two snippets: > > (let* ((type (bindat-type > (data strz))) > (data [#x4e #x6f #x6e #x65 #x00]) > (unpacked (bindat-unpack type data))) > (bindat-get-field unpacked 'data)) > > (let* ((type (bindat-type > (data str 4))) > (data [#x4e #x6f #x6e #x65]) > (unpacked (bindat-unpack type data))) > (bindat-get-field unpacked 'data)) > > Both fail similarly: Copying in Stefan Monnier, in case he has any comments. > Debugger entered--Lisp error: (wrong-type-argument listp [78 111 110 101]= ) > apply(unibyte-string [78 111 110 101]) > (if (stringp s) s (apply #'unibyte-string s)) > (let ((i 0) s) (while (and (if len (< i len) t) (/=3D (aref bindat-raw = (+ bindat-idx i)) 0)) (setq i (1+ i))) (setq s (substring bindat-raw bindat= -idx (+ bindat-idx i))) (setq bindat-idx (+ bindat-idx (or len (1+ i)))) (i= f (stringp s) s (apply #'unibyte-string s))) > bindat--unpack-strz(nil) > [...] > > Debugger entered--Lisp error: (wrong-type-argument listp [78 111 110 101]= ) > apply(unibyte-string [78 111 110 101]) > (if (stringp s) s (apply #'unibyte-string s)) > (let ((s (substring bindat-raw bindat-idx (+ bindat-idx len)))) (setq b= indat-idx (+ bindat-idx len)) (if (stringp s) s (apply #'unibyte-string s))= ) > bindat--unpack-str(4) > [...] > > With the attached patch both return the string "None" as expected (well, > as I'd expect, *grins*). The diff also adds some extra tests. > > I'm running 28.2 but I've declared both functions as they're in the > current master and the patch is on top of the current master, too. > > It's the first time that I look into this package so I'm not sure this > is the way it's meant to work but similar operations work fine on > vectors of bytes, for instance: > > (let* ((type (bindat-type > (first uint 16) > (second uint 16))) > (data [#x00 #xff #xff #x00]) > (unpacked (bindat-unpack type data))) > (list (bindat-get-field unpacked 'first) > (bindat-get-field unpacked 'second) > (bindat-length type unpacked))) > > (which returns (255 65280 4) as expected) > > Please simply ignore this if I'm talking rubbish :) > > Hope this helps. > > From a8831aadf57f39862560b37d507be2a3bcb99aa2 Mon Sep 17 00:00:00 2001 > From: Nacho Barrientos > Date: Thu, 20 Oct 2022 14:16:43 +0200 > Subject: [PATCH] bindat (src, strz): operate on vectors too > > --- > lisp/emacs-lisp/bindat.el | 4 ++-- > test/lisp/emacs-lisp/bindat-tests.el | 19 ++++++++++++++++++- > 2 files changed, 20 insertions(+), 3 deletions(-) > > diff --git a/lisp/emacs-lisp/bindat.el b/lisp/emacs-lisp/bindat.el > index 0ecac3d52a..83188b369e 100644 > --- a/lisp/emacs-lisp/bindat.el > +++ b/lisp/emacs-lisp/bindat.el > @@ -163,7 +163,7 @@ bindat--unpack-str > (let ((s (substring bindat-raw bindat-idx (+ bindat-idx len)))) > (setq bindat-idx (+ bindat-idx len)) > (if (stringp s) s > - (apply #'unibyte-string s)))) > + (apply #'unibyte-string (append s nil))))) > > (defun bindat--unpack-strz (&optional len) > (let ((i 0) s) > @@ -172,7 +172,7 @@ bindat--unpack-strz > (setq s (substring bindat-raw bindat-idx (+ bindat-idx i))) > (setq bindat-idx (+ bindat-idx (or len (1+ i)))) > (if (stringp s) s > - (apply #'unibyte-string s)))) > + (apply #'unibyte-string (append s nil))))) > > (defun bindat--unpack-bits (len) > (let ((bits nil) (bnum (1- (* 8 len))) j m) > diff --git a/test/lisp/emacs-lisp/bindat-tests.el b/test/lisp/emacs-lisp/= bindat-tests.el > index 0c03c51e2e..2abf714852 100644 > --- a/test/lisp/emacs-lisp/bindat-tests.el > +++ b/test/lisp/emacs-lisp/bindat-tests.el > @@ -252,7 +252,24 @@ bindat-test--str-strz-multibyte > (should (equal (bindat-unpack spec "abc\0") "abc")) > ;; Missing null terminator. > (should-error (bindat-unpack spec "")) > - (should-error (bindat-unpack spec "a")))) > + (should-error (bindat-unpack spec "a"))) > + > + (ert-deftest bindat-test--strz-array-unpack () > + (should (equal (bindat-unpack spec [#x61 #x62 #x63 #x00]) "abc")))) > + > +(let ((spec (bindat-type str 3))) > + (ert-deftest bindat-test--str-simple-array-unpack () > + (should (equal (bindat-unpack spec [#x61 #x62 #x63]) "abc")))) > + > +(let ((spec (bindat-type > + (first u8) > + (string str 3) > + (last uint 16)))) > + (ert-deftest bindat-test--str-combined-array-unpack () > + (let ((unpacked (bindat-unpack spec [#xff #x63 #x62 #x61 #xff #xff])= )) > + (should (equal (bindat-get-field unpacked 'string) "cba")) > + (should (equal (bindat-get-field unpacked 'first) (- (expt 2 8) 1)= )) > + (should (equal (bindat-get-field unpacked 'last) (- (expt 2 16) 1)= ))))) > > (let ((spec '((x strz 2)))) > (ert-deftest bindat-test--strz-legacy-fixedlen-len () > -- > 2.38.0