File: gvcmz_bunch.c

package info (click to toggle)
fis-gtm 6.3-007-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster, sid
  • size: 36,284 kB
  • sloc: ansic: 328,861; asm: 5,182; csh: 5,102; sh: 1,918; awk: 291; makefile: 69; sed: 13
file content (204 lines) | stat: -rwxr-xr-x 5,375 bytes parent folder | download | duplicates (2)
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
/****************************************************************
 *								*
 * Copyright (c) 2001-2015 Fidelity National Information	*
 * Services, Inc. and/or its subsidiaries. All rights reserved.	*
 *								*
 *	This source code contains the intellectual property	*
 *	of its copyright holder(s), and is made available	*
 *	under a license.  If you do not know the terms of	*
 *	the license, please stop and do not read further.	*
 *								*
 ****************************************************************/

#include "mdef.h"
#include "cmidef.h"
#include "hashtab_mname.h"	/* needed for cmmdef.h */
#include "cmmdef.h"
#include "gdsroot.h"
#include "gdsblk.h"
#include "gtm_facility.h"
#include "fileinfo.h"
#include "gdsbt.h"
#include "gdsfhead.h"
#include "stringpool.h"
#include "copy.h"
#include "gtm_string.h"
#include "gvcmz.h"

#define FORWARD_REC(x)	buffptr += x->len + SIZEOF(*x) - 1; CM_GET_USHORT(tmp_short, buffptr, usr->convert_byteorder);\
	buffptr += tmp_short + SIZEOF(unsigned short); x = (bunch_rec *)buffptr;

GBLDEF bool		zdefactive;
GBLDEF unsigned short	zdefbufsiz;
GBLREF gd_region	*gv_cur_region;
GBLREF gv_key		*gv_currkey;
GBLREF spdesc		stringpool;

error_def(ERR_ZDEFOFLOW);
error_def(ERR_BADSRVRNETMSG);

typedef struct bunch_rec_struct {
	unsigned char	rn;
	unsigned short	len;
	unsigned short	cc;
	unsigned short	prv;
	unsigned char	base[1];
} bunch_rec;

