summaryrefslogtreecommitdiffstats
path: root/tinySIGCOMP/src/tcomp_udvm.nack.c
blob: f4220763a63c963a0d685569ac582cb89a5416a4 (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
/*
* Copyright (C) 2009-2010 Mamadou Diop.
*
* Contact: Mamadou Diop <diopmamadou(at)doubango.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_udvm.nack.c
 * @brief  SigComp UDVM machine (NACK).
 *
 * @author Mamadou Diop <diopmamadou(at)yahoo.fr>
 *
 * @date Created: Sat Nov 8 16:54:58 2009 mdiop
 */
#include "tcomp_udvm.h"

#include "tsk_sha1.h"

void tcomp_udvm_createNackInfo(tcomp_udvm_t *udvm, uint8_t reasonCode, tcomp_buffer_handle_t* lpDetails, int16_t memory_address_of_instruction)
{
#define NACK_HEADER_INDEX		0
#define NACK_VERSION_INDEX		(NACK_HEADER_INDEX+2)
#define NACK_REASON_CODE_INDEX	(NACK_VERSION_INDEX+1)
#define NACK_OPCODE_INDEX		(NACK_REASON_CODE_INDEX+1)
#define NACK_PC_INDEX			(NACK_OPCODE_INDEX+1)
#define NACK_SHA1_INDEX			(NACK_PC_INDEX+2)
#define NACK_DETAILS_INDEX		(NACK_PC_INDEX+SHA1HashSize)
	/*  0   1   2   3   4   5   6   7
        +---+---+---+---+---+---+---+---+
        | 1   1   1   1   1 | T |   0   |
        +---+---+---+---+---+---+---+---+
        |                               |
        :    returned feedback item     :
        |                               |
        +---+---+---+---+---+---+---+---+
        |         code_len = 0          |
        +---+---+---+---+---+---+---+---+
        | code_len = 0  |  version = 1  |
        +---+---+---+---+---+---+---+---+
        |          Reason Code          |
        +---+---+---+---+---+---+---+---+
        |  OPCODE of failed instruction |
        +---+---+---+---+---+---+---+---+
        |   PC of failed instruction    |
        |                               |
        +---+---+---+---+---+---+---+---+
        |                               |
        : SHA-1 Hash of failed message  :
        |                               |
        +---+---+---+---+---+---+---+---+
        |                               |
        :         Error Details         :
        |                               |
        +---+---+---+---+---+---+---+---+
	*/

	uint16_t mem_add_instruction = (memory_address_of_instruction >=0) ? memory_address_of_instruction : udvm->last_memory_address_of_instruction;
	tsk_sha1context_t sha;
	uint8_t *nackbuffer_ptr;

	tcomp_buffer_allocBuff(udvm->lpResult->nack_info, NACK_SHA1_INDEX + TSK_SHA1_DIGEST_SIZE);
	nackbuffer_ptr = tcomp_buffer_getBuffer(udvm->lpResult->nack_info);
	
	nackbuffer_ptr[NACK_HEADER_INDEX] = 0xf8;
	nackbuffer_ptr[NACK_VERSION_INDEX] = NACK_VERSION;
	nackbuffer_ptr[NACK_REASON_CODE_INDEX] = reasonCode;
	nackbuffer_ptr[NACK_OPCODE_INDEX] = *TCOMP_UDVM_GET_BUFFER_AT(mem_add_instruction);
	nackbuffer_ptr[NACK_PC_INDEX] = (mem_add_instruction >> 8);
	nackbuffer_ptr[NACK_PC_INDEX+1] = (mem_add_instruction & 0x00ff);
	
	/*
	* SHA-1 computation
	*/
	tsk_sha1reset(&sha);
	tsk_sha1input(&sha, udvm->sigCompMessage->startPtr, udvm->sigCompMessage->totalSize);
	tsk_sha1result(&sha, (char*)(nackbuffer_ptr + NACK_SHA1_INDEX));
	
	/*
	* Details
	*/
	if(lpDetails && tcomp_buffer_getSize(lpDetails))
	{
		tcomp_buffer_appendBuff(udvm->lpResult->nack_info, tcomp_buffer_getBuffer(lpDetails), tcomp_buffer_getSize(lpDetails));
	}
	else if(reasonCode == NACK_BYTECODES_TOO_LARGE)
	{
		uint16_t memorySize = TCOMP_UDVM_GET_SIZE();
		tcomp_buffer_appendBuff(udvm->lpResult->nack_info, &memorySize, 2);
	}
	else if(reasonCode == NACK_CYCLES_EXHAUSTED)
	{
		uint8_t cpb = udvm->stateHandler->sigcomp_parameters->cpbValue;
		tcomp_buffer_appendBuff(udvm->lpResult->nack_info, &cpb, 1);
	}

	udvm->lpResult->isNack = 1;
}
OpenPOWER on IntegriCloud