<commit_before>/*
 * Copyright (c) 2017 <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */ 

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/cpuctl.h>
#include <sys/errno.h>
#include <sys/ioctl.h>

#include "cpuid.h"


// --------


/*
 * Gives the CPU model.
 *
 *  - model: Pointer to a char array with minimum length 49.
 */
void getcpumodel(char *model) {
	cpuctl_cpuid_count_args_t cpuid;


	// Check if supported.
	cpuid.level = 0x80000000;
	cpuid.level_type = 0;

	if (ioctl(cmdopts.fd, CPUCTL_CPUID_COUNT, &cpuid) == -1) {
		exit_error(1, "ERROR: ioctl CPUCTL_CPUID_COUNT failed: %s\n", errno);
	}

	if (cpuid.data[0] < 0x80000004) {
		strcpy(model, "Unknown CPU Model");
		return;
	}

	// Block 0
	cpuid.level = 0x80000002;
	cpuid.level_type = 0;

	if (ioctl(cmdopts.fd, CPUCTL_CPUID_COUNT, &cpuid) == -1) {
		exit_error(1, "ERROR: ioctl CPUCTL_CPUID_COUNT failed: %s\n", errno);
	}

	memcpy(model, cpuid.data, sizeof(uint32_t));
	memcpy(model + 4, cpuid.data + 1, sizeof(uint32_t));
	memcpy(model + 8, cpuid.data + 2, sizeof(uint32_t));
	memcpy(model + 12, cpuid.data + 3, sizeof(uint32_t));


	// Block 1
	cpuid.level = 0x80000003;
	cpuid.level_type = 0;

	if (ioctl(cmdopts.fd, CPUCTL_CPUID_COUNT, &cpuid) == -1) {
		exit_error(1, "ERROR: ioctl CPUCTL_CPUID_COUNT failed: %s\n", errno);
	}

	memcpy(model + 16 , cpuid.data, sizeof(uint32_t));
	memcpy(model + 20, cpuid.data + 1, sizeof(uint32_t));
	memcpy(model + 24, cpuid.data + 2, sizeof(uint32_t));
	memcpy(model + 28, cpuid.data + 3, sizeof(uint32_t));


	// Block 2
	cpuid.level = 0x80000004;
	cpuid.level_type = 0;

	if (ioctl(cmdopts.fd, CPUCTL_CPUID_COUNT, &cpuid) == -1) {
		exit_error(1, "ERROR: ioctl CPUCTL_CPUID_COUNT failed: %s\n", errno);
	}

	memcpy(model + 32 , cpuid.data, sizeof(uint32_t));
	memcpy(model + 36, cpuid.data + 1, sizeof(uint32_t));
	memcpy(model + 40, cpuid.data + 2, sizeof(uint32_t));
	memcpy(model + 44, cpuid.data + 3, sizeof(uint32_t));

}

/*
 * Gives the CPU vendor.
 *
 *  - vendor: Pointer to a char array with minimum length 13.
 */
void getcpuvendor(char *vendor) {
	cpuctl_cpuid_count_args_t cpuid;

	cpuid.level = 0x0;
	cpuid.level_type = 0;

	if (ioctl(cmdopts.fd, CPUCTL_CPUID_COUNT, &cpuid) == -1) {
		exit_error(1, "ERROR: ioctl CPUCTL_CPUID_COUNT failed: %s\n", errno);
	}

	// Yes, this is ugly.
	memcpy(vendor, cpuid.data + 1, sizeof(uint32_t));
	memcpy(vendor + 4, cpuid.data + 3, sizeof(uint32_t));
	memcpy(vendor + 8, cpuid.data + 2, sizeof(uint32_t));
	vendor[12] = '\0';
}


/*
 * Returns the CPU family.
 */
