summaryrefslogtreecommitdiff
path: root/lib/rlib.c
blob: 38e8268f985f3cfd2d191be2571de9b7e9691508 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
 * AUTHOR
 * N. Nielsen
 *
 * LICENSE
 * This software is in the public domain.
 *
 * The software is provided "as is", without warranty of any kind,
 * express or implied, including but not limited to the warranties
 * of merchantability, fitness for a particular purpose, and
 * noninfringement. In no event shall the author(s) be liable for any
 * claim, damages, or other liability, whether in an action of
 * contract, tort, or otherwise, arising from, out of, or in connection
 * with the software or the use or other dealings in the software.
 *
 * SUPPORT
 * Send bug reports to: <nielsen@memberwebs.com>
 */

/* =========================================================================
 *  Main RLIB
 *  2000-2001 Copyright, Nate Nielsen
 */

#include <stdarg.h>
#include "common/usuals.h"
#include "common/compat.h"
#include "lib/rlib.h"
#include "priv.h"
#include "rep.h"
#include "common/binfile.h"
#include "common/repfile.h"

/* rlibInit: --------------------------------------------------------------
 *  Initialize a stream. We already expect stream to be zero'd
 */

int rlibInit(r_stream* stream, long options)
{
	byte* op = NULL;
	int i, j;
	struct internal_state* state;

	if(stream->state != NULL)
		return R_INVARG;

	/*  Allocate a state */
	state = stream->state =
		(struct internal_state*)malloc(sizeof(struct internal_state));

	if(!state)
		return R_NOMEM;

	zero(*state);

	/* Init the stream for the current session */
	if(!vmInit(stream))
  {
    free(state);
    stream->state = NULL;
		return R_NOMEM;
  }

	state->options = options;

	/*  Init our case table */
	/*  we only do this once for the whole bunch */
	for (i = 0; i < 256; i++)
	   stream->state->caseTranslate[i] = i;
	for (j = 'a'; j <= 'z'; j++)
	   stream->state->caseTranslate[j] = j - ('a' - 'A');

	return R_OK;
}


/* rlibCompile: ----------------------------------------------------
 *  Compile a script
 */
int rlibCompile(r_script* script, const char* data)
{
	return compilerRun(script, data);
}


/* rlibClear: -------------------------------------------------------------
 *  Prepare stream for a new file
 */
void rlibClear(r_stream* stream)
{
	if(stream)
	{
		if(stream->state)
			vmClean(stream);
	}
}



/* rLibRun: ---------------------------------------------------------------
 *  The replacement coordinator, output etc...
*/
int rlibRun(r_stream* stream, r_script* script, int done)
{
	#define RETURN(r) do { retv = r; goto finally; } while(0)

	struct internal_state* state = stream->state;
	size_t backup, curOffset;
	int retv = R_OK;

	if(!stream->state)
		return R_INVARG;

	/* Usually have to have an output function, unless just a matcher */
	if(!stream->fWrite && !(state->options & RLIB_MODE_MATCHER))
		return R_INVARG;
	/* And if we are a matcher we have to have a match function */
	if(state->options & RLIB_MODE_MATCHER && !stream->fMatch)
		return R_INVARG;


	/*
	 * Keep at least 1/2 amount of backup
	 * Backup is only kept if no matches occur in backup area
	 */
	backup = done ? 0 : stream->availIn / 2;

	/*
	 * Since there are conversions (between absolute and relative offsets)
	 * below, we need to keep inOffset the same for this session. So copy
	 * it and update it again at the end of the function.
	 */
	curOffset = 0;

	/*  Need some data to work with */
	if(!stream->nextIn || !stream->availIn)
		RETURN(R_IN);


	stream->total = 0;

	/* Execute the script */
	retv = vmExecute(stream, script);

	/* Error? */
	if(retv < 0)
		RETURN(retv);

	/* Fall through to commit code */
	if(retv == R_DONE)
		backup = 0;

	if(!(state->options & RLIB_MODE_MATCHER))
	{
		/*
		 * Here we write out what we've found so far and advance the
		 * pointers
		 *
		 * On the first round we look something like this
		 *
		 *               <- state->searched ->
		 *   |---------------------------------------------|    <-  stream->nextIn
		 *   |----------------------------------------|		   <-  stream->nextOut
		 *                replacements found             ^- leftovers
		 */

		/* toWrite is the size of the current block we need to transfer */
		size_t toWrite;
		/* block is the amount we can actually transfer (depending on output buffer) */
		size_t block = stream->availIn;

#ifdef VERBOSE
		dumpRelpacements(stream);
#endif

		while(state->replaces)
		{
			/* Get the amount of data till the current replace */
			toWrite = block = ABS_TO_REL(state->replaces->beg, state) - curOffset;

			if(!(state->options & RLIB_MODE_PARSER))
			{
				/* ... write out up to current replacement... */
				if(!(stream->fWrite)(stream, stream->nextIn, block))
					RETURN(R_IOERR);
			}

			stream->nextIn += block;
			stream->availIn -= block;
			curOffset += block;


			/* ... check space for replacement ... */
			block = strlen(state->replaces->text);

			/* ...write out replacement... */
			if(!(stream->fWrite)(stream, (byte*)state->replaces->text, block))
				RETURN(R_IOERR);

			/* ... and skip (!) the text that we replaced */
			block = state->replaces->end - state->replaces->beg;
			stream->nextIn += block;
			stream->availIn -= block;
			curOffset += block;

#ifdef _DEBUG
			/* Check if things are in the right order */
			if(state->replaces->next)
				ASSERT(state->replaces->end <= state->replaces->next->end);
#endif
			/* Go to the next replacement */
			state->replaces = replacementPop(state->replaces);
		}

		/*
		 * Now we check how much data we have left and copy
		 * up to backup if we have more
		 */

		/* Copy out till backup marker */
		if((stream->availIn) >= backup)
		{
			/* Get block size ... */
			toWrite = block = stream->availIn - backup;

			if(!(state->options & RLIB_MODE_PARSER))
			{
				if(!(stream->fWrite)(stream, stream->nextIn, block))
					RETURN(R_IOERR);
			}

			stream->nextIn += block;
			stream->availIn -= block;
			curOffset += block;
		}
	}

	/*
	 * After this the search could start anew
	 * unless in flush mode or done
	 */
	if(done)
		RETURN(R_DONE);
	else
		RETURN(R_IN);

finally:
	state->offset += curOffset;

	return retv;
}


