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
|
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <zlib.h>
#include <bzlib.h>
#include "sanitizer.h"
#include "input.h"
int sanitizerClose(void *context);
int sanitizerProcess(void *context, char *buffer, int len);
/* UTF8sanitizer algorithm has some nasty edge cases when trying to operate
* in a 'block at a time' mode. For example, in the following scenario:
*
* INPUT sequence is 2 buffers with a 6 byte char starting at X1
*
* [ len = 5 ] [len = 1]
* X1 X2 X3 X4 X5 X6
*
* OUTPUT: nothing is generated for first buffer
* This will itself cause caller to assume EOF (hopefully normal reader will read >> 5 bytes).
* subsequent read of len=1 whille return all 6 bytes potentially causing output buffer overflow (and overwriting input data)
*
* The solution is to provice a small output buffer to hold anything bigger than a single byte
*
*/
struct Context {
long long line;
long long chars1, chars2, chars3, chars4, chars5, chars6;
int state, current_size;
int long_char[6];
int out_char[10];
int pend;
int verbose;
void *file;
};
int sanitizerClose(void *context)
{
struct Context *ctx = context;
int r = inputClose(ctx->file);
if (ctx->verbose) {
fprintf(stderr, "Summary:\n");
fprintf(stderr, "chars1: %lld\n", ctx->chars1);
fprintf(stderr, "chars2: %lld\n", ctx->chars2);
fprintf(stderr, "chars3: %lld\n", ctx->chars3);
fprintf(stderr, "chars4: %lld\n", ctx->chars4);
fprintf(stderr, "chars5: %lld\n", ctx->chars5);
fprintf(stderr, "chars6: %lld\n", ctx->chars6);
fprintf(stderr, "lines : %lld\n", ctx->line);
}
free(ctx);
return r;
}
xmlTextReaderPtr sanitizerOpen(const char *name)
{
struct Context *ctx = malloc (sizeof(*ctx));
if (!ctx)
return NULL;
memset(ctx, 0, sizeof(*ctx));
ctx->verbose = 0;
ctx->state = 1;
ctx->pend = 0;
ctx->file = inputOpen(name);
if (!ctx->file) {
fprintf(stderr, "Input reader create failed\n");
return NULL;
}
return xmlReaderForIO(sanitizerProcess, sanitizerClose, (void *)ctx, NULL, NULL, 0);
}
int sanitizerProcess(void *context, char *buffer, int len)
{
struct Context *ctx = context;
int current_char, i, out = 0;
while (out < len) {
if (ctx->pend) {
buffer[out++] = ctx->out_char[--ctx->pend];
continue;
}
current_char=inputGetChar(ctx->file);
if (inputEof(ctx->file))
break;
if ((current_char & 128) == 0) {
//Handle_ASCII_char();
if (current_char == '\n')
ctx->line++;
else
ctx->chars1++;
if (ctx->state != 1) {
if (ctx->verbose)
fprintf(stderr, "Error at line %lld\n", ctx->line);
buffer[out++] = '_';
ctx->state = 1;
}
// buffer[out++] = current_char;
ctx->out_char[ctx->pend++] = current_char;
} else if ((current_char & (128+64)) == 128) {
// Handle_continue_char();
if(ctx->state > 1) {
ctx->state--;
if(ctx->state==1) {
// long char finished
//for(i=1; i<ctx->current_size; i++) {
// buffer[out++] = ctx->long_char[i-1];
//}
//buffer[out++] = current_char;
ctx->out_char[ctx->pend++] = current_char;
for(i=ctx->current_size-1; i>0; i--) {
ctx->out_char[ctx->pend++] = ctx->long_char[i-1];
}
}
} else {
if (ctx->verbose)
fprintf(stderr, "Error at line %lld\n", ctx->line);
buffer[out++] = '_';
ctx->state=1;
}
} else if ((current_char & (128+64+32)) == (128+64)) {
//Handle_two_bytes();
ctx->state=2;
ctx->chars2++;
ctx->current_size=2;
} else if ((current_char & (128+64+32+16)) == (128+64+32)) {
//Handle_three_bytes();
ctx->state=3;
ctx->chars3++;
ctx->current_size=3;
} else if ((current_char & (128+64+32+16+8)) == (128+64+32+16)) {
//Handle_four_bytes();
ctx->state=4;
ctx->chars4++;
ctx->current_size=4;
} else if ((current_char & (128+64+32+16+8+4)) == (128+64+32+16+8)) {
//Handle_five_bytes();
ctx->state=5;
ctx->chars5++;
ctx->current_size=5;
} else if ((current_char & (128+64+32+16+8+4+2)) == (128+64+32+16+8+4)) {
//Handle_six_bytes();
ctx->state=6;
ctx->chars6++;
ctx->current_size=6;
}
if(ctx->state>1) {
ctx->long_char[ctx->current_size-ctx->state]=current_char;
}
}
return out;
}
|