Merge hdhomerun_drops into master

Update libhdhomerun and firmware to release 20130328

Conflicts:
	libhdhomerun/hdhomerun_channels.c

Change-Id: Idac9378db9d8e32c2afad988460f90b07e9bc5d7
diff --git a/fwhdhomerun/hdhomerun3_atsc_firmware_20130328.bin b/fwhdhomerun/hdhomerun3_atsc_firmware_20130328.bin
new file mode 100644
index 0000000..7f7e33b
--- /dev/null
+++ b/fwhdhomerun/hdhomerun3_atsc_firmware_20130328.bin
Binary files differ
diff --git a/fwhdhomerun/hdhomerun3_cablecard_firmware_20130328.bin b/fwhdhomerun/hdhomerun3_cablecard_firmware_20130328.bin
new file mode 100644
index 0000000..8e7fd35
--- /dev/null
+++ b/fwhdhomerun/hdhomerun3_cablecard_firmware_20130328.bin
Binary files differ
diff --git a/libhdhomerun/Makefile b/libhdhomerun/Makefile
old mode 100755
new mode 100644
diff --git a/libhdhomerun/README b/libhdhomerun/README
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun.h b/libhdhomerun/hdhomerun.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_channels.c b/libhdhomerun/hdhomerun_channels.c
old mode 100755
new mode 100644
index 4cfaf4a..79da716
--- a/libhdhomerun/hdhomerun_channels.c
+++ b/libhdhomerun/hdhomerun_channels.c
@@ -79,6 +79,16 @@
 	{  0,   0,         0,       0}
 };
 
+/* KR cable channels. */
+static const struct hdhomerun_channelmap_range_t hdhomerun_channelmap_range_kr_cable[] = {
+	{  2,   4,  57000000, 6000000},
+	{  5,   6,  79000000, 6000000},
+	{  7,  13, 177000000, 6000000},
+	{ 14,  22, 123000000, 6000000},
+	{ 23, 153, 219000000, 6000000},
+	{  0,   0,         0,       0}
+};
+
 /* US antenna channels. */
 static const struct hdhomerun_channelmap_range_t hdhomerun_channelmap_range_us_bcast[] = {
 	{  2,   4,  57000000, 6000000},
@@ -130,43 +140,57 @@
 static const struct hdhomerun_channelmap_record_t hdhomerun_channelmap_table[] = {
 	{"au-bcast", hdhomerun_channelmap_range_au_bcast, "au-bcast",               "AU"},
 	{"au-cable", hdhomerun_channelmap_range_eu_cable, "au-cable",               "AU"},
-	{"eu-bcast", hdhomerun_channelmap_range_eu_bcast, "eu-bcast",               "EU PA"},
-	{"eu-cable", hdhomerun_channelmap_range_eu_cable, "eu-cable",               "EU"},
+	{"eu-bcast", hdhomerun_channelmap_range_eu_bcast, "eu-bcast",               NULL},
+	{"eu-cable", hdhomerun_channelmap_range_eu_cable, "eu-cable",               NULL},
 	{"tw-bcast", hdhomerun_channelmap_range_us_bcast, "tw-bcast",               "TW"},
 	{"tw-cable", hdhomerun_channelmap_range_us_cable, "tw-cable",               "TW"},
 
-	{"us-bcast", hdhomerun_channelmap_range_us_bcast, "us-bcast",               "CA US"},
-	{"us-cable", hdhomerun_channelmap_range_us_cable, "us-cable us-hrc us-irc", "CA PA US"},
-	{"us-hrc",   hdhomerun_channelmap_range_us_hrc  , "us-cable us-hrc us-irc", "CA PA US"},
-	{"us-irc",   hdhomerun_channelmap_range_us_irc,   "us-cable us-hrc us-irc", "CA PA US"},
+	{"kr-bcast", hdhomerun_channelmap_range_us_bcast, "kr-bcast",               "KR"},
+	{"kr-cable", hdhomerun_channelmap_range_kr_cable, "kr-cable",               "KR"},
+	{"us-bcast", hdhomerun_channelmap_range_us_bcast, "us-bcast",               NULL},
+	{"us-cable", hdhomerun_channelmap_range_us_cable, "us-cable us-hrc us-irc", NULL},
+	{"us-hrc",   hdhomerun_channelmap_range_us_hrc  , "us-cable us-hrc us-irc", NULL},
+	{"us-irc",   hdhomerun_channelmap_range_us_irc,   "us-cable us-hrc us-irc", NULL},
 
 	{NULL,       NULL,                                NULL,                     NULL}
 };
 
-const char *hdhomerun_channelmap_get_channelmap_from_country_source(const char *countrycode, const char *source)
+const char *hdhomerun_channelmap_get_channelmap_from_country_source(const char *countrycode, const char *source, const char *supported)
 {
-	bool_t country_found = FALSE;
+	const char *default_result = NULL;
 
 	const struct hdhomerun_channelmap_record_t *record = hdhomerun_channelmap_table;
 	while (record->channelmap) {
+		/* Ignore records that do not match the requested source. */
+		if (!strstr(record->channelmap, source)) {
+			record++;
+			continue;
+		}
+
+		/* Ignore records that are not supported by the hardware. */
+		if (!strstr(supported, record->channelmap)) {
+			record++;
+			continue;
+		}
+
+		/* If this record is the default result then remember it and keep searching. */
+		if (!record->countrycodes) {
+			default_result = record->channelmap;
+			record++;
+			continue;
+		}
+
+		/* Ignore records that have a countrycode filter and do not match. */
 		if (!strstr(record->countrycodes, countrycode)) {
 			record++;
 			continue;
 		}
 
-		if (strstr(record->channelmap, source)) {
-			return record->channelmap;
-		}
-
-		country_found = TRUE;
-		record++;
+		/* Record found with exact match for source and countrycode. */
+		return record->channelmap;
 	}
 
-	if (!country_found) {
-		return hdhomerun_channelmap_get_channelmap_from_country_source("EU", source);
-	}
-
-	return NULL;
+	return default_result;
 }
 
 const char *hdhomerun_channelmap_get_channelmap_scan_group(const char *channelmap)
@@ -334,7 +358,7 @@
 		entry->channel_number = channel_number;
 		entry->frequency = range->frequency + ((uint32_t)(channel_number - range->channel_range_start) * range->spacing);
 		entry->frequency = hdhomerun_channel_frequency_round_normal(entry->frequency);
-		sprintf(entry->name, "%s:%u", channelmap, entry->channel_number);
+		hdhomerun_sprintf(entry->name, entry->name + sizeof(entry->name), "%s:%u", channelmap, entry->channel_number);
 
 		hdhomerun_channel_list_build_insert(channel_list, entry);
 	}
