Gabriele Contini
2019-12-14 e2c2e9dc1a9a7f7eafff371cdf29bb731015238c
src/library/pc-identifiers.c
@@ -23,14 +23,11 @@
#endif
#endif
static FUNCTION_RETURN generate_disk_pc_id(PcIdentifier * identifiers,
      unsigned int * num_identifiers, bool use_label);
static FUNCTION_RETURN generate_disk_pc_id(PcIdentifier *identifiers, unsigned int *num_identifiers, bool use_label);
static FUNCTION_RETURN generate_ethernet_pc_id(PcIdentifier * identifiers,
      unsigned int * num_identifiers, int use_mac);
static FUNCTION_RETURN generate_ethernet_pc_id(PcIdentifier *identifiers, unsigned int *num_identifiers, int use_mac);
static FUNCTION_RETURN generate_default_pc_id(PcIdentifier * identifiers,
      unsigned int * num_identifiers) {
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, function_return;
   unsigned int caller_identifiers, i, j, k, array_index;
@@ -49,20 +46,17 @@
      *num_identifiers = disk_num * adapter_num;
      function_return = FUNC_RET_OK;
   } else {
      adapterInfoPtr = (OsAdapterInfo*) malloc(
            (*num_identifiers) * sizeof(OsAdapterInfo));
      adapterInfoPtr = (OsAdapterInfo *)malloc((*num_identifiers) * sizeof(OsAdapterInfo));
      adapter_num = *num_identifiers;
      result_adapterInfos = getAdapterInfos(adapterInfoPtr, &adapter_num);
      if (result_adapterInfos != FUNC_RET_OK
            && result_adapterInfos != FUNC_RET_BUFFER_TOO_SMALL) {
      if (result_adapterInfos != FUNC_RET_OK && result_adapterInfos != FUNC_RET_BUFFER_TOO_SMALL) {
         free(adapterInfoPtr);
         return generate_disk_pc_id(identifiers, num_identifiers, false);
      }
      diskInfoPtr = (DiskInfo*) malloc((*num_identifiers) * sizeof(DiskInfo));
      disk_num = *num_identifiers;
      result_diskinfos = getDiskInfos(diskInfoPtr, &disk_num);
      if (result_diskinfos != FUNC_RET_OK
            && result_diskinfos != FUNC_RET_BUFFER_TOO_SMALL) {
      if (result_diskinfos != FUNC_RET_OK && result_diskinfos != FUNC_RET_BUFFER_TOO_SMALL) {
         free(diskInfoPtr);
         free(adapterInfoPtr);
         return generate_ethernet_pc_id(identifiers, num_identifiers, true);
@@ -79,8 +73,7 @@
               goto end;
            }
            for (k = 0; k < 6; k++)
               identifiers[array_index][k] = diskInfoPtr[i].disk_sn[k + 2]
                     ^ adapterInfoPtr[j].mac_address[k + 2];
               identifiers[array_index][k] = diskInfoPtr[i].disk_sn[k + 2] ^ adapterInfoPtr[j].mac_address[k + 2];
         }
      }
end:
@@ -95,8 +88,7 @@
   return function_return;
}
static FUNCTION_RETURN generate_ethernet_pc_id(PcIdentifier * identifiers,
      unsigned int * num_identifiers, int use_mac) {
static FUNCTION_RETURN generate_ethernet_pc_id(PcIdentifier *identifiers, unsigned int *num_identifiers, int use_mac) {
   FUNCTION_RETURN result_adapterInfos;
   unsigned int j, k;
   OsAdapterInfo *adapterInfos;
@@ -104,18 +96,15 @@
   if (identifiers == NULL || *num_identifiers == 0) {
      result_adapterInfos = getAdapterInfos(NULL, &adapters);
      if (result_adapterInfos == FUNC_RET_OK
            || result_adapterInfos == FUNC_RET_BUFFER_TOO_SMALL) {
      if (result_adapterInfos == FUNC_RET_OK || result_adapterInfos == FUNC_RET_BUFFER_TOO_SMALL) {
         *num_identifiers = adapters;
         result_adapterInfos = FUNC_RET_OK;
      }
   } else {
      defined_adapters = adapters = *num_identifiers;
      adapterInfos = (OsAdapterInfo*) malloc(
            adapters * sizeof(OsAdapterInfo));
      adapterInfos = (OsAdapterInfo *)malloc(adapters * sizeof(OsAdapterInfo));
      result_adapterInfos = getAdapterInfos(adapterInfos, &adapters);
      if (result_adapterInfos == FUNC_RET_BUFFER_TOO_SMALL
            || result_adapterInfos == FUNC_RET_OK) {
      if (result_adapterInfos == FUNC_RET_BUFFER_TOO_SMALL || result_adapterInfos == FUNC_RET_OK) {
         for (j = 0; j < adapters; j++) {
            for (k = 0; k < 6; k++)
               if (use_mac) {
@@ -130,17 +119,14 @@
                  }
               }
         }
         result_adapterInfos = (
               adapters > defined_adapters ?
                     FUNC_RET_BUFFER_TOO_SMALL : FUNC_RET_OK);
         result_adapterInfos = (adapters > defined_adapters ? FUNC_RET_BUFFER_TOO_SMALL : FUNC_RET_OK);
      }
      free(adapterInfos);
   }
   return result_adapterInfos;
}
static FUNCTION_RETURN generate_disk_pc_id(PcIdentifier * identifiers,
      unsigned int * num_identifiers, bool use_label) {
static FUNCTION_RETURN generate_disk_pc_id(PcIdentifier *identifiers, unsigned int *num_identifiers, bool use_label) {
   size_t disk_num, available_disk_info = 0;
   FUNCTION_RETURN result_diskinfos;
   unsigned int i, j;
@@ -179,14 +165,12 @@
      if (use_label) {
         if (diskInfos[i].label[0] != 0) {
            memset(identifiers[j], 0, sizeof(PcIdentifier)); //!!!!!!!
            strncpy((char*)identifiers[j], diskInfos[i].label,
                  sizeof(PcIdentifier));
            strncpy((char *)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));
            memcpy(identifiers[j], &diskInfos[i].disk_sn[2], sizeof(PcIdentifier));
            j++;
         }
      }
@@ -211,26 +195,25 @@
 * @return
 */
FUNCTION_RETURN generate_pc_id(PcIdentifier * identifiers,
      unsigned int * array_size, IDENTIFICATION_STRATEGY strategy) {
FUNCTION_RETURN generate_pc_id(PcIdentifier *identifiers, unsigned int *array_size, IDENTIFICATION_STRATEGY strategy) {
   FUNCTION_RETURN result;
   unsigned int i, j;
   const unsigned int original_array_size = *array_size;
   unsigned char strategy_num;
   switch (strategy) {
   case DEFAULT:
      case STRATEGY_DEFAULT:
      result = generate_default_pc_id(identifiers, array_size);
      break;
   case ETHERNET:
      case STRATEGY_ETHERNET:
      result = generate_ethernet_pc_id(identifiers, array_size, true);
      break;
   case IP_ADDRESS:
      case STRATEGY_IP_ADDRESS:
      result = generate_ethernet_pc_id(identifiers, array_size, false);
      break;
   case DISK_NUM:
      case STRATEGY_DISK_NUM:
      result = generate_disk_pc_id(identifiers, array_size, false);
      break;
   case DISK_LABEL:
      case STRATEGY_DISK_LABEL:
      result = generate_disk_pc_id(identifiers, array_size, true);
      break;
   default:
@@ -260,8 +243,7 @@
   int i;
   char DoInvert;
   for (i = 0; i < 2; ++i)
      CRC[i] = 0;                    // Init before calculation
   for (i = 0; i < 2; ++i) CRC[i] = 0;  // Init before calculation
   for (i = 0; i < strlen(BitString); ++i) {
      DoInvert = ('1' == BitString[i]) ^ CRC[1];         // XOR required?
@@ -270,15 +252,13 @@
      CRC[0] = DoInvert;
   }
   for (i = 0; i < 2; ++i)
      Res[1 - i] = CRC[i] ? '1' : '0'; // Convert binary to ASCII
   for (i = 0; i < 2; ++i) Res[1 - i] = CRC[i] ? '1' : '0';  // Convert binary to ASCII
   Res[2] = 0;                                         // Set string terminator
   return (Res);
}
FUNCTION_RETURN encode_pc_id(PcIdentifier identifier1, PcIdentifier identifier2,
      PcSignature pc_identifier_out) {
FUNCTION_RETURN encode_pc_id(PcIdentifier identifier1, PcIdentifier identifier2, PcSignature pc_identifier_out) {
   //TODO base62 encoding, now uses base64
   PcIdentifier concat_identifiers[2];
   char* b64_data = NULL;
@@ -292,19 +272,15 @@
      free(b64_data);
      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]);
   sprintf(pc_identifier_out, "%.4s-%.4s-%.4s-%.4s", &b64_data[0], &b64_data[4], &b64_data[8], &b64_data[12]);
   //free(concat_identifiers);
   free(b64_data);
   return FUNC_RET_OK;
}
FUNCTION_RETURN parity_check_id(PcSignature pc_identifier) {
   return FUNC_RET_OK;
}
FUNCTION_RETURN parity_check_id(PcSignature pc_identifier) { return FUNC_RET_OK; }
FUNCTION_RETURN generate_user_pc_signature(PcSignature identifier_out,
      IDENTIFICATION_STRATEGY strategy) {
FUNCTION_RETURN generate_user_pc_signature(PcSignature identifier_out, IDENTIFICATION_STRATEGY strategy) {
   FUNCTION_RETURN result;
   PcIdentifier* identifiers;
   unsigned int req_buffer_size = 0;
@@ -316,8 +292,7 @@
      return FUNC_RET_ERROR;
   }
   req_buffer_size = req_buffer_size < 2 ? 2 : req_buffer_size;
   identifiers = (PcIdentifier *) malloc(
         sizeof(PcIdentifier) * req_buffer_size);
   identifiers = (PcIdentifier *)malloc(sizeof(PcIdentifier) * req_buffer_size);
   memset(identifiers, 0, sizeof(PcIdentifier) * req_buffer_size);
   result = generate_pc_id(identifiers, &req_buffer_size, strategy);
   if (result != FUNC_RET_OK) {
@@ -343,24 +318,22 @@
 * @param str_code: the code in the string format XXXX-XXXX-XXXX-XXXX
 * @return
 */
static FUNCTION_RETURN decode_pc_id(PcIdentifier identifier1_out,
      PcIdentifier identifier2_out, PcSignature pc_signature_in) {
static FUNCTION_RETURN decode_pc_id(PcIdentifier identifier1_out, PcIdentifier identifier2_out,
                           PcSignature pc_signature_in) {
   //TODO base62 encoding, now uses base64
   unsigned char * concat_identifiers = NULL;
   char base64ids[17];
   int identifiers_size;
   sscanf(pc_signature_in, "%4s-%4s-%4s-%4s", &base64ids[0], &base64ids[4],
         &base64ids[8], &base64ids[12]);
   sscanf(pc_signature_in, "%4s-%4s-%4s-%4s", &base64ids[0], &base64ids[4], &base64ids[8], &base64ids[12]);
   concat_identifiers = unbase64(base64ids, 16, &identifiers_size);
   if (identifiers_size > sizeof(PcIdentifier) * 2) {
      free(concat_identifiers);
      return FUNC_RET_BUFFER_TOO_SMALL;
   }
   memcpy(identifier1_out, concat_identifiers, sizeof(PcIdentifier));
   memcpy(identifier2_out, concat_identifiers + sizeof(PcIdentifier),
         sizeof(PcIdentifier));
   memcpy(identifier2_out, concat_identifiers + sizeof(PcIdentifier), sizeof(PcIdentifier));
   free(concat_identifiers);
   return FUNC_RET_OK;
}
@@ -401,23 +374,22 @@
         }
         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);
         calculated_identifiers = (PcIdentifier *)malloc(sizeof(PcIdentifier) * calc_identifiers_size);
         memset(calculated_identifiers, 0, sizeof(PcIdentifier) * calc_identifiers_size);
         generate_pc_id(calculated_identifiers, &calc_identifiers_size,
               current_strategy_id);
         generate_pc_id(calculated_identifiers, &calc_identifiers_size, current_strategy_id);
      }
      //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",
         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);
            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))) {
         if (!memcmp(user_identifiers[i], calculated_identifiers[j], sizeof(PcIdentifier))) {
            free(calculated_identifiers);
            return LICENSE_OK;
         }