void gvcmz_bunch(mval *v)
{
	struct CLB	*lnk;
	link_info	*usr;
	unsigned char	*buffptr, *bufftop, *insert_record, *msgptr;
	unsigned char	*new_key, *rec_key;
	unsigned short	cc, len, i;
	signed char	is_gt;
	bool		overlay;
	unsigned short	newrec_len, oldrec_len, tmp_short;
	short		new_space;
	bunch_rec 	*brec, nrec;
	int4		status;

	assert(zdefactive);
	lnk = gv_cur_region->dyn.addr->cm_blk;
	usr = (link_info *)lnk->usr;

	if (zdefbufsiz > usr->buffer_size)
	{
		if (!usr->buffer)
		{
			ENSURE_STP_FREE_SPACE(lnk->mbl);
			msgptr = lnk->mbf = stringpool.free;
		} else
			msgptr = lnk->mbf = usr->buffer;

		*msgptr++ = CMMS_B_BUFRESIZE;
		CM_PUT_USHORT(msgptr, zdefbufsiz, usr->convert_byteorder);
		lnk->ast = 0;
		lnk->cbl = S_HDRSIZE + SIZEOF(unsigned short);
		status = cmi_write(lnk);
		if (CMI_ERROR(status))
		{
			((link_info *)(lnk->usr))->neterr = TRUE;
			gvcmz_error(CMMS_B_BUFRESIZE, status);
			return;
		}
		status = cmi_read(lnk);
		if (CMI_ERROR(status))
		{
			((link_info *)(lnk->usr))->neterr = TRUE;
			gvcmz_error(CMMS_B_BUFRESIZE, status);
			return;
		}
		if (*lnk->mbf != CMMS_C_BUFRESIZE)
		{
			if (*lnk->mbf != CMMS_E_ERROR)
				rts_error(VARLSTCNT(1) ERR_BADSRVRNETMSG);
			gvcmz_errmsg(lnk,FALSE);
			return;
		}
		usr->buffer_size = zdefbufsiz;
		if (usr->buffer)
		{
			free(usr->buffer);
			usr->buffer = 0;
		}
	}

	if (!usr->buffer)
		usr->buffer = malloc(usr->buffer_size);

	if (!usr->buffer_used)
	{
		buffptr = usr->buffer;
		*buffptr++ = CMMS_B_BUFFLUSH;
		buffptr += SIZEOF(short);
		usr->buffer_used = (unsigned char *)buffptr - usr->buffer;
	} else
		buffptr = usr->buffer + 3;	/* trancode + # of transactions */
	bufftop = usr->buffer + usr->buffer_used;

	cc = 0;
	overlay = FALSE;
	brec = (bunch_rec *)buffptr;
	for (; buffptr < bufftop ;)
	{
		if (cc < brec->cc)
		{
			FORWARD_REC(brec);
			continue;
		}
		if (cc > brec->cc)
			break;
		new_key = &gv_currkey->base[cc];
		len = (brec->len > gv_currkey->end - cc + 1 ? gv_currkey->end - cc + 1 : brec->len);
		rec_key = &brec->base[0];
		for (i = 0; i < len; i++)
		{
			is_gt = *new_key++ - *rec_key++;
			if (is_gt)
				break;
		}
		if (i == len)
		{
			len = brec->len + brec->cc - 1;
			if (gv_currkey->end > len)
			{
				cc += i;
				FORWARD_REC(brec);
				continue;
			}
			if( gv_currkey->end == len)
			{
				if (brec->rn != gv_cur_region->cmx_regnum)
				{
					cc += i;
					FORWARD_REC(brec);
					continue;
				} else
					overlay = TRUE;
			}
			break;
		} else if (is_gt > 0)
		{
			cc += i;
			FORWARD_REC(brec);
			continue;
		} else
			break;
	}
	nrec.rn = gv_cur_region->cmx_regnum;
	nrec.len = gv_currkey->end - cc + 1;
	nrec.prv = gv_currkey->prev;
	nrec.cc = cc;
	newrec_len = SIZEOF(nrec) - 1 + nrec.len + SIZEOF(unsigned short) + v->str.len;
	if (overlay)
	{
		oldrec_len = SIZEOF(*brec) - 1 + brec->len;
		CM_GET_USHORT(tmp_short, buffptr + oldrec_len, usr->convert_byteorder);
		oldrec_len += tmp_short + SIZEOF(unsigned short);
		new_space = newrec_len - oldrec_len;
		insert_record = buffptr;
		FORWARD_REC(brec);
	} else
	{	insert_record = buffptr;
		new_space = newrec_len;
	}
	if (usr->buffer_used + new_space >= usr->buffer_size)
		VMS_ONLY(rts_error(VARLSTCNT(4) ERR_ZDEFOFLOW, 2, lnk->nod.dsc$w_length, lnk->nod.dsc$a_pointer);)
		UNIX_ONLY(rts_error(VARLSTCNT(4) ERR_ZDEFOFLOW, 2, lnk->nod.len, lnk->nod.addr);)

	memcpy(buffptr + new_space, buffptr, bufftop - buffptr);	/* shuffle buffer to make room for new record */
	memcpy(insert_record, &nrec, SIZEOF(nrec) - 1);
	insert_record += SIZEOF(nrec) - 1;
	memcpy(insert_record, &gv_currkey->base[cc], nrec.len);
	insert_record += nrec.len;
	tmp_short = (unsigned short)v->str.len;
	assert((int4)tmp_short == v->str.len); /* ushort <- int4 assignment lossy? */
	CM_PUT_USHORT(insert_record, tmp_short, usr->convert_byteorder);
	insert_record += SIZEOF(unsigned short);
	memcpy(insert_record, v->str.addr, v->str.len);
	usr->buffer_used += new_space;
	if (!overlay)
		usr->buffered_count++;
}