From 345c7b764f0721e9bbc993a3cd7f803fd42cf943 Mon Sep 17 00:00:00 2001
From: open-license-manager <rillf@maildrop.cc>
Date: 周一, 15 9月 2014 06:58:38 +0800
Subject: [PATCH] moving files for refactoring

---
 src/library/pc-identifiers.c |  176 ++++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 109 insertions(+), 67 deletions(-)

diff --git a/src/library/pc-identifiers.c b/src/library/pc-identifiers.c
index a2f7ad8..75d70fe 100644
--- a/src/library/pc-identifiers.c
+++ b/src/library/pc-identifiers.c
@@ -2,45 +2,51 @@
  * pc-identifiers.c
  *
  *  Created on: Apr 16, 2014
- *      Author: devel
+ *      
  */
 
 #include "os/os.h"
 #include "pc-identifiers.h"
 #include <stdlib.h>
 #include <string.h>
-#include <stdbool.h>
 #include "base/base64.h"
+#include "base/base.h"
+#ifdef __linux__
+#include <stdbool.h>
+#include <valgrind/memcheck.h>
+#else
+#include <Windows.h>
+#endif
 
 static FUNCTION_RETURN generate_default_pc_id(PcIdentifier * identifiers,
 		unsigned int * num_identifiers) {
 	size_t adapter_num, disk_num;
 	FUNCTION_RETURN result_adapterInfos, result_diskinfos;
-	unsigned int required_id_size, i, j, k;
+	unsigned int required_id_size, defined_identifiers, i, j, k;
 	DiskInfo * diskInfos;
-	AdapterInfo *adapterInfos;
+	OsAdapterInfo *adapterInfos;
 
 	result_adapterInfos = getAdapterInfos(NULL, &adapter_num);
-	if (result_adapterInfos != OK) {
+	if (result_adapterInfos != FUNC_RET_OK) {
 		//call generate_disk_pc_id;
 		return result_adapterInfos;
 	}
 	result_diskinfos = getDiskInfos(NULL, &disk_num);
-	if (result_diskinfos == OK) {
+	if (result_diskinfos == FUNC_RET_OK) {
 		required_id_size = disk_num * adapter_num;
 	} else {
 		required_id_size = disk_num;
 	}
-	int defined_identifiers = *num_identifiers;
+	defined_identifiers = *num_identifiers;
 	*num_identifiers = required_id_size;
 	if (identifiers == NULL) {
-		return OK;
+		return FUNC_RET_OK;
 	} else if (required_id_size > defined_identifiers) {
-		return BUFFER_TOO_SMALL;
+		return FUNC_RET_BUFFER_TOO_SMALL;
 	}
 	diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo));
 	result_diskinfos = getDiskInfos(diskInfos, &disk_num);
-	adapterInfos = (AdapterInfo*) malloc(adapter_num * sizeof(AdapterInfo));
+	adapterInfos = (OsAdapterInfo*) malloc(adapter_num * sizeof(OsAdapterInfo));
 	result_adapterInfos = getAdapterInfos(adapterInfos, &adapter_num);
 	for (i = 0; i < disk_num; i++) {
 		for (j = 0; j < adapter_num; j++) {
@@ -53,32 +59,33 @@
 
 	free(diskInfos);
 	free(adapterInfos);
-	return OK;
+	return FUNC_RET_OK;
 }
 
 static FUNCTION_RETURN generate_ethernet_pc_id(PcIdentifier * identifiers,
-		unsigned int * num_identifiers, bool use_mac) {
+		unsigned int * num_identifiers, int use_mac) {
 	size_t adapters;
+	int defined_adapters;
 	FUNCTION_RETURN result_adapterInfos;
 	unsigned int i, j, k;
-	AdapterInfo *adapterInfos;
+	OsAdapterInfo *adapterInfos;
 
 	result_adapterInfos = getAdapterInfos(NULL, &adapters);
-	if (result_adapterInfos != OK) {
+	if (result_adapterInfos != FUNC_RET_OK) {
 		return result_adapterInfos;
 	}
 
-	int defined_adapters = *num_identifiers;
+	defined_adapters = *num_identifiers;
 	*num_identifiers = adapters;
 	if (identifiers == NULL) {
-		return OK;
+		return FUNC_RET_OK;
 	} else if (adapters > defined_adapters) {
-		return BUFFER_TOO_SMALL;
+		return FUNC_RET_BUFFER_TOO_SMALL;
 	}
 
-	adapterInfos = (AdapterInfo*) malloc(adapters * sizeof(AdapterInfo));
+	adapterInfos = (OsAdapterInfo*)malloc(adapters * sizeof(OsAdapterInfo));
 	result_adapterInfos = getAdapterInfos(adapterInfos, &adapters);
-	for (j = 0; j < adapters; i++) {
+	for (j = 0; j < adapters; j++) {
 		for (k = 0; k < 6; k++)
 			if (use_mac) {
 				identifiers[j][k] = adapterInfos[j].mac_address[k + 2];
@@ -93,43 +100,61 @@
 			}
 	}
 	free(adapterInfos);
-	return OK;
+	return FUNC_RET_OK;
 }
 
 static FUNCTION_RETURN generate_disk_pc_id(PcIdentifier * identifiers,
 		unsigned int * num_identifiers, bool use_label) {
-	size_t disk_num;
+	size_t disk_num, available_disk_info=0;
 	FUNCTION_RETURN result_diskinfos;
-	unsigned int i, k;
+	unsigned int i, k, j;
+	int defined_identifiers;
+	char firstChar;
 	DiskInfo * diskInfos;
 
 	result_diskinfos = getDiskInfos(NULL, &disk_num);
-	if (result_diskinfos != OK) {
+	if (result_diskinfos != FUNC_RET_OK) {
 		return result_diskinfos;
 	}
-
-	int defined_identifiers = *num_identifiers;
-	*num_identifiers = disk_num;
-	if (identifiers == NULL) {
-		return OK;
-	} else if (disk_num > defined_identifiers) {
-		return BUFFER_TOO_SMALL;
+	diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo));
+	//memset(diskInfos,0,disk_num * sizeof(DiskInfo));
+	result_diskinfos = getDiskInfos(diskInfos, &disk_num);
+	if (result_diskinfos != FUNC_RET_OK) {
+		free(diskInfos);
+		return result_diskinfos;
+	}
+	for (i = 0; i < disk_num; i++) {
+		firstChar = use_label ? diskInfos[i].label[0] : diskInfos[i].disk_sn[0];
+		available_disk_info += firstChar == 0 ? 0 : 1;
 	}
 
-	diskInfos = (DiskInfo*) malloc(disk_num * sizeof(DiskInfo));
-	result_diskinfos = getDiskInfos(diskInfos, &disk_num);
+	defined_identifiers = *num_identifiers;
+	*num_identifiers = available_disk_info;
+	if (identifiers == NULL) {
+		free(diskInfos);
+		return FUNC_RET_OK;
+	} else if (available_disk_info > defined_identifiers) {
+		free(diskInfos);
+		return FUNC_RET_BUFFER_TOO_SMALL;
+	}
 
+	j=0;
 	for (i = 0; i < disk_num; i++) {
-		for (k = 0; k < 6; k++) {
-			if (use_label) {
-				identifiers[i][k] = diskInfos[i].label[k];
-			} else {
-				identifiers[i][k] = diskInfos[i].disk_sn[k + 2];
+		if(use_label){
+			if(diskInfos[i].label[0]!=0){
+				memset(identifiers[j],0,sizeof(PcIdentifier)); //!!!!!!!
+				strncpy(identifiers[j],diskInfos[i].label,sizeof(PcIdentifier));
+				j++;
+			}
+		}else{
+			if(diskInfos[i].disk_sn[0]!=0){
+				memcpy(identifiers[j],&diskInfos[i].disk_sn[2],sizeof(PcIdentifier));
+				j++;
 			}
 		}
 	}
 	free(diskInfos);
-	return OK;
+	return FUNC_RET_OK;
 }
 
 /**
@@ -147,6 +172,7 @@
  * @param
  * @return
  */
+
 FUNCTION_RETURN generate_pc_id(PcIdentifier * identifiers,
 		unsigned int * array_size, IDENTIFICATION_STRATEGY strategy) {
 	FUNCTION_RETURN result;
@@ -158,10 +184,10 @@
 		result = generate_default_pc_id(identifiers, array_size);
 		break;
 	case ETHERNET:
-		result = generate_ethernet_pc_id(identifiers, array_size, true);
+		result = generate_ethernet_pc_id(identifiers, array_size, true );
 		break;
 	case IP_ADDRESS:
-		result = generate_ethernet_pc_id(identifiers, array_size, false);
+		result = generate_ethernet_pc_id(identifiers, array_size, false );
 		break;
 	case DISK_NUM:
 		result = generate_disk_pc_id(identifiers, array_size, false);
@@ -170,10 +196,10 @@
 		result = generate_disk_pc_id(identifiers, array_size, true);
 		break;
 	default:
-		return ERROR;
+		return FUNC_RET_ERROR;
 	}
 
-	if (result == OK && identifiers != NULL) {
+	if (result == FUNC_RET_OK && identifiers != NULL) {
 		strategy_num = strategy << 5;
 		for (i = 0; i < *array_size; i++) {
 			//encode strategy in the first three bits of the pc_identifier
@@ -181,7 +207,7 @@
 		}
 		//fill array if larger
 		for (i = *array_size; i < original_array_size; i++) {
-			identifiers[i][0] = STRATEGY_UNKNOWN;
+			identifiers[i][0] = STRATEGY_UNKNOWN << 5;
 			for (j = 1; j < sizeof(PcIdentifier); j++) {
 				identifiers[i][j] = 42; //padding
 			}
@@ -215,27 +241,28 @@
 
 FUNCTION_RETURN encode_pc_id(PcIdentifier identifier1, PcIdentifier identifier2,
 		PcSignature pc_identifier_out) {
-	//TODO base62 encoding, now uses base64
+//TODO base62 encoding, now uses base64
 	PcIdentifier concat_identifiers[2];
+	char* b64_data;
 	int b64_size = 0;
 	size_t concatIdentifiersSize = sizeof(PcIdentifier) * 2;
-	//concat_identifiers = (PcIdentifier *) malloc(concatIdentifiersSize);
+//concat_identifiers = (PcIdentifier *) malloc(concatIdentifiersSize);
 	memcpy(&concat_identifiers[0], identifier1, sizeof(PcIdentifier));
 	memcpy(&concat_identifiers[1], identifier2, sizeof(PcIdentifier));
-	char* b64_data = base64(concat_identifiers, concatIdentifiersSize,
+	b64_data = base64(concat_identifiers, concatIdentifiersSize,
 			&b64_size);
 	if (b64_size > sizeof(PcSignature)) {
-		return BUFFER_TOO_SMALL;
+		return FUNC_RET_BUFFER_TOO_SMALL;
 	}
 	sprintf(pc_identifier_out, "%.4s-%.4s-%.4s-%.4s", &b64_data[0],
 			&b64_data[4], &b64_data[8], &b64_data[12]);
-	//free(concat_identifiers);
+//free(concat_identifiers);
 	free(b64_data);
-	return OK;
+	return FUNC_RET_OK;
 }
 
 FUNCTION_RETURN parity_check_id(PcSignature pc_identifier) {
-	return OK;
+	return FUNC_RET_OK;
 }
 
 FUNCTION_RETURN generate_user_pc_signature(PcSignature identifier_out,
@@ -244,21 +271,28 @@
 	PcIdentifier* identifiers;
 	unsigned int req_buffer_size = 0;
 	result = generate_pc_id(NULL, &req_buffer_size, strategy);
-	if (result != OK) {
+	if (result != FUNC_RET_OK) {
 		return result;
 	}
 	if (req_buffer_size == 0) {
-		return ERROR;
+		return FUNC_RET_ERROR;
 	}
 	req_buffer_size = req_buffer_size < 2 ? 2 : req_buffer_size;
 	identifiers = (PcIdentifier *) malloc(
 			sizeof(PcIdentifier) * req_buffer_size);
 	result = generate_pc_id(identifiers, &req_buffer_size, strategy);
-	if (result != OK) {
+	if (result != FUNC_RET_OK) {
 		free(identifiers);
 		return result;
 	}
+#ifdef __linux__
+	VALGRIND_CHECK_VALUE_IS_DEFINED(identifiers[0]);
+	VALGRIND_CHECK_VALUE_IS_DEFINED(identifiers[1]);
+#endif
 	result = encode_pc_id(identifiers[0], identifiers[1], identifier_out);
+#ifdef __linux__
+	VALGRIND_CHECK_VALUE_IS_DEFINED(identifier_out);
+#endif
 	free(identifiers);
 	return result;
 }
@@ -272,24 +306,23 @@
  */
 static FUNCTION_RETURN decode_pc_id(PcIdentifier identifier1_out,
 		PcIdentifier identifier2_out, PcSignature pc_signature_in) {
-	//TODO base62 encoding, now uses base64
+//TODO base62 encoding, now uses base64
 
 	unsigned char * concat_identifiers;
 	char base64ids[17];
 	int identifiers_size;
 
-	sscanf(pc_signature_in, "%.4s-%.4s-%.4s-%.4s", &base64ids[0], &base64ids[4],
+	sscanf(pc_signature_in, "%4s-%4s-%4s-%4s", &base64ids[0], &base64ids[4],
 			&base64ids[8], &base64ids[12]);
-	concat_identifiers = unbase64(base64ids, 16,
-			&identifiers_size);
+	concat_identifiers = unbase64(base64ids, 16, &identifiers_size);
 	if (identifiers_size > sizeof(PcIdentifier) * 2) {
-		return BUFFER_TOO_SMALL;
+		return FUNC_RET_BUFFER_TOO_SMALL;
 	}
 	memcpy(identifier1_out, concat_identifiers, sizeof(PcIdentifier));
 	memcpy(identifier2_out, concat_identifiers + sizeof(PcIdentifier),
 			sizeof(PcIdentifier));
 	free(concat_identifiers);
-	return OK;
+	return FUNC_RET_OK;
 }
 
 static IDENTIFICATION_STRATEGY strategy_from_pc_id(PcIdentifier identifier) {
@@ -300,17 +333,19 @@
 	PcIdentifier user_identifiers[2];
 	FUNCTION_RETURN result;
 	IDENTIFICATION_STRATEGY previous_strategy_id, current_strategy_id;
-	PcIdentifier* calculated_identifiers;
-	unsigned int calc_identifiers_size;
-	int i, j;
-	//bool found;
-
+	PcIdentifier* calculated_identifiers = NULL;
+	unsigned int calc_identifiers_size = 0;
+	int i = 0, j = 0;
+//bool found;
+#ifdef _DEBUG
+	printf("Comparing pc identifiers: \n");
+#endif
 	result = decode_pc_id(user_identifiers[0], user_identifiers[1], str_code);
-	if (result != OK) {
+	if (result != FUNC_RET_OK) {
 		return result;
 	}
 	previous_strategy_id = STRATEGY_UNKNOWN;
-	//found = false;
+//found = false;
 	for (i = 0; i < 2; i++) {
 		current_strategy_id = strategy_from_pc_id(user_identifiers[i]);
 		if (current_strategy_id == STRATEGY_UNKNOWN) {
@@ -320,7 +355,7 @@
 			if (calculated_identifiers != NULL) {
 				free(calculated_identifiers);
 			}
-			current_strategy_id = previous_strategy_id;
+			previous_strategy_id = current_strategy_id;
 			generate_pc_id(NULL, &calc_identifiers_size, current_strategy_id);
 			calculated_identifiers = (PcIdentifier *) malloc(
 					sizeof(PcIdentifier) * calc_identifiers_size);
@@ -329,6 +364,13 @@
 		}
 		//maybe skip the byte 0
 		for (j = 0; j < calc_identifiers_size; j++) {
+#ifdef _DEBUG
+			printf("generated id: %02x%02x%02x%02x%02x%02x index %d, user_supplied id %02x%02x%02x%02x%02x%02x idx: %d\n",
+					calculated_identifiers[j][0],calculated_identifiers[j][1],calculated_identifiers[j][2],
+					calculated_identifiers[j][3],calculated_identifiers[j][4],calculated_identifiers[j][5],j,
+					user_identifiers[i][0],user_identifiers[i][1],user_identifiers[i][2],user_identifiers[i][3],user_identifiers[i][4],user_identifiers[i][5],i);
+
+#endif
 			if (!memcmp(user_identifiers[i], calculated_identifiers[j],
 					sizeof(PcIdentifier))) {
 				free(calculated_identifiers);

--
Gitblit v1.9.1