Received: by 2002:a05:6902:102b:0:0:0:0 with SMTP id x11csp199231ybt; Thu, 18 Jun 2020 23:03:29 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxjibwimcu9viOp1JK4ggxaGarH4TmVFn6VVZ0wwbXmEGQqjez7gn0YJiILRcet814Ejtfe X-Received: by 2002:a17:906:68ca:: with SMTP id y10mr2079092ejr.322.1592546609136; Thu, 18 Jun 2020 23:03:29 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1592546609; cv=none; d=google.com; s=arc-20160816; b=Ar8NwR6i+Iid0cDXx9tEC3eXt9/oEjoto5fIBiyGGGAgoFsIJBqaEU80d5y1fIdGg1 okdru3fSDPZdGQ6EkzLLJTLQPbew6WQwtdmKfUaQEfDDOi/WP4HiLzNk666v8Z8ad5p2 G1nQtpIE92xAFviEdPhon+R/S7LvfwdxtuCoEkjuWgXpR8JqHagvAGhVebEocDRTRFSc 1/1hr13oufy+eP/CkVat9i1hRRBOFiCHUEAQAfsdCPgHjGAqr7PHMnbV0gHPTQZT3tMx LlntBgeoxM2uRfdFwHj9wvolbrzMLXcni3xSSMneg6QgexZQcxIx0Jvw7nSVZh7ncEGG OULA== 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=vt+KyJ7gnQ2ipb5XmncFBMFTX3WmHdNHW8gTrYDKunc=; b=I0Az5FAqjTdRrBC20OG3uA5lVr4stDCwGe0KhBkD8fMOtGc7ydCWxj/SL+rj3Knr3S eqMMMHDblQmz87xGWFzibvph7LATPql47AaeZbRzhqrThcOn5ucmnBZwtLfFscI3Y35U 3ambabOOBJ5fHplB0t8FiseXI3W9dxRwLkDQxN/yMikmfIqbRE4OUVLRn3hpYB797it1 4LnyRm4ilP/+HfIOc0mlBSWsH0HZye2e1WLqmZv+OP+a5eDpuM76Rm6xUiY2oMixbb6f +qTR+lLS4/4d7NTe1GaPw+N2ge4f9JvRtVULrBMU429a+Y1pmE4N4I9mX5Fch+lMOeMc La7Q== 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 u8si3251902edp.357.2020.06.18.23.02.43; Thu, 18 Jun 2020 23:03:29 -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 S1727114AbgFSGCR (ORCPT + 99 others); Fri, 19 Jun 2020 02:02:17 -0400 Received: from mga01.intel.com ([192.55.52.88]:62658 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726382AbgFSGCR (ORCPT ); Fri, 19 Jun 2020 02:02:17 -0400 IronPort-SDR: GKXkgBj8BqMtQ9z+ZLcTyXSC0LnfNbNh3twvHRShGUBanc/U1/3XhUss1AcZ0VOYLrHQlbTa3/ VxTGsCZnHrpg== X-IronPort-AV: E=McAfee;i="6000,8403,9656"; a="160959304" X-IronPort-AV: E=Sophos;i="5.75,253,1589266800"; d="scan'208";a="160959304" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Jun 2020 23:02:16 -0700 IronPort-SDR: ya9aubVcVbEmltdD1cyVhWk6pi0VBuGXcn6SpJbgKjFRHKUKXU3dJIygpxh5kz7S5e2uan5j3i KD3NPB8veiBQ== X-IronPort-AV: E=Sophos;i="5.75,253,1589266800"; d="scan'208";a="299948780" Received: from han1-mobl3.jf.intel.com ([10.255.228.38]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 18 Jun 2020 23:02:16 -0700 From: tedd.an@linux.intel.com To: linux-bluetooth@vger.kernel.org Cc: Tedd Ho-Jeong An Subject: [PATCH 1/2] midi: Fix SysEx parser in MIDI plugin Date: Thu, 18 Jun 2020 23:01:52 -0700 Message-Id: <20200619060153.65114-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 | 30 +++++++++++++++++++++++------- 1 file changed, 23 insertions(+), 7 deletions(-) diff --git a/profiles/midi/libmidi.c b/profiles/midi/libmidi.c index 4b4df799f..cc5f079e7 100644 --- a/profiles/midi/libmidi.c +++ b/profiles/midi/libmidi.c @@ -331,6 +331,24 @@ 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 && !(data[i++] & 0x80)); + + return (i < size && data[i] == 0xF7) ? i : 0; +} size_t midi_read_raw(struct midi_read_parser *parser, const uint8_t *data, @@ -368,14 +386,13 @@ 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); + /* Search for End of SysEx message in one BLE message */ + if ((sysex_length = sysex_eox_len(data + i, size - i)) > 0) { midi_size = handle_end_of_sysex(parser, ev, data + i, sysex_length); } else { @@ -424,10 +441,9 @@ 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); + if ((sysex_length = sysex_eox_len(data + i, size - i)) > 0) { midi_size = handle_end_of_sysex(parser, ev, data + i, sysex_length); } else { -- 2.25.4