diff --git a/libhdhomerun/hdhomerun_channels.h b/libhdhomerun/hdhomerun_channels.h
old mode 100755
new mode 100644
index 7772ce8..8b79166
--- a/libhdhomerun/hdhomerun_channels.h
+++ b/libhdhomerun/hdhomerun_channels.h
@@ -37,7 +37,7 @@
 struct hdhomerun_channel_entry_t;
 struct hdhomerun_channel_list_t;
 
-extern LIBTYPE const char *hdhomerun_channelmap_get_channelmap_from_country_source(const char *countrycode, const char *source);
+extern LIBTYPE const char *hdhomerun_channelmap_get_channelmap_from_country_source(const char *countrycode, const char *source, const char *supported);
 extern LIBTYPE const char *hdhomerun_channelmap_get_channelmap_scan_group(const char *channelmap);
 
 extern LIBTYPE uint16_t hdhomerun_channel_entry_channel_number(struct hdhomerun_channel_entry_t *entry);
diff --git a/libhdhomerun/hdhomerun_channelscan.c b/libhdhomerun/hdhomerun_channelscan.c
old mode 100755
new mode 100644
index ed1ec43..5203280
--- a/libhdhomerun/hdhomerun_channelscan.c
+++ b/libhdhomerun/hdhomerun_channelscan.c
@@ -60,6 +60,7 @@
 
 void channelscan_destroy(struct hdhomerun_channelscan_t *scan)
 {
+	hdhomerun_channel_list_destroy(scan->channel_list);
 	free(scan);
 }
 