/* rlibSetVar: -----------------------------------------------------------
 *  Set a variable for the script to use
 */
int rlibSetVar(r_stream* stream, const char* var, const char* val)
{
	if(!stream || !stream->state || !var || !val)
		return R_INVARG;

	variablesClear(&(stream->state->vars), var);
	return variablesAdd(&(stream->state->vars), var, val) ? R_OK : R_NOMEM;
}


/* rlibDump: --------------------------------------------------------------
 *  Dump the rep script opts for debugging
 */
void rlibDump(r_script* script, FILE* f)
{
	opsDump(script->ops, f);
}


/* rlibFree: --------------------------------------------------------------
 *  Free associated structures data etc...
 */
void rlibFree(r_stream* stream, r_script* script)
{
	struct internal_state* state;

	if(stream)
	{
		if(state = stream->state)
		{
			/* Let execution free it's stuff */
			vmFree(stream);

			/* And free the state */
			free(state);
			stream->state = NULL;
		}

		zero(*stream);
	}

	if(script)
	{
		if(script->ops)
			free(script->ops);

		if(script->error)
			free(script->error);

		zero(*script);
	}

}


/* scriptSetError: -------------------------------------------------
 *  Set the stream error text to whatever
 */
void scriptSetError(r_script* script, const char* format, ...)
{
	char* msg;

	va_list vl;
	va_start(vl, format);

	if(script->error)
		free(script->error);

	if(vasprintf(&msg, format, vl) != -1)
		script->error = msg;
	else
		script->error = NULL;

	va_end(vl);
}

/* compileAlready: ------------------------------------------------------
 *  See if the file has already been compiled and load if so
 */
int compileAlready(r_context* ctx, FILE* file)
{
	/* Should have already read header */
	bfval val;
	BFILE h = NULL;
	int retv = R_OK;
	r_uint temp;

	if(!(h = bfStartFile(file)))
		RETURN(R_INVARG);

	if(!repfReadHeader(h))
		RETURN(R_IN);

	while(bfReadValueInfo(h, &val))
	{
		if(ferror(file))
			RETURN(R_IOERR);

		if(feof(file))
			RETURN(R_INVARG);

		switch(val.id)
		{
		case REPVAL_BUFSIZE:
			if(ctx->block == 0)
				bfReadValueData(h, &val, &(ctx->block));
			continue;

		case REPVAL_PARSEMODE:
			if(bfReadValueData(h, &val, &temp))
				ctx->options |= (temp != 0) ? RLIB_MODE_PARSER : 0;
			continue;

		case REPVAL_SCRIPT:
			{
				ctx->script.ops = (r_byte*)malloc(val.len);
				if(!ctx->script.ops)
					RETURN(R_NOMEM);

				bfReadValueData(h, &val, ctx->script.ops);
				ctx->script.len = val.len;
				continue;
			}
			break;
		}

		bfSkipValueData(h, &val);
	}

finally:
	if(h)
		bfClose(h);

	return retv;
}


