Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp1007931ybt; Fri, 19 Jun 2020 21:26:01 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwoetw9f50260eKg5IDtyuhKz2T3dozK7Ex45UsETtrwqIPP+GZ/J29mmO9Fvci6AOiDLWg X-Received: by 2002:a17:906:e298:: with SMTP id gg24mr6468136ejb.120.1592627160924; Fri, 19 Jun 2020 21:26:00 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592627160; cv=none; d=google.com; s=arc-20160816; b=rp1l/qpEbuUyenm8nuUYJonrsRDq9EIDLGJJw8SWK7Lh7XZd/R8kPTnGN6C4wQvI28 OtAYYGCYqQUAbcNbzWKWSeSnU33J2FOBG2VABAvpOdTnfyyK2mje0oW3dDsPAZw3vEFR dSAONlYpSrOugfp4H5jsoLG+b65W26nFKpWPmwPeBjQp5bizv2QeLWFsgZODlIh/86iq Ng1vEpxQjuAMdZct9nD6TVdBoudCPvB2frDayZwyj7+L5Cu94ku56Fk2UxjW7olq2NID SQrQgPe8l3kMjFbvf6F+qD5x6AMhLrPrG3vv9+wDO7E9mAIZCu0S8oVuxIis1eZbBTEe mBCg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:ironport-sdr:ironport-sdr; bh=lqHm1BzM6AzFi2wL6MSueDojmM1Zdc770sKXgP8zbe8=; b=amwqrGjRa85pvH/b+wOdRZ3ncb6GU46zkcRHLzgcg0YzglzclG1NAskbCaZRiPtRTK ovW8HLX0ToV73T3Go7hbDFYmZPHDwGi2veNYORh6G3/NNWyMYcuEVMBWf5Q7rsUIsuNq uXiAgyoL6AnHvkDEqt13AJwp4HKbvuoLE/OyAYbFWO0zv3FcBnByr3NiMwcxi0+k66bS QYeyRQb+tfRfYdtiABRvyRIWJWHhUCcc36FR6+rAL4Pj9njBQjFagiOsdegyBvkg3UH4 KK6j5BXGUAqFQ92MX15ukzQ9wxsQHZNcgNuhTRT3JJN/eSFuUPav7BZzERePBfjKHpdL v7xw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id f6si5296102ejf.208.2020.06.19.21.25.37; Fri, 19 Jun 2020 21:26:00 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-bluetooth-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-bluetooth-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S2388047AbgFSSfK (ORCPT + 99 others); Fri, 19 Jun 2020 14:35:10 -0400 Received: from mga14.intel.com ([192.55.52.115]:3425 "EHLO mga14.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728430AbgFSSfI (ORCPT ); Fri, 19 Jun 2020 14:35:08 -0400 IronPort-SDR: hZ+sW2wz8Lbq7wWmiTPuZjdKUYnb6ZL/NNeGd0B5NUBCUuqO8hKE44i5HJbdVPYaoU5RjtiG6u OlnA75tpBuOA== X-IronPort-AV: E=McAfee;i="6000,8403,9657"; a="142134879" X-IronPort-AV: E=Sophos;i="5.75,256,1589266800"; d="scan'208";a="142134879" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga103.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jun 2020 11:35:06 -0700 IronPort-SDR: JL9w5/NIR8gTsVcoU1tM/ELSgkWZKzp54pNUQHxD6I9eU9QUyUB0E1DPizhTSfh7UCHE8N2OOm zlDwOIcp8neA== X-IronPort-AV: E=Sophos;i="5.75,256,1589266800"; d="scan'208";a="300155234" Received: from han1-mobl3.jf.intel.com ([10.254.73.18]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 19 Jun 2020 11:35:06 -0700 From: tedd.an@linux.intel.com To: linux-bluetooth@vger.kernel.org Cc: Tedd Ho-Jeong An Subject: [PATCH V2 1/2] midi: Fix SysEx parser in MIDI plugin Date: Fri, 19 Jun 2020 11:34:55 -0700 Message-Id: <20200619183456.38696-1-tedd.an@linux.intel.com> X-Mailer: git-send-email 2.25.4 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-bluetooth-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-bluetooth@vger.kernel.org From: Tedd Ho-Jeong An The SysEx end of message parser didn't consider the fact that timestampsLow are in the stream and it might have the EOX (F7) byte as well. Fix that by always assuming the first system message byte is the timestampLow. Future work would involve support other type of system message bytes, such as real-time. --- profiles/midi/libmidi.c | 38 +++++++++++++++++++++++++++++++------- 1 file changed, 31 insertions(+), 7 deletions(-) diff --git a/profiles/midi/libmidi.c b/profiles/midi/libmidi.c index 4b4df799f..7d57e7335 100644 --- a/profiles/midi/libmidi.c +++ b/profiles/midi/libmidi.c @@ -331,6 +331,30 @@ static size_t handle_end_of_sysex(struct midi_read_parser *parser, return sysex_length + 1; /* +1 because of timestampLow */ } +/* Searches the end of a SysEx message that contains a timestampLow + * before the SysEx end byte. Returns the number of bytes of valid + * SysEx payload in the buffer. + */ +static size_t sysex_eox_len(const uint8_t *data, size_t size) +{ + size_t i = 0; + + MIDI_ASSERT(size > 0); + + if (data[i] == 0xF0) + i++; + + /* Search for timestamp low */ + while (i < size) { + if ((data[i] & 0x80)) { + i++; + break; + } + i++; + } + + return (i < size && data[i] == 0xF7) ? i : 0; +} size_t midi_read_raw(struct midi_read_parser *parser, const uint8_t *data, @@ -368,14 +392,14 @@ size_t midi_read_raw(struct midi_read_parser *parser, const uint8_t *data, /* System Common Messages */ case 0xF0: /* SysEx Start */ { - uint8_t *pos; + size_t sysex_length; /* cleanup Running Status Message */ parser->rstatus = 0; - /* Avoid parsing if SysEx is contained in one BLE packet */ - if ((pos = memchr(data + i, 0xF7, size - i))) { - const size_t sysex_length = pos - (data + i); + sysex_length = sysex_eox_len(data + i, size - i); + /* Search for End of SysEx message in one BLE message */ + if (sysex_length > 0) { midi_size = handle_end_of_sysex(parser, ev, data + i, sysex_length); } else { @@ -424,10 +448,10 @@ size_t midi_read_raw(struct midi_read_parser *parser, const uint8_t *data, /* Check for SysEx messages */ if (parser->sysex_stream.len > 0) { - uint8_t *pos; + size_t sysex_length; - if ((pos = memchr(data + i, 0xF7, size - i))) { - const size_t sysex_length = pos - (data + i); + sysex_length = sysex_eox_len(data + i, size - i); + if (sysex_length > 0) { midi_size = handle_end_of_sysex(parser, ev, data + i, sysex_length); } else { -- 2.25.4