@@ -67,7 +68,7 @@
 {
 	/* Set channel. */
 	char channel_str[64];
-	sprintf(channel_str, "auto:%ld", (unsigned long)frequency);
+	hdhomerun_sprintf(channel_str, channel_str + sizeof(channel_str), "auto:%u", (unsigned int)frequency);
 
 	int ret = hdhomerun_device_set_tuner_channel(scan->hd, channel_str);
 	if (ret <= 0) {
@@ -175,8 +176,7 @@
 		struct hdhomerun_channelscan_program_t program;
 		memset(&program, 0, sizeof(program));
 
-		strncpy(program.program_str, line, sizeof(program.program_str));
-		program.program_str[sizeof(program.program_str) - 1] = 0;
+		hdhomerun_sprintf(program.program_str, program.program_str + sizeof(program.program_str), "%s", line);
 
 		unsigned int program_number;
 		unsigned int virtual_major, virtual_minor;
@@ -237,8 +237,10 @@
 
 	/* Combine channels with same frequency. */
 	result->frequency = hdhomerun_channel_entry_frequency(entry);
-	strncpy(result->channel_str, hdhomerun_channel_entry_name(entry), sizeof(result->channel_str) - 1);
-	result->channel_str[sizeof(result->channel_str) - 1] = 0;
+
+	char *ptr = result->channel_str;
+	char *end = result->channel_str + sizeof(result->channel_str);
+	hdhomerun_sprintf(ptr, end, hdhomerun_channel_entry_name(entry));
 
 	while (1) {
 		entry = hdhomerun_channel_list_prev(scan->channel_list, entry);
@@ -252,8 +254,8 @@
 			break;
 		}
 
-		char *ptr = strchr(result->channel_str, 0);
-		sprintf(ptr, ", %s", hdhomerun_channel_entry_name(entry));
+		ptr = strchr(ptr, 0);
+		hdhomerun_sprintf(ptr, end, ", %s", hdhomerun_channel_entry_name(entry));
 	}
 
 	return 1;
diff --git a/libhdhomerun/hdhomerun_channelscan.h b/libhdhomerun/hdhomerun_channelscan.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_config.c b/libhdhomerun/hdhomerun_config.c
old mode 100755
new mode 100644
index a88268e..56469d7
--- a/libhdhomerun/hdhomerun_config.c
+++ b/libhdhomerun/hdhomerun_config.c
@@ -93,8 +93,8 @@
 
 static uint32_t parse_ip_addr(const char *str)
 {
-	unsigned long a[4];
-	if (sscanf(str, "%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2], &a[3]) != 4) {
+	unsigned int a[4];
+	if (sscanf(str, "%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3]) != 4) {
 		return 0;
 	}
 
@@ -126,8 +126,8 @@
 	int index;
 	for (index = 0; index < count; index++) {
 		struct hdhomerun_discover_device_t *result = &result_list[index];
-		printf("hdhomerun device %08lX found at %u.%u.%u.%u\n",
-			(unsigned long)result->device_id,
+		printf("hdhomerun device %08X found at %u.%u.%u.%u\n",
+			(unsigned int)result->device_id,
 			(unsigned int)(result->ip_addr >> 24) & 0x0FF, (unsigned int)(result->ip_addr >> 16) & 0x0FF,
 			(unsigned int)(result->ip_addr >> 8) & 0x0FF, (unsigned int)(result->ip_addr >> 0) & 0x0FF
 		);
@@ -303,8 +303,8 @@
 			break;
 		}
 
-		cmd_scan_printf(fp, "SCANNING: %lu (%s)\n",
-			(unsigned long)result.frequency, result.channel_str
+		cmd_scan_printf(fp, "SCANNING: %u (%s)\n",
+			(unsigned int)result.frequency, result.channel_str
 		);
 
 		ret = hdhomerun_device_channelscan_detect(hd, &result);
@@ -671,7 +671,7 @@
 	/* Device ID check. */
 	uint32_t device_id_requested = hdhomerun_device_get_device_id_requested(hd);
 	if (!hdhomerun_discover_validate_device_id(device_id_requested)) {
-		fprintf(stderr, "invalid device id: %08lX\n", (unsigned long)device_id_requested);
+		fprintf(stderr, "invalid device id: %08X\n", (unsigned int)device_id_requested);
 	}
 
 	/* Connect to device and check model. */
diff --git a/libhdhomerun/hdhomerun_control.c b/libhdhomerun/hdhomerun_control.c
old mode 100755
new mode 100644
index 2873e53..15f18fe
--- a/libhdhomerun/hdhomerun_control.c
+++ b/libhdhomerun/hdhomerun_control.c
@@ -368,8 +368,16 @@
 {
 	struct hdhomerun_pkt_t *tx_pkt = &cs->tx_pkt;
 	struct hdhomerun_pkt_t *rx_pkt = &cs->rx_pkt;
+	bool_t upload_delay = FALSE;
 	uint32_t sequence = 0;
 
+	/* Special case detection. */
+	char *version_str;
+	int ret = hdhomerun_control_get(cs, "/sys/version", &version_str, NULL);
+	if (ret > 0) {
+		upload_delay = strcmp(version_str, "20120704beta1") == 0;
+	}
+
 	/* Upload. */
 	while (1) {
 		uint8_t data[256];
@@ -388,6 +396,10 @@
 		}
 
 		sequence += (uint32_t)length;
+
+		if (upload_delay) {
+			msleep_approx(25);
+		}
 	}
 
 	if (sequence == 0) {
diff --git a/libhdhomerun/hdhomerun_control.h b/libhdhomerun/hdhomerun_control.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_debug.c b/libhdhomerun/hdhomerun_debug.c
old mode 100755
new mode 100644
index 9686f61..52ac321
--- a/libhdhomerun/hdhomerun_debug.c
+++ b/libhdhomerun/hdhomerun_debug.c
@@ -300,12 +300,8 @@
 	pthread_mutex_lock(&dbg->print_lock);
 
 	if (dbg->prefix) {
-		int len = snprintf(ptr, end - ptr, "%s ", dbg->prefix);
-		len = (len <= 0) ? 0 : len;
-		ptr += len;
-		if (ptr > end) {
-			ptr = end;
-		}
+		hdhomerun_sprintf(ptr, end, "%s ", dbg->prefix);
+		ptr = strchr(ptr, 0);
 	}
 
 	pthread_mutex_unlock(&dbg->print_lock);
@@ -313,29 +309,17 @@
 	/*
 	 * Message text.
 	 */
-	int len = vsnprintf(ptr, end - ptr, fmt, args);
-	len = (len < 0) ? 0 : len; /* len does not include null */
-	ptr += len;
-	if (ptr > end) {
-		ptr = end;
-	}
+	hdhomerun_vsprintf(ptr, end, fmt, args);
+	ptr = strchr(ptr, 0);
 
 	/*
 	 * Force newline.
 	 */
-	if ((ptr[-1] != '\n') && (ptr + 1 <= end)) {
-		*ptr++ = '\n';
+	if (ptr[-1] != '\n') {
+		hdhomerun_sprintf(ptr, end, "\n");
 	}
 
 	/*
-	 * Force NULL.
-	 */
-	if (ptr + 1 > end) {
-		ptr = end - 1;
-	}
-	*ptr++ = 0;
-
-	/*
 	 * Enqueue.
 	 */
 	pthread_mutex_lock(&dbg->queue_lock);
diff --git a/libhdhomerun/hdhomerun_debug.h b/libhdhomerun/hdhomerun_debug.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_device.c b/libhdhomerun/hdhomerun_device.c
old mode 100755
new mode 100644
index 7bbb75a..6bd71d2
--- a/libhdhomerun/hdhomerun_device.c
+++ b/libhdhomerun/hdhomerun_device.c
@@ -68,8 +68,8 @@
 	hd->tuner = 0;
 	hd->lockkey = 0;
 
-	sprintf(hd->name, "%08lX-%u", (unsigned long)hd->device_id, hd->tuner);
-	hd->model[0] = 0; /* clear cached model string */
+	hdhomerun_sprintf(hd->name, hd->name + sizeof(hd->name), "%08X-%u", (unsigned int)hd->device_id, hd->tuner);
+	hdhomerun_sprintf(hd->model, hd->model + sizeof(hd->model), ""); /* clear cached model string */
 
 	return 1;
 }
@@ -87,9 +87,9 @@
 	hd->tuner = 0;
 	hd->lockkey = 0;
 
-	unsigned long ip = multicast_ip;
-	sprintf(hd->name, "%lu.%lu.%lu.%lu", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, (ip >> 0) & 0xFF);
-	sprintf(hd->model, "multicast");
+	unsigned int ip = multicast_ip;
+	hdhomerun_sprintf(hd->name, hd->name + sizeof(hd->name), "%u.%u.%u.%u", (ip >> 24) & 0xFF, (ip >> 16) & 0xFF, (ip >> 8) & 0xFF, (ip >> 0) & 0xFF);
+	hdhomerun_sprintf(hd->model, hd->model + sizeof(hd->model), "multicast");
 
 	return 1;
 }
@@ -120,7 +120,7 @@
 	}
 
 	hd->tuner = tuner;
-	sprintf(hd->name, "%08lX-%u", (unsigned long)hd->device_id, hd->tuner);
+	hdhomerun_sprintf(hd->name, hd->name + sizeof(hd->name), "%08X-%u", (unsigned int)hd->device_id, hd->tuner);
 
 	return 1;
 }
@@ -193,17 +193,17 @@
 	}
 
 	if (*ptr == 0) {
-		unsigned long device_id;
-		if (sscanf(device_str, "%lx", &device_id) != 1) {
+		unsigned int device_id;
+		if (sscanf(device_str, "%x", &device_id) != 1) {
 			return NULL;
 		}
 		return hdhomerun_device_create((uint32_t)device_id, 0, 0, dbg);
 	}
 
 	if (*ptr == '-') {
-		unsigned long device_id;
+		unsigned int device_id;
 		unsigned int tuner;
-		if (sscanf(device_str, "%lx-%u", &device_id, &tuner) != 2) {
+		if (sscanf(device_str, "%x-%u", &device_id, &tuner) != 2) {
 			return NULL;
 		}
 		return hdhomerun_device_create((uint32_t)device_id, 0, tuner, dbg);
@@ -212,10 +212,10 @@
 	return NULL;
 }
 
-static struct hdhomerun_device_t *hdhomerun_device_create_from_str_ip_result(unsigned long a[4], unsigned int port, unsigned int tuner, struct hdhomerun_debug_t *dbg)
+static struct hdhomerun_device_t *hdhomerun_device_create_from_str_ip_result(unsigned int a[4], unsigned int port, unsigned int tuner, struct hdhomerun_debug_t *dbg)
 {
-	unsigned long device_ip = (a[0] << 24) | (a[1] << 16) | (a[2] << 8) | (a[3] << 0);
-	struct hdhomerun_device_t *hd = hdhomerun_device_create(HDHOMERUN_DEVICE_ID_WILDCARD, (uint32_t)device_ip, tuner, dbg);
+	uint32_t device_ip = (uint32_t)((a[0] << 24) | (a[1] << 16) | (a[2] << 8) | (a[3] << 0));
+	struct hdhomerun_device_t *hd = hdhomerun_device_create(HDHOMERUN_DEVICE_ID_WILDCARD, device_ip, tuner, dbg);
 	if (!hd) {
 		return NULL;
 	}
@@ -229,17 +229,17 @@
 
 static struct hdhomerun_device_t *hdhomerun_device_create_from_str_ip(const char *device_str, struct hdhomerun_debug_t *dbg)
 {
-	unsigned long a[4];
+	unsigned int a[4];
 	unsigned int port = 0;
 	unsigned int tuner = 0;
 
-	if (sscanf(device_str, "%lu.%lu.%lu.%lu:%u", &a[0], &a[1], &a[2], &a[3], &port) == 5) {
+	if (sscanf(device_str, "%u.%u.%u.%u:%u", &a[0], &a[1], &a[2], &a[3], &port) == 5) {
 		return hdhomerun_device_create_from_str_ip_result(a, port, tuner, dbg);
 	}
-	if (sscanf(device_str, "%lu.%lu.%lu.%lu-%u", &a[0], &a[1], &a[2], &a[3], &tuner) == 5) {
+	if (sscanf(device_str, "%u.%u.%u.%u-%u", &a[0], &a[1], &a[2], &a[3], &tuner) == 5) {
 		return hdhomerun_device_create_from_str_ip_result(a, port, tuner, dbg);
 	}
-	if (sscanf(device_str, "%lu.%lu.%lu.%lu", &a[0], &a[1], &a[2], &a[3]) == 4) {
+	if (sscanf(device_str, "%u.%u.%u.%u", &a[0], &a[1], &a[2], &a[3]) == 4) {
 		return hdhomerun_device_create_from_str_ip_result(a, port, tuner, dbg);
 	}
 
@@ -398,8 +398,8 @@
 		return 0;
 	}
 
-	unsigned long value = 0;
-	sscanf(ptr + strlen(tag), "%lu", &value);
+	unsigned int value = 0;
+	sscanf(ptr + strlen(tag), "%u", &value);
 
 	return (uint32_t)value;
 }
@@ -480,7 +480,7 @@
 	memset(status, 0, sizeof(struct hdhomerun_tuner_status_t));
 
 	char name[32];
-	sprintf(name, "/tuner%u/status", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/status", hd->tuner);
 
 	char *status_str;
 	int ret = hdhomerun_control_get(hd->cs, name, &status_str, NULL);
@@ -572,7 +572,7 @@
 	memset(vstatus, 0, sizeof(struct hdhomerun_tuner_vstatus_t));
 
 	char var_name[32];
-	sprintf(var_name, "/tuner%u/vstatus", hd->tuner);
+	hdhomerun_sprintf(var_name, var_name + sizeof(var_name), "/tuner%u/vstatus", hd->tuner);
 
 	char *vstatus_str;
 	int ret = hdhomerun_control_get(hd->cs, var_name, &vstatus_str, NULL);
@@ -640,7 +640,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/streaminfo", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/streaminfo", hd->tuner);
 	return hdhomerun_control_get(hd->cs, name, pstreaminfo, NULL);
 }
 
@@ -652,7 +652,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/channel", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/channel", hd->tuner);
 	return hdhomerun_control_get(hd->cs, name, pchannel, NULL);
 }
 
@@ -664,7 +664,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/vchannel", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/vchannel", hd->tuner);
 	return hdhomerun_control_get(hd->cs, name, pvchannel, NULL);
 }
 
@@ -676,7 +676,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/channelmap", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/channelmap", hd->tuner);
 	return hdhomerun_control_get(hd->cs, name, pchannelmap, NULL);
 }
 
@@ -688,7 +688,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/filter", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/filter", hd->tuner);
 	return hdhomerun_control_get(hd->cs, name, pfilter, NULL);
 }
 
