Blame SOURCES/0005-lsusb-Split-out-routine-that-fetches-value-for-given.patch

d2dc68
From e497fcd9d69f2ba6b8672e430ee273db4afbea17 Mon Sep 17 00:00:00 2001
d2dc68
From: Michael Drake <michael.drake@codethink.co.uk>
d2dc68
Date: Thu, 7 Jun 2018 11:01:37 +0100
d2dc68
Subject: [PATCH 5/9] lsusb: Split out routine that fetches value for given
d2dc68
 field.
d2dc68
d2dc68
Field value lookup was done in multiple places, so it's useful
d2dc68
as a helper function.
d2dc68
d2dc68
Signed-off-by: Michael Drake <michael.drake@codethink.co.uk>
d2dc68
---
d2dc68
 desc-dump.c | 106 +++++++++++++++++++++++++++++++++---------------------------
d2dc68
 1 file changed, 58 insertions(+), 48 deletions(-)
d2dc68
d2dc68
diff --git a/desc-dump.c b/desc-dump.c
d2dc68
index 9210df7..393ff70 100644
d2dc68
--- a/desc-dump.c
d2dc68
+++ b/desc-dump.c
d2dc68
@@ -2,7 +2,7 @@
d2dc68
 /*
d2dc68
  * USB descriptor dumping
d2dc68
  *
d2dc68
- * Copyright (C) 2017 Michael Drake <michael.drake@codethink.co.uk>
d2dc68
+ * Copyright (C) 2017-2018 Michael Drake <michael.drake@codethink.co.uk>
d2dc68
  */
d2dc68
 
d2dc68
 #include "config.h"
d2dc68
@@ -109,6 +109,57 @@ static unsigned long long get_n_bytes_as_ull(
d2dc68
 	return ret;
d2dc68
 }
d2dc68
 
