Imported Upstream version 013
[platform/upstream/usbutils.git] / usbhid-dump / src / usbhid-dump.c
index 56a2956..45da1ed 100644 (file)
@@ -1,33 +1,15 @@
-/** @file
- * @brief usbhid-dump - entry point *
- * Copyright (C) 2010-2011 Nikolai Kondrashov
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * usbhid-dump - entry point *
  *
- * This file is part of usbhid-dump.
- *
- * Usbhid-dump is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * Usbhid-dump is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with usbhid-dump; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
- *
- * @author Nikolai Kondrashov <spbnick@gmail.com>
- *
- * @(#) $Id$
+ * Copyright (C) 2010-2011 Nikolai Kondrashov <spbnick@gmail.com>
  */
 
 #include "config.h"
 
-#include "uhd/iface_list.h"
-#include "uhd/libusb.h"
-#include "uhd/misc.h"
+#include "iface_list.h"
+#include "misc.h"
+#include <libusb.h>
 
 #include <assert.h>
 #include <stdbool.h>
@@ -40,6 +22,7 @@
 #include <unistd.h>
 #include <getopt.h>
 #include <stdio.h>
+#include <stdint.h>
 
 /* Define LIBUSB_CALL for libusb <= 1.0.8 */
 #ifndef LIBUSB_CALL
@@ -178,12 +161,20 @@ dump_iface_list_descriptor(const uhd_iface *list)
 
     UHD_IFACE_LIST_FOR_EACH(iface, list)
     {
+        if (iface->rd_len > sizeof(buf))
+        {
+            err = LIBUSB_ERROR_NO_MEM;
+            LIBUSB_IFACE_FAILURE(iface, "report descriptor too long: %hu",
+                                 iface->rd_len);
+            return false;
+        }
+
         rc = libusb_control_transfer(iface->dev->handle,
                                      /* See HID spec, 7.1.1 */
                                      0x81,
                                      LIBUSB_REQUEST_GET_DESCRIPTOR,
                                      (LIBUSB_DT_REPORT << 8), iface->number,
-                                     buf, sizeof(buf), UHD_IO_TIMEOUT);
+                                     buf, iface->rd_len, UHD_IO_TIMEOUT);
         if (rc < 0)
         {
             err = rc;
@@ -517,8 +508,12 @@ run(bool            dump_descriptor,
     /* Create libusb context */
     LIBUSB_GUARD(libusb_init(&ctx), "create libusb context");
 
-    /* Set libusb debug level */
-    libusb_set_debug(ctx, 3);
+    /* Set libusb debug level to informational only */
+#if HAVE_LIBUSB_SET_OPTION
+    libusb_set_option(ctx, LIBUSB_OPTION_LOG_LEVEL, LIBUSB_LOG_LEVEL_INFO);
+#else
+    libusb_set_debug(ctx, LIBUSB_LOG_LEVEL_INFO);
+#endif
 
     /* Open device list */
     LIBUSB_GUARD(uhd_dev_list_open(ctx, bus_num, dev_addr,
@@ -922,7 +917,7 @@ main(int argc, char **argv)
     /*
      * Extract program invocation name
      */
-    name = rindex(argv[0], '/');
+    name = strrchr(argv[0], '/');
     if (name == NULL)
         name = argv[0];
     else