@@ -700,7 +700,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/program", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/program", hd->tuner);
 	return hdhomerun_control_get(hd->cs, name, pprogram, NULL);
 }
 
@@ -712,7 +712,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/target", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/target", hd->tuner);
 	return hdhomerun_control_get(hd->cs, name, ptarget, NULL);
 }
 
@@ -735,8 +735,8 @@
 		}
 		*ptr++ = 0;
 
-		unsigned long raw;
-		if (sscanf(result, "%lx", &raw) != 1) {
+		unsigned int raw;
+		if (sscanf(result, "%x", &raw) != 1) {
 			break;
 		}
 
@@ -770,7 +770,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/plotsample", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/plotsample", hd->tuner);
 	return hdhomerun_device_get_tuner_plotsample_internal(hd, name, psamples, pcount);
 }
 
@@ -792,7 +792,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/lockkey", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/lockkey", hd->tuner);
 	return hdhomerun_control_get(hd->cs, name, powner, NULL);
 }
 
@@ -834,8 +834,8 @@
 	}
 
 	if (pversion_num) {
-		unsigned long version_num;
-		if (sscanf(version_str, "%lu", &version_num) != 1) {
+		unsigned int version_num;
+		if (sscanf(version_str, "%u", &version_num) != 1) {
 			*pversion_num = 0;
 		} else {
 			*pversion_num = (uint32_t)version_num;
@@ -887,7 +887,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/channel", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/channel", hd->tuner);
 	return hdhomerun_control_set_with_lockkey(hd->cs, name, channel, hd->lockkey, NULL, NULL);
 }
 
@@ -899,7 +899,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/vchannel", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/vchannel", hd->tuner);
 	return hdhomerun_control_set_with_lockkey(hd->cs, name, vchannel, hd->lockkey, NULL, NULL);
 }
 
