1 /*
2  * Copyright (C) 2001, 2002, and 2003  Roy Keene
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public License
6  * as published by the Free Software Foundation; either version 2
7  * of the License, or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
17  *
18  *      email: dact@rkeene.org
19  */
20 
21 #include "dact.h"
22 #ifdef HAVE_STDLIB_H
23 #include <stdlib.h>
24 #endif
25 #include <stdio.h>
26 #ifdef HAVE_STRING_H
27 #include <string.h>
28 #endif
29 #include "header.h"
30 
31 char *dact_hdr_ext_val=NULL;
32 uint32_t dact_hdr_ext_sze=0;
33 uint32_t dact_hdr_ext_pos=0;
34 
35 
36 
dact_hdr_ext_alloc(uint32_t size)37 int dact_hdr_ext_alloc(uint32_t size) {
38 	if (dact_hdr_ext_val==NULL) {
39 		if (!(dact_hdr_ext_val=malloc(DACT_HEADER_BLKSIZE))) {
40 			dact_hdr_ext_val=NULL;
41 			return(0);
42 		}
43 		dact_hdr_ext_sze=DACT_HEADER_BLKSIZE;
44 	}
45 	if ((dact_hdr_ext_pos+size)>(dact_hdr_ext_sze)) {
46 		dact_hdr_ext_sze=(((dact_hdr_ext_pos+size-1)/DACT_HEADER_BLKSIZE)+1)*DACT_HEADER_BLKSIZE;
47 		dact_hdr_ext_val=realloc(dact_hdr_ext_val,dact_hdr_ext_sze);
48 	}
49 	return(1);
50 }
51 
dact_hdr_ext_regs(const int id,const char * val,const uint32_t size)52 int dact_hdr_ext_regs(const int id, const char *val, const uint32_t size) {
53 	if (!dact_hdr_ext_alloc(size+3)) return(0);
54 	dact_hdr_ext_val[dact_hdr_ext_pos]=(id&0xff);
55 	dact_hdr_ext_val[dact_hdr_ext_pos+1]=((size>>8)&0xff);
56 	dact_hdr_ext_val[dact_hdr_ext_pos+2]=(size&0xff);
57 	memcpy(dact_hdr_ext_val+dact_hdr_ext_pos+3,val,size);
58 	dact_hdr_ext_pos+=(size+3);
59 	return(1);
60 }
61 
dact_hdr_ext_regn(const int id,const uint32_t val,const uint32_t size)62 int dact_hdr_ext_regn(const int id, const uint32_t val, const uint32_t size) {
63 	int i;
64 
65 	if (!dact_hdr_ext_alloc(size+3)) return(0);
66 	dact_hdr_ext_val[dact_hdr_ext_pos]=(id&0xff);
67 	dact_hdr_ext_val[dact_hdr_ext_pos+1]=((size>>8)&0xff);
68 	dact_hdr_ext_val[dact_hdr_ext_pos+2]=(size&0xff);
69 	for (i=0;i<size;i++) {
70 		dact_hdr_ext_val[dact_hdr_ext_pos+3+i]=((val>>((size-i-1)*8)) &0xff);
71 	}
72 	dact_hdr_ext_pos+=(size+3);
73 	return(1);
74 
75 }
76 
dact_hdr_ext_size(void)77 uint32_t dact_hdr_ext_size(void) {
78 	return(dact_hdr_ext_pos);
79 }
80 
dact_hdr_ext_data(void)81 char *dact_hdr_ext_data(void) {
82 	memset(dact_hdr_ext_val+dact_hdr_ext_pos, DACT_HDR_NOP, dact_hdr_ext_sze-dact_hdr_ext_pos);
83 	return(dact_hdr_ext_val);
84 }
85 
dact_hdr_ext_clear(void)86 void dact_hdr_ext_clear(void) {
87 	if (dact_hdr_ext_val!=NULL) free(dact_hdr_ext_val);
88 	dact_hdr_ext_pos=0;
89 	dact_hdr_ext_sze=0;
90 	dact_hdr_ext_val=NULL;
91 	return;
92 }
93 
94 
95