d2dc68
+/**
d2dc68
+ * Get the size of a descriptor field in bytes.
d2dc68
+ *
d2dc68
+ * Normally the size is provided in the entry's size parameter, but some
d2dc68
+ * fields have a variable size, with the actual size being stored in as
d2dc68
+ * the value of another field.
d2dc68
+ *
d2dc68
+ * \param[in] buf    Descriptor data.
d2dc68
+ * \param[in] desc   First field in the descriptor definition array.
d2dc68
+ * \param[in] entry  The descriptor definition field to get size for.
d2dc68
+ * \return Size of the field in bytes.
d2dc68
+ */
d2dc68
+static unsigned int get_entry_size(
d2dc68
+		const unsigned char *buf,
d2dc68
+		const struct desc *desc,
d2dc68
+		const struct desc *entry);
d2dc68
+
d2dc68
+/**
d2dc68
+ * Read a value from a field of given name.
d2dc68
+ *
d2dc68
+ * \param[in] buf    Descriptor data.
d2dc68
+ * \param[in] desc   First field in the descriptor definition array.
d2dc68
+ * \param[in] field  The name of the field to get the value for.
d2dc68
+ * \return The value from the given field.
d2dc68
+ */
d2dc68
+static unsigned long long get_value_from_field(
d2dc68
+		const unsigned char *buf,
d2dc68
+		const struct desc *desc,
d2dc68
+		const char *field)
d2dc68
+{
d2dc68
+	size_t offset = 0;
d2dc68
+	const struct desc *current;
d2dc68
+	unsigned long long value = 0;
d2dc68
+
d2dc68
+	/* Search descriptor definition array for the field who's value
d2dc68
+	 * gives the value of the entry we're interested in. */
d2dc68
+	for (current = desc; current->field != NULL; current++) {
d2dc68
+		if (strcmp(current->field, field) == 0) {
d2dc68
+			value = get_n_bytes_as_ull(buf, offset,
d2dc68
+					current->size);
d2dc68
+			break;
d2dc68
+		}
d2dc68
+
d2dc68
+		/* Keep track of our offset in the descriptor data
d2dc68
+		 * as we look for the field we want. */
d2dc68
+		offset += get_entry_size(buf, desc, current);
d2dc68
+	}
d2dc68
+
d2dc68
+	return value;
d2dc68
+}
d2dc68
+
d2dc68
 /**
d2dc68
  * Dump a number as hex to stdout.
d2dc68
  *
d2dc68
@@ -270,18 +321,7 @@ static void value_renderer(
d2dc68
 	}
d2dc68
 }
d2dc68
 
d2dc68
-/**
d2dc68
- * Get the size of a descriptor field in bytes.
d2dc68
- *
d2dc68
- * Normally the size is provided in the entry's size parameter, but some
d2dc68
- * fields have a variable size, with the actual size being stored in as
d2dc68
- * the value of another field.
d2dc68
- *
d2dc68
- * \param[in] buf    Descriptor data.
d2dc68
- * \param[in] desc   First field in the descriptor definition array.
d2dc68
- * \param[in] entry  The descriptor definition field to get size for.
d2dc68
- * \return Size of the field in bytes.
d2dc68
- */
d2dc68
+/* Documented at forward declaration above. */
d2dc68
 static unsigned int get_entry_size(
d2dc68
 		const unsigned char *buf,
d2dc68
 		const struct desc *desc,
d2dc68
@@ -292,24 +332,7 @@ static unsigned int get_entry_size(
d2dc68
 
d2dc68
 	if (entry->size_field != NULL) {
d2dc68
 		/* Variable field length, given by `size_field`'s value. */
d2dc68
-		size_t offset = 0;
d2dc68
-
d2dc68
-		/* Search descriptor definition array for the field who's value
d2dc68
-		 * gives the size of the entry we're interested in. */
d2dc68
-		for (current = desc; current->field != NULL; current++) {
d2dc68
-			if (strcmp(current->field, entry->size_field) == 0) {
d2dc68
-				/* Found the field who's value gives us the
d2dc68
-				 * size of, so read that field's value out of
d2dc68
-				 * the descriptor data buffer. */
d2dc68
-				size = get_n_bytes_as_ull(buf, offset,
d2dc68
-						current->size);
d2dc68
-				break;
d2dc68
-			}
d2dc68
-
d2dc68
-			/* Keep track of our offset in the descriptor data
d2dc68
-			 * as we look for the field we want. */
d2dc68
-			offset += get_entry_size(buf, desc, current);
d2dc68
-		}
d2dc68
+		size = get_value_from_field(buf, desc, entry->size_field);
d2dc68
 	}
d2dc68
 
d2dc68
 	if (size == 0) {
d2dc68
@@ -347,27 +370,14 @@ static unsigned int get_array_entry_count(
d2dc68
 
d2dc68
 	if (array_entry->array.length_field1) {
d2dc68
 		/* We can get the array size from the length_field1. */
d2dc68
-		size_t offset = 0;
d2dc68
-		for (current = desc; current->field != NULL; current++) {
d2dc68
-			if (strcmp(current->field, array_entry->array.length_field1) == 0) {
d2dc68
-				entries = get_n_bytes_as_ull(buf, offset, current->size);
d2dc68
-				break;
d2dc68
-			}
d2dc68
+		entries = get_value_from_field(buf, desc,
d2dc68
+				array_entry->array.length_field1);
d2dc68
 
d2dc68
-			offset += get_entry_size(buf, desc, current);
d2dc68
-		}
d2dc68
-		offset = 0; /* skip first three common 1-byte fields */
d2dc68
 		if (array_entry->array.length_field2 != NULL) {
d2dc68
 			/* There's a second field specifying length.  The two
d2dc68
 			 * lengths are multiplied. */
d2dc68
-			for (current = desc; current->field != NULL; current++) {
d2dc68
-				if (strcmp(current->field, array_entry->array.length_field2) == 0) {
d2dc68
-					entries *= get_n_bytes_as_ull(buf, offset, current->size);
d2dc68
-					break;
d2dc68
-				}
d2dc68
-
d2dc68
-				offset += get_entry_size(buf, desc, current);
d2dc68
-			}
d2dc68
+			entries *= get_value_from_field(buf, desc,
d2dc68
+					array_entry->array.length_field2);
d2dc68
 		}
d2dc68
 
d2dc68
 		/* If the bits flag is set, then the entry count so far
d2dc68
-- 
d2dc68
2.14.4
d2dc68