/*
 * object-attr.c - abstract object attribute access
 * Copyright (c) 2016,2019 Red Hat Inc., Durham, North Carolina.
 * All Rights Reserved.
 *
 * This software may be freely redistributed and/or modified under the
 * terms of the GNU General Public License as published by the Free
 * Software Foundation; either version 2, or (at your option) any
 * later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING. If not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor
 * Boston, MA 02110-1335, USA.
 *
 * Authors:
 *   Steve Grubb <sgrubb@redhat.com>
 *   Radovan Sroka <rsroka@redhat.com>
 */

#include "config.h"
#include <stddef.h>	// For NULL
#include <stdbool.h>
#include <string.h>

#include "message.h"
#include "object-attr.h"

static const nv_t table[] = {
{	ALL_OBJ, 	"all" },
{	PATH, 		"path" },
{	ODIR, 		"dir" },
{	DEVICE,		"device" },
{	FTYPE,		"ftype" },
{	OBJ_TRUST,	"trust"},
{	FILE_HASH,	"filehash" },
};

#define MAX_OBJECTS (sizeof(table)/sizeof(table[0]))

int obj_name_to_val(const char *name)
{
	static bool warned;

	// Accept the legacy name for compatibility with older rule sets.
	if (strcmp(name, "sha256hash") == 0) {
		/* Announce deprecation once per start while keeping the alias usable. */
		if (!warned) {
			msg(LOG_NOTICE,
			    "sha256hash object name is deprecated; use filehash instead");
			warned = true;
		}

		return FILE_HASH;
	}

	unsigned int i = 0;
	while (i < MAX_OBJECTS) {
		if (strcmp(name, table[i].name) == 0)
			return table[i].value;
		i++;
	}
	return -1;
}

const char *obj_val_to_name(unsigned int v)
{
	if (v < OBJ_START || v > OBJ_END)
		return NULL;

	unsigned int index = v - OBJ_START;
	if (index < MAX_OBJECTS)
		return table[index].name;

	return NULL;
}