@@ -911,7 +911,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/channelmap", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/channelmap", hd->tuner);
 	return hdhomerun_control_set_with_lockkey(hd->cs, name, channelmap, hd->lockkey, NULL, NULL);
 }
 
@@ -923,29 +923,17 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/filter", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/filter", hd->tuner);
 	return hdhomerun_control_set_with_lockkey(hd->cs, name, filter, hd->lockkey, NULL, NULL);
 }
 
-static int hdhomerun_device_set_tuner_filter_by_array_append(char **pptr, char *end, uint16_t range_begin, uint16_t range_end)
+static bool_t hdhomerun_device_set_tuner_filter_by_array_append(char *ptr, char *end, uint16_t range_begin, uint16_t range_end)
 {
-	char *ptr = *pptr;
-
-	size_t available = end - ptr;
-	size_t required;
-
 	if (range_begin == range_end) {
-		required = snprintf(ptr, available, "0x%04x ", range_begin) + 1;
+		return hdhomerun_sprintf(ptr, end, "0x%04x ", (unsigned int)range_begin);
 	} else {
-		required = snprintf(ptr, available, "0x%04x-0x%04x ", range_begin, range_end) + 1;
+		return hdhomerun_sprintf(ptr, end, "0x%04x-0x%04x ", (unsigned int)range_begin, (unsigned int)range_end);
 	}
-
-	if (required > available) {
-		return FALSE;
-	}
-
-	*pptr = strchr(ptr, 0);
-	return TRUE;
 }
 
 int hdhomerun_device_set_tuner_filter_by_array(struct hdhomerun_device_t *hd, unsigned char filter_array[0x2000])
@@ -963,9 +951,10 @@
 			if (range_begin == 0xFFFF) {
 				continue;
 			}
-			if (!hdhomerun_device_set_tuner_filter_by_array_append(&ptr, end, range_begin, range_end)) {
+			if (!hdhomerun_device_set_tuner_filter_by_array_append(ptr, end, range_begin, range_end)) {
 				return 0;
 			}
+			ptr = strchr(ptr, 0);
 			range_begin = 0xFFFF;
 			range_end = 0xFFFF;
 			continue;
@@ -981,16 +970,17 @@
 	}
 
 	if (range_begin != 0xFFFF) {
-		if (!hdhomerun_device_set_tuner_filter_by_array_append(&ptr, end, range_begin, range_end)) {
+		if (!hdhomerun_device_set_tuner_filter_by_array_append(ptr, end, range_begin, range_end)) {
 			return 0;
 		}
+		ptr = strchr(ptr, 0);
 	}
 
 	/* Remove trailing space. */
 	if (ptr > filter) {
 		ptr--;
+		*ptr = 0;
 	}
-	*ptr = 0;
 
 	return hdhomerun_device_set_tuner_filter(hd, filter);
 }
@@ -1003,7 +993,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/program", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/program", hd->tuner);
 	return hdhomerun_control_set_with_lockkey(hd->cs, name, program, hd->lockkey, NULL, NULL);
 }
 
@@ -1015,7 +1005,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/target", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/target", hd->tuner);
 	return hdhomerun_control_set_with_lockkey(hd->cs, name, target, hd->lockkey, NULL, NULL);
 }
 
@@ -1034,7 +1024,7 @@
 	char target[64];
 	uint32_t local_ip = hdhomerun_control_get_local_addr(hd->cs);
 	uint16_t local_port = hdhomerun_video_get_local_port(hd->vs);
