Class: Unmangler::Borland

Inherits:
Base
  • Object
show all
Defined in:
lib/unmangler/borland.rb

Defined Under Namespace

Classes: PEntry

Constant Summary collapse

UM_UNKNOWN =
0x00000000
UM_FUNCTION =
0x00000001
UM_CONSTRUCTOR =
0x00000002
UM_DESTRUCTOR =
0x00000003
UM_OPERATOR =
0x00000004
UM_CONVERSION =
0x00000005
UM_DATA =
0x00000006
UM_THUNK =
0x00000007
UM_TPDSC =
0x00000008
UM_VTABLE =
0x00000009
UM_VRDF_THUNK =
0x0000000a
UM_DYN_THUNK =
0x0000000b
UM_KINDMASK =
0x000000ff
UM_QUALIFIED =

Modifier (is it a member, template?).

0x00000100
UM_TEMPLATE =
0x00000200
UM_VIRDEF_FLAG =
0x00000400
UM_FRIEND_LIST =
0x00000800
UM_CTCH_HNDL_TBL =
0x00001000
UM_OBJ_DEST_TBL =
0x00002000
UM_THROW_LIST =
0x00004000
UM_EXC_CTXT_TBL =
0x00008000
UM_LINKER_PROC =
0x00010000
UM_SPECMASK =
0x0001fc00
UM_MODMASK =
0x00ffff00
UM_BUFOVRFLW =

Some @kind of error occurred.

0x01000000
UM_HASHTRUNC =
0x02000000
UM_ERROR =
0x04000000
UM_ERRMASK =
0x7f000000
UM_NOT_MANGLED =

This symbol is not a mangled name.

0x80000000
MAXBUFFLEN =

maximum output length

8192
QUALIFIER =

New mangle scheme lengths:

len == 254 ==> old hash
len == 253 ==> new MD5 hash
len < 253  ==> unhashed
'@'
ARGLIST =
'$'
TMPLCODE =
'%'
OPS =
{
  "add" => "+",  "adr" => "&",  "and" => "&",  "asg" => "=",  "land"=> "&&",
  "lor" => "||", "call"=> "()", "cmp" => "~",  "fnc" => "()", "dec" => "--",
  "div" => "/",  "eql" => "==", "geq" => ">=", "gtr" => ">",  "inc" => "++",
  "ind" => "*",  "leq" => "<=", "lsh" => "<<", "lss" => "<",  "mod" => "%",
  "mul" => "*",  "neq" => "!=", "new" => "new","not" => "!",  "or"  => "|",
  "rand"=> "&=", "rdiv"=> "/=", "rlsh"=> "<<=","rmin"=> "-=", "rmod"=> "%=",
  "rmul"=> "*=", "ror" => "|=", "rplu"=> "+=", "rrsh"=> ">>=","rsh" => ">>",
  "rxor"=> "^=", "subs"=> "[]", "sub" => "-",  "xor" => "^",  "arow"=> "->",
  "nwa" => "new[]", "dele"=> "delete", "dla" => "delete[]",
  # not in unmangle.c, but from IDA
  "cctr" => "`class constructor`", "cdtr" => "`class destructor`"
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#assert, #isdigit, #safe_unmangle, safe_unmangle, unmangle

Instance Attribute Details

#kindObject

Returns the value of attribute kind.



12
13
14
# File 'lib/unmangler/borland.rb', line 12

def kind
  @kind
end

Instance Method Details

#advanceObject



77
78
79
80
# File 'lib/unmangler/borland.rb', line 77

def advance
  @source.inc!
  input
end

#copy_args(_end, tmplargs) ⇒ Object



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
# File 'lib/unmangler/borland.rb', line 481

def copy_args(_end, tmplargs)
  c = input()
  first = true
  _begin = start = nil
  scanned = false
  param_table = []

  tmplargs =
    case tmplargs
    when 0, nil, false; false
    else true
    end

  while c && ![0, "\0", _end].include?(c)
    if first
      first = false
    else
      append ', '
    end

    _begin   = @source.dup
    start    = @target.dup

    param_table << PEntry.new( @target.dup )
    scanned = false

    while c == 'x' || c == 'w'
      # Decode 'const'/'volatile' modifiers [BCB-265738]
      case c
      when 'x'; copy_string("const ")
      when 'w'; copy_string("volatile ")
      end
      scanned = true
      c = advance()
    end

    if scanned && c != 't'
      @source = _begin.dup
    end

    if c == 't'
      c = advance()
      ptindex = c.to_i(36) - 1
      assert(param_table[ptindex].start)
      assert(param_table[ptindex].len > 0)
      copy_string param_table[ptindex].start[0, param_table[ptindex].len]
      advance()
    else
      copy_type(@target, ! tmplargs)
    end

    param_table.last.len = @target - param_table.last.start

    c = input()

    if (tmplargs && c == '$') # non-type template argument
      termchar = nil
      @target = start.dup
      c = advance()
      advance()
      loop do # loop is for fall through emulation
        case c
        when 'T'
          copy_string("<type ")
          termchar = '>'
          c = 'i'; redo # fall through

        when 'i'
          if _begin[0,5] == "4bool"
            copy_string( input() == '0' ? "false" : "true" )
            advance()
            break
          end
          c = 'j'; redo # fall through

        when 'j','g','e'
          copy_until('$')
          copy_char(termchar) if termchar
          break

        when 'm'
          copy_until('$')
          append '::*'
          copy_until('$')
          break

        else
          raise "Unknown template arg @kind"
        end # case
      end # loop

      assert(input() == '$')
      c = advance()
    end # if
  end # while (c && c != _end)
end

#copy_char(c) ⇒ Object



82
83
84
85
86
# File 'lib/unmangler/borland.rb', line 82

def copy_char c
  #puts "[d] copy_char #{c.inspect} from #{caller[0]}"
  @target[0] = (c == 0 ? "\x00" : c)
  @target.inc!
end

#copy_delphi4args(_end, tmplargs) ⇒ Object

def copy_type



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
# File 'lib/unmangler/borland.rb', line 398

def copy_delphi4args(_end, tmplargs)
  first = true
  _begin = start = nil
  termchar = nil

  tmplargs =
    case tmplargs
    when 0, nil, false; false
    else true
    end

  c = input()
  while (c && c != _end)
    if first
      first = false
    else
      append ', '
    end

    _begin = @source.dup
    start  = @target.dup

    advance()    # skip the @kind character

    # loop is for fallthrough emulation
    loop do
      case c
      when 't'
        copy_type(@target, ! tmplargs)
        break

      when 'T'
        copy_string("<type ")
        termchar = '>'
        c = 'i'; redo # fall through

      when 'i'
        if _begin[0,5] == '4bool'
          copy_string( input() == '0' ? "false" : "true" )
          advance()
          break
        else
          # XXX ZZZ fall through, but not sure that its intended behaviour
          # in original code
          c = 'j'; redo
        end

      when 'j','g','e'
        copy_type(@target, ! tmplargs)
        @target = start.dup
        assert(input() == '$'); advance()
        copy_until('$', TMPLCODE)
        copy_char(termchar) if termchar
        break

      when 'm'
        copy_type(@target, ! tmplargs)
        @target = start.dup
        assert(input() == '$'); advance()

        copy_until('$')
        append '::*'
        copy_until('$', TMPLCODE)
        break

      else
        raise "Unknown template arg @kind"
      end # case
    end # loop

    c = input()
    if (c != _end)
      assert(c == '$')
      c = advance()
    end
  end # while c && c != _end
end

#copy_name(tmplname) ⇒ Object



624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
# File 'lib/unmangler/borland.rb', line 624

def copy_name tmplname
  start = save_setqual = nil
  c = input()

  tmplname =
    case tmplname
    when 0, nil, false; false
    else true
    end

  # Start outputting the qualifier names and the base name.

  while true
    if @set_qual
      @base_name = @target.dup
    end

    # Examine the string to see what this is.  Either it's a
    # qualifier name, a member name, a function name, a template
    # name, or a special name. We wouldn't be here if this were a
    # regular name.

    if isdigit(c)
      # If there's a number at the beginning of a name, it
      #   could only be a vtable symbol flag.

      flags = c[0].ord - '0'.ord + 1

      @vtbl_flags << "huge"     if( flags & 1 != 0 )
      @vtbl_flags << "fastthis" if( flags & 2 != 0 )
      @vtbl_flags << "rtti"     if( flags & 4 != 0 )

      @kind = (@kind & ~UM_KINDMASK) | UM_VTABLE

      c = advance()
      assert(c == 0 || c == '$')
    end

    case c
      when '#'    # special symbol used for cond syms
        c = advance()
        if c == '$'
          assert(advance() == 'c')
          assert(advance() == 'f')
          assert(advance() == '$')
          assert(advance() == '@')

          copy_string("__vdflg__ ")
          advance()
          copy_name(0)

          @kind |= UM_VIRDEF_FLAG
        end
        return

      when QUALIFIER    # virdef flag or linker proc
          advance()
          copy_string("__linkproc__ ")
          copy_name(0)
          @kind |= UM_LINKER_PROC
          return

      when TMPLCODE    # template name
        advance()
        copy_tmpl_args()

        if (input() != QUALIFIER)
          @kind |= UM_TEMPLATE
        end

      when ARGLIST    # special name, or arglist
        return if tmplname

        c = advance()
        if c == 'x'
          c = advance()
          if c == 'p' || c == 't'
            assert(advance() == ARGLIST)
            advance()
            copy_string("__tpdsc__ ")
            copy_type(@target, 0)
            @kind = (@kind & ~UM_KINDMASK) | UM_TPDSC
            return
          else
            raise "What happened?"
          end
        end # if c == 'x'

        if c == 'b'
          c = advance()
          start = @source.dup

          if (c == 'c' || c == 'd') && advance() == 't' && advance() == 'r'
              assert(advance() == ARGLIST)

              # The actual outputting of the name will happen
              #   outside of this function, to be sure that we
              #   don't include any special name characters.

              if (c == 'c')
                @kind = (@kind & ~UM_KINDMASK) | UM_CONSTRUCTOR
              else
                @kind = (@kind & ~UM_KINDMASK) | UM_DESTRUCTOR
              end
          else
            @source = start.dup
            copy_string("operator ")
            start = @target.dup
            copy_until(ARGLIST)
            @target = start      # no dup() here intentionally
            # copy_op now will overwrite already copied encoded operator name
            # i.e. "subs" => "[]"
            copy_op start[0..-1]
            # trim string if decoded operator name was shorter than encoded
            @target[0..-1] = ''
            @kind = (@kind & ~UM_KINDMASK) | UM_OPERATOR
          end

        elsif (c == 'o')
          advance()
          copy_string("operator ")
          save_setqual = @set_qual
          @set_qual = false
          copy_type(@target, 0)
          @set_qual = save_setqual
          assert(input() == ARGLIST)
          @kind = (@kind & ~UM_KINDMASK) | UM_CONVERSION

        elsif (c == 'v' || c == 'd')
          tkind = c
          c = advance()
          if (tkind == 'v' && c == 's')
            c = advance()
            assert(c == 'f' || c == 'n')
            advance()
            copy_string("__vdthk__")
            @kind = (@kind & ~UM_KINDMASK) | UM_VRDF_THUNK
          elsif (c == 'c')
            c = advance()
            assert(isdigit(c))
            c = advance()
            assert(c == '$')
            c = advance()

            copy_string("__thunk__ [")
            @kind = (@kind & ~UM_KINDMASK) |
            (tkind == 'v' ? UM_THUNK : UM_DYN_THUNK)

            copy_char(c)
            copy_char(',')

            while ((c = advance()) != '$'); copy_char(c); end
            copy_char(',')
            while ((c = advance()) != '$'); copy_char(c); end
            copy_char(',')
            while ((c = advance()) != '$'); copy_char(c); end
            copy_char(']')

            advance()  # skip last '$'
            return
          end
        else
          raise "Unknown special name"
        end

      when '_'
        start = @source.dup

        if advance() == '$'
          c = advance()

          # At the moment there are five @kind of special names:
          #   frndl FL friend list
          #   chtbl CH catch handler table
          #   odtbl DC object destructor table
          #   thrwl TL throw list
          #   ectbl ETC exception context table

          append '__'

          case @source[0,2]
            when 'FL'
              copy_string("frndl")
              @kind |= UM_FRIEND_LIST

            when 'CH'
              copy_string("chtbl")
              @kind |= UM_CTCH_HNDL_TBL

            when 'DC'
              copy_string("odtbl")
              @kind |= UM_OBJ_DEST_TBL

            when 'TL'
              copy_string("thrwl")
              @kind |= UM_THROW_LIST

            when 'EC'
              copy_string("ectbl")
              @kind |= UM_EXC_CTXT_TBL
          end

          append '__ '

          while (c >= 'A' && c <= 'Z'); c = advance(); end

          assert(c == '$')
          assert(advance() == '@')
          advance()

          copy_name(0)

          return
        end # if advance() == '$'

        @source = start.dup
        copy_until(QUALIFIER, ARGLIST)

      else
        # default case
        copy_until(QUALIFIER, ARGLIST)
    end # case c

    # If we're processing a template name, then '$' is allowed to
    #   end the name.

    c = input()

    assert(c == nil || c == QUALIFIER || c == ARGLIST)

    if c == QUALIFIER
      c = advance()

      if @set_qual
        @prevqual = @qualend && @qualend.dup
        @qualend  = @target.dup
      end

      append '::'

      if (c == 0)
        @kind = (@kind & ~UM_KINDMASK) | UM_VTABLE
      end
    else
      break
    end
  end # while true
end

#copy_op(src) ⇒ Object



134
135
136
# File 'lib/unmangler/borland.rb', line 134

def copy_op src
  copy_string(OPS[src] || "?#{src}?")
end

#copy_return_type(start, callconv, regconv, process_return) ⇒ Object



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
# File 'lib/unmangler/borland.rb', line 138

def copy_return_type(start, callconv, regconv, process_return)
  start = start.dup if start.is_a?(StringPtr)

  ret_len = 0
  # Process the return type of a function, and shuffle the output
  #   text around so it looks like the return type came first.
  ret_type = @target.dup

  unless [0,nil,false].include?(process_return)
    copy_type(@target, 0)
    copy_char(' ')
  end

  copy_string(callconv) if callconv
  copy_string(regconv) if regconv

  ret_len = @target - ret_type

  # Set up the return type to have a space after it.

  # "foo((*)(float, int)double "

  buff = ret_type[0, ret_len]
  start[ret_len, ret_type-start] = start[0, ret_type-start]
  start[0, ret_len] = buff[0, ret_len]

  # "foo(double (*)(float, int)"

  # If we are inserting this return type at the very beginning of a
  #   string, it means the location of all the qualifier names is
  #   about to move.

  if @adjust_quals
    @namebase += ret_len if @namebase
    @qualend  += ret_len if @qualend
    @prevqual += ret_len if @prevqual
    @base_name+= ret_len if @base_name
    @base_end += ret_len if @base_end
  end
end

#copy_string(s, len = 0) ⇒ Object Also known as: append



88
89
90
91
92
93
94
# File 'lib/unmangler/borland.rb', line 88

def copy_string s, len=0
  if len == 0
    @target << s
  else
    @target << s[0,len]
  end
end

#copy_tmpl_argsObject

parse template name and arguments according to the grammar:

tmpl_args:
    % generic_name args %
args:
    $ new_args
    bcb3_args


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
618
619
620
621
622
# File 'lib/unmangler/borland.rb', line 584

def copy_tmpl_args
  c = input()
  save_setqual = nil
  isDelphi4name = (c == 'S' || c == 'D') && (@source =~ /\A(Set|DynamicArray|SmallString|DelphiInterface)\$/)

  # Output the base name of the template. We use 'copy_name' instead of
  # 'copy_until', since this could be a template constructor name, f.ex.

  copy_name(1)
  assert(input() == ARGLIST)
  advance()

  # using @target[-1] will be ambiguous for ruby's string[-1] - last char of string
  copy_char(' ') if (@target-1)[0] == '<'
  copy_char('<')

  # Copy the template arguments over.  Also, save the
  # '@set_qual' variable, since we don't want to mix up the
  # status of the currently known qualifier name with a
  # name from a template argument, for example.

  save_setqual = @set_qual
  @set_qual = false

  if isDelphi4name
    copy_delphi4args(TMPLCODE, 1)
  else
    copy_args(TMPLCODE, 1)
  end

  @set_qual = save_setqual

  # using @target[-1] will be ambiguous for ruby's string[-1] - last char of string
  copy_char(' ') if (@target-1)[0] == '>'
  copy_char('>')

  assert(input() == TMPLCODE)
  advance()
end

#copy_type(start, arglvl) ⇒ Object

def copy_return_type



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
# File 'lib/unmangler/borland.rb', line 179

def copy_type(start, arglvl)
  start = start.dup if start.is_a?(StringPtr)

  tname = buff  = nil
  c             = input()
  is_const      = false
  is_volatile   = false
  is_signed     = false
  is_unsigned   = false
  savedsavechar = nil

  arglvl =
    case arglvl
    when 0, nil, false; false
    else true
    end

  maxloop = 101
  loop do
    assert((maxloop-=1) > 0)
    case c
    when 'u'; is_unsigned = true
    when 'z'; is_signed   = true
    when 'x'; is_const    = true
    when 'w'; is_volatile = true
    when 'y'
      # 'y' for closure is followed by 'f' or 'n'
      c = advance()
      assert(c == 'f' || c == 'n')
      copy_string("__closure")
    else
      break
    end

    c = advance()
  end # loop

  if isdigit(c)    # enum or class name
    i = 0

    begin      # compute length
        i = i*10 + c.to_i
        c = advance()
    end while isdigit(c)

    # Output whether this class name was const or volatile.

    # These were already printed (see  [BCB-265738])
    #if 0
    #  if (is_const) copy_string("const ")
    #  if (is_volatile) copy_string("volatile ")
    #endif

    # ZZZ
    s0 = @source.string.dup
    @source[i] = "\x00"
    @source.trim!
    copy_name(0)
    @source.string = s0
    @target.trim!
    return
  end # if isdigit(c)

  @savechar = c
  tname = nil

  if c == 'M' # member pointer
    name = @target.dup
    # We call 'copy_type' because it knows how to extract
    # length-prefixed names.
    advance()
    copy_type(@target, 0)
    len = @target - name
    #len = MAXBUFFLEN - 1 if (len > MAXBUFFLEN - 1)
    buff = name[0,len]
    @target = name
  end

  case c
  when 'v'; tname = "void"
  when 'c'; tname = "char"
  when 'b'; tname = "wchar_t"
  when 's'; tname = "short"
  when 'i'; tname = "int"
  when 'l'; tname = "long"
  when 'f'; tname = "float"
  when 'd'; tname = "double"
  when 'g'; tname = "long double"
  when 'j'; tname = "long long"
  when 'o'; tname = "bool"
  when 'e'; tname = "..."

  when 'C'      # C++ wide char
    c = advance()
    if c == 's'
      tname = "char16_t"
    elsif c == 'i'
      tname = "char32_t"
    else
      raise "Unknown wide char type: 'C#{c}'"
    end

  when 'M','r','h','p' # member pointer, reference, rvalue reference, pointer
    if (@savechar == 'M')
      case c = input()  # [BTS-??????]
      when 'x'; is_const = true; c = advance()    # [BCB-272500]
      when 'w'; is_volatile = true; c = advance()
      end
    else
      c = advance()
    end

    if (c == 'q')    # function pointer
      copy_char('(')

      if (@savechar == 'M')
        copy_string(buff)
        append "::"
      end

      append "*)"

      @savechar = c
    end

    savedsavechar = @savechar;    # [BTS-263572]
    copy_type(start, 0)
    @savechar = savedsavechar

    case @savechar
    when 'r'; copy_char('&')
    when 'h'; append('&&')
    when 'p'; append(' *')
    when 'M'
      assert(buff)
      copy_char(' ')
      copy_string(buff)
      append '::*'
    end

  when 'a'      # array
    dims = ''

    begin
      c = advance()
      dims << '['
      c = advance() if (c == '0') # 0 size means unspecified
      while (c != '$')  # collect size, up to '$'
        dims << c
        c = advance()
      end
      assert(c == '$')
      c = advance()
      dims << ']'
    end while (c == 'a')  # collect all dimensions

    copy_type(@target, 0)
    copy_string(dims)

  when 'q'      # function
    callconv = regconv = hasret = save_adjqual = nil

    # We want the return type first, but find it last. So we emit
    # all but the return type, get the return type, then shuffle
    # to get them in the right place.

    loop do
      break if (advance() != 'q')

      case advance()
      when 'c'; callconv = "__cdecl "
      when 'p'; callconv = "__pascal "
      when 'r'; callconv = "__fastcall "
      when 'f'; callconv = "__fortran "
      when 's'; callconv = "__stdcall "
      when 'y'; callconv = "__syscall "
      when 'i'; callconv = "__interrupt "
      when 'g'; regconv = "__saveregs "
      end
    end

    save_adjqual = @adjust_quals
    @adjust_quals = false

    copy_char('(')
    copy_args('$', 0)
    copy_char(')')

    @adjust_quals = save_adjqual

    hasret = input() == '$'
    advance() if hasret

    if (hasret || callconv || regconv)
      copy_return_type(start, callconv, regconv, hasret)
    end

  when ARGLIST      # template arg list
    # break
  when TMPLCODE      # template reference
    # break

  else
    raise "Unknown type: #{c.inspect}"
  end # case

  if (tname)
    copy_string("const ")    if is_const
    copy_string("volatile ") if is_volatile
    copy_string("signed ")   if is_signed
    copy_string("unsigned ") if is_unsigned
    copy_string(tname)       if (!arglvl || @savechar != 'v')
    advance()
  else
    copy_string(" const")    if is_const
    copy_string(" volatile") if is_volatile
  end
end

#copy_until(*term_chars) ⇒ Object

copy all remaining input until input end or any of term_chars is met



99
100
101
102
103
104
105
106
107
108
109
110
# File 'lib/unmangler/borland.rb', line 99

def copy_until *term_chars
  term =
    case term_chars.size
      when 0; raise "no term_chars"
      when 1; term_chars.first
      else
        Regexp.new( "[" + term_chars.map{ |c| Regexp::escape(c) }.join + "]" )
    end
  len = @source.index(term) || @source[0..-1].size
  @target << @source[0,len]
  @source += len
end

#inputObject



68
69
70
71
72
73
74
75
# File 'lib/unmangler/borland.rb', line 68

def input
#    if @srcindx >= @hashstart
#      raise 'UM_HASHTRUNC'
#    else
    c = @source[0]
    #c == "\x00" ? 0 : c
#    end
end

#strchr(haystack, needle) ⇒ Object



112
113
114
115
116
117
118
# File 'lib/unmangler/borland.rb', line 112

def strchr haystack, needle
  if idx = haystack.index(needle)
    StringPtr.new(haystack, idx)
  else
    nil
  end
end

#unmangle(src, args = {}) ⇒ Object

umKind unmangle(src, dest, maxlen, qualP, baseP, doArgs)

doArgs
  if this argument is non-0 (aka TRUE), it means that when
  unmangling a function name, its arguments should also be
  unmangled as part of the output name.  Otherwise, only the name
  will be unmangled, and not the arguments.


881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
# File 'lib/unmangler/borland.rb', line 881

def unmangle src, args = {}
  # all Borland mangled names start with '@' character.
  return src if !src || src.empty? || src[0] != '@'

  # check for Microsoft compatible fastcall names, which are of the form:
  # @funcName@<one or more digits indicating size of all parameters>
  return src if src =~ /\A@.*@\d+\Z/

  # unmangle args? defaulting to true
  doArgs = args.fetch(:args, true)

  @vtbl_flags = []
  @kind = 0
  @source_string = ''
  @source = StringPtr.new(@source_string)
  @result = ''
  @target = StringPtr.new(@result)

  #return UM_ERROR if src.size > 254
  @hashstart =
    case src.size
    when 254 # old hash for bcc version 0x600 and earlier
      250
    when 253 # new hash for bcc version 0x610 and later
      231
    else
      MAXBUFFLEN
    end

  @savechar = 0

  src = src[1..-1] # skip initial '@'

# ZZZ XXX not sure if it's needed now
#    if src[/[a-z]/]
#      # contains lowercase letters => not Pascal
#      # b/c Pascal names can not contain lowercase letters
#    else
#      # Pascal, convert uppercase Pascal names to lowercase
#      src.downcase!
#    end

  # This is at LEAST a member name, if not a fully mangled template
  # or function name. So, begin outputting the subnames. We set up
  # the pointers in globals so that we don't have to pass
  # everything around all the time.

  @kind      = UM_UNKNOWN
  @source_string = src
  @source    = StringPtr.new(@source_string)
  @prevqual  = @qualend = @base_name = @base_end = nil
  @set_qual  = true

  # Start outputting the qualifier names and the base name.

  @namebase = @target.dup

  copy_name(0)
  @set_qual = false
  @base_end = @target.dup

  if (@kind & UM_KINDMASK) == UM_TPDSC || (@kind & UM_SPECMASK) != 0
    p = strchr(@namebase, ' ')
    assert(p)
    @namebase = p + 1
  end

  if [UM_CONSTRUCTOR,UM_DESTRUCTOR].include?( @kind & UM_KINDMASK )
    copy_char('~') if @kind & UM_KINDMASK == UM_DESTRUCTOR

    if @qualend
      start = @prevqual ? (@prevqual+2) : @namebase.dup
      len = @qualend - start
      copy_string(start, len)
    else
      # It's a bcc-created static constructor??
      # give it a name.
      copy_string("unknown")
    end
  end

  # If there's a function argument list, copy it over in expanded
  #   form.

  if input() == ARGLIST && doArgs # function args
    c = advance()
    assert(c == 'q' || c == 'x' || c == 'w')

    # Output the function parameters, and return type in the case
    #   of template function specializations.

    @set_qual = false
    @adjust_quals = true

    copy_type(@namebase, 0)

    if ((@kind & UM_KINDMASK) == UM_UNKNOWN)
      @kind |= UM_FUNCTION
    end

  elsif ((@kind & UM_KINDMASK) == UM_UNKNOWN)
    @kind |= UM_DATA
  elsif @vtbl_flags.any?
    copy_string(" (" + @vtbl_flags.join(", ") + ")")
  end

  # Put some finishing touches on the @kind of this entity.

  if (@qualend)
    @kind |= UM_QUALIFIED
  end

  # trim unwanted result tail, if any
  @target[0..-1] = ''

  # If the user wanted the qualifier and base name saved, then do it now.

  # TODO
#    if (@kind & UM_ERRMASK) == 0
#      if @qualend
#        len = @qualend - @namebase
#        @qualP = @namebase[0, len]
#      end
#
#      if @base_name
#        len = @base_end - @base_name
#        @baseP = @base_name[0, len]
#      end
#    end

  # move '__fastcall' to start of the string if its found in middle of string
  pos = @result.index(" __fastcall ")
  if pos && pos != 0
    @result = "__fastcall " + @result.sub("__fastcall ", "")
  end

  # sometimes const args are marked "const const",
  # original tdump.exe tool also have this bug
  @result.gsub! "const const ", "const "

  # doArgs implicitly includes calling convention, but '__tpdsc__' is always
  # returned by original code, so strip it here if doArgs == false
  unless doArgs
    @result.sub! /\A__tpdsc__ /,''
  end

  # copy IDA syntax for class ctor/dtor:
  #   was: Classes::TThread::operator `class destructor`()
  #   now: Classes::TThread::`class destructor`()
  @result.sub! '::operator `class', '::`class'

  @result
end