const char *getcpufamily(void) {
	cpuctl_cpuid_count_args_t cpuid;<commit_msg>Remove superfluous whitespaces from the CPU model.<commit_after>/*
 * Copyright (c) 2017 <NAME>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */ 

#include <ctype.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/cpuctl.h>
#include <sys/errno.h>
#include <sys/ioctl.h>

#include "cpuid.h"


// --------


/*
 * Gives the CPU model.
 *
 *  - model: Pointer to a char array with minimum length 49.
 */
void getcpumodel(char *model) {
	char *tmp;
	cpuctl_cpuid_count_args_t cpuid;
	uint32_t count;


	// Check if supported.
	cpuid.level = 0x80000000;
	cpuid.level_type = 0;

	if (ioctl(cmdopts.fd, CPUCTL_CPUID_COUNT, &cpuid) == -1) {
		exit_error(1, "ERROR: ioctl CPUCTL_CPUID_COUNT failed: %s\n", errno);
	}

	if (cpuid.data[0] < 0x80000004) {
		strcpy(model, "Unknown CPU Model");
		return;
	}

	// Block 0
	cpuid.level = 0x80000002;
	cpuid.level_type = 0;

	if (ioctl(cmdopts.fd, CPUCTL_CPUID_COUNT, &cpuid) == -1) {
		exit_error(1, "ERROR: ioctl CPUCTL_CPUID_COUNT failed: %s\n", errno);
	}

	memcpy(model, cpuid.data, sizeof(uint32_t));
	memcpy(model + 4, cpuid.data + 1, sizeof(uint32_t));
	memcpy(model + 8, cpuid.data + 2, sizeof(uint32_t));
	memcpy(model + 12, cpuid.data + 3, sizeof(uint32_t));


	// Block 1
	cpuid.level = 0x80000003;
	cpuid.level_type = 0;

	if (ioctl(cmdopts.fd, CPUCTL_CPUID_COUNT, &cpuid) == -1) {
		exit_error(1, "ERROR: ioctl CPUCTL_CPUID_COUNT failed: %s\n", errno);
	}

	memcpy(model + 16 , cpuid.data, sizeof(uint32_t));
	memcpy(model + 20, cpuid.data + 1, sizeof(uint32_t));
	memcpy(model + 24, cpuid.data + 2, sizeof(uint32_t));
	memcpy(model + 28, cpuid.data + 3, sizeof(uint32_t));


	// Block 2
	cpuid.level = 0x80000004;
	cpuid.level_type = 0;

	if (ioctl(cmdopts.fd, CPUCTL_CPUID_COUNT, &cpuid) == -1) {
		exit_error(1, "ERROR: ioctl CPUCTL_CPUID_COUNT failed: %s\n", errno);
	}

	memcpy(model + 32 , cpuid.data, sizeof(uint32_t));
	memcpy(model + 36, cpuid.data + 1, sizeof(uint32_t));
	memcpy(model + 40, cpuid.data + 2, sizeof(uint32_t));
	memcpy(model + 44, cpuid.data + 3, sizeof(uint32_t));

	// Remove superfluous whitespaces
	count = 0;
	tmp = model;

	while (*tmp != '\0') {
		if (!isspace(*tmp)) {
			break;
		}
		 count++;
		 tmp++;
	}

	if (count < strlen(model)) {
		memmove(model, model + count, 49 - count);
	}

	tmp = model + strlen(model);

	while (tmp != model) {
		if (!isspace(*tmp)) {
			break;
		}

		*tmp = '\0';
		tmp--;
	}

}

/*
 * Gives the CPU vendor.
 *
 *  - vendor: Pointer to a char array with minimum length 13.
 */
void getcpuvendor(char *vendor) {
	cpuctl_cpuid_count_args_t cpuid;

	cpuid.level = 0x0;
	cpuid.level_type = 0;

	if (ioctl(cmdopts.fd, CPUCTL_CPUID_COUNT, &cpuid) == -1) {
		exit_error(1, "ERROR: ioctl CPUCTL_CPUID_COUNT failed: %s\n", errno);
	}

	// Yes, this is ugly.
	memcpy(vendor, cpuid.data + 1, sizeof(uint32_t));
	memcpy(vendor + 4, cpuid.data + 3, sizeof(uint32_t));
	memcpy(vendor + 8, cpuid.data + 2, sizeof(uint32_t));
	vendor[12] = '\0';
}


/*
 * Returns the CPU family.
 */
const char *getcpufamily(void) {
	cpuctl_cpuid_count_args_t cpuid;
{'fraction_non_alphanumeric': 0.12854717438758187, 'fraction_numerical': 0.03553237933543536, 'mean_word_length': 4.004247572815534, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '703921', 'n_tokens_mistral': 3275, 'n_tokens_neox': 2899, 'n_words': 896}