-	sprintf(target, "%s://%u.%u.%u.%u:%u",
+	hdhomerun_sprintf(target, target + sizeof(target), "%s://%u.%u.%u.%u:%u",
 		protocol,
 		(unsigned int)(local_ip >> 24) & 0xFF, (unsigned int)(local_ip >> 16) & 0xFF,
 		(unsigned int)(local_ip >> 8) & 0xFF, (unsigned int)(local_ip >> 0) & 0xFF,
@@ -1107,10 +1097,10 @@
 	uint32_t new_lockkey = random_get32();
 
 	char name[32];
-	sprintf(name, "/tuner%u/lockkey", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/lockkey", hd->tuner);
 
 	char new_lockkey_str[64];
-	sprintf(new_lockkey_str, "%u", (unsigned int)new_lockkey);
+	hdhomerun_sprintf(new_lockkey_str, new_lockkey_str + sizeof(new_lockkey_str), "%u", (unsigned int)new_lockkey);
 
 	int ret = hdhomerun_control_set_with_lockkey(hd->cs, name, new_lockkey_str, hd->lockkey, NULL, perror);
 	if (ret <= 0) {
@@ -1137,7 +1127,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/lockkey", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/lockkey", hd->tuner);
 	int ret = hdhomerun_control_set_with_lockkey(hd->cs, name, "none", hd->lockkey, NULL, NULL);
 
 	hd->lockkey = 0;
@@ -1155,7 +1145,7 @@
 	}
 
 	char name[32];
-	sprintf(name, "/tuner%u/lockkey", hd->tuner);
+	hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/lockkey", hd->tuner);
 	int ret = hdhomerun_control_set(hd->cs, name, "force", NULL, NULL);
 
 	hd->lockkey = 0;
@@ -1355,13 +1345,11 @@
 		return NULL;
 	}
 	if (ret == 0) {
-		strncpy(hd->model, "hdhomerun_atsc", sizeof(hd->model) - 1);
-		hd->model[sizeof(hd->model) - 1] = 0;
+		hdhomerun_sprintf(hd->model, hd->model + sizeof(hd->model), "hdhomerun_atsc");
 		return hd->model;
 	}
 
-	strncpy(hd->model, model_str, sizeof(hd->model) - 1);
-	hd->model[sizeof(hd->model) - 1] = 0;
+	hdhomerun_sprintf(hd->model, hd->model + sizeof(hd->model), "%s", model_str);
 	return hd->model;
 }
 
@@ -1389,7 +1377,7 @@
 
 	if (hd->cs) {
 		char name[32];
-		sprintf(name, "/tuner%u/debug", hd->tuner);
+		hdhomerun_sprintf(name, name + sizeof(name), "/tuner%u/debug", hd->tuner);
 
 		char *debug_str;
 		char *error_str;
diff --git a/libhdhomerun/hdhomerun_device.h b/libhdhomerun/hdhomerun_device.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_device_selector.c b/libhdhomerun/hdhomerun_device_selector.c
old mode 100755
new mode 100644
index f57f125..08a47f2
--- a/libhdhomerun/hdhomerun_device_selector.c
+++ b/libhdhomerun/hdhomerun_device_selector.c
@@ -201,7 +201,7 @@
 
 		/* Create and add device. */
 		char device_name[32];
-		sprintf(device_name, "%S", wdevice_name);
+		hdhomerun_sprintf(device_name, device_name + sizeof(device_name), "%S", wdevice_name);
 
 		struct hdhomerun_device_t *hd = hdhomerun_device_create_from_str(device_name, hds->dbg);
 		if (!hd) {
@@ -258,9 +258,9 @@
 		*ptr = 0;
 	}
 
-	unsigned long a[4];
-	unsigned long target_port;
-	if (sscanf(target, "%lu.%lu.%lu.%lu:%lu", &a[0], &a[1], &a[2], &a[3], &target_port) != 5) {
+	unsigned int a[4];
+	unsigned int target_port;
+	if (sscanf(target, "%u.%u.%u.%u:%u", &a[0], &a[1], &a[2], &a[3], &target_port) != 5) {
 		hdhomerun_debug_printf(hds->dbg, "hdhomerun_device_selector_choose_test: device %s in use, no target set (%s)\n", name, target);
 		return FALSE;
 	}
diff --git a/libhdhomerun/hdhomerun_device_selector.h b/libhdhomerun/hdhomerun_device_selector.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_discover.c b/libhdhomerun/hdhomerun_discover.c
old mode 100755
new mode 100644
index 0307013..83a8f4e
--- a/libhdhomerun/hdhomerun_discover.c
+++ b/libhdhomerun/hdhomerun_discover.c
@@ -46,6 +46,7 @@
 	unsigned int sock_count;
 	struct hdhomerun_pkt_t tx_pkt;
 	struct hdhomerun_pkt_t rx_pkt;
+	struct hdhomerun_debug_t *dbg;
 };
 
 static bool_t hdhomerun_discover_sock_add(struct hdhomerun_discover_t *ds, uint32_t local_ip, uint32_t subnet_mask)
@@ -72,6 +73,7 @@
 
 	/* Bind socket. */
 	if (!hdhomerun_sock_bind(sock, local_ip, 0, FALSE)) {
+		hdhomerun_debug_printf(ds->dbg, "discover: failed to bind to %u.%u.%u.%u:0\n", (unsigned int)(local_ip >> 24) & 0xFF, (unsigned int)(local_ip >> 16) & 0xFF, (unsigned int)(local_ip >> 8) & 0xFF, (unsigned int)(local_ip >> 0) & 0xFF);
 		hdhomerun_sock_destroy(sock);
 		return FALSE;
 	}
@@ -86,13 +88,15 @@
 	return TRUE;
 }
 
