]> sigrok.org Git - libserialport.git/blob - windows.c
windows: wc_to_utf8: Eliminate variable-length array.
[libserialport.git] / windows.c
1 /*
2  * This file is part of the libserialport project.
3  *
4  * Copyright (C) 2013-2014 Martin Ling <martin-libserialport@earth.li>
5  * Copyright (C) 2014 Aurelien Jacobs <aurel@gnuage.org>
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public License
18  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
19  */
20
21 #include <config.h>
22 #include "libserialport.h"
23 #include "libserialport_internal.h"
24
25 /* USB path is a string of at most 8 decimal numbers < 128 separated by dots. */
26 #define MAX_USB_PATH ((8 * 3) + (7 * 1) + 1)
27
28 static void enumerate_hub(struct sp_port *port, const char *hub_name,
29                           const char *parent_path, DEVINST dev_inst);
30
31 static char *wc_to_utf8(PWCHAR wc_buffer, ULONG size)
32 {
33         ULONG wc_length = size / sizeof(WCHAR);
34         WCHAR *wc_str = NULL;
35         char *utf8_str = NULL;
36
37         /* Allocate aligned wide char buffer */
38         if (!(wc_str = malloc(size + sizeof(WCHAR))))
39                 goto wc_to_utf8_end;
40
41         /* Zero-terminate the wide char string. */
42         memcpy(wc_str, wc_buffer, size);
43         wc_str[wc_length] = 0;
44
45         /* Compute the size of the UTF-8 converted string. */
46         if (!(size = WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
47                                          NULL, 0, NULL, NULL)))
48                 goto wc_to_utf8_end;
49
50         /* Allocate UTF-8 output buffer. */
51         if (!(utf8_str = malloc(size)))
52                 goto wc_to_utf8_end;
53
54         /* Actually converted to UTF-8. */
55         if (!WideCharToMultiByte(CP_ACP, WC_NO_BEST_FIT_CHARS, wc_str, -1,
56                                  utf8_str, size, NULL, NULL)) {
57                 free(utf8_str);
58                 utf8_str = NULL;
59                 goto wc_to_utf8_end;
60         }
61
62 wc_to_utf8_end:
63         if (wc_str)
64                 free(wc_str);
65
66         return utf8_str;
67 }
68
69 static char *get_root_hub_name(HANDLE host_controller)
70 {
71         USB_ROOT_HUB_NAME root_hub_name;
72         PUSB_ROOT_HUB_NAME root_hub_name_wc;
73         char *root_hub_name_utf8;
74         ULONG size = 0;
75
76         /* Compute the size of the root hub name string. */
77         if (!DeviceIoControl(host_controller, IOCTL_USB_GET_ROOT_HUB_NAME, 0, 0,
78                              &root_hub_name, sizeof(root_hub_name), &size, NULL))
79                 return NULL;
80
81         /* Allocate wide char root hub name string. */
82         size = root_hub_name.ActualLength;
83         if (!(root_hub_name_wc = malloc(size)))
84                 return NULL;
85
86         /* Actually get the root hub name string. */
87         if (!DeviceIoControl(host_controller, IOCTL_USB_GET_ROOT_HUB_NAME,
88                              NULL, 0, root_hub_name_wc, size, &size, NULL)) {
89                 free(root_hub_name_wc);
90                 return NULL;
91         }
92
93         /* Convert the root hub name string to UTF-8. */
94         root_hub_name_utf8 = wc_to_utf8(root_hub_name_wc->RootHubName, size - offsetof(USB_ROOT_HUB_NAME, RootHubName));
95         free(root_hub_name_wc);
96         return root_hub_name_utf8;
97 }
98
99 static char *get_external_hub_name(HANDLE hub, ULONG connection_index)
100 {
101         USB_NODE_CONNECTION_NAME ext_hub_name;
102         PUSB_NODE_CONNECTION_NAME ext_hub_name_wc;
103         char *ext_hub_name_utf8;
104         ULONG size;
105
106         /* Compute the size of the external hub name string. */
107         ext_hub_name.ConnectionIndex = connection_index;
108         if (!DeviceIoControl(hub, IOCTL_USB_GET_NODE_CONNECTION_NAME,
109                              &ext_hub_name, sizeof(ext_hub_name),
110                              &ext_hub_name, sizeof(ext_hub_name), &size, NULL))
111                 return NULL;
112
113         /* Allocate wide char external hub name string. */
114         size = ext_hub_name.ActualLength;
115         if (size <= sizeof(ext_hub_name)
116             || !(ext_hub_name_wc = malloc(size)))
117                 return NULL;
118
119         /* Get the name of the external hub attached to the specified port. */
120         ext_hub_name_wc->ConnectionIndex = connection_index;
121         if (!DeviceIoControl(hub, IOCTL_USB_GET_NODE_CONNECTION_NAME,
122                              ext_hub_name_wc, size,
123                              ext_hub_name_wc, size, &size, NULL)) {
124                 free(ext_hub_name_wc);
125                 return NULL;
126         }
127
128         /* Convert the external hub name string to UTF-8. */
129         ext_hub_name_utf8 = wc_to_utf8(ext_hub_name_wc->NodeName, size - offsetof(USB_NODE_CONNECTION_NAME, NodeName));
130         free(ext_hub_name_wc);
131         return ext_hub_name_utf8;
132 }
133
134 static char *get_string_descriptor(HANDLE hub_device, ULONG connection_index,
135                                    UCHAR descriptor_index)
136 {
137         char desc_req_buf[sizeof(USB_DESCRIPTOR_REQUEST) +
138                           MAXIMUM_USB_STRING_LENGTH] = { 0 };
139         PUSB_DESCRIPTOR_REQUEST desc_req = (void *)desc_req_buf;
140         PUSB_STRING_DESCRIPTOR desc = (void *)(desc_req + 1);
141         ULONG size = sizeof(desc_req_buf);
142
143         desc_req->ConnectionIndex = connection_index;
144         desc_req->SetupPacket.wValue = (USB_STRING_DESCRIPTOR_TYPE << 8)
145                                        | descriptor_index;
146         desc_req->SetupPacket.wIndex = 0;
147         desc_req->SetupPacket.wLength = size - sizeof(*desc_req);
148
149         if (!DeviceIoControl(hub_device,
150                              IOCTL_USB_GET_DESCRIPTOR_FROM_NODE_CONNECTION,
151                              desc_req, size, desc_req, size, &size, NULL)
152             || size < 2
153             || desc->bDescriptorType != USB_STRING_DESCRIPTOR_TYPE
154             || desc->bLength != size - sizeof(*desc_req)
155             || desc->bLength % 2)
156                 return NULL;
157
158         return wc_to_utf8(desc->bString, desc->bLength - offsetof(USB_STRING_DESCRIPTOR, bString));
159 }
160
161 static void enumerate_hub_ports(struct sp_port *port, HANDLE hub_device,
162                                 ULONG nb_ports, const char *parent_path, DEVINST dev_inst)
163 {
164         char path[MAX_USB_PATH];
165         ULONG index = 0;
166
167         for (index = 1; index <= nb_ports; index++) {
168                 PUSB_NODE_CONNECTION_INFORMATION_EX connection_info_ex;
169                 ULONG size = sizeof(*connection_info_ex) + (30 * sizeof(USB_PIPE_INFO));
170
171                 if (!(connection_info_ex = malloc(size)))
172                         break;
173
174                 connection_info_ex->ConnectionIndex = index;
175                 if (!DeviceIoControl(hub_device,
176                                      IOCTL_USB_GET_NODE_CONNECTION_INFORMATION_EX,
177                                      connection_info_ex, size,
178                                      connection_info_ex, size, &size, NULL)) {
179                         /*
180                          * Try to get CONNECTION_INFORMATION if
181                          * CONNECTION_INFORMATION_EX did not work.
182                          */
183                         PUSB_NODE_CONNECTION_INFORMATION connection_info;
184
185                         size = sizeof(*connection_info) + (30 * sizeof(USB_PIPE_INFO));
186                         if (!(connection_info = malloc(size))) {
187                                 free(connection_info_ex);
188                                 continue;
189                         }
190                         connection_info->ConnectionIndex = index;
191                         if (!DeviceIoControl(hub_device,
192                                              IOCTL_USB_GET_NODE_CONNECTION_INFORMATION,
193                                              connection_info, size,
194                                              connection_info, size, &size, NULL)) {
195                                 free(connection_info);
196                                 free(connection_info_ex);
197                                 continue;
198                         }
199
200                         connection_info_ex->ConnectionIndex = connection_info->ConnectionIndex;
201                         connection_info_ex->DeviceDescriptor = connection_info->DeviceDescriptor;
202                         connection_info_ex->DeviceIsHub = connection_info->DeviceIsHub;
203                         connection_info_ex->DeviceAddress = connection_info->DeviceAddress;
204                         free(connection_info);
205                 }
206
207                 if (connection_info_ex->DeviceIsHub) {
208                         /* Recursively enumerate external hub. */
209                         PCHAR ext_hub_name;
210                         if ((ext_hub_name = get_external_hub_name(hub_device, index))) {
211                                 snprintf(path, sizeof(path), "%s%ld.",
212                                          parent_path, connection_info_ex->ConnectionIndex);
213                                 enumerate_hub(port, ext_hub_name, path, dev_inst);
214                         }
215                         free(connection_info_ex);
216                 } else {
217                         snprintf(path, sizeof(path), "%s%ld",
218                                  parent_path, connection_info_ex->ConnectionIndex);
219
220                         /* Check if this device is the one we search for. */
221                         if (strcmp(path, port->usb_path)) {
222                                 free(connection_info_ex);
223                                 continue;
224                         }
225
226                         /* Finally grab detailed information regarding the device. */
227                         port->usb_address = connection_info_ex->DeviceAddress + 1;
228                         port->usb_vid = connection_info_ex->DeviceDescriptor.idVendor;
229                         port->usb_pid = connection_info_ex->DeviceDescriptor.idProduct;
230
231                         if (connection_info_ex->DeviceDescriptor.iManufacturer)
232                                 port->usb_manufacturer = get_string_descriptor(hub_device, index,
233                                            connection_info_ex->DeviceDescriptor.iManufacturer);
234                         if (connection_info_ex->DeviceDescriptor.iProduct)
235                                 port->usb_product = get_string_descriptor(hub_device, index,
236                                            connection_info_ex->DeviceDescriptor.iProduct);
237                         if (connection_info_ex->DeviceDescriptor.iSerialNumber) {
238                                 port->usb_serial = get_string_descriptor(hub_device, index,
239                                            connection_info_ex->DeviceDescriptor.iSerialNumber);
240                                 if (port->usb_serial == NULL) {
241                                         //composite device, get the parent's serial number
242                                         char device_id[MAX_DEVICE_ID_LEN];
243                                         if (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS) {
244                                                 if (CM_Get_Device_IDA(dev_inst, device_id, sizeof(device_id), 0) == CR_SUCCESS)
245                                                         port->usb_serial = strdup(strrchr(device_id, '\\')+1);
246                                         }
247                                 }
248                         }
249
250                         free(connection_info_ex);
251                         break;
252                 }
253         }
254 }
255
256 static void enumerate_hub(struct sp_port *port, const char *hub_name,
257                           const char *parent_path, DEVINST dev_inst)
258 {
259         USB_NODE_INFORMATION hub_info;
260         HANDLE hub_device;
261         ULONG size = sizeof(hub_info);
262         char *device_name;
263
264         /* Open the hub with its full name. */
265         if (!(device_name = malloc(strlen("\\\\.\\") + strlen(hub_name) + 1)))
266                 return;
267         strcpy(device_name, "\\\\.\\");
268         strcat(device_name, hub_name);
269         hub_device = CreateFile(device_name, GENERIC_WRITE, FILE_SHARE_WRITE,
270                                 NULL, OPEN_EXISTING, 0, NULL);
271         free(device_name);
272         if (hub_device == INVALID_HANDLE_VALUE)
273                 return;
274
275         /* Get the number of ports of the hub. */
276         if (DeviceIoControl(hub_device, IOCTL_USB_GET_NODE_INFORMATION,
277                             &hub_info, size, &hub_info, size, &size, NULL))
278                 /* Enumerate the ports of the hub. */
279                 enumerate_hub_ports(port, hub_device,
280                    hub_info.u.HubInformation.HubDescriptor.bNumberOfPorts, parent_path, dev_inst);
281
282         CloseHandle(hub_device);
283 }
284
285 static void enumerate_host_controller(struct sp_port *port,
286                                       HANDLE host_controller_device,
287                                       DEVINST dev_inst)
288 {
289         char *root_hub_name;
290
291         if ((root_hub_name = get_root_hub_name(host_controller_device))) {
292                 enumerate_hub(port, root_hub_name, "", dev_inst);
293                 free(root_hub_name);
294         }
295 }
296
297 static void get_usb_details(struct sp_port *port, DEVINST dev_inst_match)
298 {
299         HDEVINFO device_info;
300         SP_DEVINFO_DATA device_info_data;
301         ULONG i, size = 0;
302
303         device_info = SetupDiGetClassDevs(&GUID_CLASS_USB_HOST_CONTROLLER, NULL, NULL,
304                                           DIGCF_PRESENT | DIGCF_DEVICEINTERFACE);
305         device_info_data.cbSize = sizeof(device_info_data);
306
307         for (i = 0; SetupDiEnumDeviceInfo(device_info, i, &device_info_data); i++) {
308                 SP_DEVICE_INTERFACE_DATA device_interface_data;
309                 PSP_DEVICE_INTERFACE_DETAIL_DATA device_detail_data;
310                 DEVINST dev_inst = dev_inst_match;
311                 HANDLE host_controller_device;
312
313                 device_interface_data.cbSize = sizeof(device_interface_data);
314                 if (!SetupDiEnumDeviceInterfaces(device_info, 0,
315                                                  &GUID_CLASS_USB_HOST_CONTROLLER,
316                                                  i, &device_interface_data))
317                         continue;
318
319                 if (!SetupDiGetDeviceInterfaceDetail(device_info,&device_interface_data,
320                                                      NULL, 0, &size, NULL)
321                     && GetLastError() != ERROR_INSUFFICIENT_BUFFER)
322                         continue;
323
324                 if (!(device_detail_data = malloc(size)))
325                         continue;
326                 device_detail_data->cbSize = sizeof(*device_detail_data);
327                 if (!SetupDiGetDeviceInterfaceDetail(device_info,&device_interface_data,
328                                                      device_detail_data, size, &size,
329                                                      NULL)) {
330                         free(device_detail_data);
331                         continue;
332                 }
333
334                 while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS
335                        && dev_inst != device_info_data.DevInst) { }
336                 if (dev_inst != device_info_data.DevInst) {
337                         free(device_detail_data);
338                         continue;
339                 }
340
341                 port->usb_bus = i + 1;
342
343                 host_controller_device = CreateFile(device_detail_data->DevicePath,
344                                                     GENERIC_WRITE, FILE_SHARE_WRITE,
345                                                     NULL, OPEN_EXISTING, 0, NULL);
346                 if (host_controller_device != INVALID_HANDLE_VALUE) {
347                         enumerate_host_controller(port, host_controller_device, dev_inst_match);
348                         CloseHandle(host_controller_device);
349                 }
350                 free(device_detail_data);
351         }
352
353         SetupDiDestroyDeviceInfoList(device_info);
354         return;
355 }
356
357 SP_PRIV enum sp_return get_port_details(struct sp_port *port)
358 {
359         /*
360          * Description limited to 127 char, anything longer
361          * would not be user friendly anyway.
362          */
363         char description[128];
364         SP_DEVINFO_DATA device_info_data = { .cbSize = sizeof(device_info_data) };
365         HDEVINFO device_info;
366         int i;
367
368         device_info = SetupDiGetClassDevs(NULL, 0, 0,
369                                           DIGCF_PRESENT | DIGCF_ALLCLASSES);
370         if (device_info == INVALID_HANDLE_VALUE)
371                 RETURN_FAIL("SetupDiGetClassDevs() failed");
372
373         for (i = 0; SetupDiEnumDeviceInfo(device_info, i, &device_info_data); i++) {
374                 HKEY device_key;
375                 DEVINST dev_inst;
376                 char value[8], class[16];
377                 DWORD size, type;
378                 CONFIGRET cr;
379
380                 /* Check if this is the device we are looking for. */
381                 device_key = SetupDiOpenDevRegKey(device_info, &device_info_data,
382                                                   DICS_FLAG_GLOBAL, 0,
383                                                   DIREG_DEV, KEY_QUERY_VALUE);
384                 if (device_key == INVALID_HANDLE_VALUE)
385                         continue;
386                 size = sizeof(value);
387                 if (RegQueryValueExA(device_key, "PortName", NULL, &type, (LPBYTE)value,
388                                      &size) != ERROR_SUCCESS || type != REG_SZ) {
389                         RegCloseKey(device_key);
390                         continue;
391                 }
392                 RegCloseKey(device_key);
393                 value[sizeof(value) - 1] = 0;
394                 if (strcmp(value, port->name))
395                         continue;
396
397                 /* Check port transport type. */
398                 dev_inst = device_info_data.DevInst;
399                 size = sizeof(class);
400                 cr = CR_FAILURE;
401                 while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS &&
402                        (cr = CM_Get_DevNode_Registry_PropertyA(dev_inst,
403                                  CM_DRP_CLASS, 0, class, &size, 0)) != CR_SUCCESS) { }
404                 if (cr == CR_SUCCESS) {
405                         if (!strcmp(class, "USB"))
406                                 port->transport = SP_TRANSPORT_USB;
407                 }
408
409                 /* Get port description (friendly name). */
410                 dev_inst = device_info_data.DevInst;
411                 size = sizeof(description);
412                 while ((cr = CM_Get_DevNode_Registry_PropertyA(dev_inst,
413                           CM_DRP_FRIENDLYNAME, 0, description, &size, 0)) != CR_SUCCESS
414                        && CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS) { }
415                 if (cr == CR_SUCCESS)
416                         port->description = strdup(description);
417
418                 /* Get more informations for USB connected ports. */
419                 if (port->transport == SP_TRANSPORT_USB) {
420                         char usb_path[MAX_USB_PATH] = "", tmp[MAX_USB_PATH];
421                         char device_id[MAX_DEVICE_ID_LEN];
422
423                         /* Recurse over parents to build the USB device path. */
424                         dev_inst = device_info_data.DevInst;
425                         do {
426                                 /* Verify that this layer of the tree is USB related. */
427                                 if (CM_Get_Device_IDA(dev_inst, device_id,
428                                                       sizeof(device_id), 0) != CR_SUCCESS
429                                     || strncmp(device_id, "USB\\", 4))
430                                         continue;
431
432                                 /* Discard one layer for composite devices. */
433                                 char compat_ids[512], *p = compat_ids;
434                                 size = sizeof(compat_ids);
435                                 if (CM_Get_DevNode_Registry_PropertyA(dev_inst,
436                                                                       CM_DRP_COMPATIBLEIDS, 0,
437                                                                       &compat_ids,
438                                                                       &size, 0) == CR_SUCCESS) {
439                                         while (*p) {
440                                                 if (!strncmp(p, "USB\\COMPOSITE", 13))
441                                                         break;
442                                                 p += strlen(p) + 1;
443                                         }
444                                         if (*p)
445                                                 continue;
446                                 }
447
448                                 /* Stop the recursion when reaching the USB root. */
449                                 if (!strncmp(device_id, "USB\\ROOT", 8))
450                                         break;
451
452                                 /* Prepend the address of current USB layer to the USB path. */
453                                 DWORD address;
454                                 size = sizeof(address);
455                                 if (CM_Get_DevNode_Registry_PropertyA(dev_inst, CM_DRP_ADDRESS,
456                                                         0, &address, &size, 0) == CR_SUCCESS) {
457                                         strcpy(tmp, usb_path);
458                                         snprintf(usb_path, sizeof(usb_path), "%d%s%s",
459                                                  (int)address, *tmp ? "." : "", tmp);
460                                 }
461                         } while (CM_Get_Parent(&dev_inst, dev_inst, 0) == CR_SUCCESS);
462
463                         port->usb_path = strdup(usb_path);
464
465                         /* Wake up the USB device to be able to read string descriptor. */
466                         char *escaped_port_name;
467                         HANDLE handle;
468                         if (!(escaped_port_name = malloc(strlen(port->name) + 5)))
469                                 RETURN_ERROR(SP_ERR_MEM, "Escaped port name malloc failed");
470                         sprintf(escaped_port_name, "\\\\.\\%s", port->name);
471                         handle = CreateFile(escaped_port_name, GENERIC_READ, 0, 0,
472                                             OPEN_EXISTING,
473                                             FILE_ATTRIBUTE_NORMAL|FILE_FLAG_OVERLAPPED, 0);
474                         free(escaped_port_name);
475                         CloseHandle(handle);
476
477                         /* Retrieve USB device details from the device descriptor. */
478                         get_usb_details(port, device_info_data.DevInst);
479                 }
480                 break;
481         }
482
483         SetupDiDestroyDeviceInfoList(device_info);
484
485         RETURN_OK();
486 }
487
488 SP_PRIV enum sp_return list_ports(struct sp_port ***list)
489 {
490         HKEY key;
491         TCHAR *value, *data;
492         DWORD max_value_len, max_data_size, max_data_len;
493         DWORD value_len, data_size, data_len;
494         DWORD type, index = 0;
495         LSTATUS result;
496         char *name;
497         int name_len;
498         int ret = SP_OK;
499
500         DEBUG("Opening registry key");
501         if ((result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, _T("HARDWARE\\DEVICEMAP\\SERIALCOMM"),
502                         0, KEY_QUERY_VALUE, &key)) != ERROR_SUCCESS) {
503                 /* It's possible for this key to not exist if there are no serial ports
504                  * at all. In that case we're done. Return a failure for any other error. */
505                 if (result != ERROR_FILE_NOT_FOUND) {
506                         SetLastError(result);
507                         SET_FAIL(ret, "RegOpenKeyEx() failed");
508                 }
509                 goto out_done;
510         }
511         DEBUG("Querying registry key value and data sizes");
512         if ((result = RegQueryInfoKey(key, NULL, NULL, NULL, NULL, NULL, NULL, NULL,
513                                 &max_value_len, &max_data_size, NULL, NULL)) != ERROR_SUCCESS) {
514                 SetLastError(result);
515                 SET_FAIL(ret, "RegQueryInfoKey() failed");
516                 goto out_close;
517         }
518         max_data_len = max_data_size / sizeof(TCHAR);
519         if (!(value = malloc((max_value_len + 1) * sizeof(TCHAR)))) {
520                 SET_ERROR(ret, SP_ERR_MEM, "Registry value malloc failed");
521                 goto out_close;
522         }
523         if (!(data = malloc((max_data_len + 1) * sizeof(TCHAR)))) {
524                 SET_ERROR(ret, SP_ERR_MEM, "Registry data malloc failed");
525                 goto out_free_value;
526         }
527         DEBUG("Iterating over values");
528         while (
529                 value_len = max_value_len + 1,
530                 data_size = max_data_size,
531                 RegEnumValue(key, index, value, &value_len,
532                         NULL, &type, (LPBYTE)data, &data_size) == ERROR_SUCCESS)
533         {
534                 if (type == REG_SZ) {
535                         data_len = data_size / sizeof(TCHAR);
536                         data[data_len] = '\0';
537 #ifdef UNICODE
538                         name_len = WideCharToMultiByte(CP_ACP, 0, data, -1, NULL, 0, NULL, NULL);
539 #else
540                         name_len = data_len + 1;
541 #endif
542                         if (!(name = malloc(name_len))) {
543                                 SET_ERROR(ret, SP_ERR_MEM, "Registry port name malloc failed");
544                                 goto out;
545                         }
546 #ifdef UNICODE
547                         WideCharToMultiByte(CP_ACP, 0, data, -1, name, name_len, NULL, NULL);
548 #else
549                         strcpy(name, data);
550 #endif
551                         DEBUG_FMT("Found port %s", name);
552                         if (!(*list = list_append(*list, name))) {
553                                 SET_ERROR(ret, SP_ERR_MEM, "List append failed");
554                                 free(name);
555                                 goto out;
556                         }
557                         free(name);
558                 }
559                 index++;
560         }
561 out:
562         free(data);
563 out_free_value:
564         free(value);
565 out_close:
566         RegCloseKey(key);
567 out_done:
568
569         return ret;
570 }