int repLoad(r_context* ctx, FILE* f)
{
	int retv = R_OK;
	char* buff = NULL;
	size_t len;
	int r;

	rlibFree(NULL, &(ctx->script));

	/* Okay now try and read header, and possibly process
	   an already executable file. */
	switch(r = compileAlready(ctx, f))
	{
	/* It's already compiled */
	case R_OK:
		RETURN(R_OK);
		break;

	/* It's not compiled so compile */
	case R_IN:
		break;

	/* Failed processing */
	default:
		if(r < 0)
			RETURN(r);
		break;
	};

	/* Get file size */
	len = 0;

	if(fseek(f, 0, SEEK_END) || !(len = ftell(f)) ||
	   fseek(f, 0, SEEK_SET))
		RETURN(R_IOERR);

	buff = (char*)malloc(len + 1);
	if(!buff)
		RETURN(R_NOMEM);

	if(fread(buff, 1, len, f) != len)
		RETURN(R_IOERR);

	/* Needs compiling */
	buff[len] = '\0';

	/* Init the stream */
	r = rlibCompile(&(ctx->script), buff);
	if(r < 0)
		RETURN(r);

finally:
	if(buff)
		free(buff);

	return retv;
}

int repInit(r_context* ctx)
{
	rlibInit(&(ctx->stream), ctx->options);
	return R_OK;
}

#ifdef _DEBUG
static void* memstr(void* mem, size_t sz, const char* str)
{
	size_t len = strlen(str);
	const char* t;
	const char* e;

	ASSERT(str && mem);

	if(len > sz)
		return NULL;

	for(t = (const char*)mem, e = t + (sz - len); t < e; t++)
	{
		if(memcmp(t, str, len) == 0)
			return (void*)t;
	}

	return NULL;
}
#endif

int repFile(r_context* ctx, FILE* fIn)
{
	r_uint batch;				/*  Current batch size */
	r_uint block = ctx->block;	/*  Block size */
	r_byte* buff = NULL;		/*  Input buffer */
	int retv = R_OK;
	int r = R_IN;
	int total = 0;

	if(!block)
	{
		if(!fseek(fIn, 0, SEEK_END) && (block = ftell(fIn)) != ~0)
			block++;

		else
			block = 0;

		fseek(fIn, 0, SEEK_SET);
	}

	else
	{
		block *= 2;
	}

	/* Okay now if it's a sane value just allocate it */
	if(block)
	{
		if(block > 0x0F00000)
			RETURN(R_NOMEM);

		/* Allocate buffers */
		buff = (r_byte*)malloc(block);
		if(!buff)
			RETURN(R_NOMEM);
	}

	/*  Hook buffers to the stream */
	ctx->stream.nextIn = buff;
	ctx->stream.availIn = 0;

	/*  While we either have more data to put in or more output... */
	while(r != R_DONE)
	{
		/*  If rlib wants data then give it */
		if(r == R_IN)
		{
			/* This is a normal standard read */
			if(buff)
			{
				/*  Move data to front */
				memmove(buff, ctx->stream.nextIn, ctx->stream.availIn);

				/*  Set pointer to data */
				ctx->stream.nextIn = buff;

				/*  Read rest of block */
				batch = fread(ctx->stream.nextIn + ctx->stream.availIn,
						      sizeof(r_byte), block - ctx->stream.availIn, fIn);

				ctx->stream.availIn += batch;

				if(ferror(fIn))
					RETURN(R_IOERR);
			}

			/* Here we read as much as possible in one shot allocating as we go*/
			else
			{
				batch = 0;
				block = 0;

				while(!feof(fIn))
				{
					if(ferror(fIn))
						RETURN(R_IOERR);

					block += 0x4000;

					if(block > MAX_BUFF ||
					   !(buff = reallocf(buff, block)))
						RETURN(R_NOMEM);

					batch += fread(buff + batch, sizeof(r_byte),
						           0x4000, fIn);
				}

				ctx->stream.nextIn = buff;
				ctx->stream.availIn += batch;
			}
		}

		/*  call rlib */
		r = rlibRun(&(ctx->stream), &(ctx->script), feof(fIn));

		// Oops!
		if(r < 0)
			RETURN(r);

		total += ctx->stream.total;
	}

finally:
	if(buff)
		free(buff);

	ctx->stream.total = total;

	return retv;
}

int fileOutput(struct r_stream* stream, byte* data, size_t len)
{
	FILE* f = (FILE*)stream->arg;
	return (fwrite(data, 1, len, f) == len) && (!ferror(f));
}

int repFiles(r_context* ctx, FILE* fIn, FILE* fOut)
{
	void* arg = ctx->stream.arg;
	r_write func = ctx->stream.fWrite;
	int ret;

	ctx->stream.fWrite = fileOutput;
	ctx->stream.arg = fOut;

	ret = repFile(ctx, fIn);

	ctx->stream.fWrite = func;
	ctx->stream.arg = arg;

	return ret;
}

void repFree(r_context* ctx)
{
	rlibFree(&(ctx->stream), &(ctx->script));
}