-struct hdhomerun_discover_t *hdhomerun_discover_create(void)
+struct hdhomerun_discover_t *hdhomerun_discover_create(struct hdhomerun_debug_t *dbg)
 {
 	struct hdhomerun_discover_t *ds = (struct hdhomerun_discover_t *)calloc(1, sizeof(struct hdhomerun_discover_t));
 	if (!ds) {
 		return NULL;
 	}
 
+	ds->dbg = dbg;
+
 	/* Create a routable socket (always first entry). */
 	if (!hdhomerun_discover_sock_add(ds, 0, 0)) {
 		free(ds);
@@ -125,8 +129,13 @@
 	struct hdhomerun_local_ip_info_t ip_info_list[HDHOMERUN_DISOCVER_MAX_SOCK_COUNT];
 	int count = hdhomerun_local_ip_info(ip_info_list, HDHOMERUN_DISOCVER_MAX_SOCK_COUNT);
 	if (count < 0) {
+		hdhomerun_debug_printf(ds->dbg, "discover: hdhomerun_local_ip_info returned error\n");
 		count = 0;
 	}
+	if (count > HDHOMERUN_DISOCVER_MAX_SOCK_COUNT) {
+		hdhomerun_debug_printf(ds->dbg, "discover: too many local IP addresses\n");
+		count = HDHOMERUN_DISOCVER_MAX_SOCK_COUNT;
+	}
 
 	int index;
 	for (index = 0; index < count; index++) {
@@ -404,7 +413,7 @@
 		return 0;
 	}
 
-	struct hdhomerun_discover_t *ds = hdhomerun_discover_create();
+	struct hdhomerun_discover_t *ds = hdhomerun_discover_create(NULL);
 	if (!ds) {
 		return -1;
 	}
diff --git a/libhdhomerun/hdhomerun_discover.h b/libhdhomerun/hdhomerun_discover.h
old mode 100755
new mode 100644
index 2f0d906..633d3a2
--- a/libhdhomerun/hdhomerun_discover.h
+++ b/libhdhomerun/hdhomerun_discover.h
@@ -59,7 +59,7 @@
 /*
  * Optional: persistent discover instance available for discover polling use.
  */
-extern LIBTYPE struct hdhomerun_discover_t *hdhomerun_discover_create(void);
+extern LIBTYPE struct hdhomerun_discover_t *hdhomerun_discover_create(struct hdhomerun_debug_t *dbg);
 extern LIBTYPE void hdhomerun_discover_destroy(struct hdhomerun_discover_t *ds);
 extern LIBTYPE int hdhomerun_discover_find_devices(struct hdhomerun_discover_t *ds, uint32_t target_ip, uint32_t device_type, uint32_t device_id, struct hdhomerun_discover_device_t result_list[], int max_count);
 
diff --git a/libhdhomerun/hdhomerun_os.h b/libhdhomerun/hdhomerun_os.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_os_posix.c b/libhdhomerun/hdhomerun_os_posix.c
old mode 100755
new mode 100644
index 25eece8..91b9edf
--- a/libhdhomerun/hdhomerun_os_posix.c
+++ b/libhdhomerun/hdhomerun_os_posix.c
@@ -36,12 +36,12 @@
 {
 	FILE *fp = fopen("/dev/urandom", "rb");
 	if (!fp) {
-		return (uint32_t)rand();
+		return (uint32_t)getcurrenttime();
 	}
 
 	uint32_t Result;
 	if (fread(&Result, 4, 1, fp) != 1) {
-		Result = (uint32_t)rand();
+		Result = (uint32_t)getcurrenttime();
 	}
 
 	fclose(fp);
@@ -103,3 +103,33 @@
 		msleep_approx(stop_time - current_time);
 	}
 }
+
+bool_t hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap)
+{
+	if (buffer >= end) {
+		return FALSE;
+	}
+
+	int length = vsnprintf(buffer, end - buffer - 1, fmt, ap);
+	if (length < 0) {
+		*buffer = 0;
+		return FALSE;
+	}
+
+	if (buffer + length + 1 > end) {
+		*(end - 1) = 0;
+		return FALSE;
+
+	}
+
+	return TRUE;
+}
+
+bool_t hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	bool_t result = hdhomerun_vsprintf(buffer, end, fmt, ap);
+	va_end(ap);
+	return result;
+}
diff --git a/libhdhomerun/hdhomerun_os_posix.h b/libhdhomerun/hdhomerun_os_posix.h
old mode 100755
new mode 100644
index 2969b20..3d94991
--- a/libhdhomerun/hdhomerun_os_posix.h
+++ b/libhdhomerun/hdhomerun_os_posix.h
@@ -66,6 +66,9 @@
 extern LIBTYPE void msleep_approx(uint64_t ms);
 extern LIBTYPE void msleep_minimum(uint64_t ms);
 
+extern LIBTYPE bool_t hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap);
+extern LIBTYPE bool_t hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...);
+
 #ifdef __cplusplus
 }
 #endif
