summaryrefslogtreecommitdiffstats
path: root/fluent-bit/lib/onigmo/testconvu.rb
blob: 384731e0da3ae56b5d85b04f85e7d5f4906a996b (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
#!/usr/local/bin/ruby
# testconvu.rb
# Copyright (C) 2004-2006  K.Kosako (sndgk393 AT ybb DOT ne DOT jp)

WINDOWS = (ARGV.size > 0 && /^-win/i =~ ARGV[0])
ARGV.shift if WINDOWS

BIG_ENDIAN    = 1
LITTLE_ENDIAN = 2

def eucjp_char_pos(s, byte_pos)
  pos = 0
  i   = 0
  while (i < byte_pos)
    x = s[i].ord
    if ((x >= 0xa1 && x <= 0xfe) || x == 0x8e)
      i += 2
    elsif (x == 0x8f)
      i += 3
    else
      i += 1
    end
    pos += 1
  end
  return pos
end

def utf16_byte_pos(endian, s, char_pos)
  i = 0
  while (char_pos > 0)
    x = (endian == BIG_ENDIAN ? s[i] : s[i+1]).ord
    if (x >= 0xd8 && x <= 0xdb)
      i += 4
    else
      i += 2
    end
    char_pos -= 1
  end
  return i
end

def s_escape(s)
  q = ''
  s.each_byte { |b|
    if (b < 0x20 || b >= 0x7f || b == 0x22 || b == 0x5c)
      q << sprintf("\\%03o", b)
    else
      q << b.chr
    end
  }
  q
end

def conv_to_utf16(endian, s)
  begin
    if (endian == BIG_ENDIAN)
      q = s.encode('UTF-16BE', 'EUC-JP')
    else
      q = s.encode('UTF-16LE', 'EUC-JP')
    end
    q.force_encoding('ASCII-8BIT')
  rescue Encoding::InvalidByteSequenceError
    q = 'Invalid character'
  rescue Encoding::UndefinedConversionError
    STDERR.printf("Encoding::UndefinedConversionError: [%s]\n", s)
    return ''
  end

  q << "\000\000"
  s_escape(q)
end

def conv_reg(endian, s)
  s = s.gsub(/\\([0-7]{2,3})\\([0-7]{2,3})/) {
              $1.to_i(8).chr + $2.to_i(8).chr
            }

  s = s.gsub(/\\x([0-9A-Fa-f]{2})\\x([0-9A-Fa-f]{2})/) {
              $1.to_i(16).chr + $2.to_i(16).chr
            }

  if (endian == BIG_ENDIAN)
    s = s.gsub(/(\\[0-7]{2,3})/) { "\\000" + $1 }
    s = s.gsub(/(\\x[0-9A-Fa-f]{2})/) { "\\x00" + $1 }
  else
    s = s.gsub(/(\\[0-7]{2,3})/) { $1 + "\\000" }
    s = s.gsub(/(\\x[0-9A-Fa-f]{2})/) { $1 + "\\x00" }
  end

  s = s.gsub(/\\/, '\\\\')  #'

  if (WINDOWS)
    s = s.gsub(/\?\?/, '?\\?')   # escape ANSI trigraph
  end
  conv_to_utf16(endian, s)
end

def conv_str(endian, s, from, to)
  if (s[0] == ?')
    s = s[1..-2]
    q = s.gsub(/\\/, '\\\\')  #'
  else
    q = s[1..-2]
    q.gsub!(/\\n/, "\x0a")
    q.gsub!(/\\t/, "\x09")
    q.gsub!(/\\v/, "\x0b")
    q.gsub!(/\\r/, "\x0d")
    q.gsub!(/\\f/, "\x0c")
    q.gsub!(/\\a/, "\x07")
    q.gsub!(/\\e/, "\x1b")

    q.gsub!(/\\([0-7]{2,3})/)      { $1.to_i(8).chr }
    q.gsub!(/\\x([0-9A-Fa-f]{2})/) { $1.to_i(16).chr }
  end

  q.force_encoding('ASCII-8BIT')
  from = from.to_i
  to   = to.to_i
  eucjp_from = eucjp_char_pos(q, from)
  eucjp_to   = eucjp_char_pos(q, to)

  s = conv_to_utf16(endian, q)

  from = utf16_byte_pos(endian, s, eucjp_from)
  to   = utf16_byte_pos(endian, s, eucjp_to)
  return s, from, to
end

print(<<"EOS")
/*
 * This program was generated by testconv.rb.
 */
#include<stdio.h>

#ifdef POSIX_TEST
#include "onigmoposix.h"
#else
#include "onigmo.h"
#endif

static int nsucc  = 0;
static int nfail  = 0;
static int nerror = 0;

static FILE* err_file;

#ifndef POSIX_TEST
static OnigRegion* region;
static OnigEncoding ENC;
#endif

#define ulen(p) onigenc_str_bytelen_null(ENC, (UChar* )p)

static void uconv(char* from, char* to, int len)
{
  int i;
  unsigned char c;
  char *q;

  q = to;

  for (i = 0; i < len; i += 2) {
    c = (unsigned char )from[i];
    if (c == 0) {
      c = (unsigned char )from[i+1];
      if (c < 0x20 || c >= 0x7f || c == 0x5c || c == 0x22) {
        sprintf(q, "\\\\%03o", c);
        q += 4;
      }
      else {
        sprintf(q, "%c", c);
        q++;
      }
    }
    else {
      sprintf(q, "\\\\%03o", c);
      q += 4;
      c = (unsigned char )from[i+1];
      sprintf(q, "\\\\%03o", c);
      q += 4;
    }
  }

  *q = 0;
}

static void xx(char* pattern, char* str, int from, int to, int mem, int not)
{
  int r;
  char cpat[4000], cstr[4000];

#ifdef POSIX_TEST
  regex_t reg;
  char buf[200];
  regmatch_t pmatch[20];

  uconv(pattern, cpat, ulen(pattern));
  uconv(str,     cstr, ulen(str));

  r = regcomp(&reg, pattern, REG_EXTENDED | REG_NEWLINE);
  if (r) {
    regerror(r, &reg, buf, sizeof(buf));
    fprintf(err_file, "ERROR: %s\\n", buf);
    nerror++;
    return ;
  }

  r = regexec(&reg, str, reg.re_nsub + 1, pmatch, 0);
  if (r != 0 && r != REG_NOMATCH) {
    regerror(r, &reg, buf, sizeof(buf));
    fprintf(err_file, "ERROR: %s\\n", buf);
    nerror++;
    return ;
  }

  if (r == REG_NOMATCH) {
    if (not) {
      fprintf(stdout, "OK(N): /%s/ '%s'\\n", cpat, cstr);
      nsucc++;
    }
    else {
      fprintf(stdout, "FAIL: /%s/ '%s'\\n", cpat, cstr);
      nfail++;
    }
  }
  else {
    if (not) {
      fprintf(stdout, "FAIL(N): /%s/ '%s'\\n", cpat, cstr);
      nfail++;
    }
    else {
      if (pmatch[mem].rm_so == from && pmatch[mem].rm_eo == to) {
        fprintf(stdout, "OK: /%s/ '%s'\\n", cpat, cstr);
        nsucc++;
      }
      else {
        fprintf(stdout, "FAIL: /%s/ '%s' %d-%d : %d-%d\\n", cpat, cstr,
	        (int)from, (int)to, (int)pmatch[mem].rm_so, (int)pmatch[mem].rm_eo);
        nfail++;
      }
    }
  }
  regfree(&reg);

#else
  regex_t* reg;
  OnigCompileInfo ci;
  OnigErrorInfo einfo;
  OnigSyntaxType syn = *ONIG_SYNTAX_DEFAULT;

  /* ONIG_OPTION_OFF(syn.options, ONIG_OPTION_ASCII_RANGE); */

  uconv(pattern, cpat, ulen(pattern));
  uconv(str,     cstr, ulen(str));

#if 0
  r = onig_new(&reg, (UChar* )pattern, (UChar* )(pattern + ulen(pattern)),
	       ONIG_OPTION_DEFAULT, ENC, &syn, &einfo);
#else
  ci.num_of_elements = 5;
  ci.pattern_enc = ENC;
  ci.target_enc  = ENC;
  ci.syntax      = &syn;
  ci.option      = ONIG_OPTION_DEFAULT;
  ci.case_fold_flag = ONIGENC_CASE_FOLD_DEFAULT;

  r = onig_new_deluxe(&reg, (UChar* )pattern,
          (UChar* )(pattern + ulen(pattern)),
          &ci, &einfo);
#endif

  if (r) {
    char s[ONIG_MAX_ERROR_MESSAGE_LEN];
    onig_error_code_to_str((UChar* )s, r, &einfo);
    fprintf(err_file, "ERROR: %s\\n", s);
    nerror++;
    return ;
  }

  r = onig_search(reg, (UChar* )str, (UChar* )(str + ulen(str)),
		  (UChar* )str, (UChar* )(str + ulen(str)),
		  region, ONIG_OPTION_NONE);
  if (r < ONIG_MISMATCH) {
    char s[ONIG_MAX_ERROR_MESSAGE_LEN];
    onig_error_code_to_str((UChar* )s, r);
    fprintf(err_file, "ERROR: %s\\n", s);
    nerror++;
    return ;
  }

  if (r == ONIG_MISMATCH) {
    if (not) {
      fprintf(stdout, "OK(N): /%s/ '%s'\\n", cpat, cstr);
      nsucc++;
    }
    else {
      fprintf(stdout, "FAIL: /%s/ '%s'\\n", cpat, cstr);
      nfail++;
    }
  }
  else {
    if (not) {
      fprintf(stdout, "FAIL(N): /%s/ '%s'\\n", cpat, cstr);
      nfail++;
    }
    else {
      if (region->beg[mem] == from && region->end[mem] == to) {
        fprintf(stdout, "OK: /%s/ '%s'\\n", cpat, cstr);
        nsucc++;
      }
      else {
        fprintf(stdout, "FAIL: /%s/ '%s' %d-%d : %d-%d\\n", cpat, cstr,
	        (int)from, (int)to, (int)region->beg[mem], (int)region->end[mem]);
        nfail++;
      }
    }
  }
  onig_free(reg);
#endif
}

static void x2(char* pattern, char* str, int from, int to)
{
  xx(pattern, str, from, to, 0, 0);
}

static void x3(char* pattern, char* str, int from, int to, int mem)
{
  xx(pattern, str, from, to, mem, 0);
}

static void n(char* pattern, char* str)
{
  xx(pattern, str, 0, 0, 0, 1);
}

extern int main(int argc, char* argv[])
{
  err_file = stdout;

#ifndef POSIX_TEST
  region = onig_region_new();
#endif
EOS


PAT = '\\/([^\\\\\\/]*(?:\\\\.[^\\\\\\/]*)*)\\/'
CM  = /\s*,\s*/
RX2 = %r{\Ax\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)\)}
RI2 = %r{\Ai\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)\)}
RX3 = %r{\Ax\(#{PAT}#{CM}('[^']*'|"[^"]*")#{CM}(\S+)#{CM}(\S+)#{CM}(\S+)\)}
RN  = %r{\An\(#{PAT}#{CM}('[^']*'|"[^"]*")\)} #'

def convert(endian, fp)

  if (endian == BIG_ENDIAN)
    se = 'BE'
  else
    se = 'LE'
  end

  print(<<"EOS")
#ifdef POSIX_TEST
  reg_set_encoding(REG_POSIX_ENCODING_UTF16_#{se});
#else
  ENC = ONIG_ENCODING_UTF16_#{se};
#endif
EOS

  while line = fp.gets()
    if (m = RX2.match(line))
      reg = conv_reg(endian, m[1])
      str, from, to = conv_str(endian, m[2], m[3], m[4])
      printf("  x2(\"%s\", \"%s\", %s, %s);\n", reg, str, from, to)
    elsif (m = RI2.match(line))
      reg = conv_reg(endian, m[1])
      str, from, to = conv_str(endian, m[2], m[3], m[4])
      printf("  x2(\"%s\", \"%s\", %s, %s);\n", reg, str, from, to)
    elsif (m = RX3.match(line))
      reg = conv_reg(endian, m[1])
      str, from, to = conv_str(endian, m[2], m[3], m[4])
      printf("  x3(\"%s\", \"%s\", %s, %s, %s);\n", reg, str, from, to, m[5])
    elsif (m = RN.match(line))
      reg = conv_reg(endian, m[1])
      str, from, to = conv_str(endian, m[2], 0, 0)
      printf("  n(\"%s\", \"%s\");\n", reg, str)
    else
    end
  end
end

File::open(ARGV[0]) { |fp|
  convert(BIG_ENDIAN, fp)
}

#File::open(ARGV[0]) { |fp|
#  convert(LITTLE_ENDIAN, fp)
#}

print(<<'EOS')
  fprintf(stdout,
       "\nRESULT   SUCC: %d,  FAIL: %d,  ERROR: %d      (by Onigmo %s)\n",
       nsucc, nfail, nerror, onig_version());

#ifndef POSIX_TEST
  onig_region_free(region, 1);
  onig_end();
#endif

  return ((nfail == 0 && nerror == 0) ? 0 : -1);
}
EOS

# END OF SCRIPT