summaryrefslogtreecommitdiffstats
path: root/tinySIGCOMP/src/tcomp_state.c
blob: af9be22a6b484ae206f1aaa1e74177c987ae573c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* Copyright (C) 2010-2011 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango[dot]org>
*	
* This file is part of Open Source Doubango Framework.
*
* DOUBANGO is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*	
* DOUBANGO 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 DOUBANGO.
*
*/

/**@file tcomp_state.c
 * @brief  SigComp state.
 *
 * @author Mamadou Diop <diopmamadou(at)yahoo.fr>
 *

 */
#include "tcomp_state.h"
#include "tsk_memory.h"
#include "tsk_debug.h"
#include "tsk_sha1.h"

/** Creates new SigComp state.
*/
tcomp_state_t* tcomp_state_create(uint32_t length, uint32_t address, uint32_t instruction, uint32_t minimum_access_length, uint32_t retention_priority)
{
	tcomp_state_t *state;
	if((state = tsk_object_new(tcomp_state_def_t))){
		state->length = length;
		state->address = address;
		state->instruction = instruction;
		state->minimum_access_length = minimum_access_length;
		state->retention_priority = retention_priority;

		state->value = tcomp_buffer_create_null();
		state->identifier = tcomp_buffer_create_null();
	}
	else{
		TSK_DEBUG_ERROR("Failed to create new state.");
	}
	return state;
}

/** Compares two sigomp states.
* @param state1 First state to compare.
* @param state2 Second state to compare.
* @retval 1 if the two handles are equals and 0 otherwise.
*/
int tcomp_state_equals(const tcomp_state_t *state1, const tcomp_state_t *state2)
{
	if(state1 && state2)
	{
		return tcomp_buffer_equals(state1->identifier, state2->identifier);
	}
	else if(!state1 && !state2) return 1;
	else return 0;
}

/**Computes the state identifier by calculating a 20-byte SHA-1 hash [RFC-3174] over the
*  byte string formed by concatenating the state_length, state_address,
*  state_instruction, minimum_access_length and state_value (in the order given).
* @param state The state to make valid.
*/
void tcomp_state_makeValid(tcomp_state_t* state)
{
	tsk_sha1context_t sha;

	if(!state){
		TSK_DEBUG_ERROR("Invalid parameter");
		return;
	}

	/* Lock */
	tsk_safeobj_lock(state);
	
	tcomp_buffer_allocBuff(state->identifier, TSK_SHA1_DIGEST_SIZE);

	/*=============
		* Calculates a 20-byte SHA-1 hash [RFC-3174] over the byte string formed by concatenating the state_length, state_address,
	    * state_instruction, minimum_access_length and state_value (in the order given).  This is the state_identifier. 
	*/
	{
		uint8_t i;
		int32_t err = tsk_sha1reset(&sha);
		uint8_t firstPart[8];
		
#ifdef __SYMBIAN32__
		uint32_t values[4];
		values[0] = state->length,
		values[1] = state->address,
		values[2] = state->instruction,
		values[3] = state->minimum_access_length;
		
#else
		uint32_t values[4] = { state->length, state->address, state->instruction, state->minimum_access_length };
#endif
				
		for(i=0; i<4; i++)
		{
		#if 0 /*BIG_ENDIAN*/// Do not change this (it's for my own tests)
			firstPart[i] = (values[i] & 0xff);
			firstPart[i+1] = (values[i] >> 8);
		#else
			firstPart[2*i] = (values[i] >> 8);
			firstPart[2*i+1] = (values[i]& 0xff);
		#endif
		}

		tsk_sha1input(&sha, firstPart, 8);
		tsk_sha1input(&sha, tcomp_buffer_getBuffer(state->value), tcomp_buffer_getSize(state->value));
		err = tsk_sha1result(&sha, (uint8_t*)tcomp_buffer_getBuffer(state->identifier));
	}

	/* unlock */
	tsk_safeobj_unlock(state);
}

int32_t tcomp_state_inc_usage_count(tcomp_state_t* self)
{
	if(!self){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	return ++self->usage_count;
}

int32_t tcomp_state_dec_usage_count(tcomp_state_t* self)
{
	if(!self || self->usage_count <= 0){
		TSK_DEBUG_ERROR("Invalid parameter");
		return 0;
	}
	return --self->usage_count;
}



//========================================================
//	State object definition
//
static tsk_object_t* tcomp_state_ctor(tsk_object_t * self, va_list * app)
{
	tcomp_state_t *state = self;
	if(state){
		/* Initialize safeobject */
		tsk_safeobj_init(state);
	}
	else{
		TSK_DEBUG_ERROR("Failed to create new state.");
	}
	return state;
}

static tsk_object_t* tcomp_state_dtor(tsk_object_t *self)
{
	tcomp_state_t *state = self;
	if(state){
		TSK_DEBUG_INFO("==SigComp - Free state with id=");
		tcomp_buffer_print(state->identifier);

		/* Deinitialize safeobject */
		tsk_safeobj_deinit(state);

		TSK_OBJECT_SAFE_FREE(state->identifier);
		TSK_OBJECT_SAFE_FREE(state->value);
	}
	else{
		TSK_DEBUG_ERROR("Null SigComp state.");
	}

	return self;
}


static int tcomp_state_cmp(const void *obj1, const void *obj2)
{
	const tcomp_state_t *state1 = obj1;
	const tcomp_state_t *state2 = obj2;

	if(state1 && state2){
		return tcomp_buffer_equals(state1->identifier, state2->identifier) ? 0 : -1;
	}
	else if(!state1 && !state2) return 0;
	else return -1;
}

static const tsk_object_def_t tcomp_state_def_s = 
{
	sizeof(tcomp_state_t),
	tcomp_state_ctor,
	tcomp_state_dtor,
	tcomp_state_cmp
};
const tsk_object_def_t *tcomp_state_def_t = &tcomp_state_def_s;

OpenPOWER on IntegriCloud