diff --git a/libhdhomerun/hdhomerun_os_windows.c b/libhdhomerun/hdhomerun_os_windows.c
old mode 100755
new mode 100644
index 5f844db..4de10e4
--- a/libhdhomerun/hdhomerun_os_windows.c
+++ b/libhdhomerun/hdhomerun_os_windows.c
@@ -36,7 +36,7 @@
 {
 	HCRYPTPROV hProv;
 	if (!CryptAcquireContext(&hProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
-		return (uint32_t)rand();
+		return (uint32_t)getcurrenttime();
 	}
 
 	uint32_t Result;
@@ -127,6 +127,36 @@
 	ReleaseMutex(*mutex);
 }
 
+bool_t hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap)
+{
+	if (buffer >= end) {
+		return FALSE;
+	}
+
+	int length = _vsnprintf(buffer, end - buffer - 1, fmt, ap);
+	if (length < 0) {
+		*buffer = 0;
+		return FALSE;
+	}
+
+	if (buffer + length + 1 > end) {
+		*(end - 1) = 0;
+		return FALSE;
+
+	}
+
+	return TRUE;
+}
+
+bool_t hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...)
+{
+	va_list ap;
+	va_start(ap, fmt);
+	bool_t result = hdhomerun_vsprintf(buffer, end, fmt, ap);
+	va_end(ap);
+	return result;
+}
+
 /*
  * The console output format should be set to UTF-8, however in XP and Vista this breaks batch file processing.
  * Attempting to restore on exit fails to restore if the program is terminated by the user.
diff --git a/libhdhomerun/hdhomerun_os_windows.h b/libhdhomerun/hdhomerun_os_windows.h
old mode 100755
new mode 100644
index ff9e1d4..b151fd1
--- a/libhdhomerun/hdhomerun_os_windows.h
+++ b/libhdhomerun/hdhomerun_os_windows.h
@@ -69,7 +69,6 @@
 #define atoll _atoi64
 #define strdup _strdup
 #define strcasecmp _stricmp
-#define snprintf _snprintf
 #define fseeko _fseeki64
 #define ftello _ftelli64
 #define THREAD_FUNC_PREFIX DWORD WINAPI
@@ -89,6 +88,9 @@
 extern LIBTYPE void pthread_mutex_lock(pthread_mutex_t *mutex);
 extern LIBTYPE void pthread_mutex_unlock(pthread_mutex_t *mutex);
 
+extern LIBTYPE bool_t hdhomerun_vsprintf(char *buffer, char *end, const char *fmt, va_list ap);
+extern LIBTYPE bool_t hdhomerun_sprintf(char *buffer, char *end, const char *fmt, ...);
+
 /*
  * The console output format should be set to UTF-8, however in XP and Vista this breaks batch file processing.
  * Attempting to restore on exit fails to restore if the program is terminated by the user.
diff --git a/libhdhomerun/hdhomerun_pkt.c b/libhdhomerun/hdhomerun_pkt.c
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_pkt.h b/libhdhomerun/hdhomerun_pkt.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_sock.h b/libhdhomerun/hdhomerun_sock.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_sock_posix.c b/libhdhomerun/hdhomerun_sock_posix.c
old mode 100755
new mode 100644
index 5a8c376..e59f903
--- a/libhdhomerun/hdhomerun_sock_posix.c
+++ b/libhdhomerun/hdhomerun_sock_posix.c
@@ -99,6 +99,19 @@
 		struct ifreq *ifr = (struct ifreq *)ptr;
 		ptr += _SIZEOF_ADDR_IFREQ(*ifr);
 
+		/* Flags. */
+		if (ioctl(sock, SIOCGIFFLAGS, ifr) != 0) {
+			continue;
+		}
+
+		if ((ifr->ifr_flags & IFF_UP) == 0) {
+			continue;
+		}
+		if ((ifr->ifr_flags & IFF_RUNNING) == 0) {
+			continue;
+		}
+
+		/* Local IP address. */
 		if (ioctl(sock, SIOCGIFADDR, ifr) != 0) {
 			continue;
 		}
@@ -109,6 +122,7 @@
 			continue;
 		}
 
+		/* Subnet mask. */
 		if (ioctl(sock, SIOCGIFNETMASK, ifr) != 0) {
 			continue;
 		}
@@ -116,13 +130,14 @@
 		struct sockaddr_in *subnet_mask_in = (struct sockaddr_in *)&(ifr->ifr_addr);
 		uint32_t subnet_mask = ntohl(subnet_mask_in->sin_addr.s_addr);
 
-		struct hdhomerun_local_ip_info_t *ip_info = &ip_info_list[count++];
-		ip_info->ip_addr = ip_addr;
-		ip_info->subnet_mask = subnet_mask;
-
-		if (count >= max_count) {
-			break;
+		/* Report. */
+		if (count < max_count) {
+			struct hdhomerun_local_ip_info_t *ip_info = &ip_info_list[count];
+			ip_info->ip_addr = ip_addr;
+			ip_info->subnet_mask = subnet_mask;
 		}
+
+		count++;
 	}
 
 	free(ifc.ifc_buf);
diff --git a/libhdhomerun/hdhomerun_sock_windows.c b/libhdhomerun/hdhomerun_sock_windows.c
old mode 100755
new mode 100644
index d03a059..7bb14f5
--- a/libhdhomerun/hdhomerun_sock_windows.c
+++ b/libhdhomerun/hdhomerun_sock_windows.c
@@ -91,14 +91,13 @@
 				continue;
 			}
 
-			struct hdhomerun_local_ip_info_t *ip_info = &ip_info_list[count++];
-			ip_info->ip_addr = ip_addr;
-			ip_info->subnet_mask = subnet_mask;
-
-			if (count >= max_count) {
-				break;
+			if (count < max_count) {
+				struct hdhomerun_local_ip_info_t *ip_info = &ip_info_list[count];
+				ip_info->ip_addr = ip_addr;
+				ip_info->subnet_mask = subnet_mask;
 			}
 
+			count++;
 			IPAddr = IPAddr->Next;
 		}
 
diff --git a/libhdhomerun/hdhomerun_types.h b/libhdhomerun/hdhomerun_types.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/hdhomerun_video.c b/libhdhomerun/hdhomerun_video.c
old mode 100755
new mode 100644
index 52b8dca..abfb7ac
--- a/libhdhomerun/hdhomerun_video.c
+++ b/libhdhomerun/hdhomerun_video.c
@@ -372,10 +372,10 @@
 	struct hdhomerun_video_stats_t stats;
 	hdhomerun_video_get_stats(vs, &stats);
 
-	hdhomerun_debug_printf(vs->dbg, "video sock: pkt=%lu net=%lu te=%lu miss=%lu drop=%lu\n",
-		(unsigned long)stats.packet_count, (unsigned long)stats.network_error_count,
-		(unsigned long)stats.transport_error_count, (unsigned long)stats.sequence_error_count,
-		(unsigned long)stats.overflow_error_count
+	hdhomerun_debug_printf(vs->dbg, "video sock: pkt=%u net=%u te=%u miss=%u drop=%u\n",
+		(unsigned int)stats.packet_count, (unsigned int)stats.network_error_count,
+		(unsigned int)stats.transport_error_count, (unsigned int)stats.sequence_error_count,
+		(unsigned int)stats.overflow_error_count
 	);
 }
 
diff --git a/libhdhomerun/hdhomerun_video.h b/libhdhomerun/hdhomerun_video.h
old mode 100755
new mode 100644
diff --git a/libhdhomerun/lgpl.txt b/libhdhomerun/lgpl.txt
old mode 100755
new mode 100644