From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <dcsapak@zita.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id E55F862E1F
 for <pbs-devel@lists.proxmox.com>; Tue, 14 Jul 2020 13:10:31 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id DC8DE26191
 for <pbs-devel@lists.proxmox.com>; Tue, 14 Jul 2020 13:10:01 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id A1EA12613C
 for <pbs-devel@lists.proxmox.com>; Tue, 14 Jul 2020 13:09:58 +0200 (CEST)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id 6D1224436E
 for <pbs-devel@lists.proxmox.com>; Tue, 14 Jul 2020 13:09:58 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Tue, 14 Jul 2020 13:09:54 +0200
Message-Id: <20200714110957.31884-7-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20200714110957.31884-1-d.csapak@proxmox.com>
References: <20200714110957.31884-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.000 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 KAM_LAZY_DOMAIN_SECURITY 1 Sending domain does not have any anti-forgery
 methods
 NO_DNS_FOR_FROM         0.379 Envelope sender has no MX or A DNS records
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_NONE                0.001 SPF: sender does not publish an SPF Record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [websocket.rs]
Subject: [pbs-devel] [PATCH proxmox 6/9] proxmox/tools/websocket: improve
 mask_bytes and create_frame interface
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Tue, 14 Jul 2020 11:10:31 -0000

by using a Box<[u8]> instead of a vector (we do not need it)

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 proxmox/src/tools/websocket.rs | 33 ++++++++++++++++-----------------
 1 file changed, 16 insertions(+), 17 deletions(-)

diff --git a/proxmox/src/tools/websocket.rs b/proxmox/src/tools/websocket.rs
index 078225a..cd09add 100644
--- a/proxmox/src/tools/websocket.rs
+++ b/proxmox/src/tools/websocket.rs
@@ -44,18 +44,17 @@ impl OpCode {
     }
 }
 
-fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Vec<u8>) -> &mut Vec<u8> {
+fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Box<[u8]>) {
     let mask = match mask {
-        Some([0,0,0,0]) | None => return data,
+        Some([0,0,0,0]) | None => return,
         Some(mask) => mask,
     };
 
     if data.len() < 32 {
-        let mut_data = data.as_mut_slice();
-        for i in 0..mut_data.len() {
-            mut_data[i] ^= mask[i%4];
+        for i in 0..data.len() {
+            data[i] ^= mask[i%4];
         }
-        return data;
+        return;
     }
 
     let mut newmask: u32 = u32::from_le_bytes(mask);
@@ -75,8 +74,6 @@ fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Vec<u8>) -> &mut Vec<u8> {
         *s ^= newmask as u8;
         newmask = newmask.rotate_right(8);
     }
-
-    data
 }
 
 /// Can be used to create a complete WebSocket Frame.
@@ -125,7 +122,7 @@ fn mask_bytes(mask: Option<[u8; 4]>, data: &mut Vec<u8>) -> &mut Vec<u8> {
 /// ```
 pub fn create_frame(
     mask: Option<[u8; 4]>,
-    mut data: Vec<u8>,
+    data: &[u8],
     frametype: OpCode,
 ) -> io::Result<Vec<u8>> {
     let first_byte = 0b10000000 | (frametype as u8);
@@ -155,8 +152,10 @@ pub fn create_frame(
     if let Some(mask) = mask {
         buf.extend_from_slice(&mask);
     }
+    let mut data = data.to_vec().into_boxed_slice();
+    mask_bytes(mask, &mut data);
 
-    buf.append(&mut mask_bytes(mask, &mut data));
+    buf.append(&mut data.into_vec());
     Ok(buf)
 }
 
@@ -212,7 +211,7 @@ impl<W: AsyncWrite + Unpin> AsyncWrite for WebSocketWriter<W> {
 
         if this.frame.is_none() {
             // create frame buf
-            let frame = match create_frame(this.mask, buf.to_vec(), frametype) {
+            let frame = match create_frame(this.mask, buf, frametype) {
                 Ok(f) => f,
                 Err(e) => {
                     return Poll::Ready(Err(e));
@@ -514,12 +513,11 @@ impl<R: AsyncReadExt + Unpin + Send + 'static> AsyncRead for WebSocketReader<R>
 
                     if header.is_control_frame() {
                         if read_buffer.len() >= header.payload_len {
+                            let mut data = read_buffer.remove_data(header.payload_len);
+                            mask_bytes(header.mask, &mut data);
                             (this.callback)(
                                 header.frametype,
-                                mask_bytes(
-                                    header.mask,
-                                    &mut read_buffer.remove_data(header.payload_len).into_vec(),
-                                ),
+                                &data,
                             );
                             this.state =  if read_buffer.is_empty() {
                                 ReaderState::NoData
@@ -538,8 +536,9 @@ impl<R: AsyncReadExt + Unpin + Send + 'static> AsyncRead for WebSocketReader<R>
 
                     let len = min(buf.len() - offset, min(header.payload_len, read_buffer.len()));
 
-                    let mut data = read_buffer.remove_data(len).into_vec();
-                    buf[offset..offset+len].copy_from_slice(mask_bytes(header.mask, &mut data));
+                    let mut data = read_buffer.remove_data(len);
+                    mask_bytes(header.mask, &mut data);
+                    buf[offset..offset+len].copy_from_slice(&data);
                     offset += len;
 
                     header.payload_len -= len;
-- 
2.20.1