Linux bluetooth development
 help / color / mirror / Atom feed
From: Szymon Janc <szymon.janc@tieto.com>
To: linux-bluetooth@vger.kernel.org
Cc: Szymon Janc <szymon.janc@tieto.com>
Subject: [PATCH 3/4] android/hidhost: Don't use sscanf in hex2buf
Date: Fri, 18 Apr 2014 15:40:05 +0200	[thread overview]
Message-ID: <1397828406-29951-3-git-send-email-szymon.janc@tieto.com> (raw)
In-Reply-To: <1397828406-29951-1-git-send-email-szymon.janc@tieto.com>

Add cheaper version (based on similar code in oFono) that doesn't
require sscanf.
---
 android/hidhost.c | 34 ++++++++++++++++++++++++++++++----
 1 file changed, 30 insertions(+), 4 deletions(-)

diff --git a/android/hidhost.c b/android/hidhost.c
index ea83733..ab7f7a8 100644
--- a/android/hidhost.c
+++ b/android/hidhost.c
@@ -30,6 +30,7 @@
 #include <errno.h>
 #include <unistd.h>
 #include <fcntl.h>
+#include <ctype.h>
 
 #include <glib.h>
 
@@ -162,12 +163,37 @@ static void hid_device_remove(struct hid_device *dev)
 	hid_device_free(dev);
 }
 
-static void hex2buf(const uint8_t *hex, uint8_t *buf, int num)
+static bool hex2buf(const uint8_t *hex, uint8_t *buf, int buf_size)
 {
-	int i;
+	int i, j;
+	char c;
+	uint8_t b;
 
-	for (i = 0; i < num; i++)
-		sscanf((const char *)(hex + (i * 2)), "%02hhX", &buf[i]);
+	for (i = 0, j = 0; i < buf_size; i++, j++) {
+		c = toupper(hex[j]);
+
+		if (c >= '0' && c <= '9')
+			b = c - '0';
+		else if (c >= 'A' && c <= 'F')
+			b = 10 + c - 'A';
+		else
+			return false;
+
+		j++;
+
+		c = toupper(hex[j]);
+
+		if (c >= '0' && c <= '9')
+			b = b * 16 + c - '0';
+		else if (c >= 'A' && c <= 'F')
+			b = b * 16 + 10 + c - 'A';
+		else
+			return false;
+
+		buf[i] = b;
+	}
+
+	return true;
 }
 
 static void handle_uhid_output(struct hid_device *dev,
-- 
1.9.1


  parent reply	other threads:[~2014-04-18 13:40 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-04-18 13:40 [PATCH 1/4] android/hidhost: Fix handle_uhid_output Szymon Janc
2014-04-18 13:40 ` [PATCH 2/4] android/hidhost: Cleanup handle_uhid_output Szymon Janc
2014-04-18 13:40 ` Szymon Janc [this message]
2014-04-18 13:40 ` [PATCH 4/4] android/hidhost: Check if hex2buf succed Szymon Janc
2014-04-23  8:13 ` [PATCH 1/4] android/hidhost: Fix handle_uhid_output Szymon Janc

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1397828406-29951-3-git-send-email-szymon.janc@tieto.com \
    --to=szymon.janc@tieto.com \
    --cc=linux-bluetooth@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox