]> sigrok.org Git - libserialport.git/blobdiff - windows.c
change type of result variables to ssize_t
[libserialport.git] / windows.c
index a951378b4791782c628c3c850a4d2f6b1bbe02de..2825a92082672a3c05aa5997390dc5d25f5b00b7 100644 (file)
--- a/windows.c
+++ b/windows.c
@@ -18,8 +18,6 @@
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <config.h>
-#include "libserialport.h"
 #include "libserialport_internal.h"
 
 /* USB path is a string of at most 8 decimal numbers < 128 separated by dots. */
 static void enumerate_hub(struct sp_port *port, const char *hub_name,
                           const char *parent_path, DEVINST dev_inst);
 
-static char *wc_to_utf8(PWCHAR wc_buffer, ULONG size)
+static char *wc_to_utf8(PWCHAR wc_buffer, ULONG wc_bytes)
 {
-       ULONG wc_length = size / sizeof(WCHAR);
+       ULONG wc_length = wc_bytes / sizeof(WCHAR);
+       ULONG utf8_bytes;
        WCHAR *wc_str = NULL;
        char *utf8_str = NULL;
 
        /* Allocate aligned wide char buffer */
-       if (!(wc_str = malloc(size + sizeof(WCHAR))))
+       if (!(wc_str = malloc((wc_length + 1) * sizeof(WCHAR))))
                goto wc_to_utf8_end;
 
        /* Zero-terminate the wide char string. */
-       memcpy(wc_str, wc_buffer, size);
+       memcpy(wc_str, wc_buffer, wc_bytes);
        wc_str[wc_length] = 0;
 
        /* Compute the size of the UTF-8 converted string. */
-       if (!(size = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
+       if (!(utf8_bytes = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
                                         NULL, 0, NULL, NULL)))
                goto wc_to_utf8_end;
 
        /* Allocate UTF-8 output buffer. */
-       if (!(utf8_str = malloc(size)))
+       if (!(utf8_str = malloc(utf8_bytes)))
                goto wc_to_utf8_end;
 
        /* Actually converted to UTF-8. */
        if (!WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
-                                utf8_str, size, NULL, NULL)) {
+                                utf8_str, utf8_bytes, NULL, NULL)) {
                free(utf8_str);
                utf8_str = NULL;
                goto wc_to_utf8_end;
@@ -144,7 +143,7 @@ static char *get_string_descriptor(HANDLE hub_device, ULONG connection_index,
        desc_req->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8)
                                       | descriptor_index;
        desc_req->SetupPacket.wIndex = 0;
-       desc_req->SetupPacket.wLength = size - sizeof(*desc_req);
+       desc_req->SetupPacket.wLength = (USHORT) (size - sizeof(*desc_req));
 
        if (!DeviceIoControl(hub_device,
                             IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
@@ -266,8 +265,8 @@ static void enumerate_hub(struct sp_port *port, const char *hub_name,
                return;
        strcpy(device_name, "\\\\.\\");
        strcat(device_name, hub_name);
-       hub_device = CreateFile(device_name, GENERIC_WRITE, FILE_SHARE_WRITE,
-                               NULL, OPEN_EXISTING, 0, NULL);
+       hub_device = CreateFileA(device_name, GENERIC_WRITE, FILE_SHARE_WRITE,
+                                NULL, OPEN_EXISTING, 0, NULL);
        free(device_name);
        if (hub_device == INVALID_HANDLE_VALUE)
                return;
@@ -468,9 +467,9 @@ SP_PRIV enum sp_return get_port_details(struct sp_port *port)
                        if (!(escaped_port_name = malloc(strlen(port->name) + 5)))
                                RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed");
                        sprintf(escaped_port_name, "\\\\.\\%s", port->name);
-                       handle = CreateFile(escaped_port_name, GENERIC_READ, 0, 0,
-                                           OPEN_EXISTING,
-                                           FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);
+                       handle = CreateFileA(escaped_port_name, GENERIC_READ, 0, 0,
+                                            OPEN_EXISTING,
+                                            FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);
                        free(escaped_port_name);
                        CloseHandle(handle);