Fresh IDE . Check-in [b8ab87cfc4]
Not logged in

This repository is a mirror!

The original is located on: https://fresh.flatassembler.net/fossil/repo/fresh
If you want to follow the project, please update your remote-url

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Merged with FreshLibDev. Some serious conflicts with the existing code are possible, especially because of TText interface has been changed!
Timelines: family | ancestors | descendants | both | NoCanvasGUI
Files: files | file ages | folders
SHA1:b8ab87cfc4f5cb187cc5160b1682ade2369e3b3d
User & Date: johnfound 2019-04-22 17:00:00
Context
2019-04-27
16:52
TAsmEdit refactored to the TText interface changes. check-in: 59f19b3b5c user: johnfound tags: NoCanvasGUI
2019-04-22
17:00
Merged with FreshLibDev. Some serious conflicts with the existing code are possible, especially because of TText interface has been changed! check-in: b8ab87cfc4 user: johnfound tags: NoCanvasGUI
16:34
Draw the menu selection without the icon. check-in: 06f6f3d9ee user: johnfound tags: NoCanvasGUI
16:08
Fixed the forgotten procedure OpenOrCreate for opening SQLite database. check-in: f891176243 user: johnfound tags: FreshLibDev
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to freshlib/data/Linux/random.asm.

45
46
47
48
49
50
51

52
53
54
55
56
57
58
        add     esi, eax
        sub     [.count], eax
        jnz     .loop

        mov     eax, sys_close
        int     $80


        mov     [esp+4*regEAX], edi
        clc
        popad
        return

.error2:
        mov     eax, sys_close







>







45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
        add     esi, eax
        sub     [.count], eax
        jnz     .loop

        mov     eax, sys_close
        int     $80

        clc
        mov     [esp+4*regEAX], edi
        clc
        popad
        return

.error2:
        mov     eax, sys_close

Changes to freshlib/data/all.asm.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
include 'i18n.asm'
include 'arrays.asm'
include 'strlib.asm'
include 'base64.asm'
include 'uconfig.asm'
include 'memstream.asm'
include 'markdown.asm'
include 'hashes.asm'

include 'bitstream.asm'
include 'bytestream.asm'
include 'deflate.asm'

include 'buffergap.asm'
include 'counter_array.asm'
include 'random.asm'






<









1
2
3
4
5
6

7
8
9
10
11
12
13
14
15
include 'i18n.asm'
include 'arrays.asm'
include 'strlib.asm'
include 'base64.asm'
include 'uconfig.asm'
include 'memstream.asm'

include 'hashes.asm'

include 'bitstream.asm'
include 'bytestream.asm'
include 'deflate.asm'

include 'buffergap.asm'
include 'counter_array.asm'
include 'random.asm'

Changes to freshlib/data/arrays.asm.

651
652
653
654
655
656
657



658
659
660
661
662
663
664
; Frees the array of items
;--------------------------------------------
proc ListFree, .ptrList, .FreeProc
begin
        pushad

        mov     edi, [.ptrList]



        mov     ebx, [edi+TArray.count]

        cmp     [.FreeProc], 0
        je      .endwhile

.freeloop:
        dec     ebx







>
>
>







651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
; Frees the array of items
;--------------------------------------------
proc ListFree, .ptrList, .FreeProc
begin
        pushad

        mov     edi, [.ptrList]
        test    edi, edi
        jz      .finish

        mov     ebx, [edi+TArray.count]

        cmp     [.FreeProc], 0
        je      .endwhile

.freeloop:
        dec     ebx

Added freshlib/data/bbcode.asm.

































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
618
619
620
621
622
623
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
module "BBCode translator"

iglobal
PHashTable tableBBtags, tpl_func,                      \
        'b',       tagStrong,                          \
        '*',       tagListItem,                        \
        'i',       tagEm,                              \
        'u',       tagUnderlined,                      \
        's',       tagDel,                             \
        'c',       tagInlineCode,                      \
        'url',     tagURL,                             \
        'img',     tagImg,                             \
        'quote',   tagQuote,                           \
        'code',    tagCode,                            \
        'size',    tagSize,                            \
        'color',   tagColor,                           \
        'list',    tagUL,                              \
        'ul',      tagUL,                              \
        'ol',      tagOL,                              \
        'table',   tagTable,                           \
        'tr',      tagTR,                              \
        'th',      tagTH,                              \
        'td',      tagTD,                              \
        'spoiler', tagSpoiler,                         \
        'email',   tagEmail,                           \
        'h1',      tagH1,                              \
        'h2',      tagH2,                              \
        'h3',      tagH3,                              \
        'h4',      tagH4,                              \
        'h5',      tagH5,                              \
        'h6',      tagH6,                              \
        ':)',      emoSmile,                           \
        ':d',      emoLol,                             \
        ':rofl:',  emoRofl,                            \
        ';)',      emoWink,                            \
        ':p',      emoTongue,                          \
        ':(',      emoSad,                             \
        ':`(',     emoCry,                             \
        '>:(',     emoAngry
endg

struct TOneTag
  .pOpenP    dd ?       ; Open tag prefix. for example '<img src="'
  .lOpenP    dd ?
  .pOpenS    dd ?       ; Open tag suffix. for example '" alt="'
  .lOpenS    dd ?
  .pClose    dd ?       ; Close tag. for example '" />'
  .lClose    dd ?
  .flags     dd ?       ; encoding flags for this tag. the first byte contain flags for the text between .pOpenP and .pOpenS
                        ; the second byte for the text between .pOpenS and .pClose
ends

; examples:
; [b]:                                  '<b>', 0, '</b>'
; [quote=title]text[/quote]:            '<blockquote><header>', '</header>', '</blockquote>'
; [img]image_url[/img]:                 '<img alt="', '" src="', '" />'
; [code=language][/code]:               '<code class="', '">', '</code>'


struc flex [arg] {
common
  local f1, f2, cnt, sz
  cnt = 0
  sz = 0
  f1 = 1
  f2 equ FALSE
forward
  match any, arg \{
    match `arg, arg \\{ f2 equ TRUE \\}
  \}

  cnt = cnt + 1
  if arg eqtype ''
    virtual at 0
      db arg
      sz = sz + $
    end virtual
  end if

common
  local lbl

  match =txt string, arg \{
    f1 = 0
    lbl text string
    f2 equ FALSE
  \}

  if (sz > 4) | (cnt>1)
    f1 = 0
  end if

  if f1
    . = arg
    .length = 0
  else
    . = lbl
    .length = lbl#\.length
  end if
  match =TRUE, f2 \{ lbl text arg \}
}


struc onetag prefix, suffix, close, flags {
common
  local ..pref, ..suff, ..close
  local ..fpr, ..fsf, ..fcl

  label . dword
if used .
  ..pref flex prefix
  ..suff flex suffix
  ..close flex close

  .pOpenP dd ..pref
  .lOpenP dd ..pref#\.length
  .pOpenS dd ..suff
  .lOpenS dd ..suff#\.length
  .pClose dd ..close
  .lClose dd ..close#\.length
  .flags  dd flags
end if
}


fEmptyAttr    = $00000001        ; empty value of the attribute is acceptable.
fBlockTag     = $00000002        ; This is a block tag. Allows paragraph generation and resets its state.
fDisableTags  = $00000004        ; Disable tag generation inside this block; The only tag that is recongized is the respective closing tag.
fLines        = $00000008        ; the tag can contain text lines.
fInlineTag    = $00000010
fMinimagBlock = $80000000        ; The tags, that are open/close by ;tagname/;end in MiniMagAsm

iglobal
  align 4

  tagStrong       onetag txt '<strong>',                              0,              txt '</strong>',    fInlineTag
  tagEm           onetag txt '<em>',                                  0,              txt '</em>',        fInlineTag
  tagUnderlined   onetag txt '<span class="underlined">',             0,              txt '</span>',      fInlineTag
  tagDel          onetag txt '<del>',                                 0,              txt '</del>',       fInlineTag
  tagInlineCode   onetag txt '<code>',                                0,              txt '</code>',      fInlineTag
  tagCode         onetag txt '<code class="block ',                   txt '">',       txt '</code>',      fBlockTag or fEmptyAttr or fDisableTags or fMinimagBlock
  tagQuote        onetag txt '<blockquote><header>',                  txt '</header>',txt '</blockquote>',fBlockTag or fEmptyAttr or fMinimagBlock
  tagUL           onetag txt '<ul>',                                  0,              txt '</ul>',        fBlockTag or fMinimagBlock
  tagOL           onetag txt '<ol type="',                            txt '">',       txt '</ol>',        fBlockTag or fEmptyAttr or fMinimagBlock
  tagListItem     onetag txt '<li>',                                  0,              txt '</li>',        fBlockTag
  tagTable        onetag txt '<table>',                               0,              txt '</table>',     fBlockTag or fMinimagBlock
  tagTR           onetag txt '<tr>',                                  0,              txt '</tr>',        fBlockTag
  tagTH           onetag txt '<th>',                                  0,              txt '</th>',        fBlockTag
  tagTD           onetag txt '<td>',                                  0,              txt '</td>',        fBlockTag
  tagSpoiler      onetag txt '<details><summary>',                    txt '</summary>',txt '</details>',   fBlockTag or fMinimagBlock
  tagURL          onetag txt '<a href="',                             txt '">',       txt '</a>',         0
  tagImg          onetag txt '<img class="block" alt="',              txt '" src="',  txt '" />',         fBlockTag  or fDisableTags
  tagInlineImg    onetag txt '<img class="inline" alt="',             txt '" src="',  txt '" />',         fInlineTag or fDisableTags
  tagSize         onetag txt '<span style="font-size:',               txt '">',       txt '</span>',      fInlineTag
  tagColor        onetag txt '<span style="color:',                   txt '">',       txt '</span>',      fInlineTag
  tagEmail        onetag txt '<a href="mailto:',                      txt '">',       txt '</a>',         0
  tagH1           onetag txt '<h1>',                                  0,              txt '</h1>',        fBlockTag or fLines
  tagH2           onetag txt '<h2>',                                  0,              txt '</h2>',        fBlockTag or fLines
  tagH3           onetag txt '<h3>',                                  0,              txt '</h3>',        fBlockTag or fLines
  tagH4           onetag txt '<h4>',                                  0,              txt '</h4>',        fBlockTag or fLines
  tagH5           onetag txt '<h5>',                                  0,              txt '</h5>',        fBlockTag or fLines
  tagH6           onetag txt '<h6>',                                  0,              txt '</h6>',        fBlockTag or fLines
  tagPara         onetag txt '<p>',                                   0,              txt '</p>',         fLines
  tagHR           onetag txt '<hr>',                                  0,              0,                  0

  tagImgMM        onetag txt '<img class="block" src="',              txt '" alt="',  txt '" />',           fBlockTag  or fDisableTags
  tagInlineImgMM  onetag txt '<img class="inline" src="',             txt '" alt="',  txt '" />',           fInlineTag or fDisableTags
  tagVideoMM      onetag txt '<video controls><source src="',         txt '">',       txt '</video>',     0

  ; Emoticons tags.

  emoSmile        onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'smile.gif" alt=":)">'>,   0,              0,              0
  emoLol          onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'lol.gif" alt=":D">'>,     0,              0,              0
  emoRofl         onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'rofl.gif" alt=":D">'>,    0,              0,              0
  emoWink         onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'wink.gif" alt=";)">'>,    0,              0,              0
  emoTongue       onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'tongue.gif" alt=":P">'>,  0,              0,              0
  emoSad          onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'sad.gif" alt=":(">'>,     0,              0,              0
  emoCry          onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'cry.gif" alt=":`(">'>,    0,              0,              0
  emoAngry        onetag <'<img class="emoticon" src="', EMOTICONS_PATH, 'angry.gif" alt=">:(">'>,  0,              0,              0

endg

; 1 - paragraph starts on every new line.
; 2 - paragraph starts on every two consecutive new lines (like in MarkDown)
NEW_PARAGRAPH_EMPTY_LINES = 1

;--------------------------------------------------------------
; Converts BB code into HTML. Starts from the position [.from]
; and ends at the end of the text or at first NULL character.
;
; Arguments:
;    .pBBCode - pointer to the TText structure with the BBCode
;               formatted text.
;    .from    - the position where to start.
;
; Returns:
;    edx - The same structure (possibly reallocated) with
;    the translated to html code text.
;

proc TranslateBBCode, .pBBCode, .from
.pStack dd ?
.ofsStart dd ?
.tagType  dd ?
.tagFlags dd ?

.fNewLine dd ?
begin
        pushad

        stdcall CreateArray, 4
        mov     [.pStack], eax

        or      [.fNewLine], -1 ; first new line implicit.
        and     [.tagFlags], 0

        mov     edx, [.pBBCode]

        OutputValue "BBCode TText.GapBegin: ", [edx+TText.GapBegin], 10, -1
        OutputValue "               GapEnd: ", [edx+TText.GapEnd], 10, -1
        OutputValue "               Length: ", [edx+TText.Length], 10, -1
        OutputValue "       Translate from: ", [.from], 10, -1

        mov     ebx, [.from]
        jmp     .loop

.next:
        inc     ebx

.loop:
        mov     ecx, [edx+TText.GapEnd]
        cmp     ebx, [edx+TText.GapBegin]
        cmovb   ecx, [edx+TText.GapBegin]
        sub     ecx, [edx+TText.GapBegin]
        add     ecx, ebx
        cmp     ecx, [edx+TText.Length]
        jae     .end_of_text

        movzx   eax, byte [edx+ecx]

        test    al, al
        jz      .end_of_text

        cmp     al, $0d
        je      .new_line

        cmp     al, $0a
        je      .new_line

        cmp     al, $20
        jbe     .next           ; skip all whitespace

        cmp     al, "["
        je      .start_tag

        cmp     [.fNewLine], NEW_PARAGRAPH_EMPTY_LINES
        mov     [.fNewLine], 0
        jb      .paragraph_ok

        test    [.tagFlags], fDisableTags
        jnz     .paragraph_ok

        push    eax
        stdcall TextMoveGap, edx, ebx
        stdcall TextSetGapSize, edx, 4
        mov     eax, [edx+TText.GapBegin]
        mov     dword [edx+eax], "<p>"
        add     [edx+TText.GapBegin], 3
        add     ebx, 3
        pop     eax

.paragraph_ok:

.continue:

; html encoding from here

        test    al, al          ; all values > 127 are unicode and should not be encoded.
        js      .next

        movzx   eax, byte [tbl_html+eax]
        test    al, al
        jz      .del_char
        jns     .next           ; the same as above

        lea     esi, [eax+tbl_html]     ; the address of the replacement string.
        lodsb
        movzx   ecx, al         ; length

; insert the replacement html encoding from esi
        stdcall TextMoveGap, edx, ebx
        stdcall TextSetGapSize, edx, ecx
        inc     [edx+TText.GapEnd]      ; delete the previous char.

        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx
        add     ebx, ecx

        rep movsb
        jmp     .loop

.del_char:
        stdcall TextMoveGap, edx, ebx
        inc     [edx+TText.GapEnd]      ; delete the current char.
        jmp     .loop

.not_a_tag_pop:

        add     esp, 4

.not_a_tag:

        mov     ebx, [.ofsStart]
        mov     eax, "["
        jmp     .continue

;------------------------------------------------------------------------------------

.new_line:
        inc     ebx

        mov     ecx, [edx+TText.GapEnd]
        cmp     ebx, [edx+TText.GapBegin]
        cmovb   ecx, [edx+TText.GapBegin]
        sub     ecx, [edx+TText.GapBegin]
        add     ecx, ebx
        cmp     ecx, [edx+TText.Length]
        jae     .end_of_text

        inc     [.fNewLine]

        xor     al, byte [edx+ecx]
        cmp     al, $0a xor $0d
        jne     .loop
        jmp     .next   ; skip the next char. it is part of CRLF (or LFCR sequence).

;-----------------------------------------------------------------------------

.start_tag:

        xor     ecx, ecx
        mov     [.ofsStart], ebx

.first_non_ws:
        inc     ebx

        mov     esi, [edx+TText.GapEnd]
        cmp     ebx, [edx+TText.GapBegin]
        cmovb   esi, [edx+TText.GapBegin]
        sub     esi, [edx+TText.GapBegin]
        add     esi, ebx
        cmp     esi, [edx+TText.Length]
        jae     .not_a_tag

        mov     al, [edx+esi]

        test    al, al
        jz      .not_a_tag

        cmp     al, ' '
        jbe     .first_non_ws

        mov     edi, ebx        ; the start of the hash name

        cmp     al, "/"
        je      .closing_tag

        test    [.tagFlags], fDisableTags
        jnz     .loop

.hash1:
        cmp     al, "="
        je      .tag_found
        cmp     al, "]"
        je      .tag_found

        mov     ah, al
        and     ah, $40
        shr     ah, 1
        or      al, ah  ; case insensitive hash.

        xor     cl, al
        mov     cl, [ tpl_func + ecx]

        inc     ebx

        mov     esi, [edx+TText.GapEnd]
        cmp     ebx, [edx+TText.GapBegin]
        cmovb   esi, [edx+TText.GapBegin]
        sub     esi, [edx+TText.GapBegin]
        add     esi, ebx
        cmp     esi, [edx+TText.Length]
        jae     .not_a_tag

        mov     al, [edx+esi]
        jmp     .hash1


.tag_found:

        mov     [.tagType], eax ; al = "=" or "]"

        mov     esi, [tableBBtags + sizeof.TPHashItem * ecx + TPHashItem.pKeyname]
        test    esi, esi
        jz      .not_a_tag      ; empty hash table cell.

        push    ecx

        mov     eax, ebx
        sub     eax, edi        ; the length of the tag
        movzx   ecx, byte [esi-1]  ; the length of the string in the hash table.
        cmp     ecx, eax
        jne     .not_a_tag_pop

        lea     eax, [ebx+1]
        stdcall TextMoveGap, edx, eax           ; the gap is after the tag, so, the offset and the position are the same.

        add     edi, edx           ; = pointer to the tag start. (edi contains the offset);
        xchg    esi, edi

.cmp_loop:
        dec     ecx
        js      .equal

        lodsb

        mov     ah, al
        and     ah, $40
        shr     ah, 1
        or      al, ah

        scasb
        je      .cmp_loop
        jmp     .not_a_tag_pop   ; the strings are different, so it is a collision. Ignore.

.equal:
        pop     ecx

; the tag name is the same!

        mov     eax, [tableBBtags + sizeof.TPHashItem * ecx + TPHashItem.Value]
        cmp     [eax+TOneTag.pClose], 0
        je      .stack_ok                       ; no need to push to the stack tags without closing tag.

        push    edx
        stdcall AddArrayItems, [.pStack], 1
        mov     [.pStack], edx
        mov     [eax], ecx      ; the last tag for close.
        pop     edx

.stack_ok:

        mov     edi, ebx   ; edi points one char before the attribute value or to the tag content value, depending on the tag type (with or without attribute).
        add     edi, [edx+TText.GapEnd]
        sub     edi, [edx+TText.GapBegin]

.skip_ws1:
        inc     edi
        cmp     edi, [edx+TText.Length]
        jae     .attr_here                      ; it is an empty attribute actually...

        cmp     byte [edx+edi], 0
        je      .attr_here

        cmp     byte [edx+edi], ' '
        jbe     .skip_ws1

.attr_here:
        add     edi, [edx+TText.GapBegin]
        sub     edi, [edx+TText.GapEnd]

; Here edi contains the position of the first non whitespace character of the attribute.
; Don't forget to increment it accordingly on inserting text before it.

        OutputValue "Open tag with hash=", ecx, 10, -1

        mov     eax, [edx+TText.GapBegin]
        sub     eax, [.ofsStart]

        sub     [edx+TText.GapBegin], eax       ; delete the start of the tag.
        sub     ebx, eax                        ; move ebx back, because of the deleted tag text.
        sub     edi, eax

        mov     eax, [tableBBtags + sizeof.TPHashItem * ecx + TPHashItem.Value]

        push    [eax+TOneTag.flags]
        pop     [.tagFlags]

        test    [.tagFlags], fBlockTag
        jnz     .clear_newline

        cmp     [.fNewLine], 1
        jbe     .copy_tag_prefix

        mov     esi, [tagPara.pOpenP]
        mov     ecx, [tagPara.lOpenP]
        add     edi, ecx
        call    .copy_tag

.clear_newline:

        and     [.fNewLine], 0

.copy_tag_prefix:
        mov     esi, [eax+TOneTag.pOpenP]
        mov     ecx, [eax+TOneTag.lOpenP]
        add     edi, ecx
        call    .copy_tag

        mov     esi, [eax+TOneTag.pOpenS]
        mov     ecx, [eax+TOneTag.lOpenS]
        test    esi, esi
        jz      .mono_open_tag  ; the open tag consists of only one part, without attributes.

        cmp     byte [.tagType], '='
        je      .process_attribute

        test    [eax+TOneTag.flags], fEmptyAttr         ; if the tag allows empty attribute, simply copy
        jnz     .copy                                   ; the tag suffix without trying to replace the attribute.

; here copy and encode the attribute value:
.process_attribute:
        inc     ebx

        push    esi ecx

        lea     esi, [edi-1]

.attr_loop:
        inc     esi

        mov     eax, esi
        add     eax, [edx+TText.GapEnd]
        sub     eax, [edx+TText.GapBegin]    ; eax = eax + (GapEnd - GapBegin)
        cmp     eax, [edx+TText.Length]
        jae     .end_of_attr2

        movzx   eax, byte [edx+eax]

        test    al, al
        jz      .end_of_attr2

        cmp     al, "]"
        je      .end_of_attr1

        cmp     al, "["
        je      .end_of_attr2

; encode with HTML encoding.

        test    al, al          ; all values > 127 are unicode and should not be encoded.
        js      .store_char

        movzx   eax, byte [tbl_html+eax]
        test    al, al
        jz      .attr_loop
        jns     .store_char     ; it is non-encodable character, just copy it.

        push    esi

        lea     esi, [eax+tbl_html]     ; the address of the replacement string.
        lodsb
        movzx   ecx, al         ; length

        stdcall TextSetGapSize, edx, ecx
        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx
        add     ebx, ecx
        add     [esp], ecx      ; increase ESI that is in the stack right now.

        rep movsb
        pop     esi
        jmp     .attr_loop

.store_char:
        stdcall TextSetGapSize, edx, 1
        mov     edi, [edx+TText.GapBegin]
        mov     [edx+edi], al
        inc     [edx+TText.GapBegin]
        inc     ebx
        inc     esi
        jmp     .attr_loop


.end_of_attr1:

        inc     esi     ; delete "]"
        dec     ebx

        add     esi, [edx+TText.GapEnd]
        sub     esi, [edx+TText.GapBegin]
        mov     [edx+TText.GapEnd], esi

.end_of_attr2:

        pop     ecx esi

.copy:
        call    .copy_tag
        test    [.tagFlags], fBlockTag
        jz      .next

        mov     [.fNewLine], NEW_PARAGRAPH_EMPTY_LINES  ; after start of a block tag, the first text starts a paragraph.
        jmp     .next


.mono_open_tag:

        cmp     byte [.tagType], '='
        jne     .next

; search the end of the attribute

        dec     edi
        add     edi, [edx+TText.GapEnd]
        sub     edi, [edx+TText.GapBegin]    ; edi = edi + (GapEnd - GapBegin)

.attr_loop2:
        inc     edi
        cmp     edi, [edx+TText.Length]
        jae     .end_of_attr3

        cmp     byte [edx+edi], 0
        je      .end_of_attr3

        cmp     byte [edx+edi], "]"
        jne     .attr_loop2

        inc     edi

.end_of_attr3:
        mov     [edx+TText.GapEnd], edi
        jmp     .next


.closing_tag:

.hash2:
        inc     ebx

        mov     esi, [edx+TText.GapEnd]
        cmp     ebx, [edx+TText.GapBegin]
        cmovb   esi, [edx+TText.GapBegin]
        sub     esi, [edx+TText.GapBegin]
        add     esi, ebx
        cmp     esi, [edx+TText.Length]
        jae     .not_a_tag

        mov     al, [edx+esi]
        test    al, al
        jz      .not_a_tag

        cmp     al, "]"
        je      .close_here

        mov     ah, al
        and     ah, $40
        shr     ah, 1
        or      al, ah  ; case insensitive hash.

        xor     cl, al
        mov     cl, [ tpl_func + ecx]
        jmp     .hash2

.close_here:

        OutputValue "Closing tag with hash=", ecx, 10, -1

        inc     edi     ; skip "/" char.

        mov     esi, [tableBBtags + sizeof.TPHashItem * ecx + TPHashItem.pKeyname]
        test    esi, esi
        jz      .not_a_tag

        test    [.tagFlags], fDisableTags
        jz      .process_closing

        mov     eax, [tableBBtags + sizeof.TPHashItem * ecx + TPHashItem.Value]
        test    [eax+TOneTag.flags], fDisableTags     ; is this the same tag, that set fDisableTags
        jz      .not_a_tag

.process_closing:
        and     [.tagFlags], 0

        push    ecx

        mov     eax, ebx
        sub     eax, edi        ; the length of the tag
        movzx   ecx, byte [esi-1]  ; the length of the string in the hash table.
        cmp     ecx, eax
        jne     .not_a_closing_tag

        lea     eax, [ebx+1]
        stdcall TextMoveGap, edx, eax           ; the gap is after the tag, so, the offset and the position are the same.

        add     edi, edx        ; the pointer to the tag start.
        xchg    esi, edi

.cmp_loop2:
        dec     ecx
        js      .equal2

        lodsb

        mov     ah, al
        and     ah, $40
        shr     ah, 1
        or      al, ah

        scasb
        je      .cmp_loop2

.not_a_closing_tag:
        pop     ecx
        jmp     .not_a_tag   ; the strings are different, so it is a collision. Ignore.

.equal2:
        pop     ecx

; the tag name is the same!

        mov     eax, [edx+TText.GapBegin]
        sub     eax, [.ofsStart]

        sub     ebx, eax                        ; move ebx back, because of the deleted tag text.
        sub     [edx+TText.GapBegin], eax       ; delete the start of the tag.

        mov     edi, [.pStack]
        mov     eax, [edi+TArray.count]
        test    eax, eax
        jz      .next           ; the stack is empty, so ignore the closing tag!

.close_loop:
        dec     eax
        js      .error_stack

        push    ecx
        mov     esi, [edi+TArray.array + 4*eax]
        mov     esi, [tableBBtags + sizeof.TPHashItem * esi + TPHashItem.Value]
        mov     ecx, [esi+TOneTag.lClose]
        mov     esi, [esi+TOneTag.pClose]
        call    .copy_tag
        pop     ecx

        cmp     ecx, [edi+TArray.array + 4*eax]
        jne     .close_loop

        mov     [edi+TArray.count], eax
        jmp     .next

.error_stack:
        inc     eax
        mov     [edi+TArray.count], eax
        jmp     .next


.end_of_text:
; here, close all open tags and free the stack array.

        stdcall TextMoveGap, edx, ebx

        mov     edi, [.pStack]
        mov     eax, [edi+TArray.count]

.closeall:
        dec     eax
        js      .finish

        mov     esi, [edi+TArray.array + 4*eax]
        mov     esi, [tableBBtags + sizeof.TPHashItem * esi + TPHashItem.Value]

        mov     ecx, [esi+TOneTag.lClose]
        mov     esi, [esi+TOneTag.pClose]
        call    .copy_tag
        jmp     .closeall

.finish:
        stdcall FreeMem, edi

        mov     [esp+4*regEDX], edx
        popad
        return


.copy_tag:
        push    edi
        stdcall TextSetGapSize, edx, ecx

        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx
        add     ebx, ecx

        rep movsb
        pop     edi
        retn
endp



macro HtmlEntities name, [char, encoding] {
common
  local ..tabl

  label ..tabl byte
  if used name
    label name byte
    db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
    db $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00, $00
    db $20, $21, $22, $23, $24, $25, $26, $27, $28, $29, $2a, $2b, $2c, $2d, $2e, $2f
    db $30, $31, $32, $33, $34, $35, $36, $37, $38, $39, $3a, $3b, $3c, $3d, $3e, $3f
    db $40, $41, $42, $43, $44, $45, $46, $47, $48, $49, $4a, $4b, $4c, $4d, $4e, $4f
    db $50, $51, $52, $53, $54, $55, $56, $57, $58, $59, $5a, $5b, $5c, $5d, $5e, $5f
    db $60, $61, $62, $63, $64, $65, $66, $67, $68, $69, $6a, $6b, $6c, $6d, $6e, $6f
    db $70, $71, $72, $73, $74, $75, $76, $77, $78, $79, $7a, $7b, $7c, $7d, $7e, $7f

forward
  local ..offs, ..name, ..len

  if encoding eqtype ''
    ..name db ..len, encoding
    ..len = $ - ..name - 1
    ..offs = ..name - ..tabl
  else
    ..offs = encoding
  end if

  assert ..offs < 256

forward
  local ..char, ..ascii
  virtual at 0
    ..char db char
    load ..ascii byte from ..char
  end virtual
  assert ..ascii < 128
  store byte ..offs at ..tabl + ..ascii

common
  end if
  DispSize "HTML entities table:", $-..tabl
}


iglobal
  HtmlEntities tbl_html,        \
    $09, $0d,                   \
    $0a, $0a,                   \
    $0d, $0d,                   \
    '<', '&lt;',                \
    '>', '&gt;',                \
    '"', '&quot;',              \
    "'", '&apos;',              \
    '&', '&amp;'
endg





endmodule

Changes to freshlib/data/buffergap.asm.

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
..
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
..
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
...
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
...
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
...
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
...
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
;
;  Notes: TText is so called "buffer gap" method of editing text information.
;         it allows fast insert and delete operations.
;_________________________________________________________________________________________
module "Buffer gap engine"


gapDefaultSize = 256

; There are 3 coordinate systems in the TText structure:
;
;   1. "Offset"   - this is simple offset inside the memory, relative to the begining of the structure.
;
;   2. "Position" - this is the coordinate of the text characters in the buffer, excluding the gap (which can be placed anywhere in the text).
;
................................................................................
; The structure TText is backward structure. All members has negative offsets and the
; pointer to the structure points to its end, not to the start.
;
; The structure has fixed header and variable, dynamically changed length.
; All the text in the structure has positive offsets and all the header fields, negative.

rstruct TText
  .TextLen    dd  ?       ; In bytes
  .GapStart   dd  ?       ; In offset units.
  .GapLen     dd  ?       ; In bytes
  .struc_size dd  ?       ; the size of the structure (it can be extended up).
  label .     byte        ; the text in UTF-8 encoding.
ends



proc TextIndexToPos, .pText, .index
................................................................................
; If eax is above the size of the text, the position of the buffer end is returned.
; All positions are unsigned, so passing eax = $ffffffff will return the end of the
; data in the buffer.
;
; See also the pos2ofs macro. Because of the simple operation it is better to use it
; or even explicit processing.

proc TextPosToOffset
begin
        cmp     eax, [esi+TText.TextLen]
        cmova   eax, [esi+TText.TextLen]


        cmp     eax, [esi+TText.GapStart]
        jbe     @f
        add     eax, [esi+TText.GapLen]

@@:



        return
endp


macro pos2ofs  pos, text {
local ..skip

  if pos eqtype eax & text eqtype eax
    cmp    pos, [text+TText.TextLen]
    cmova  pos, [text+TText.TextLen]
    cmp    pos, [text+TText.GapStart]
    jb     ..skip
    add    pos, [text+TText.GapLen]

..skip:


  else
    error "Error! Arguments must be registers!"
    err
  end if
}


................................................................................
;   EAX: offset in bytes.
;
; if the buffer offset is placed inside the gap, the start of the gap is returned.
;
; See also ofs2pos macro. Very often it is better to use the macro or explicit processing,
; instead of procedure call, because the operation is very simple.

proc TextOffsetToPos
begin




        cmp     eax, [esi+TText.GapStart]



        jb      .finish

        sub     eax, [esi+TText.GapLen]
        cmp     eax, [esi+TText.GapStart]
        cmovb   eax, [esi+TText.GapStart]

        cmp     eax, [esi+TText.TextLen]
        cmova   eax, [esi+TText.TextLen]


.finish:

        return
endp


macro ofs2pos ofs, text {
local ..skip

  if ofs eqtype eax & text eqtype eax



        cmp     ofs, [text+TText.GapStart]
        jb      .skip

        sub     ofs, [text+TText.GapLen]
        cmp     ofs, [text+TText.GapStart]
        cmovb   ofs, [text+TText.GapStart]

        cmp     ofs, [text+TText.TextLen]
        cmova   ofs, [text+TText.TextLen]
..skip:
  else
    error "Error! Arguments must be registers!"
    err
  end if
}


; Moves the gap to the desired position.
; Arguments:
;   .pText        - pointer to the TText
;   .position     - position of the gap in position units.
; Returns:
;   Nothing
;
; This operation can't fail. Preserves all registers.

proc TextMoveGap, .pText, .position
begin
        pushad



        mov     edx, [.pText]
        mov     eax, [.position]



        cmp     [edx+TText.GapLen], 0
        je      .end_move

        pos2ofs eax, edx



        mov     esi, [edx+TText.GapStart]
        mov     edi, [edx+TText.GapStart]



        mov     ecx, eax
        sub     ecx, [edx+TText.GapStart]
        je      .finish                                 ; source and destination are the same - no move.
        jb      .move_back


        add     esi, [edx+TText.GapLen]
        sub     ecx, [edx+TText.GapLen]
        jz      .finish


        add     edi, edx
        add     esi, edx







        push    ecx
        shr     ecx, 2
        rep movsd
        pop     ecx
        and     ecx, 3
        rep movsb

        sub     edi, edx
        mov     [edx+TText.GapStart], edi


.finish:



        popad
        return


.move_back:
        neg     ecx

        std

        add     edi, [edx+TText.GapLen]



        add     edi, edx
        add     esi, edx

        push    ecx
        shr     ecx, 2
        jz      .dword_ok


        sub     esi, 4
        sub     edi, 4
        rep movsd
        add     esi, 4
        add     edi, 4

.dword_ok:
        pop     ecx
        and     ecx, 3
        dec     esi
        dec     edi
        rep movsb

        cld



.end_move:






        mov     [edx+TText.GapStart], eax

        popad
        return





endp




proc TextCreate, .struc_size
begin
        push    ecx

        mov     ecx, [.struc_size]
        mov     eax, ecx
        add     eax, gapDefaultSize

        stdcall GetMem, eax
        jc      .finish

        lea     eax, [eax+ecx]
        mov     [eax+TText.struc_size], ecx

        and     [eax+TText.TextLen], 0
        and     [eax+TText.GapStart], 0
        mov     [eax+TText.GapLen], gapDefaultSize

.finish:
        pop     ecx
        return
endp


................................................................................
; Arguments:
;   .pText - pointer to the source structure
;
; Returns:
;   EAX - pointer to the new TText structure.
;---------------------------------------------------------------------------------

proc TextDup, .pText
begin
        pushad

        mov     esi, [.pText]
        test    esi, esi
        jz      .finish

        mov     ecx, [esi+TText.TextLen]
        add     ecx, [esi+TText.GapLen]
        add     ecx, [esi+TText.struc_size]

        stdcall GetMem, ecx

        mov     edi, eax

        mov     edx, [esi+TText.struc_size]
        add     eax, edx

        mov     [esp+4*regEAX], eax
        sub     esi, edx

        mov     ebx, ecx
        shr     ecx, 2
        and     ebx, 3

        rep movsd

        mov     ecx, ebx
        rep movsb

.finish:
        popad
        return
endp



proc TextFree, .pBuffer
begin
        push    eax
        mov     eax, [.pBuffer]
        test    eax, eax
        jz      .exit

        sub     eax, [eax+TText.struc_size]
        stdcall FreeMem, eax

.exit:
................................................................................
;
; In order to provide less frequent reallocations, the procedure
; always allocates bigger gap than requested.
; max([.desired_size]*2, gapDefaultSize) memory is allocated,
; if the buffer is to be reallocated.
;
; Arguments:
;   .pSize        - pointer to TText
;   .desired_size - desired size of the gap.
;
; Returns
;   edx - pointer to the same TText. Can be changed because of memory reallocation.
;   CF = 0 if OK
;   CF = 1 if the memory reallocation failed.
;
................................................................................

proc TextSetGapSize, .pText, .desired_size
begin
        pushad

        mov     edx, [.pText]
        mov     ecx, [.desired_size]



        cmp     [edx+TText.GapLen], ecx

        jae     .gap_ok

        lea     ecx, [2*ecx]
        mov     eax, gapDefaultSize
        cmp     ecx, eax
        cmovl   ecx, eax


        mov     eax, [edx+TText.TextLen]
        lea     ebx, [ecx+eax]

        add     eax, [edx+TText.GapLen]           ; current data size.
        add     ebx, [edx+TText.struc_size]       ; desired size. it will always be higher than the current.

        push    [edx+TText.struc_size]
        sub     edx, [edx+TText.struc_size]
        stdcall ResizeMem, edx, ebx
        pop     edx
        lea     edx, [edx+eax]
        jc      .gap_ok                           ; well, it is just an error, but how to proceed?

        sub     ebx, [edx+TText.struc_size]       ; total size of the buffer






; the space is allocated and there are 2 gaps. So, merge them now. The new gap is always at the end.
; the size of the new gap is

        mov     eax, [edx+TText.TextLen]
        mov     ecx, [edx+TText.TextLen]

        add     eax, [edx+TText.GapLen]
        sub     ecx, [edx+TText.GapStart]         ; the length of the text after the gap.
        jz      .gap_moved                              ; gap does not need to be moved

        std

        lea     edi, [edx + ebx]
        lea     esi, [edx + eax]

        push    ecx


        shr     ecx, 2
        jz      .dword_ok

        sub     esi, 4
        sub     edi, 4




        rep movsd

        add     esi, 4
        add     edi, 4

.dword_ok:
        pop     ecx
        and     ecx, 3
        jz      .byte_ok

        dec     esi
        dec     edi
        rep movsb

.byte_ok:
        cld


.gap_moved:
        sub     ebx, [edx+TText.TextLen]

        mov     [edx+TText.GapLen], ebx
        clc

.gap_ok:

        mov     [esp+4*regEDX], edx





        popad
        return
endp




................................................................................
        pop     eax
        return
endp




; Inserts on the current gap position the string .hString
; which can be pointer to NULL terminated string or
; string handle.
;








proc TextAddString, .pText, .hString
begin
        push    eax ebx ecx edi esi


        mov     edx, [.pText]

        stdcall StrLen, [.hString]
        mov     ecx, eax
        mov     ebx, eax

        stdcall StrPtr, [.hString]
        mov     esi, eax

        stdcall TextSetGapSize, edx, ecx
        jc      .finish






























        mov     eax, [edx+TText.GapStart]
















        lea     edi, [edx+eax]




















































        shr     ecx, 2
        rep movsd

        mov     ecx, ebx
        and     ecx, 3
        rep movsb

        add     [edx+TText.GapStart], ebx
        add     [edx+TText.TextLen], ebx
        sub     [edx+TText.GapLen], ebx   ; here the CF should be always 0

.finish:
        pop     edi edx ecx eax


        return
endp

























































; Moves the gap to the end of the text and inserts dword 0 after it, so
; the buffer can be used as an ASCIIZ string.


proc TextCompact
begin

        stdcall TextMoveGap, esi, [esi+TText.TextLen]




        return
endp



endmodule







|







 







|
|
|







 







|

|
|
>

|
|
|
>

>
>
>








<
<
|

|
>

>
>







 







|

>
>
>
>
|
>
>
>


|
|
<

|
<
>


>








>
>
>
|


|
|
<

|
|






>














>
>

|

>
>
|
<

<
>
>

<
<
>
>

|
<
<
<
>

<
<
|
>



>
>
>
>
>
>








|
<

>
|
>
>
>



<

<
<


<
>
>

|
|


|
<
>

|
|
<
<
<

<

|
<
<
|


>
>

<
>
>
>
>
>
>
|
>
|
<
>
>

>
>

<






>









>
|
|
|







 







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|



|


|







 







|







 







>
>

<
>
|




|

>
|
|
<
|
|






|


>
>
>
>
>




<
|
<
|
<
<



|
|


>

<
|

|
|
>
>
>



<
<
<
<
<
<
<
<
<
<
<
<
<


<
<
|
>
|


<
>

>
>
>
>
>







 







|



>
>
>
>
>
>
>

|

<
>













>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


>




|
<
<


<
>
>






>
>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|

>

|

>
|
>
>
>
>






11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
..
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
..
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
...
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
...
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
...
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
...
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
618
619
620
621
622
623
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
;
;  Notes: TText is so called "buffer gap" method of editing text information.
;         it allows fast insert and delete operations.
;_________________________________________________________________________________________
module "Buffer gap engine"


gapDefaultSize = 1024  ; 256

; There are 3 coordinate systems in the TText structure:
;
;   1. "Offset"   - this is simple offset inside the memory, relative to the begining of the structure.
;
;   2. "Position" - this is the coordinate of the text characters in the buffer, excluding the gap (which can be placed anywhere in the text).
;
................................................................................
; The structure TText is backward structure. All members has negative offsets and the
; pointer to the structure points to its end, not to the start.
;
; The structure has fixed header and variable, dynamically changed length.
; All the text in the structure has positive offsets and all the header fields, negative.

rstruct TText
  .Length     dd  ?       ; The total length of the buffer in bytes
  .GapBegin   dd  ?       ; In offset units.
  .GapEnd     dd  ?       ; In offset units.
  .struc_size dd  ?       ; the size of the structure (it can be extended up).
  label .     byte        ; the text in UTF-8 encoding.
ends



proc TextIndexToPos, .pText, .index
................................................................................
; If eax is above the size of the text, the position of the buffer end is returned.
; All positions are unsigned, so passing eax = $ffffffff will return the end of the
; data in the buffer.
;
; See also the pos2ofs macro. Because of the simple operation it is better to use it
; or even explicit processing.

proc TextPosToOffset, .pText, .pos
begin
        push    ebx
        mov     eax, [.pos]
        mov     ebx, [.pText]

        cmp     eax, [ebx+TText.GapBegin]
        jb      @f
        add     eax, [ebx+TText.GapEnd]
        sub     eax, [ebx+TText.GapBegin]
@@:
        cmp     eax, [ebx+TText.TextLen]
        cmova   eax, [ebx+TText.TextLen]
        pop     ebx
        return
endp


macro pos2ofs  pos, text {
local ..skip

  if pos eqtype eax & text eqtype eax


    cmp    pos, [text+TText.GapBegin]
    jb     ..skip
    add    pos, [text+TText.GapEnd]
    sub    pos, [text+TText.GapBegin]
..skip:
    cmp    pos, [text+TText.Length]
    cmova  pos, [text+TText.Length]
  else
    error "Error! Arguments must be registers!"
    err
  end if
}


................................................................................
;   EAX: offset in bytes.
;
; if the buffer offset is placed inside the gap, the start of the gap is returned.
;
; See also ofs2pos macro. Very often it is better to use the macro or explicit processing,
; instead of procedure call, because the operation is very simple.

proc TextOffsetToPos, .pText, .offs
begin
        push    ebx
        mov     eax, [.offs]
        mov     ebx, [.pText]

        cmp     eax, [ebx+TText.Length]
        cmova   eax, [ebx+TText.Length]

        cmp     eax, [ebx+TText.GapBegin]
        jb      .finish

        add     eax, [ebx+TText.GapEnd]
        sub     eax, [ebx+TText.GapBegin]


        cmp     eax, [ebx+TText.GapBegin]

        cmovb   eax, [ebx+TText.GapBegin]

.finish:
        pop     ebx
        return
endp


macro ofs2pos ofs, text {
local ..skip

  if ofs eqtype eax & text eqtype eax
        cmp     ofs, [text+TText.Length]
        cmova   ofs, [text+TText.Length]

        cmp     ofs, [text+TText.GapBegin]
        jb      .skip

        add     ofs, [text+TText.GapBegin]
        sub     ofs, [text+TText.GapEnd]


        cmp     ofs, [text+TText.GapBegin]
        cmovb   ofs, [text+TText.GapBegin]
..skip:
  else
    error "Error! Arguments must be registers!"
    err
  end if
}


; Moves the gap to the desired position.
; Arguments:
;   .pText        - pointer to the TText
;   .position     - position of the gap in position units.
; Returns:
;   Nothing
;
; This operation can't fail. Preserves all registers.

proc TextMoveGap, .pText, .position
begin
        pushad

;        int3

        mov     edx, [.pText]
        mov     ecx, [.position]

        mov     eax, [edx+TText.Length]
        mov     edi, [edx+TText.GapBegin]
        mov     esi, [edx+TText.GapEnd]



        cmp     esi, edi
        je      .finish         ; the gap length is 0 nothing to move



        sub     eax, esi
        add     eax, edi

        cmp     ecx, eax



        cmova   ecx, eax        ; the maximal possible position for the gap.



        sub     ecx, edi        ; [edx+TText.GapBegin]  ; ecx is the distance to move the gap.
        jz      .finish                                 ; source and destination are the same - no move.

        add     edi, edx
        add     esi, edx

        add     [edx+TText.GapBegin], ecx
        add     [edx+TText.GapEnd], ecx

        test    ecx, ecx
        js      .move_back

        push    ecx
        shr     ecx, 2
        rep movsd
        pop     ecx
        and     ecx, 3
        rep movsb

.finish:


; fill the gap with placeholder char in order to make debug easy! It lowers the performance a lot!!!
if defined options.DebugMode & options.DebugMode
        call    __fill_the_gap
end if

        popad
        return


.move_back:


        std


        neg     ecx
        xchg    esi, edi

        dec     esi
        dec     edi

        push    ecx
        and     ecx, 3

        rep movsb

        sub     esi, 3
        sub     edi, 3





        pop     ecx
        shr     ecx, 2


        rep movsd

        cld
        jmp     .finish
endp



proc __fill_the_gap
begin
        push    edi ecx eax

        mov     edi, [edx+TText.GapBegin]
        mov     ecx, [edx+TText.GapEnd]
        sub     ecx, edi
        add     edi, edx

        mov     al, '*'
        rep stosb

        pop     eax ecx edi
        return
endp




proc TextCreate, .struc_size
begin
        push    ecx

        mov     ecx, [.struc_size]
        mov     eax, ecx
        add     eax, gapDefaultSize

        stdcall GetMem, eax
        jc      .finish

        lea     eax, [eax+ecx]
        mov     [eax+TText.struc_size], ecx
        xor     ecx, ecx
        mov     [eax+TText.GapBegin], ecx
        mov     [eax+TText.GapEnd], gapDefaultSize
        mov     [eax+TText.Length], gapDefaultSize

.finish:
        pop     ecx
        return
endp


................................................................................
; Arguments:
;   .pText - pointer to the source structure
;
; Returns:
;   EAX - pointer to the new TText structure.
;---------------------------------------------------------------------------------

;proc TextDup, .pText
;begin
;        pushad
;
;        mov     esi, [.pText]
;        test    esi, esi
;        jz      .finish
;
;        mov     ecx, [esi+TText.TextLen]
;        add     ecx, [esi+TText.GapLen]
;        add     ecx, [esi+TText.struc_size]
;
;        stdcall GetMem, ecx
;
;        mov     edi, eax
;
;        mov     edx, [esi+TText.struc_size]
;        add     eax, edx
;
;        mov     [esp+4*regEAX], eax
;        sub     esi, edx
;
;        mov     ebx, ecx
;        shr     ecx, 2
;        and     ebx, 3
;
;        rep movsd
;
;        mov     ecx, ebx
;        rep movsb
;
;.finish:
;        popad
;        return
;endp



proc TextFree, .pText
begin
        push    eax
        mov     eax, [.pText]
        test    eax, eax
        jz      .exit

        sub     eax, [eax+TText.struc_size]
        stdcall FreeMem, eax

.exit:
................................................................................
;
; In order to provide less frequent reallocations, the procedure
; always allocates bigger gap than requested.
; max([.desired_size]*2, gapDefaultSize) memory is allocated,
; if the buffer is to be reallocated.
;
; Arguments:
;   .pText        - pointer to TText
;   .desired_size - desired size of the gap.
;
; Returns
;   edx - pointer to the same TText. Can be changed because of memory reallocation.
;   CF = 0 if OK
;   CF = 1 if the memory reallocation failed.
;
................................................................................

proc TextSetGapSize, .pText, .desired_size
begin
        pushad

        mov     edx, [.pText]
        mov     ecx, [.desired_size]
        mov     eax, [edx+TText.GapEnd]
        sub     eax, [edx+TText.GapBegin]


        cmp     eax, ecx
        jae     .finish         ; CF==0

        lea     ecx, [2*ecx]
        mov     eax, gapDefaultSize
        cmp     ecx, eax
        cmovl   ecx, eax                          ; ecx is the new gap size here.

        mov     ebx, [edx+TText.Length]           ; current data size. text+gap
        add     ebx, [edx+TText.GapBegin]
        sub     ebx, [edx+TText.GapEnd]

        add     ebx, ecx
        add     ebx, [edx+TText.struc_size]       ; the new buffer size. it will always be higher than the current.

        push    [edx+TText.struc_size]
        sub     edx, [edx+TText.struc_size]
        stdcall ResizeMem, edx, ebx
        pop     edx
        lea     edx, [edx+eax]
        jc      .finish                           ; well, it is just an error, but how to proceed?

        sub     ebx, [edx+TText.struc_size]       ; total size of the buffer

        lea     edi, [edx+ebx]
        xchg    ebx,[edx+TText.Length]            ; ebx is now the old buffer length.

        lea     esi, [edx+ebx]

; the space is allocated and there are 2 gaps. So, merge them now. The new gap is always at the end.
; the size of the new gap is


        mov     ecx, ebx

        sub     ecx, [edx+TText.GapEnd]



        std

        dec     esi
        dec     edi

        push    ecx
        and     ecx, 3


        rep movsb

        sub     esi, 3
        sub     edi, 3

        pop     ecx
        shr     ecx, 2

        rep movsd














        cld



        sub     edi, edx
        add     edi, 4
        mov     [edx+TText.GapEnd], edi
        clc


.finish:
        mov     [esp+4*regEDX], edx

; fill the gap with placeholder char in order to make debug easy! It lowers the performance a lot!!!
if defined options.DebugMode & options.DebugMode
        call    __fill_the_gap
end if
        popad
        return
endp




................................................................................
        pop     eax
        return
endp




; Inserts a string in the TText structure
; which can be pointer to NULL terminated string or
; string handle.
;
; Arguments:
;   .pText - pointer to the TText structure
;   .position  - position where the string to be inserted.
;   .hString - pointer or handle of string to be inserted.
; Returns:
;   edx: pointer to TText structure. Can be changed because of reallocations.
;   eax: length of the string added.

proc TextAddString, .pText, .position, .hString
begin

        pushad

        mov     edx, [.pText]

        stdcall StrLen, [.hString]
        mov     ecx, eax
        mov     ebx, eax

        stdcall StrPtr, [.hString]
        mov     esi, eax

        stdcall TextSetGapSize, edx, ecx
        jc      .finish

        stdcall TextMoveGap, edx, [.position]

        mov     edi, [edx+TText.GapBegin]
        add     edi, edx

        shr     ecx, 2
        rep movsd
        mov     ecx, ebx
        and     ecx, 3
        rep movsb

        add     [edx+TText.GapBegin], ebx

.finish:
        mov     [esp+4*regEAX], ebx
        mov     [esp+4*regEDX], edx
        popad
        return
endp





proc TextAddStr2, .pText, .position, .hString, .maxLength
begin
        pushad
        mov     edx, [.pText]

        mov     eax, [.hString]
        cmp     eax, $c0000000
        jb      .proceed

        stdcall StrLen, [.hString]
        mov     [.maxLength], eax

.proceed:
        stdcall StrPtr, [.hString]
        mov     esi, eax

        add     [.maxLength], 4  ; some reserve
        stdcall TextSetGapSize, edx, [.maxLength]
        jc      .finish

        stdcall TextMoveGap, edx, [.position]

        mov     edi, [edx+TText.GapBegin]
        add     edi, edx

.copy_loop:
        lodsd
        stosd

        mov     ebx, eax
        not     eax
        sub     ebx, $01010101
        and     eax, ebx
        and     eax, $80808080
        jz      .copy_loop

        xor     eax, eax
        sub     edi, 5

.final_loop:
        inc     edi
        cmp     [edi], al
        jne     .final_loop

        sub     edi, edx
        sub     edi, [edx+TText.GapBegin]
        add     [edx+TText.GapBegin], edi

        clc
        mov     [esp+4*regEAX], edi

.finish:
        mov     [esp+4*regEDX], edx
        popad
        return
endp



proc TextAddBytes, .pText, .position, .pData, .DataLen
begin
        pushad
        mov     edx, [.pText]
        mov     esi, [.pData]

        stdcall TextSetGapSize, edx, [.DataLen]
        jc      .finish

        stdcall TextMoveGap, edx, [.position]

        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        mov     ebx, [.DataLen]

        mov     ecx, ebx
        shr     ecx, 2
        rep movsd

        mov     ecx, ebx
        and     ecx, 3
        rep movsb

        add     [edx+TText.GapBegin], ebx



.finish:

        mov     [esp+4*regEDX], edx
        popad
        return
endp




proc TextAddText, .pText, .position, .pTextSrc
begin
        pushad

        mov     edx, [.pText]
        mov     ebx, [.pTextSrc]

        mov     ecx, [ebx+TText.Length]
        sub     ecx, [ebx+TText.GapEnd]
        add     ecx, [ebx+TText.GapBegin]       ; total length
        mov     [esp+4*regEAX], ecx

        stdcall TextSetGapSize, edx, ecx
        stdcall TextMoveGap, edx, [.position]

        mov     edi, edx
        mov     esi, ebx

        add     edi, [edx+TText.GapBegin]
        mov     eax, [ebx+TText.GapBegin]

        mov     ecx, eax
        and     ecx, 3
        shr     eax, 2

        rep movsb
        mov     ecx, eax
        rep movsd

        mov     eax, [ebx+TText.Length]
        mov     esi, ebx

        sub     eax, [ebx+TText.GapEnd]
        add     esi, [ebx+TText.GapEnd]

        mov     ecx, eax
        and     ecx, 3
        shr     eax, 2

        rep movsb
        mov     ecx, eax
        rep movsd

        sub     edi, edx
        mov     [edx+TText.GapBegin], edi

        mov     [esp+4*regEDX], edx
        popad
        return
endp



; Moves the gap to the end of the text and if possible inserts byte 0 after it, so
; the buffer can be used as an ASCIIZ string.
; returns the length of the string in eax and the pointer to the TText object in edx (it can be changed!)

proc TextCompact, .pText
begin
        mov     edx, [.pText]
        stdcall TextMoveGap, edx, -1
        stdcall TextSetGapSize, edx, 4

        mov     eax, [edx+TText.GapBegin]   ; the length of the text.
        and     dword [edx+eax], 0
        return
endp



endmodule

Changes to freshlib/data/encodings.asm.

147
148
149
150
151
152
153
154

155
156
157
158
159
160
161
        movzx   eax, word [ebx+2*eax-$100]   ; unicode character

        cmp     [.char], eax
        je      .found
        ja      .bigger

        mov     ch, dl
        dec     ch

        jmp     .search_loop

.bigger:
        mov     cl, dl
        inc     cl
        jmp     .search_loop








|
>







147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
        movzx   eax, word [ebx+2*eax-$100]   ; unicode character

        cmp     [.char], eax
        je      .found
        ja      .bigger

        mov     ch, dl
        sub     ch, 1
        jc      .not_found
        jmp     .search_loop

.bigger:
        mov     cl, dl
        inc     cl
        jmp     .search_loop

Changes to freshlib/data/hashes.asm.

11
12
13
14
15
16
17


18
19
20
21
22
23
24
...
597
598
599
600
601
602
603




























604
605
606
607
608
609
610
...
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
;
;  Notes: Contains: MD5, ADLER32, FNV1b, CRC32
;
;_________________________________________________________________________________________

module "Hashes library"




; MD5 algorithm.
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

struct TContextMD5
    .__cntx rb 88
    .value  rb 16
................................................................................

.finish:
        popad
        return
endp































;
;
; Gets the array of strings and generates a table for PersonHash, that
; allows non-collision hash of the given set.
;
; Arguments:
................................................................................
          db 208,  85,  78, 184,  32,  89,  20, 165,  27,  22,  11, 130,  98,  80,  17, 198
          db 200, 211,  16, 100,  51, 232,   3,  96,  73, 187,  14,  53, 121, 199,  18, 103
          db 228, 180, 156, 252, 168,  49,   8, 171,  79, 204,  10, 139,  40,  61, 220, 212
          db  13, 221, 109,  25, 255, 120,  70,  28,  48, 213, 234,  50, 138,   9,  52, 142
          db 225, 172, 106,  54, 214, 163, 140,  34, 238, 224,  56, 226, 102, 137, 133, 173

end if







endmodule







>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<
<
<





11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
...
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
...
682
683
684
685
686
687
688



689
690
691
692
693
;
;  Notes: Contains: MD5, ADLER32, FNV1b, CRC32
;
;_________________________________________________________________________________________

module "Hashes library"

include "sha1.asm"


; MD5 algorithm.
;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@

struct TContextMD5
    .__cntx rb 88
    .value  rb 16
................................................................................

.finish:
        popad
        return
endp



proc StrPearsonHash, .hString, .pFuncTable
begin
        pushad

        mov     ebx, [.pFuncTable]
        xor     edx, edx

        stdcall StrLen, [.hString]
        mov     ecx, eax
        jecxz   .byte_ok

        stdcall StrPtr, [.hString]
        mov     esi, eax

.loop:
        lodsb
        xor     dl, al
        mov     dl, [ebx+edx]
        loop    .loop

.byte_ok:
        mov     [esp+4*regEAX], edx
        popad
        return
endp



;
;
; Gets the array of strings and generates a table for PersonHash, that
; allows non-collision hash of the given set.
;
; Arguments:
................................................................................
          db 208,  85,  78, 184,  32,  89,  20, 165,  27,  22,  11, 130,  98,  80,  17, 198
          db 200, 211,  16, 100,  51, 232,   3,  96,  73, 187,  14,  53, 121, 199,  18, 103
          db 228, 180, 156, 252, 168,  49,   8, 171,  79, 204,  10, 139,  40,  61, 220, 212
          db  13, 221, 109,  25, 255, 120,  70,  28,  48, 213, 234,  50, 138,   9,  52, 142
          db 225, 172, 106,  54, 214, 163, 140,  34, 238, 224,  56, 226, 102, 137, 133, 173

end if







endmodule

Changes to freshlib/data/markdown.asm.

1
2
3
4
5
6
7
8
..
11
12
13
14
15
16
17
18


19
20
21
22
23
24
25
..
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
...
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
623
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
...
668
669
670
671
672
673
674
675
676
677



678
679
680






681

682
683




684
685
686
687
688
689
690
...
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
....
1012
1013
1014
1015
1016
1017
1018
1019
1020


1021
1022
1023
1024
1025
1026
1027
1028
....
1068
1069
1070
1071
1072
1073
1074
1075


1076
1077
1078
1079
1080
1081
1082
1083
1084
....
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
....
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354

1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392

















1393
1394
1395
1396
1397
1398
1399
....
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
....
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514

1515
1516
1517
1518
1519
1520
1521
1522































































1523
1524
1525
1526
1527
1528
1529
....
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
....
1598
1599
1600
1601
1602
1603
1604






1605
1606
1607
1608
1609
1610
1611


1612
1613
1614
1615
1616
1617
1618
1619
....
1620
1621
1622
1623
1624
1625
1626




















1627
1628
1629
1630
1631
1632
1633
....
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
....
1840
1841
1842
1843
1844
1845
1846

1847
1848
1849
1850
1851
1852
1853
....
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
module "markdown parser"

struct TMarkdownResults
  .hContent     dd ?
  .hIndex       dd ?
  .hKeywords    dd ?
  .hDescription dd ?
ends
................................................................................
fcloseImage             = 2
fcloseInlineImage       = 3
fcloseObject            = 4


fblkQuote       = 1
fblkTable       = 2




fstateHeader    = $01
fstatePara      = $02
fstateWhite     = $04
fstateBold      = $08
fstateUnderline = $20
fstateStrikeout = $40
................................................................................

fstateTextNow  = $200         ; don't use the second byte for any other flags.
fstateTextPrev = $100

fstateInlineSource    =  $10000
fstateBlockSource     =  $20000
fstateTable           = $100000
fstateSubChapter      = $200000


; Translates some markdown text to HTML
; Arguments:
;     .markdown - pointer or handler of the markdown text.
;     .procFixLink - [proc .ptrLink] procedure that process the link address and returns
;                    handle to the processed link address. if [.procFixLink]=NULL, the links
;                    are inserted as they are in the HTML.
;     .fBacklinks - flag, that defines whether to insert after the headers back links to the
;                   table of contents item.
;     .pResult - pointer to TMarkdownResults structure where the result strings are returned.

;
; Returns:
;   The procedure returns  4 string handles in the TMarkdownResults structure pointed by
;   .pResult argument.

proc TranslateMarkdown2, .markdown, .procFixLink, .fBacklinks, .pResult, .lParam

................................................................................
        lea     esi, [esi+1]

.td_ok:
        or      [.state], fstatePara
        jmp     .process_char

.not_td_open:
        mov     word [edi], '<p'
        lea     edi, [edi+2]
        or      [.state], fstatePara

; is it bullet list?

        cmp     al, '*'
        jne     .close_p_tag

        cmp     byte [esi], ' '
        jne     .close_p_tag

        mov     dword [edi], ' cla'
        mov     dword [edi+4], 'ss="'
        mov     dword [edi+8], 'uli"'
        lea     edi, [edi+12]


        inc     esi
        mov     al, [esi]
        inc     esi


.close_p_tag:
        mov     byte [edi], '>'

        lea     edi, [edi+1]
        jmp     .process_char

.process_inline_code:
        mov     ecx, fstateInlineSource
        mov     ebx, 'code'
        jmp     .ProcessTag



.process_bold:
        mov     ecx, fstateBold
        mov     ebx, 'b'
        jmp     .ProcessTag



.process_italic:
        mov     ecx, fstateItalic
        mov     ebx, 'i'
        jmp     .ProcessTag



.process_underline:
        mov     ecx, fstateUnderline
        mov     ebx, 'u'
        jmp     .ProcessTag



.process_strikeout:
        mov     ecx, fstateStrikeout
        mov     ebx, 's'



; ecx = state mask
; ebx = html tag

.ProcessTag:
        mov     dl, al

        test    [.state], ecx
        jz      .start_tag

; close tag
................................................................................
        je      .normal_char

        or      [.state], ecx

; open tag
        mov     byte [edi], '<'
        lea     edi, [edi+1]

.finish_tag:
        mov     [edi], ebx




.taglen:
        lea     edi, [edi+1]






        shr     ebx, 8

        jnz     .taglen





        mov     byte [edi], '>'
        lea     edi, [edi+1]

        jmp     .scan_line2


.white_space:
................................................................................
        mov     ecx, [.last_header]
        add     ecx, '/h0>'
        mov     [edi], ecx
        lea     edi, [edi+4]

        and     [.state], not (fstateHeader or fstateTextNow)   ; For the paragraph flow, the header loops like empty line.

; after the header end, starts subchapter

        mov     dword [edi], '<div'
        mov     dword [edi+4], ' cla'
        mov     dword [edi+8], 'ss="'
        mov     dword [edi+12], 'chap'
        mov     dword [edi+16], 'ter"'
        mov     byte  [edi+20], '>'
        add     edi, 21

        or      [.state], fstateSubChapter

.crlf:
        test    [.state], fstateTextPrev or fstateTextNow
        jz     @f
        mov     word [edi], $0a0d
        lea     edi, [edi+2]
@@:
        test    al, al
        jnz     .line_start2

        call    .close_subchapter

.finish:
        mov     dword [edi], 0
        stdcall StrCat, [.result], [.pHTML]
        retn


.is_link_begin:
................................................................................
        push    esi

        mov     esi, edx
        cmp     byte [edx], '$'
        jne     @f
        inc     esi
@@:
        mov     dword [edi],   '<obj'
        mov     dword [edi+4], 'ect '


        add     edi, 8

        cmp     byte [esi], '0'
        jb      .par_data
        cmp     byte [esi], '9'
        ja      .par_data

        mov     dword [edi],  ' wid'
................................................................................
        jmp     .copy_height

.height_ok:
        mov     word [edi], '" '
        add     edi, 2

.par_data:
        mov     dword [edi],   'data'


        mov     word [edi+4],  '="'
        add     edi, 6

        lea     edx, [esi-1]
        pop     esi
        jmp     .link_tag_ok


; yes the link was found.
................................................................................
.close_link_tag:

        mov     dword [edi], '</a>'
        add     edi, 4
        retn

.close_object_tag:
        mov     dword [edi],   '</ob'
        mov     dword [edi+4], 'ject'
        mov     byte  [edi+8], '>'
        add     edi, 9
        retn

;....................................................................
; Processing of block source code
.copy_source:

.source_line:
................................................................................
        lea     edi, [edi+9]

        and     [.state], not fstateBlockSource
        jmp     .skip2

;....................................................................

.close_paragraph:
        test    [.state], fstatePara
        jz      .close_para_ok

        call    .close_inlines
        call    .close_all_links

        test    [.state], fstateTable
        jnz     .close_td

        mov     dword [edi], '</p>'
        lea     edi, [edi+4]
        jmp     .close_para_ok

.close_td:
        mov     dword [edi], '</td'
        mov     dword [edi+4], '>'+($0a0d shl 8)
        lea     edi, [edi+7]

.close_para_ok:
        and     [.state], not fstatePara
        retn

;....................................................................

.close_subchapter:

        test    [.state], fstateSubChapter
        jz      .close_subchapter_ok

        mov     dword [edi], '</di'
        mov      word [edi+4], 'v>'
        add     edi, 6

        and     [.state], not fstateSubChapter

.close_subchapter_ok:
        retn

;....................................................................

; closes all open inline tags.

.close_inlines:


        test    [.state], fstateBold
        jz      .bold_ok

        mov     dword [edi], '</b>'
        add     edi, 4

.bold_ok:
        test    [.state], fstateItalic
        jz      .italic_ok

        mov     dword [edi], '</i>'
        add     edi, 4

.italic_ok:
        test    [.state], fstateUnderline
        jz      .underline_ok

        mov     dword [edi], '</u>'
        add     edi, 4

.underline_ok:
        test    [.state], fstateStrikeout
        jz      .strike_ok

        mov     dword [edi], '</s>'
        add     edi, 4

.strike_ok:
        test    [.state], fstateInlineSource
        jz      .inline_source_ok

        mov     dword [edi],   '</co'
        mov     dword [edi+4], 'de>'
        add     edi, 7

.inline_source_ok:
        and     [.state], not (fstateBold or fstateItalic or fstateUnderline or fstateStrikeout or fstateInlineSource)

















        retn


;....................................................................
; horizontal rule element

.horiz_rule:
................................................................................
; table block;
        call    .close_paragraph

        mov     al, fblkTable
        call    .PushBlock

        mov     dword [edi], '<tab'
        mov     dword [edi+4], 'le c'
        mov     dword [edi+8], 'lass'
        mov     dword [edi+12],'="tx'
        mov     dword [edi+16], 't"><'
        mov     dword [edi+20], 'tr>'+($0d shl 24)
        mov     byte  [edi+24], $0a
        lea     edi,  [edi+25]

        or      [.state], fstateTable
        jmp     .skip2


.not_table:
        cmp     dword [esi], ';---'
................................................................................

; block quote
        call    .close_paragraph

        mov     al, fblkQuote
        call    .PushBlock

        mov     dword [edi],    '<div'
        mov     dword [edi+4],  ' cla'
        mov     dword [edi+8],  'ss="'
        mov     dword [edi+12], 'bq">'
        mov     dword [edi+16], '<h1 '
        mov     dword [edi+20], 'clas'
        mov     dword [edi+24], 's="b'
        mov     dword [edi+28], 'q">'
        lea     edi, [edi+31]

        push    esi
        add     esi, 6

        call    .CopyName

        mov     dword [edi],  '</h1'

        mov     byte [edi+4], '>'
        lea     edi, [edi+5]

        pop     esi
        jmp     .skip2


.not_quote:































































        cmp     dword [esi], ';key'
        jne     .not_keywords
        cmp     dword [esi+4], 'word'
        jne     .not_keywords
        cmp     byte [esi+8], 's'
        jne     .not_keywords

................................................................................

.not_desc:
        jmp     .skip2          ; ";" at the begining without keyword is comment. The line is ignored.


.end_block:
        call    .close_paragraph
        call    .close_subchapter
        call    .do_end_block
        jmp     .skip2

.end_all_blocks:
        call    .close_paragraph

.end_all_loop:
................................................................................
        retn

.what_block:
        cmp     eax, fblkQuote
        je      .end_quote
        cmp     eax, fblkTable
        je      .end_table







        clc
        retn

.end_quote:
        mov     dword [edi], '</di'
        mov     word  [edi+4], 'v>'


        lea     edi, [edi+6]
        clc
        retn

.end_table:
        mov     dword [edi], '</tr'
        mov     dword [edi+4], '>'+($0a0d shl 8)
        mov     dword [edi+7], '</ta'
................................................................................
        mov     dword [edi+11],'ble>'
        mov     word  [edi+15], $0a0d
        lea     edi, [edi+17]
        and     [.state], not fstateTable
        clc
        retn





















;....................................................................
.PushBlock:
        push    ecx
        mov     ecx, [.pblock]
        mov     [.blocks+ecx], al
        inc     ecx
        cmp     ecx, 1024
................................................................................
        retn

;....................................................................

; processing header items and building of the table of contents.

.header2:
        call    .close_subchapter
        xor     ecx, ecx

.scan_level2:
        inc     ecx
.scan_space2:
        lea     esi, [esi+1]
        mov     al, [esi]
................................................................................
        call    _NumToStrU
        mov     al, '.'
        stosb
        inc     ebx
        cmp     ebx, [esp]
        jbe     .loop_numbers


        pop     ecx
        jmp     .finID

.main:
        mov     eax, 'main'
        stosd

................................................................................

        mov     esi, edx

.end_cp_name:
        pop     edx ecx
        retn
endp









endmodule
|







 







|
>
>







 







<











>







 







<
<
<
<
<
<

|


|

|
<
<
|
>




>

|
<
>
|




|

>
>



|


>



|

>
>



|

>
>



|

>
>

<
>







 








|
|
>
>
>

<
<
>
>
>
>
>
>
|
>
|

>
>
>
>







 







<
<
<
<
<
<
<
<
<
<
<
<









<
<







 







|
|
>
>
|







 







|
>
>
|
|







 







|
|
<
|







 







|
|
|







<
<












<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<



>




|
|





|
|





|
|





|
|





|
|
<



>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|
<
<
<
<
<
|







 







|
|
|
|
|
<
<
<
|






|
>
|
|






>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<







 







>
>
>
>
>
>





|
|
>
>
|







 







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







<







 







>







 







<
<
<
<
<





1
2
3
4
5
6
7
8
..
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
..
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
...
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
623
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
...
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
...
766
767
768
769
770
771
772












773
774
775
776
777
778
779
780
781


782
783
784
785
786
787
788
....
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
....
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
....
1251
1252
1253
1254
1255
1256
1257
1258
1259

1260
1261
1262
1263
1264
1265
1266
1267
....
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326


1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338

















1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376

1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
....
1458
1459
1460
1461
1462
1463
1464
1465





1466
1467
1468
1469
1470
1471
1472
1473
....
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502



1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
....
1633
1634
1635
1636
1637
1638
1639

1640
1641
1642
1643
1644
1645
1646
....
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
....
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
....
1819
1820
1821
1822
1823
1824
1825

1826
1827
1828
1829
1830
1831
1832
....
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
....
2006
2007
2008
2009
2010
2011
2012





2013
2014
2015
2016
2017
module "MiniMagAsm markdown dialect parser"

struct TMarkdownResults
  .hContent     dd ?
  .hIndex       dd ?
  .hKeywords    dd ?
  .hDescription dd ?
ends
................................................................................
fcloseImage             = 2
fcloseInlineImage       = 3
fcloseObject            = 4


fblkQuote       = 1
fblkTable       = 2
fblkUList       = 3
fblkOLIst       = 4
fblkSpoiler     = 5

fstateHeader    = $01
fstatePara      = $02
fstateWhite     = $04
fstateBold      = $08
fstateUnderline = $20
fstateStrikeout = $40
................................................................................

fstateTextNow  = $200         ; don't use the second byte for any other flags.
fstateTextPrev = $100

fstateInlineSource    =  $10000
fstateBlockSource     =  $20000
fstateTable           = $100000



; Translates some markdown text to HTML
; Arguments:
;     .markdown - pointer or handler of the markdown text.
;     .procFixLink - [proc .ptrLink] procedure that process the link address and returns
;                    handle to the processed link address. if [.procFixLink]=NULL, the links
;                    are inserted as they are in the HTML.
;     .fBacklinks - flag, that defines whether to insert after the headers back links to the
;                   table of contents item.
;     .pResult - pointer to TMarkdownResults structure where the result strings are returned.
;     .lParam - caller supplied parameter that is passed to procFixLink.
;
; Returns:
;   The procedure returns  4 string handles in the TMarkdownResults structure pointed by
;   .pResult argument.

proc TranslateMarkdown2, .markdown, .procFixLink, .fBacklinks, .pResult, .lParam

................................................................................
        lea     esi, [esi+1]

.td_ok:
        or      [.state], fstatePara
        jmp     .process_char

.not_td_open:






        cmp     al, '*'
        jne     .simple_p_tag

        cmp     byte [esi], ' '
        jne     .simple_p_tag

        mov     dword [edi], '<li>'


        add     edi, 4
        or      [.state], fstatePara

        inc     esi
        mov     al, [esi]
        inc     esi
        jmp     .process_char

.simple_p_tag:

        mov     dword [edi], '<p>'
        add     edi, 3
        jmp     .process_char

.process_inline_code:
        mov     ecx, fstateInlineSource
        mov     ebx, cInlineCodeTag
        jmp     .ProcessTag

cInlineCodeTag text "code"

.process_bold:
        mov     ecx, fstateBold
        mov     ebx, cBoldTag
        jmp     .ProcessTag

cBoldTag text "strong"

.process_italic:
        mov     ecx, fstateItalic
        mov     ebx, cItalicTag
        jmp     .ProcessTag

cItalicTag text "em"

.process_underline:
        mov     ecx, fstateUnderline
        mov     ebx, cUnderlinedTag
        jmp     .ProcessTag

cUnderlinedTag text 'span class="underlined"'

.process_strikeout:
        mov     ecx, fstateStrikeout
        mov     ebx, cStrikedTag

cStrikedTag text "del"

; ecx = state mask

; ebx = pointer to html tag string
.ProcessTag:
        mov     dl, al

        test    [.state], ecx
        jz      .start_tag

; close tag
................................................................................
        je      .normal_char

        or      [.state], ecx

; open tag
        mov     byte [edi], '<'
        lea     edi, [edi+1]

.copy_start:
        mov     al, [ebx]
        inc     ebx
        test    al, al
        jz      .end_tag



        stosb
        jmp     .copy_start


.finish_tag:
        mov     al, [ebx]
        lea     ebx, [ebx+1]
        cmp     al, " "
        jbe     .end_tag

        stosb
        jmp     .finish_tag

.end_tag:
        mov     byte [edi], '>'
        lea     edi, [edi+1]

        jmp     .scan_line2


.white_space:
................................................................................
        mov     ecx, [.last_header]
        add     ecx, '/h0>'
        mov     [edi], ecx
        lea     edi, [edi+4]

        and     [.state], not (fstateHeader or fstateTextNow)   ; For the paragraph flow, the header loops like empty line.













.crlf:
        test    [.state], fstateTextPrev or fstateTextNow
        jz     @f
        mov     word [edi], $0a0d
        lea     edi, [edi+2]
@@:
        test    al, al
        jnz     .line_start2



.finish:
        mov     dword [edi], 0
        stdcall StrCat, [.result], [.pHTML]
        retn


.is_link_begin:
................................................................................
        push    esi

        mov     esi, edx
        cmp     byte [edx], '$'
        jne     @f
        inc     esi
@@:
        mov     dword [edi],   '<vid'
        mov     dword [edi+4], 'eo c'
        mov     dword [edi+8], 'ontr'
        mov     dword [edi+12],'ols '
        add     edi, 16

        cmp     byte [esi], '0'
        jb      .par_data
        cmp     byte [esi], '9'
        ja      .par_data

        mov     dword [edi],  ' wid'
................................................................................
        jmp     .copy_height

.height_ok:
        mov     word [edi], '" '
        add     edi, 2

.par_data:
        mov     dword [edi],   '><so'
        mov     dword [edi+4], 'urce'
        mov     dword [edi+8], ' src'
        mov     word [edi+12], '="'
        add     edi, 14

        lea     edx, [esi-1]
        pop     esi
        jmp     .link_tag_ok


; yes the link was found.
................................................................................
.close_link_tag:

        mov     dword [edi], '</a>'
        add     edi, 4
        retn

.close_object_tag:
        mov     dword [edi],   '</vi'
        mov     dword [edi+4], 'deo>'

        add     edi, 8
        retn

;....................................................................
; Processing of block source code
.copy_source:

.source_line:
................................................................................
        lea     edi, [edi+9]

        and     [.state], not fstateBlockSource
        jmp     .skip2

;....................................................................

.close_paragraph:       ; the <p> and <li> tags are not closed actually. Only the <td> tags.
;        test    [.state], fstatePara
;        jz      .close_para_ok

        call    .close_inlines
        call    .close_all_links

        test    [.state], fstateTable
        jnz     .close_td



        jmp     .close_para_ok

.close_td:
        mov     dword [edi], '</td'
        mov     dword [edi+4], '>'+($0a0d shl 8)
        lea     edi, [edi+7]

.close_para_ok:
        and     [.state], not fstatePara
        retn

;....................................................................

















; closes all open inline tags.

.close_inlines:
        push    esi

        test    [.state], fstateBold
        jz      .bold_ok

        mov     esi, cBoldTag
        call    .copy_one_close_tag

.bold_ok:
        test    [.state], fstateItalic
        jz      .italic_ok

        mov     esi, cItalicTag
        call    .copy_one_close_tag

.italic_ok:
        test    [.state], fstateUnderline
        jz      .underline_ok

        mov     esi, cUnderlinedTag
        call    .copy_one_close_tag

.underline_ok:
        test    [.state], fstateStrikeout
        jz      .strike_ok

        mov     esi, cStrikedTag
        call    .copy_one_close_tag

.strike_ok:
        test    [.state], fstateInlineSource
        jz      .inline_source_ok

        mov     esi, cInlineCodeTag
        call    .copy_one_close_tag


.inline_source_ok:
        and     [.state], not (fstateBold or fstateItalic or fstateUnderline or fstateStrikeout or fstateInlineSource)
        pop     esi
        retn

.copy_one_close_tag:
        mov     ax, '</'
        stosw

.coct_loop:
        lodsb
        cmp     al, " "
        jbe     .end_coct
        stosb
        jmp     .coct_loop

.end_coct:
        mov     al, '>'
        stosb
        retn


;....................................................................
; horizontal rule element

.horiz_rule:
................................................................................
; table block;
        call    .close_paragraph

        mov     al, fblkTable
        call    .PushBlock

        mov     dword [edi], '<tab'
        mov     dword [edi+4], 'le>'





        add     edi, 7

        or      [.state], fstateTable
        jmp     .skip2


.not_table:
        cmp     dword [esi], ';---'
................................................................................

; block quote
        call    .close_paragraph

        mov     al, fblkQuote
        call    .PushBlock

        mov     dword [edi],    '<blo'
        mov     dword [edi+4],  'ckqu'
        mov     dword [edi+8],  'ote>'
        mov     dword [edi+12], '<hea'
        mov     dword [edi+16], 'der>'



        lea     edi, [edi+20]

        push    esi
        add     esi, 6

        call    .CopyName

        mov     dword [edi],   '</he'
        mov     dword [edi+4], 'ader'
        mov     byte  [edi+8], '>'
        lea     edi, [edi+9]

        pop     esi
        jmp     .skip2


.not_quote:
        cmp     dword [esi], ';uli'
        jne     .not_ulist
        cmp     word [esi+4], 'st'
        jne     .not_ulist

; unnumbered list
        call    .close_paragraph

        mov     al, fblkUList
        call    .PushBlock

        mov     dword [edi], '<ul>'
        add     edi, 4
        jmp     .skip2

.not_ulist:
        cmp     dword [esi], ';oli'
        jne     .not_olist
        cmp     word [esi+4], 'st'
        jne     .not_olist

; numbered list
        call    .close_paragraph

        mov     al, fblkOLIst
        call    .PushBlock

        mov     dword [edi], '<ol>'
        add     edi, 4
        jmp     .skip2

.not_olist:
        cmp     dword [esi],   ';spo'
        jne     .not_spoiler
        cmp     dword [esi+4], 'iler'
        jne     .not_spoiler

; spoiler
        call    .close_paragraph

        mov     al, fblkSpoiler
        call    .PushBlock

        mov     dword [edi],    '<det'    ;<details><summary>Spoiler</summary>
        mov     dword [edi+4],  'ails'
        mov     dword [edi+8],  '><su'
        mov     dword [edi+12], 'mmar'
        mov     word [edi+16],  'y>'
        add     edi, 18

        push    esi
        add     esi, 8
        call    .CopyName

        mov     dword [edi],   '</su'
        mov     dword [edi+4], 'mmar'
        mov     word  [edi+8], 'y>'
        add     edi, 10

        pop     esi
        jmp     .skip2

.not_spoiler:
        cmp     dword [esi], ';key'
        jne     .not_keywords
        cmp     dword [esi+4], 'word'
        jne     .not_keywords
        cmp     byte [esi+8], 's'
        jne     .not_keywords

................................................................................

.not_desc:
        jmp     .skip2          ; ";" at the begining without keyword is comment. The line is ignored.


.end_block:
        call    .close_paragraph

        call    .do_end_block
        jmp     .skip2

.end_all_blocks:
        call    .close_paragraph

.end_all_loop:
................................................................................
        retn

.what_block:
        cmp     eax, fblkQuote
        je      .end_quote
        cmp     eax, fblkTable
        je      .end_table
        cmp     eax, fblkUList
        je      .end_ulist
        cmp     eax, fblkOLIst
        je      .end_olist
        cmp     eax, fblkSpoiler
        je      .end_spoiler

        clc
        retn

.end_quote:
        mov     dword [edi],   '</bl'
        mov     dword [edi+4], 'ockq'
        mov     dword [edi+8], 'uote'
        mov     byte [edi+12], '>'
        lea     edi, [edi+13]
        clc
        retn

.end_table:
        mov     dword [edi], '</tr'
        mov     dword [edi+4], '>'+($0a0d shl 8)
        mov     dword [edi+7], '</ta'
................................................................................
        mov     dword [edi+11],'ble>'
        mov     word  [edi+15], $0a0d
        lea     edi, [edi+17]
        and     [.state], not fstateTable
        clc
        retn

.end_olist:
        mov     dword [edi], '</ol'
        jmp     .end_list

.end_ulist:
        mov     dword [edi], '</ul'
.end_list:
        mov     dword [edi+4], '>' + ($0a0d shl 8)
        add     edi, 7
        clc
        retn

.end_spoiler:
        mov     dword [edi],   '</de'
        mov     dword [edi+4], 'tail'
        mov     word  [edi+8], 's>'
        add     edi, 10
        clc
        retn

;....................................................................
.PushBlock:
        push    ecx
        mov     ecx, [.pblock]
        mov     [.blocks+ecx], al
        inc     ecx
        cmp     ecx, 1024
................................................................................
        retn

;....................................................................

; processing header items and building of the table of contents.

.header2:

        xor     ecx, ecx

.scan_level2:
        inc     ecx
.scan_space2:
        lea     esi, [esi+1]
        mov     al, [esi]
................................................................................
        call    _NumToStrU
        mov     al, '.'
        stosb
        inc     ebx
        cmp     ebx, [esp]
        jbe     .loop_numbers

        dec     edi
        pop     ecx
        jmp     .finID

.main:
        mov     eax, 'main'
        stosd

................................................................................

        mov     esi, edx

.end_cp_name:
        pop     edx ecx
        retn
endp









endmodule

Added freshlib/data/minimag.asm.





















































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
618
619
620
621
622
623
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
872
873
874
875
876
877
878
879
880
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
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
module "MiniMag translator"


struct TMDItem
  .type   dd ?
  .indent dd ?
ends


;--------------------------------------------------------------
; Converts MiniMag code into HTML. Starts from the position [.from]
; and ends at the end of the text or at first NULL character.
;
; Arguments:
;    .pMiniMag - pointer to the TText structure with the BBCode
;               formatted text.
;    .from    - the position where to start.
;
; Returns:
;    edx - The same structure (possibly reallocated) with
;    the translated to html code text.
;

mmfDontFormat = $80000000
mmfNoInline   = $40000000

proc TranslateMiniMag, .pText, .from
.pStack dd ?

.whspc  dd ?    ; whitespace symbols count;
.flags  dd ?
.hdr    dd ?    ; header level

.pLinks dd ?

.lbl_hash dd ?
.tmp_link dd ?

begin
        pushad

        xor     eax, eax
        mov     [.flags], eax

        stdcall GetMem, 256*4
        mov     [.pLinks], eax

        stdcall CreateArray, sizeof.TMDItem
        mov     [.pStack], eax

        mov     edx, [.pText]
        stdcall TextMoveGap, edx, [.from]

        mov     ecx, [.from]
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]
        dec     ecx

.fp_new_line:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .second_pass

        mov     al, [edx+ecx]
        test    al, al
        jz      .second_pass

        cmp     al, '['
        je      .link_def_found
        jmp     .fp_check_eol

.fp_skip_eol:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .second_pass

        mov     al, [edx+ecx]
        test    al, al
        jz      .second_pass

.fp_check_eol:
        cmp     al, $0d
        je      .fp_new_line

        cmp     al, $0a
        je      .fp_new_line

; encode HTML.

        test    al, al
        js      .fp_skip_eol

        and     eax, $ff
        movzx   eax, byte [tbl_html+eax]

        test    al, al
        jns     .fp_skip_eol

.do_encode:
        lea     esi, [eax+tbl_html]
        lodsb

        stdcall TextSetGapSize, edx, eax

        xchg    eax, ecx
        sub     eax, [edx+TText.GapEnd]
        add     eax, [edx+TText.GapBegin]
        stdcall TextMoveGap, edx, eax
        inc     [edx+TText.GapEnd]      ; delete the replaced char.

        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx
        rep movsb

        mov     ecx, [edx+TText.GapEnd]
        dec     ecx
        jmp     .fp_skip_eol


.link_def_found:
        mov     eax, ecx
        sub     eax, [edx+TText.GapEnd]
        add     eax, [edx+TText.GapBegin]

        stdcall TextMoveGap, edx, eax   ; move the gap to the beginning of the link.

        lea     edi, [ecx+1]  ; the start of the label.
        xor     esi, esi
        xor     eax, eax

.hash_loop:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .second_pass

        mov     al, [edx+ecx]
        test    al, al
        jz      .second_pass

        cmp     al, ']'
        je      .end_of_lbl

        test    al, al
        js      .add_hash

        mov     al, [tbl_html+eax]
        test    al, al
        jz      .fp_skip_eol    ;????????????
        jns     .add_hash

        push    ecx edx

        mov     edx, esi
        mov     esi, eax
        add     esi, tbl_html

        lodsb
        mov     ecx, eax

.html_hash:
        lodsb
        xor     edx, eax
        mov     dl, [tpl_func + edx]
        loop    .html_hash

        mov     esi, edx
        pop     edx ecx
        jmp     .hash_loop

.add_hash:
        xor     esi, eax
        movzx   esi, byte [tpl_func + esi]
        jmp     .hash_loop

.end_of_lbl:
        mov     [.lbl_hash], esi

        mov     esi, ecx

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .second_pass

        mov     al, [edx+ecx]
        cmp     al, ' '
        jne     .fp_invalid_link

; valid label has been found...
; first: extract the label.

        sub     esi, edi
        jz      .fp_invalid_link

        cmp     esi, $ff
        ja      .fp_invalid_link

        lea     edi, [edx+edi]

        stdcall StrNew
        stdcall StrCharCat, eax, esi
        stdcall StrCatMemHTML, eax, edi, esi
        mov     [.tmp_link], eax

; fix the label length.
        stdcall StrPtr, eax
        mov     esi, eax
        mov     eax, [esi+string.len]
        dec     eax
        cmp     eax, $ff
        ja      .free_link
        mov     byte [esi], al

.link_url_start:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .free_link

        mov     al, [edx+ecx]
        test    al, al
        jz      .free_link

        cmp     al, $0d
        je      .free_link
        cmp     al, $0a
        je      .free_link

        cmp     al, ' '
        jbe     .link_url_start

        mov     edi, ecx
        mov     esi, ecx

.loop_to_end_of_link:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .free_link

        mov     al, [edx+ecx]
        test    al, al
        jz      .end_found

        cmp     al, $0d
        je      .end_of_link
        cmp     al, $0a
        je      .end_of_link

        cmp     al, ' '
        jbe     .loop_to_end_of_link

        lea     esi, [ecx+1]
        jmp     .loop_to_end_of_link


.end_of_link:
        xor     al, $0d xor $0a

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .end_found

        cmp     al, [edx+ecx]
        jne     .end_found

        inc     ecx

.end_found:
        sub     esi, edi
        jz      .free_link

        lea     edi, [edx+edi]
        stdcall StrCatMemHTML, [.tmp_link], edi, esi

; for debugging links handing;
;        stdcall FileWriteString, [STDERR], [.tmp_link]
;        stdcall FileWriteString, [STDERR], <txt 13, 10>

        mov     eax, [.lbl_hash]
        mov     edi, [.pLinks]
        mov     esi, [.tmp_link]

.hash_search_slot:                      ; resolve possible hash collisions.
        xchg    esi, [edi+4*eax]
        test    esi, esi
        jz      .end_link

        inc     al
        cmp     eax, [.lbl_hash]
        jne     .hash_search_slot

        stdcall StrDel, esi     ; the hash table is full, so simply delete the remaining link.

.end_link:
        mov     [edx+TText.GapEnd], ecx         ; delete the entire line of the link definition.
        dec     ecx
        jmp     .fp_new_line

.free_link:
        stdcall StrDel, [.tmp_link]

.fp_invalid_link:
        mov     ecx, [edx+TText.GapEnd]
        jmp     .fp_skip_eol


; Second processing pass. It will format all the tags.

.second_pass:

        mov     ebx, [.from]

.start_of_line:

        xor     eax, eax
        mov     [.hdr], eax
        mov     byte [.flags], al
        and     [.flags], not mmfNoInline

        stdcall TextMoveGap, edx, ebx

        xor     eax, eax
        mov     [.whspc], eax

.loop:
        mov     ecx, ebx
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]
        cmp     ecx, [edx+TText.Length]
        jae     .empty_line_found

        mov     al, [edx+ecx]

        test    al, al
        jz      .empty_line_found

        cmp     al, ';'
        je      .command_found

        test    [.flags], mmfDontFormat
        jnz     .non_empty_line_found

        cmp     al, '#'
        je      .header_found

        cmp     al, '*'
        je      .list_item_found

        cmp     al, $0d
        je      .empty_line_found
        cmp     al, $0a
        je      .empty_line_found

        cmp     al, ' '
        ja      .non_empty_line_found

        inc     [.whspc]
        inc     [edx+TText.GapEnd]
        jmp     .loop


.command_found:

        cmp     [.whspc], 0
        jne     .non_empty_line_found

        test    [.flags], mmfDontFormat
        jnz     .check_for_end

        lea     eax, [ecx+8]             ; spoiler
        cmp     eax, [edx+TText.Length]
        jae     .less_than_8

        cmp     dword [edx+ecx], ';spo'
        jne     .not_spoiler
        cmp     dword [edx+ecx+4], 'iler'
        jne     .not_spoiler
        cmp     byte [edx+ecx+8], ' '
        ja      .not_spoiler

        call    .close_all_non_minimag

        push    ecx
        mov     eax, tagSpoiler
        call    .add_to_stack
        pop     ecx

        mov     eax, tagSpoiler
        call    .prefix_tag

        add     ecx, 8
        add     [edx+TText.GapEnd], 8

        push    ebx
        call    .skip_tag_arg
        pop     eax
        cmp     eax, ebx
        jne     .sum_ok

        mov     ecx, [edx+TText.GapBegin]
        mov     dword [edx+ecx], 'Spoi'
        mov     dword [edx+ecx+4], 'ler '
        add     [edx+TText.GapBegin], 7
        add     ebx, 7

.sum_ok:
        mov     eax, tagSpoiler
        call    .suffix_tag

        jmp     .start_of_line


.not_spoiler:
.less_than_8:
        lea     eax, [ecx+6]
        cmp     eax, [edx+TText.Length]
        jae     .less_than_6

        cmp     dword [edx+ecx], ';quo'
        jne     .not_quote
        cmp     word [edx+ecx+4], 'te'
        jne     .not_quote
        cmp     byte [edx+ecx+6], ' '
        ja      .not_quote

; quote command here

        call    .close_all_non_minimag

        push    ecx
        mov     eax, tagQuote
        call    .add_to_stack
        pop     ecx

        mov     eax, tagQuote
        call    .prefix_tag

        add     ecx, 6
        add     [edx+TText.GapEnd], 6

        call    .skip_tag_arg

        mov     eax, tagQuote
        call    .suffix_tag

        jmp     .start_of_line


.not_quote:

        cmp     dword [edx+ecx], ';tab'
        jne     .not_table
        cmp     word [edx+ecx+4], 'le'
        jne     .not_table
        cmp     byte [edx+ecx+6], ' '
        ja      .not_table

; table command here

        call    .close_all_non_minimag

        mov     eax, tagTable
        call    .open_tag

        jmp     .delete_to_eol


.not_table:
        cmp     dword [edx+ecx], ';beg'
        jne     .not_begin
        cmp     word [edx+ecx+4], 'in'
        jne     .not_begin
        cmp     byte [edx+ecx+6], ' '
        ja      .not_begin

        or      [.flags], mmfDontFormat

        call    .close_all_non_minimag

        push    ecx
        mov     eax, tagCode
        call    .add_to_stack
        pop     ecx

        mov     eax, tagCode
        call    .prefix_tag

        add     ecx, 6
        add     [edx+TText.GapEnd], 6

        call    .skip_tag_arg

        mov     eax, tagCode
        call    .suffix_tag

        jmp     .start_of_line


.not_begin:

        cmp     dword [edx+ecx], ';uli'
        jne     .not_ulist
        cmp     word [edx+ecx+4], 'st'
        jne     .not_ulist
        cmp     byte [edx+ecx+6], ' '
        ja      .not_ulist

        call    .close_all_non_minimag

        mov     eax, tagUL
        call    .open_tag

        jmp     .delete_to_eol


.not_ulist:
        cmp     dword [edx+ecx], ';oli'
        jne     .not_olist
        cmp     word [edx+ecx+4], 'st'
        jne     .not_olist
        cmp     byte [edx+ecx+6], ' '
        ja      .not_olist

        call    .close_all_non_minimag

        mov     eax, tagOL
        call    .open_tag

        jmp     .delete_to_eol


.not_olist:

.less_than_6:
.less_than_5:

.check_for_end:

        lea     eax, [ecx+4]
        cmp     eax, [edx+TText.Length]
        jae     .less_than_4

        cmp     dword [edx+ecx], ';end'
        jne     .not_end

        cmp     byte [edx+ecx+4], ' '
        ja      .not_end

; end command here

        call    .close_all_non_minimag
        call    .pop_one_tag

        and     [.flags], not mmfDontFormat

        jmp     .delete_to_eol


.not_end:
        test    [.flags], mmfDontFormat
        jnz     .non_empty_line_found

        cmp     dword [edx+ecx], ';---'
        jne     .not_ruler

; it is a horizontal ruller command. It can separate paragraphs by <hr> tag, or the table rows by <tr> tags.

        push    ecx
        mov     esi, [.pStack]


.row_close:
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .put_hr ; empty stack!

        mov     eax, [esi+TArray.array + sizeof.TMDItem * ecx + TMDItem.type]

        cmp     eax, tagTD
        je      .close_this
        cmp     eax, tagTH
        je      .close_this
        cmp     eax, tagTR
        je      .close_this

        cmp     eax, tagTable
        je      .open_tr

.put_hr:
        mov     eax, tagHR
        call    .prefix_tag
        jmp     .end_hr

.close_this:
        mov     [esi+TArray.count], ecx
        call    .close_tag
        jmp     .row_close

.open_tr:
        mov     eax, tagTR
        call    .open_tag

.end_hr:
        pop     ecx
        jmp     .delete_to_eol


.less_than_4:
.not_ruler:

; it is simply a comment. Delete it up to the end of the line.

.delete_to_eol:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .eol_ok

        mov     al, [edx+ecx]
        test    al, al
        jz      .eol_ok
        cmp     al, $0d
        je      .eol_found2

        cmp     al, $0a
        jne     .delete_to_eol

.eol_found2:
        inc     ecx
        xor     al, $0d xor $0a
        cmp     al, [edx+ecx]
        jne     .eol_ok
        inc     ecx
.eol_ok:
        mov     [edx+TText.GapEnd], ecx
        jmp     .start_of_line


.list_item_found:

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .non_empty_line_found

        cmp     byte [edx+ecx], ' '
        ja      .non_empty_line_found

        add     [edx+TText.GapEnd], 2

; here list item begins:

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .add_leaf

        mov     eax, [esi + TArray.array + sizeof.TMDItem * ecx + TMDItem.type]
        cmp     eax, tagUL
        je      .open_li
        cmp     eax, tagOL
        jne     .add_leaf

.open_li:
        mov     eax, tagListItem
        call    .open_tag
        jmp     .add_leaf


.headers dd tagH1, tagH2, tagH3, tagH4, tagH5, tagH6

.header_found:

        xor     eax, eax

.hcnt:
        inc     eax
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .non_empty_line_found           ; not a header?

        cmp     byte [edx+ecx], '#'
        je      .hcnt

        cmp     eax, 6
        ja      .non_empty_line_found

        cmp     byte [edx+ecx], ' '
        jne     .non_empty_line_found           ; not a header?

        stc
        adc     [edx+TText.GapEnd], eax

        mov     [.hdr], eax


.non_empty_line_found:
        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .add_leaf

        mov     eax, [esi + TArray.array + sizeof.TMDItem * ecx + TMDItem.type] ; don't add paragraphs inside elements that can handle text by themselves.
        test    [eax+TOneTag.flags], fLines
        jnz     .skip_to_eol

        cmp     eax, tagCode
        je      .skip_to_eol

        cmp     eax, tagTable
        jne     .check_for_row

        mov     eax, tagTR
        call    .open_tag
        jmp     .open_td_th

.check_for_row:
        cmp     eax, tagTR
        jne     .add_leaf

.open_td_th:
        mov     eax, tagTD
        cmp     [.hdr], 0
        je      .open_td
        mov     eax, tagTH
        and     [.hdr], 0
.open_td:
        call    .open_tag

.add_leaf:
        mov     eax, tagPara

        mov     ecx, [.hdr]
        dec     ecx
        js      .do_add_leaf
        mov     eax, [.headers + 4*ecx]

.do_add_leaf:
        call    .open_tag
        jmp     .skip_to_eol


.empty_line_found:

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]

.loop_empty_line:
        dec     ecx
        js      .skip_to_eol   ; the empty line is not inside code block because there is no blocks at all.

        mov     eax, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        test    [eax+TOneTag.flags], fLines or fInlineTag
        jz      .check_wrap

        cmp     eax, tagInlineCode
        jne     .inline_code_ok
        and     [.flags], not mmfDontFormat
.inline_code_ok:

        mov     [esi+TArray.count], ecx ; close text leaf blocks.
        call    .close_tag
        jmp     .loop_empty_line

        dec     ecx
        js      .skip_to_eol   ; the empty line is not inside code block because there is no blocks at all.

.check_wrap:
        cmp     eax, tagTD
        je      .close_wrap
        cmp     eax, tagTH
        je      .close_wrap
        cmp     eax, tagListItem
        jne     .skip_to_eol

.close_wrap:
        mov     [esi+TArray.count], ecx
        call    .close_tag
        jmp     .skip_to_eol

; Here seems to be a good place for processing the inline formatting elements, together with the HTML encoding.

.skip_to_eol:

        xor     eax, eax
        mov     ecx, ebx
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]
        cmp     ecx, [edx+TText.Length]
        jae     .end_of_text

        mov     al, [edx+ecx]
        test    al, al
        jz      .end_of_text
        cmp     al, $0d
        je      .eol_found
        cmp     al, $0a
        je      .eol_found

        cmp     al, '`'
        je      .code

        test    [.flags], mmfDontFormat
        jnz     .next

        test    [.flags], mmfNoInline
        jnz     .inline_ok

        cmp     al, '*'
        je      .bold
        cmp     al, '/'
        je      .italic
        cmp     al, '_'
        je      .underline
        cmp     al, '-'
        je      .del

.inline_ok:
        cmp     al, '['
        je      .link

        mov     byte [.flags], al       ; the previous character.

.next:
        inc     ebx
        jmp     .skip_to_eol


.eol_found:
        mov     ah, al
        xor     ah, $0d xor $0a

        inc     ecx
        inc     ebx
        cmp     [edx+ecx], ah
        jne     .start_of_line
        inc     ebx
        jmp     .start_of_line


.link:
locals
  .linkT dd ?
  .imgW  dd ?
  .imgH  dd ?
  .linkStart dd ?
  .lblStart dd ?
  .lblLen   dd ?
  .urlLen   dd ?
  .level    dd ?
endl
        mov     ebx, ecx
        sub     ebx, [edx+TText.GapEnd]
        add     ebx, [edx+TText.GapBegin]
        stdcall TextMoveGap, edx, ebx

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .next

        xor     eax, eax
        mov     [.linkT], eax

        mov     al, [edx+ecx]
        test    al, al
        jz      .next

        cmp     al, '!'
        je      .block_image

        cmp     al, '?'
        je      .inline_image

        cmp     al, '$'
        je      .video

        mov     [.linkT], tagURL
        jmp     .image_ok

.video:
        mov     [.linkT], tagVideoMM
        jmp     .image

.block_image:
        mov     [.linkT], tagImgMM
        jmp     .image

.inline_image:

        mov     [.linkT], tagInlineImgMM

.image:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .next

        mov     al, [edx+ecx]
        test    al, al
        jz      .next

        cmp     al, '0'
        jb      .image_ok
        cmp     al, '9'
        ja      .image_ok

; For now...
.ignore_size:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .next

        mov     al, [edx+ecx]
        test    al, al
        jz      .next

        cmp     al, ':'
        jne     .ignore_size

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .next

        mov     al, [edx+ecx]
        test    al, al
        jz      .next

.image_ok:
; here eax contains the current byte of the address label.
; The first value of the hash is computed outside the hash loop.

        mov     edi, ecx  ; the start of the label.
        movzx   esi, byte [tpl_func + eax]    ; the initial value of the hash.

.hash_loop2:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .next

        mov     al, [edx+ecx]
        test    al, al
        jz      .next

        cmp     al, ']'
        je      .end_of_link_lbl

        xor     esi, eax
        movzx   esi, byte [tpl_func + esi]
        jmp     .hash_loop2

.end_of_link_lbl:
        mov     [.lbl_hash], esi

        mov     eax, ecx
        sub     eax, edi

        mov     [.lblStart], edi
        mov     [.lblLen], eax

; search the label in the hash table.

        push    ebx ecx

        mov     ebx, [.lbl_hash]

.lbl_search:
        mov     edi, [.pLinks]
        mov     eax, [edi+4*ebx]
        test    eax, eax
        jz      .hash_not_found

        stdcall StrPtr, eax
        mov     esi, eax
        mov     edi, edx
        add     edi, [.lblStart]
        mov     ecx, [.lblLen]

        mov     eax, [esi+string.len]
        mov     [.urlLen], eax

        movzx   eax, byte [esi] ; the first byte is the length of the label part.
        inc     esi

        sub     [.urlLen], eax
        dec     [.urlLen]

        cmp     ecx, eax
        jne     .not_this_hash

        repe cmpsb
        je      .insert_link

.not_this_hash:
        inc     bl
        cmp     ebx, [.lbl_hash]
        jne     .lbl_search

.hash_not_found:
; the link is not in the hash table, so it is a inline link.

        mov     esi, [.lblStart]
        add     esi, edx

        mov     eax, [.lblLen]
        mov     [.urlLen], eax

.insert_link:
; the label matches! Insert the tag prefix.

        pop     ecx ebx

        mov     eax, [.lblStart]
        mov     [edx+TText.GapEnd], eax ; delete the start "["

        mov     eax, [.linkT]
        call    .prefix_tag

; copy the url here. It is pointed by ESI right now.

        stdcall TextSetGapSize, edx, [.urlLen]
        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        mov     ecx, [.urlLen]
        add     ebx, ecx
        add     [edx+TText.GapBegin], ecx

        rep movsb

        mov     eax, [.linkT]
        call    .suffix_tag

; Here check for additional link content/alt content.

        mov     ecx, [edx+TText.GapEnd]
        add     ecx, [.lblLen]

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .skip_link_content

        mov     al, [edx+ecx]
        cmp     al, '['
        jne     .skip_link_content

        lea     eax, [ecx+1]
        mov     [.lblStart], eax

        and     [.level], 0

.search_second_end:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .skip_link_content

        mov     al, [edx+ecx]
        test    al, al
        jz      .skip_link_content

        cmp     al, $0d
        je      .skip_link_content
        cmp     al, $0a
        je      .skip_link_content

        cmp     al, '['         ; nested link
        jne     .nested_ok
        inc     [.level]

.nested_ok:
        cmp     al, ']'
        jne     .search_second_end

        cmp     [.level], 0
        je      .second_end

        dec     [.level]
        jmp     .search_second_end


.second_end:
        sub     ecx, [.lblStart]
        mov     [.lblLen], ecx

        mov     ecx, [.lblStart]
        mov     [edx+TText.GapEnd], ecx ; delete the link URL/label part, because there is second one.

        push    ebx

        add     ebx, [.lblLen]
        stdcall TextMoveGap, edx, ebx
        inc     [edx+TText.GapEnd]

        mov     eax, [.linkT]
        call    .close_tag

        pop     ebx
        stdcall TextMoveGap, edx, ebx

        or      [.flags], mmfNoInline
        jmp     .skip_to_eol


.skip_link_content:

        add     ebx, [.lblLen]
        stdcall TextMoveGap, edx, ebx
        inc     [edx+TText.GapEnd]

        mov     eax, [.linkT]
        call    .close_tag

        and     [.flags], not mmfNoInline
        jmp     .skip_to_eol



.bold:
        mov     eax, tagStrong
        jmp     .inline_tag

.italic:
        mov     eax, tagEm
        jmp     .inline_tag

.underline:
        mov     eax, tagUnderlined
        jmp     .inline_tag

.del:
        mov     eax, tagDel
        jmp     .inline_tag

.code:
        mov     eax, tagInlineCode

.inline_tag:

        stdcall TextMoveGap, edx, ebx

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .open_inline

        cmp     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        jne     .open_inline

        mov     [esi+TArray.count], ecx
        cmp     eax, tagInlineCode
        jne     @f
        and     [.flags], not mmfDontFormat
@@:
        call    .close_tag
        jmp     .end_inline

.open_inline:
        cmp     byte [.flags], ' '      ; the previous character.
        ja      .next

        mov     ecx, ebx
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]
        cmp     byte [edx+ecx+1], ' '
        jbe     .next

        cmp     eax, tagInlineCode
        jne     @f
        or      [.flags], mmfDontFormat
@@:
        call    .open_tag

.end_inline:
        inc     [edx+TText.GapEnd]
        jmp     .skip_to_eol


.end_of_text:

; Close all not closed tags:

        stdcall TextMoveGap, edx, ebx
        mov     edi, [.pStack]
        mov     ecx, [edi+TArray.count]

.close_all:
        dec     ecx
        js      .finalize

        mov     eax, [edi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        call    .close_tag
        jmp     .close_all

.finalize:
        mov     esi, [.pLinks]
        mov     ecx, $ff

.freelinks:
        stdcall StrDel, [esi+4*ecx]
        dec     ecx
        jns     .freelinks

        stdcall FreeMem, esi
        stdcall FreeMem, [.pStack]

        mov     [esp + 4*regEDX], edx
        popad
        return



.close_all_non_minimag:
        push    ecx esi

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]

.close_loop:
        dec     ecx
        js      .end_close

        mov     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        test    [eax+TOneTag.flags], fMinimagBlock
        jnz     .end_close

        mov     [esi+TArray.count], ecx
        call    .close_tag
        jmp     .close_loop

.end_close:
        pop     esi ecx
        retn


.pop_one_tag:
        push    ecx esi

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .end_pop_one

        mov     [esi+TArray.count], ecx
        mov     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        call    .close_tag

.end_pop_one:
        pop     esi ecx
        retn


.open_tag:
        push    ecx esi edi
        push    eax eax
        call    .add_to_stack
        pop     eax
        call    .prefix_tag
        pop     eax
        call    .suffix_tag
        pop     edi esi ecx
        retn


.suffix_tag:
        lea     eax, [eax+TOneTag.pOpenS]
        jmp     .copy_tag

.close_tag:
        lea     eax, [eax+TOneTag.pClose]

.prefix_tag:
.copy_tag:
        push    ecx esi edi

        mov     esi, [eax]
        mov     ecx, [eax+4]
        test    esi, esi
        jz      .tag_copy_end

        add     ebx, ecx

        stdcall TextSetGapSize, edx, ecx
        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx

        rep movsb

.tag_copy_end:
        pop     edi esi ecx
        retn


.add_to_stack:
        push    edx eax
        stdcall AddArrayItems, [.pStack], 1
        mov     [.pStack], edx
        pop     [eax + TMDItem.type]
        pop     edx
        retn


.skip_tag_arg:
        push    eax ecx esi

.skip_tag_loop:
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .end_arg

        mov     al, [edx+ecx]
        test    al, al
        jz      .end_arg

        cmp     al, $0a
        je      .eol1
        cmp     al, $0d
        je      .eol1

        cmp     al, ' '
        jbe     .skip_tag_loop

        mov     [edx+TText.GapEnd], ecx
        mov     esi, ebx
        dec     ecx

.search_eol:
        inc     ecx
        inc     esi
        cmp     ecx, [edx+TText.Length]
        jae     .end_arg

        movzx   eax, byte [edx+ecx]
        test    al, al
        jz      .end_arg

        cmp     al, $0a
        je      .eol1

        cmp     al, $0d
        je      .eol1

        cmp     al, ' '
        cmova   ebx, esi
        jmp     .search_eol

.eol1:
        inc     ecx
        xor     al, $0d xor $0a
        cmp     al, [edx+ecx]
        jne     .end_arg
        inc     ecx
.end_arg:
        stdcall TextMoveGap, edx, ebx
        mov     [edx+TText.GapEnd], ecx

        pop     esi ecx eax
        retn

endp



; Debug procedure. Not used in production.

proc DumpStack, .pStack
begin
        pushad

        mov     esi, [.pStack]
        xor     ecx, ecx

.loop:
        cmp     ecx, [esi+TArray.count]
        jae     .finish

        mov     edi, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]

        stdcall FileWrite, [STDERR], [edi+TOneTag.pOpenP], [edi+TOneTag.lOpenP]
        stdcall FileWrite, [STDERR], [edi+TOneTag.pOpenS], [edi+TOneTag.lOpenS]

        stdcall FileWriteString, [STDERR], txt ": "

        stdcall NumToStr, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.indent], ntsDec
        push    eax
        stdcall FileWriteString, [STDERR], eax
        stdcall StrDel ; from the stack

        stdcall FileWriteString, [STDERR], <txt 13, 10>

        inc     ecx
        jmp     .loop

.finish:
        popad
        return

endp



proc StrCatMemHTML, .dest, .psource, .length
begin
        pushad

        mov     esi, [.psource]
        mov     ecx, [.length]

        stdcall StrLen, [.dest]
        lea     edx, [eax+8*ecx+4]

        stdcall StrSetCapacity, [.dest], edx
        mov     edi, eax
        mov     ebx, eax
        add     edi, [edi+string.len]

        xor     eax, eax

.loop:
        dec     ecx
        js      .finish

        lodsb
        test    al, al
        js      .store

        mov     al, [tbl_html+eax]
        test    al, al
        jz      .loop
        js      .encode

.store:
        stosb
        jmp     .loop

.encode:
        push    esi ecx

        lea     esi, [eax+tbl_html]
        movzx   ecx, byte [esi]
        inc     esi

        rep movsb
        pop     ecx esi
        jmp     .loop

.finish:
        mov     ecx, edi
        xor     eax, eax
        sub     ecx, ebx
        stosd
        mov     [ebx+string.len], ecx

        popad
        return
endp







; Not finished, not working!!!

proc TranslateMarkdown, .pText, .from
.pStack dd ?

.qlvl   dd ?    ; quote level.
.whspc  dd ?    ; whitespace symbols count;
.flags  dd ?
.hdr    dd ?    ; header level

.q_from dd ?

.prev   dd ?

begin
        pushad

        stdcall CreateArray, sizeof.TMDItem
        mov     [.pStack], eax

        mov     edx, [.pText]
        mov     ebx, [.from]

.start_of_line:

        xor     eax, eax
        mov     [.qlvl], eax
        mov     [.flags], eax
        mov     [.hdr], eax

        dec     eax
        mov     [.q_from], eax          ; start from -1 on every line.

        stdcall TextMoveGap, edx, ebx

.zerospc:
        xor     eax, eax
        mov     [.whspc], eax

.loop:
        mov     ecx, ebx
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]

        mov     al, [edx+ecx]

        test    al, al
        jz      .empty_line_found

        cmp     al, '>'
        je      .quote_found

        cmp     al, '-'
        je      .ul_found
        cmp     al, '*'
        je      .ul_found
        cmp     al, '+'
        je      .ul_found

        cmp     al, '#'
        je      .header_found

        cmp     al, $0d
        je      .empty_line_found
        cmp     al, $0a
        je      .empty_line_found

        cmp     al, ' '
        ja      .non_empty_line_found

        inc     [.whspc]
        inc     [edx+TText.GapEnd]
        jmp     .loop


; closes all tags up to position ecx (excluding the tag on ecx)
.close_to_ecx:
        push    eax

.ecx_loop:
        mov     eax, [esi+TArray.count]
        dec     eax
        cmp     eax, ecx
        je      .end_close_ecx

        mov     [esi+TArray.count], eax
        mov     eax, [esi+TArray.array + sizeof.TMDItem*eax + TMDItem.type]
        call    .close_tag
        jmp     .ecx_loop

.end_close_ecx:
        pop     eax
        retn


; closes all tags up to position ecx (includig the tag on ecx)
.close_to_ecx_inc:
        push    eax

.ecx_loop2:
        mov     eax, [esi+TArray.count]
        dec     eax
        cmp     eax, ecx
        jl      .end_close_ecx

        mov     [esi+TArray.count], eax
        mov     eax, [esi+TArray.array + sizeof.TMDItem*eax + TMDItem.type]
        call    .close_tag
        jmp     .ecx_loop2


; Searches the tags stack for listitem and returns it's index in the stack.
; Arguments:
;   esi - pointer to the stack array.
; Returns:
;   esi - pointer to the tag stack.
;   If the list item has been found: CF = 0; ecx = index in the array;
;   If the list item has not been found: CF = 1
; Modifies eax

.search_for_li:

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]

.search_li:
        dec     ecx
        cmp     ecx, [.q_from]
        jle     .li_not_found

        mov     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]

        cmp     eax, tagListItem
        je      .li_found

        cmp     eax, tagQuote
        jne     .search_li

.li_not_found:
        stc
        retn

.li_found:
        clc
        retn


.search_for_bq:

        mov     esi, [.pStack]
        mov     ecx, [.q_from]

.search_bq:
        inc     ecx
        cmp     ecx, [esi+TArray.count]
        jae     .bq_not_found

        cmp     [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type], tagQuote
        jne     .search_bq

        clc
        retn

.bq_not_found:
        stc
        retn


.ul_found:
        call    .check_for_code_block
        jc      .skip_to_eol

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .empty_line_found

        cmp     byte [edx+ecx], ' '     ; mandatory space
        ja      .non_empty_line_found

        add     [edx+TText.GapEnd], 2   ; remove the marker.

.search_list:

        call    .search_for_li
        jnc     .list_found

        call    .close_to_ecx
        jmp     .create_new_ul


.list_found:

        OutputValue "List found at: ", [.whspc], 10, -1

        mov     edi, [.whspc]
        sub     edi, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.indent]

        call    .close_to_ecx

        cmp     edi, 4
        jge     .create_new_ul  ; create list one level of nesting up.

        mov     [esi + TArray.count], ecx  ; close the current listitem.
        call    .close_tag

        cmp     edi, -4
        jg      .create_new_li

        dec     ecx

        mov     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]             ; eax should be tagUL or tagOL and nothing else!
        mov     [esi+TArray.count], ecx
        call    .close_tag             ; close the list as well. (can be UL or OL!)

        jmp     .search_list           ; and then search back for the list on the previous level.


.create_new_ul:

        mov     eax, tagUL
        call    .open_tag

.create_new_li:
        OutputValue "List item created on:", [.whspc], 10, -1

        mov     eax, tagListItem
        call    .open_tag
        jmp     .loop           ; after creating list items, the whitespace for the current line should not be reset.


.check_for_code_block:

        mov     esi, [.pStack]
        mov     eax, [esi+TArray.count]
        dec     eax
        js      .not_a_code_block

        cmp     [esi+TArray.array + sizeof.TMDItem*eax + TMDItem.type], tagCode
        jne     .not_a_code_block

; there is open code block
        cmp     [.whspc], 4
        jb      .close_code_block

        stc
        retn

.close_code_block:

        mov     eax, tagCode
        call    .close_tag

.not_a_code_block:
        clc
        retn


.quote_found:

        DebugMsg "Quote found"

        mov     eax, [.q_from]
        OutputValue "Search quote from: ", eax, 10, -1

.quote_search:
        inc     eax
        mov     [.q_from], eax
        cmp     eax, [esi+TArray.count]
        jae     .create_new_quote

        cmp     [esi+TArray.array+sizeof.TMDItem*eax+TMDItem.type], tagQuote
        je      .del_marker
        jmp     .quote_search

.create_new_quote:

        DebugMsg "New quote created"

        call    .check_for_code_block
        jc      .skip_to_eol

        mov     eax, tagQuote
        call    .open_tag

        mov     eax, [esi+TArray.count]
        dec     eax
        mov     [.q_from], eax


.del_marker:

        DebugMsg "Delete quote marker."
        inc     [edx+TText.GapEnd]

        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .empty_line_found

        cmp     byte [edx+ecx], ' '     ; optional space.
        jne     .zerospc

        inc     [edx+TText.GapEnd]
        jmp     .zerospc




.headers dd tagH1, tagH2, tagH3, tagH4, tagH5, tagH6

.header_found:

        cmp     [.whspc], 4
        jae     .code_block

        xor     eax, eax

.hcnt:
        inc     eax
        inc     ecx
        cmp     ecx, [edx+TText.Length]
        jae     .non_empty_line_found           ; not a header?

        cmp     byte [edx+ecx], '#'
        je      .hcnt

        cmp     eax, 6
        ja      .non_empty_line_found

        cmp     byte [edx+ecx], ' '
        jne     .non_empty_line_found           ; not a header?

        stc
        adc     [edx+TText.GapEnd], eax

        mov     [.hdr], eax

.non_empty_line_found:

        OutputValue "Non empty line indent: ", [.whspc], 10, -1

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .add_leaf

        mov     ecx, [esi + TArray.array + sizeof.TMDItem * ecx + TMDItem.type] ; don't add paragraphs inside elements that can handle text by themselves.
        test    [ecx+TOneTag.flags], fLines
        jnz     .skip_to_eol

.add_leaf:
        call    .search_for_bq
        jc      .quote_ok

        call    .close_to_ecx_inc

.quote_ok:

        call    .search_for_li
        mov     eax, [.whspc]

        jc      .indent_ok
        sub     eax, [esi + TArray.array + sizeof.TMDItem*ecx+TMDItem.indent]
.indent_ok:

        cmp     eax, 4
        jge     .code_block

        cmp     eax, -4
        jg      .para

        call    .close_to_ecx_inc

        dec     ecx
        mov     eax, [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        mov     [esi+TArray.count], ecx
        call    .close_tag

        jmp     .add_leaf

.para:
        mov     eax, tagPara
        mov     ecx, [.hdr]
        dec     ecx
        js      .do_add_leaf
        mov     eax, [.headers + 4*ecx]

.do_add_leaf:
        call    .open_tag
        jmp     .skip_to_eol


.empty_line_found:

        call    .search_for_bq
        jc      .quote_ok2

        call    .close_to_ecx_inc

.quote_ok2:

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .skip_to_eol   ; the empty line is not inside code block because there is no blocks at all.

        mov     eax, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        test    [eax+TOneTag.flags], fLines
        jz      .close_list

        mov     [esi+TArray.count], ecx ; close text leaf blocks.
        call    .close_tag
        jmp     .skip_to_eol

.close_list:
        DebugMsg "Close list on second empty line."

        cmp     eax, tagListItem
        jne     .skip_to_eol

        call    .close_tag

        mov     [esi+TArray.count], ecx
        dec     ecx
        js      .skip_to_eol

        mov     eax, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        cmp     eax, tagUL
        jne     .skip_to_eol

        call    .close_tag
        mov     [esi+TArray.count], ecx
        jmp     .skip_to_eol


.code_block:

        DebugMsg "Code block detected!"

        mov     esi, [.pStack]
        mov     ecx, [esi+TArray.count]
        dec     ecx
        js      .add_new_code_block

        cmp     [esi+TArray.array + sizeof.TMDItem*ecx + TMDItem.type], tagCode
        je      .skip_to_eol

.add_new_code_block:

        mov     eax, tagCode
        call    .open_tag

;        add     [edx+TText.GapEnd], 4   ; the gap should be properly positioned here!
        jmp     .skip_to_eol


.open_new:
        cmp     ecx, [esi + TArray.count]       ; esi = indicators list
        jae     .skip_to_eol

        mov     eax, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.indent]
        mov     [.whspc], eax
        mov     eax, [esi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        call    .open_tag

        inc     ecx
        jmp     .open_new

; Here seems to be a good place for processing the inline formatting elements, together with the HTML encoding.

.skip_to_eol:

        xor     eax, eax
        mov     ecx, ebx
        add     ecx, [edx+TText.GapEnd]
        sub     ecx, [edx+TText.GapBegin]
        cmp     ecx, [edx+TText.Length]
        jae     .end_of_text

        mov     al, [edx+ecx]
        test    al, al
        jz      .end_of_text
        cmp     al, $0d
        je      .eol_found
        cmp     al, $0a
        je      .eol_found

; html encoding from here

        test    al, al          ; all values > 127 are unicode and should not be encoded.
        js      .next

        movzx   eax, byte [tbl_html+eax]
        test    al, al
        jns     .next           ; the same as above

        lea     esi, [eax+tbl_html]     ; the address of the replacement string.
        lodsb
        movzx   ecx, al         ; length

; insert the replacement html encoding from esi

        stdcall TextSetGapSize, edx, ecx
        stdcall TextMoveGap, edx, ebx
        inc     [edx+TText.GapEnd]      ; delete the previous char.

        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx
        add     ebx, ecx

        rep movsb

.next:
        inc     ebx
        jmp     .skip_to_eol


.eol_found:
        mov     ah, al
        xor     ah, $0d xor $0a

        inc     ecx
        inc     ebx
        cmp     [edx+ecx], ah
        jne     .start_of_line
        inc     ebx
        jmp     .start_of_line

.end_of_text:

; Close all not closed tags:

        stdcall TextMoveGap, edx, ebx
        mov     edi, [.pStack]
        mov     ecx, [edi+TArray.count]

.close_all:
        dec     ecx
        js      .finalize

        mov     eax, [edi + TArray.array + sizeof.TMDItem*ecx + TMDItem.type]
        call    .close_tag
        jmp     .close_all

.finalize:
        stdcall FreeMem, [.pStack]
;        stdcall FreeMem, [.pIList]

        mov     [esp + 4*regEDX], edx
        popad
        return


.open_tag:
        push    ecx esi edi
        push    eax eax
        call    .add_to_stack
        pop     eax
        call    .prefix_tag
        pop     eax
        call    .suffix_tag
        pop     edi esi ecx
        retn


.suffix_tag:
        lea     eax, [eax+TOneTag.pOpenS]
        jmp     .copy_tag

.close_tag:
        lea     eax, [eax+TOneTag.pClose]

.prefix_tag:
.copy_tag:
        push    ecx esi edi

        mov     esi, [eax]
        mov     ecx, [eax+4]
        test    esi, esi
        jz      .tag_copy_end

        add     ebx, ecx

        stdcall TextSetGapSize, edx, ecx
        mov     edi, [edx+TText.GapBegin]
        add     edi, edx
        add     [edx+TText.GapBegin], ecx

        rep movsb

.tag_copy_end:
        pop     edi esi ecx
        retn


.add_to_stack:
        push    edx eax [.whspc]
        stdcall AddArrayItems, [.pStack], 1
        mov     [.pStack], edx
        pop     [eax+TMDItem.indent]
        pop     [eax + TMDItem.type]
        pop     edx
        retn

endp






endmodule





Changes to freshlib/data/random.asm.

79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
        popad
        return

.error:
        stc
        popad
        return

endp



include '%TargetOS%/random.asm'



endmodule







<









79
80
81
82
83
84
85

86
87
88
89
90
91
92
93
94
        popad
        return

.error:
        stc
        popad
        return

endp



include '%TargetOS%/random.asm'



endmodule

Added freshlib/data/sha1.asm.































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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



; Computes the SHA-1 hash for the data in [.pData] with length in  [.len]
; The memory buffer in [.pData] must have size higher than [.len] and multiply of 64 bytes (512 bits)
;
;

proc DataSHA1, .pData, .len
.hash  rd 5     ; the h[0]..h[4] accumulators
.abcde rd 5     ; the chunk hashes
.words rd 80    ; the 80 dword chunks.
begin
        pushad

; init the accumulators
        mov     ecx, 4

.init_acc:
        mov     eax, [.sha1_h + 4*ecx]
        mov     [.hash+4*ecx], eax
        mov     [.abcde+4*ecx], eax
        dec     ecx
        jns     .init_acc

; pad the data

        mov     edx, [.len]

        mov     edi, [.pData]
        add     edi, edx
        lea     ecx, [edx+1]
        mov     al, $80
        stosb

        add     edx, $40
        and     edx, not $3f
        sub     ecx, edx
        neg     ecx

        sub     ecx, 8
        jge     @f
        add     ecx, $40
        add     edx, $40
@@:
        xor     eax, eax
        rep stosb
        stosd

        mov     eax, [.len]
        shl     eax, 3          ; in bits!!!
        bswap   eax
        stosd

; edx contains the padded length of the block. multiply of 64

        shr     edx, 2  ; the size in dwords
        mov     esi, [.pData]

.chunk_loop:

        xor     ebx, ebx

.get16dw:
        lodsd
        bswap   eax
        mov     [.words + 4*ebx], eax

        inc     ebx
        cmp     ebx, 16
        jne     .get16dw

.fill80:
        mov     eax, [.words + 4*ebx - 3*4]
        xor     eax, [.words + 4*ebx - 8*4]
        xor     eax, [.words + 4*ebx - 14*4]
        xor     eax, [.words + 4*ebx - 16*4]
        rol     eax, 1
        mov     [.words + 4*ebx], eax

        inc     ebx
        cmp     ebx, 80
        jne     .fill80

; process one chunk.

        xor     ebx, ebx

.process0_19:
        mov     ecx, [.abcde + 1*4]       ; b
        mov     eax, ecx
        not     ecx                     ; not b

        and     eax, [.abcde + 2*4]       ; c
        and     ecx, [.abcde + 3*4]       ; d
        or      ecx, eax                ; f

        mov     eax, [.abcde + 0*4]
        rol     eax, 5                  ; a rol 5
        lea     eax, [eax + ecx + $5A827999]; f + k
        add     eax, [.abcde + 4*4]       ; e
        add     eax, [.words + 4*ebx]     ; temp

        mov     ecx, [.abcde + 1*4]
        rol     ecx, 30
        push    eax            [.abcde + 0*4] ecx            [.abcde + 2*4] [.abcde + 3*4]
        pop     [.abcde + 4*4] [.abcde + 3*4] [.abcde + 2*4] [.abcde + 1*4] [.abcde + 0*4]

        inc     ebx
        cmp     ebx, 20
        jne     .process0_19

.process20_39:

        mov     ecx, [.abcde + 1*4]
        xor     ecx, [.abcde + 2*4]
        xor     ecx, [.abcde + 3*4]       ; f

        mov     eax, [.abcde + 0*4]
        rol     eax, 5
        lea     eax, [eax + ecx + $6ED9EBA1]
        add     eax, [.abcde + 4*4]
        add     eax, [.words + 4*ebx]     ; temp

        mov     ecx, [.abcde + 1*4]
        rol     ecx, 30
        push    eax [.abcde + 0*4] ecx [.abcde + 2*4] [.abcde + 3*4]
        pop     [.abcde + 4*4] [.abcde + 3*4] [.abcde + 2*4] [.abcde + 1*4] [.abcde + 0*4]

        inc     ebx
        cmp     ebx, 40
        jne     .process20_39


.process40_59:

        mov     ecx, [.abcde+1*4]       ; b
        mov     eax, ecx                ; b
        mov     edi, [.abcde+2*4]       ; c

        and     ecx, edi                ; b and c
        and     eax, [.abcde+3*4]       ; b and d
        and     edi, [.abcde+3*4]       ; c and d

        or      ecx, eax
        or      ecx, edi                ; f

        mov     eax, [.abcde+0*4]
        rol     eax, 5
        lea     eax, [eax+ecx+$8F1BBCDC]
        add     eax, [.abcde+4*4]
        add     eax, [.words+4*ebx]     ; temp

        mov     ecx, [.abcde+1*4]
        rol     ecx, 30
        push    eax [.abcde+0*4] ecx [.abcde+2*4] [.abcde+3*4]
        pop     [.abcde+4*4] [.abcde+3*4] [.abcde+2*4] [.abcde+1*4] [.abcde+0*4]

        inc     ebx
        cmp     ebx, 60
        jne     .process40_59

.process60_79:
        mov     ecx, [.abcde+1*4]
        xor     ecx, [.abcde+2*4]
        xor     ecx, [.abcde+3*4]       ; f

        mov     eax, [.abcde+0*4]
        rol     eax, 5
        lea     eax, [eax+ecx+$CA62C1D6]
        add     eax, [.abcde+4*4]
        add     eax, [.words+4*ebx]     ; temp

        mov     ecx, [.abcde+1*4]
        rol     ecx, 30
        push    eax [.abcde+0*4] ecx [.abcde+2*4] [.abcde+3*4]
        pop     [.abcde+4*4] [.abcde+3*4] [.abcde+2*4] [.abcde+1*4] [.abcde+0*4]

        inc     ebx
        cmp     ebx, 80
        jne     .process60_79

; the word array is processed

        mov     ecx, 4

.add_loop:
        mov     eax, [.abcde+4*ecx]
        add     eax, [.hash+4*ecx]
        mov     [.abcde+4*ecx], eax
        mov     [.hash+4*ecx], eax
        dec     ecx
        jns     .add_loop

        sub     edx, 16
        jnz     .chunk_loop


        stdcall GetMem, 20
        mov     edi, eax

        mov     eax, [.hash+0*4]
        mov     ebx, [.hash+1*4]
        mov     ecx, [.hash+2*4]
        mov     edx, [.hash+3*4]
        mov     esi, [.hash+4*4]

        bswap   eax
        bswap   ebx
        bswap   ecx
        bswap   edx
        bswap   esi

        mov     [edi], eax
        mov     [edi+4], ebx
        mov     [edi+8], ecx
        mov     [edi+12], edx
        mov     [edi+16], esi

        mov     [esp+4*regEAX], edi
        popad
        return

.sha1_h dd 0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0

endp



proc StrSHA1, .pSha1
begin
        push    ebx ecx esi

        mov     esi, [.pSha1]
        stdcall StrNew
        mov     ebx, eax
        mov     ecx, 20

.loop:
        movzx   eax, byte [esi]
        inc     esi

        stdcall NumToStr, eax, ntsHex or ntsFixedWidth or ntsUnsigned + 2
        stdcall StrCat, ebx, eax
        stdcall StrDel, eax
        dec     ecx
        jnz     .loop

        mov     eax, ebx

        pop     esi ecx ebx
        return
endp


Changes to freshlib/data/strlib.asm.

893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
...
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
....
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
....
2706
2707
2708
2709
2710
2711
2712

2713

2714

2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
....
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
; Compares two strings for greater, equal or less.
; Returns eax = 0 if the strings are equal.
;         eax = 1 if .str1 is grater than .str2
;         eax = -1 if .str1 is less than .str2
;***************************************************************************************
proc StrCompSort2, .str1, .str2, .fCaseSensitive
begin
        push    ebx ecx esi edi

        mov     eax, [.str1]
        mov     ecx, [.str2]

        cmp     eax, ecx
        je      .equal

................................................................................
        ja      .greater
        jb      .less

.equal:
        xor     eax, eax

.finish:
        pop     edi esi ecx ebx
        return

.greater:
        mov     eax, 1
        jmp     .finish

.less:
................................................................................
        cmp     al, $80
        jae     .store          ; it is a hack, but I hope save enough.

        mov     dl, al
        mov     bl, al
        shr     edx, 5
        and     ebx, $1f
        bt      dword [.URLCharTable+4*edx], ebx
        jnc     .store

        mov     ah, al
        mov     al, '%'
        stosb
        mov     al, ah
        shr     al, 4
................................................................................
        sub     edi, eax
        mov     [eax+string.len], edi

.finish:
        mov     eax, [.res]
        pop     edi esi edx ecx ebx
        return



; Contains 1 where the character must be % encoded and 0 where it is save to pass it directly

.URLCharTable db 11111111b       ;
              db 11111111b       ;
              db 11111111b       ;
              db 11111111b       ; 0..31 -control chars | encoded
              db 11111111b       ; $27 - $20: '&%$#"!   | encoded
              db 11111111b       ; $2f - $28: /.-,+*)(  | encoded
              db 00000000b       ; $37 - $30: 76543210  | not encoded
              db 11111100b       ; $3f - $38: ?>=<;:98  | partially
              db 00000001b       ; $47 - $40: GFEDCBA@  | partially
              db 00000000b       ; $4f - $48: ONMLKJIH  | not encoded
              db 00000000b       ; $57 - $50: WVUTSRQP  | not encoded
              db 11111000b       ; $5f - $58: _^]\[ZYX  | partially
              db 00000001b       ; $67 - $60: gfedcba`  | partially
              db 00000000b       ; $6f - $68: onmlkjih  | not encoded
              db 00000000b       ; $77 - $70: wvutsrqp  | not encoded
              db 11111000b       ; $7f - $78:  ~}|{zyx  | partially
endp


;_______________________________________________________________________
;
; proc StrURLDecode
;   Decodes URL encoded string. Does not change registers.
;   Notice, that the string is decoded in place.
................................................................................
proc IP2Str, .ip
begin
        push    ebx

        movzx   eax, byte [.ip+3]
        stdcall NumToStr, eax, ntsDec or ntsUnsigned
        mov     ebx, eax
        stdcall StrCharCat, ebx, '.'

        movzx   eax, byte [.ip+2]
        stdcall NumToStr, eax, ntsDec or ntsUnsigned
        stdcall StrCat, ebx, eax
        stdcall StrDel, eax
        stdcall StrCharCat, ebx, '.'

        movzx   eax, byte [.ip+1]
        stdcall NumToStr, eax, ntsDec or ntsUnsigned
        stdcall StrCat, ebx, eax
        stdcall StrDel, eax
        stdcall StrCharCat, ebx, '.'

        movzx   eax, byte [.ip]
        stdcall NumToStr, eax, ntsDec or ntsUnsigned
        stdcall StrCat, ebx, eax
        stdcall StrDel, eax

        mov     eax, ebx







|







 







|







 







|







 







>

>

>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|







 







|





|





|







893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
...
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
....
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
....
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
....
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
; Compares two strings for greater, equal or less.
; Returns eax = 0 if the strings are equal.
;         eax = 1 if .str1 is grater than .str2
;         eax = -1 if .str1 is less than .str2
;***************************************************************************************
proc StrCompSort2, .str1, .str2, .fCaseSensitive
begin
        push    ebx ecx edx esi edi

        mov     eax, [.str1]
        mov     ecx, [.str2]

        cmp     eax, ecx
        je      .equal

................................................................................
        ja      .greater
        jb      .less

.equal:
        xor     eax, eax

.finish:
        pop     edi esi edx ecx ebx
        return

.greater:
        mov     eax, 1
        jmp     .finish

.less:
................................................................................
        cmp     al, $80
        jae     .store          ; it is a hack, but I hope save enough.

        mov     dl, al
        mov     bl, al
        shr     edx, 5
        and     ebx, $1f
        bt      dword [URLCharTable+4*edx], ebx
        jnc     .store

        mov     ah, al
        mov     al, '%'
        stosb
        mov     al, ah
        shr     al, 4
................................................................................
        sub     edi, eax
        mov     [eax+string.len], edi

.finish:
        mov     eax, [.res]
        pop     edi esi edx ecx ebx
        return
endp


; Contains 1 where the character must be % encoded and 0 where it is save to pass it directly
if used URLCharTable
URLCharTable db 11111111b       ;
             db 11111111b       ;
             db 11111111b       ;
             db 11111111b       ; 0..31 -control chars | encoded
             db 11111111b       ; $27 - $20: '&%$#"!   | encoded
             db 11111111b       ; $2f - $28: /.-,+*)(  | encoded
             db 00000000b       ; $37 - $30: 76543210  | not encoded
             db 11111100b       ; $3f - $38: ?>=<;:98  | partially
             db 00000001b       ; $47 - $40: GFEDCBA@  | partially
             db 00000000b       ; $4f - $48: ONMLKJIH  | not encoded
             db 00000000b       ; $57 - $50: WVUTSRQP  | not encoded
             db 11111000b       ; $5f - $58: _^]\[ZYX  | partially
             db 00000001b       ; $67 - $60: gfedcba`  | partially
             db 00000000b       ; $6f - $68: onmlkjih  | not encoded
             db 00000000b       ; $77 - $70: wvutsrqp  | not encoded
             db 11111000b       ; $7f - $78:  ~}|{zyx  | partially
end if


;_______________________________________________________________________
;
; proc StrURLDecode
;   Decodes URL encoded string. Does not change registers.
;   Notice, that the string is decoded in place.
................................................................................
proc IP2Str, .ip
begin
        push    ebx

        movzx   eax, byte [.ip+3]
        stdcall NumToStr, eax, ntsDec or ntsUnsigned
        mov     ebx, eax
        stdcall StrCat, ebx, txt '.'

        movzx   eax, byte [.ip+2]
        stdcall NumToStr, eax, ntsDec or ntsUnsigned
        stdcall StrCat, ebx, eax
        stdcall StrDel, eax
        stdcall StrCat, ebx, txt '.'

        movzx   eax, byte [.ip+1]
        stdcall NumToStr, eax, ntsDec or ntsUnsigned
        stdcall StrCat, ebx, eax
        stdcall StrDel, eax
        stdcall StrCat, ebx, txt '.'

        movzx   eax, byte [.ip]
        stdcall NumToStr, eax, ntsDec or ntsUnsigned
        stdcall StrCat, ebx, eax
        stdcall StrDel, eax

        mov     eax, ebx

Changes to freshlib/data/uconfig.asm.

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
begin
        push    ebx ecx edx

        mov     eax, [.cfgFile]         ; name or handle
        test    eax, $ffff0000
        jz      .file_ok

        if defined options.DebugMode & options.DebugMode
           pushad
           stdcall StrPtr, eax
           stdcall Output, "Config file path: "
           stdcall Output, eax
           stdcall Output, cDbgNewLine
           popad
        end if

        stdcall FileOpenAccess, eax, faReadOnly
        jc      .error

        OutputValue "CfgGetStr file open success. Handle = ", eax, 16, 8

.file_ok:
        mov     ebx, eax

        stdcall FileSeek, ebx, 0, fsFromBegin

        stdcall StrDup, [.section]







|
|
|
|
|
|
|
|




|







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
begin
        push    ebx ecx edx

        mov     eax, [.cfgFile]         ; name or handle
        test    eax, $ffff0000
        jz      .file_ok

;        if defined options.DebugMode & options.DebugMode
;           pushad
;           stdcall StrPtr, eax
;           stdcall Output, "Config file path: "
;           stdcall Output, eax
;           stdcall Output, cDbgNewLine
;           popad
;        end if

        stdcall FileOpenAccess, eax, faReadOnly
        jc      .error

;        OutputValue "CfgGetStr file open success. Handle = ", eax, 16, 8

.file_ok:
        mov     ebx, eax

        stdcall FileSeek, ebx, 0, fsFromBegin

        stdcall StrDup, [.section]

Changes to freshlib/imports/Linux/libc.inc.

21
22
23
24
25
26
27
28

  stime                                   ,<NONE>,                      'stime',        \
  strerror_r                              ,<.errnum, .pBuff, .BufLen>,  'strerror_r',   \
  shmget                                  ,<.key, .size, .flags>,       'shmget',       \
  shmat                                   ,<.shmid, .addr, .flags>,     'shmat',        \
  shmdt                                   ,<.addr>,                     'shmdt',        \
  shmctl                                  ,<.shmid, .cmd, .shmid_ds>,   'shmctl',       \
  getaddrinfo                             ,<.pHostName, .pService, .pHints, .pVarResult>, 'getaddrinfo',        \
  freeaddrinfo                            ,<.pAddrInfo>,                                  'freeaddrinfo'








|
>
21
22
23
24
25
26
27
28
29
  stime                                   ,<NONE>,                      'stime',        \
  strerror_r                              ,<.errnum, .pBuff, .BufLen>,  'strerror_r',   \
  shmget                                  ,<.key, .size, .flags>,       'shmget',       \
  shmat                                   ,<.shmid, .addr, .flags>,     'shmat',        \
  shmdt                                   ,<.addr>,                     'shmdt',        \
  shmctl                                  ,<.shmid, .cmd, .shmid_ds>,   'shmctl',       \
  getaddrinfo                             ,<.pHostName, .pService, .pHints, .pVarResult>, 'getaddrinfo',        \
  freeaddrinfo                            ,<.pAddrInfo>,                                  'freeaddrinfo',       \
  crypt                                   ,<.pKey, .pSalt>,             'crypt'

Changes to freshlib/macros/Win32/_executable.inc.

63
64
65
66
67
68
69








70
71
72
73
74
75
76
      if (model eq )|(model eq default)
        section '.data' data readable writeable
      end if

      if ~(model eq manual)
        \IncludeAllGlobals
      end if








    end if
  \}

  section '.code' code readable writeable executable
}









>
>
>
>
>
>
>
>







63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
      if (model eq )|(model eq default)
        section '.data' data readable writeable
      end if

      if ~(model eq manual)
        \IncludeAllGlobals
      end if

      if type eq DLL
        section '.reloc' fixups data readable discardable
        DispSize "fixups section", $-$$
        if $=$$
          dd 0,8          ; if there are no fixups, generate dummy entry
        end if
      end if
    end if
  \}

  section '.code' code readable writeable executable
}


Changes to freshlib/macros/_display.inc.

33
34
35
36
37
38
39

40



41
42
43
44
45
46
47
..
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
macro _disp arg1, arg2
{
   if arg2 eq
    display arg1
   else
    local ..tmp, ..val


    ..tmp = arg1




    virtual at 0
     repeat 32
      if ..tmp > 0
       db ..tmp mod arg2
       ..tmp = ..tmp / arg2
      end if
................................................................................
   end if
}

macro disp [arg] { _disp arg }


macro DispSize Text, Sz {
  local size, suffix
  size = Sz;
  if defined options.ShowSizes & options.ShowSizes
    if ~(defined options.SkipZeroSizes & options.SkipZeroSizes & Sz=0)
      if Text eqtype "A"
        disp 3,"Sizeof [", Text, "] is: "
      else
        disp 3,"Sizeof [", `Text, "] is: "
      end if

      suffix = 'bytes'
      if Sz>10000
        size = size / 1024
        disp <size,10>, 'K',$0d,$0a
      else
        disp <size,10>, ' bytes.',$0d,$0a
      end if
    end if
  end if
}







>
|
>
>
>







 







|









<









33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
..
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
macro _disp arg1, arg2
{
   if arg2 eq
    display arg1
   else
    local ..tmp, ..val

    if arg1 relativeto 0
      ..tmp = arg1
    else
      ..tmp = rva arg1
    end if

    virtual at 0
     repeat 32
      if ..tmp > 0
       db ..tmp mod arg2
       ..tmp = ..tmp / arg2
      end if
................................................................................
   end if
}

macro disp [arg] { _disp arg }


macro DispSize Text, Sz {
  local size
  size = Sz;
  if defined options.ShowSizes & options.ShowSizes
    if ~(defined options.SkipZeroSizes & options.SkipZeroSizes & Sz=0)
      if Text eqtype "A"
        disp 3,"Sizeof [", Text, "] is: "
      else
        disp 3,"Sizeof [", `Text, "] is: "
      end if


      if Sz>10000
        size = size / 1024
        disp <size,10>, 'K',$0d,$0a
      else
        disp <size,10>, ' bytes.',$0d,$0a
      end if
    end if
  end if
}

Changes to freshlib/macros/_globals.inc.

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
      rb size
  \}
  match U, UGlobals \{
    U
  \}
}


; Safe fallback of the strings data macros.

;macro __InitStringsEngine {
;  local r,v,c
;  c equ 0
;
;  struc text [val] \{
;    \common
;      if ~val eqtype 1
;      virtual at 0
;    \forward
;        db val
;    \common
;      .length = $
;      end virtual
;      end if
;    rept 1 x:c+1 \\{
;      c equ x
;      r\\#x equ .
;      v\\#x equ val
;    \\}
;  \}
;
;  macro __IncludeAllStrings \{
;    \common
;    \local ..start, len, i, j, f, ch1, ch2
;    label ..start byte
;    if defined options.ShowSizes and options.ShowSizes
;      disp 3, 'String data address: $', <..start, 16>, 13
;      disp 3, 'String count: ', <c, 10>, 13
;    end if
;
;    rept c x \\{
;      if used r\\#x
;        if ~v\\#x eqtype 1
;          r\\#x db v\\#x, 0
;        else
;          label r\\#x byte at v\\#x
;        end if
;      end if
;    \\}
;  \}
;}


macro __InitStringEngine {
local ..hashtable, ..strbase, strlist

  virtual at 0
    ..hashtable::
         dd 65536 dup ($ffffffff)







<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<







61
62
63
64
65
66
67













































68
69
70
71
72
73
74
      rb size
  \}
  match U, UGlobals \{
    U
  \}
}















































macro __InitStringEngine {
local ..hashtable, ..strbase, strlist

  virtual at 0
    ..hashtable::
         dd 65536 dup ($ffffffff)

Added freshlib/macros/_minihash.inc.





























































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
; Pearsons hash function table:

struct TPHashItem
  .pKeyname    dd ?     ; pointer to the key name. on [.pKeyname-1] is 1 byte key length.
  .Value       dd ?     ; user defined value of the hash table cell.
ends

;
; Builds static Pearsons hash table.
; The table contains 256 elements of TPHashItem (in total 2048 bytes)
;
; Arguments:
;   table - the lable of the hash table.
;   Pfunc - table of 256 random (or not random) bytes - values of the Pearsons hash function.
;   key   - string that to be hashed. Pointer to this string is placed in the .pKeyname field of
;           the respective record of the hash table. The hash of the key is computed case insensitive.
;   value - user data. Placed in the .Value field of the respective TPHashItem element.
;
; Notice, that the table does not resolve hash collisions. If collision happens, the compilation ends
; with error. The conflict can be resolved in two ways - changing the hash function in order to
; avoid collisions, or changing the key that causes collision.
;
macro PHashTable table, Pfunc, [key, value] {
common
if used table
  local ..error
  ..error = 0

  if defined options.ShowHashes & options.ShowHashes
    disp 3, 'Hash table "', `table, '"', 9
  end if

  table dd 256 dup(0,0)

forward
local ..keynm, ..len, ..hash, ..char, ..prev

          db ..len
  ..keynm db key
  ..len = $ - ..keynm
         db 0

  if value
    ..hash = 0
    repeat ..len
      load ..char byte from ..keynm + % - 1
      if ..char and $40
        ..char = ..char or $20
      end if
      ..hash = ..hash xor ..char
      load ..hash byte from Pfunc + ..hash
      store byte ..char at ..keynm + % - 1
    end repeat

    if defined options.ShowHashes & options.ShowHashes
      disp 3,'Keyword hash : ', <..hash, 10>, ' on "', key, '"', 10
    end if

    load ..prev dword from table + ..hash * 8

    if ..prev = 0
      store dword ..keynm at table + ..hash * 8
      store dword value at table + ..hash * 8 + 4
    else
      disp 2,'Hash collision: ', <..hash, 10>, ' on "', key, '"', 10
      ..error = 1
    end if
  end if

common
  if defined options.ShowHashes & options.ShowHashes
    disp 6, '---', 13
  end if
  assert ~..error

  end if
}

;
; Computes the Pearsons hash in the same way as PHashTable macro.
; Useful for defining constants with the hash value of some key.
;
; usage:
;   MyHashConst phash PFUNK, "my_string"
;
struc phash Pfunc, key {
local ..keynm, ..len, ..hash, ..char

  virtual at 0
    ..keynm::
      db key
    ..len = $
  end virtual

  ..hash = 0
  repeat ..len
    load ..char byte from ..keynm:(% - 1)
    if ..char and $40
      ..char = ..char or $20
    end if
    ..hash = ..hash xor ..char
    load ..hash byte from Pfunc + ..hash
  end repeat

  if defined options.ShowHashes & options.ShowHashes
    disp 3,'Keyword hash : ', <..hash, 10>, ' on "', key, '"', 10
  end if

  . = ..hash
}

Changes to freshlib/macros/allmacros.inc.

18
19
20
21
22
23
24

25
26
include '_struct.inc'
include '_display.inc'
include '_itext.inc'
include '_globals.inc'
include '_datamacros.inc'
include '_dispatch.inc'
include '_realobjects.inc'


include '%TargetOS%/allmacros.inc'







>


18
19
20
21
22
23
24
25
26
27
include '_struct.inc'
include '_display.inc'
include '_itext.inc'
include '_globals.inc'
include '_datamacros.inc'
include '_dispatch.inc'
include '_realobjects.inc'
include '_minihash.inc'

include '%TargetOS%/allmacros.inc'

Changes to freshlib/simpledebug/Linux/debug.asm.

58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74



75
76
77
78
79
80
81


; returns dword timestamp in uS

proc GetFineTimestamp
begin
        pushad
        mov     eax, $4e        ; sys_gettimeofday
        mov     ebx, _timestamp
        xor     ecx, ecx
        int     $80

        mov     eax,dword [_timestamp]  ; seconds
        mov     ecx,1000000
        mul     ecx
        mov     ebx,eax
        mov     eax,dword [_timestamp+4]



        add     eax, ebx

        mov     [esp+4*regEAX], eax
        popad
        return

endp







|
|
|


|
|

|
|
>
>
>







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


; returns dword timestamp in uS

proc GetFineTimestamp
begin
        pushad
        mov     eax, $109       ; sys_clock_gettime
        mov     ebx, 2          ; CLOCK_PROCESS_CPUTIME_ID
        mov     ecx, _timestamp
        int     $80

        mov     eax, dword [_timestamp]  ; seconds
        mov     ecx, 1000000
        mul     ecx
        mov     ebx, eax
        mov     eax, dword [_timestamp+4]
        mov     ecx, 1000
        cdq
        div     ecx
        add     eax, ebx

        mov     [esp+4*regEAX], eax
        popad
        return

endp

Changes to freshlib/sqlite/sqlite.asm.

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
;-------------------------------------------------------------------
; Procedures for convenient work with SQLite database angine.
;-------------------------------------------------------------------




;-------------------------------------------------------------------
; If the file in [.ptrFileName] exists, the function opens it.
; if the file does not exists, new database is created and the
; initialization script from [.ptrInitScript] is executed on it.
;
; Returns:
;    CF: 0 - database was open successfully
;      eax = 0 - Existing database was open successfuly.
;      eax = 1 - New database was created and init script was
;                executed successfully.
;      eax = 2 - New database was created but init script exits
;                with error.
;    CF: 1 - the database could not be open. (error)
;-------------------------------------------------------------------
proc OpenOrCreate, .ptrFileName, .ptrDatabase, .ptrInitScript
   .hSQL dd ?
.ptrNext dd ?
begin
        push    esi ebx

        mov     esi, [.ptrDatabase]
        cinvoke sqliteOpen, [.ptrFileName], esi

        test    eax, eax
        jz      .openok


        stc
        pop     esi
        return

.openok:
        xor     ebx, ebx
        cinvoke sqliteExec, [esi], .sqlCheckEmpty, .AbortCallback, ebx, ebx






        cmp     eax, SQLITE_ABORT

        je      .finish

        cinvoke sqliteExec, [esi], [.ptrInitScript], -1, ebx, ebx, ebx
        inc     ebx

        test    eax, eax     ; SQLITE_OK
        jz      .finish

        inc     ebx

.finish:
        cinvoke sqliteFinalize, [.hSQL]
        mov     eax, ebx
        clc
        pop     ebx esi
        return

.AbortCallback:
        xor     eax, eax
        inc     eax
        retn

.sqlCheckEmpty db 'select * from sqlite_master',0

endp

|

>
>
>









|
<
|
<




<

|


<
>


>
>

|




|
>
>
>
>
>
>
|
>
|

<

>
|





<


|

<
<
<
<
<
<
<
<

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
;-------------------------------------------------------------------
; Procedures for convenient work with SQLite database engine.
;-------------------------------------------------------------------

sqlCheckEmpty text 'select count() from sqlite_master'


;-------------------------------------------------------------------
; If the file in [.ptrFileName] exists, the function opens it.
; if the file does not exists, new database is created and the
; initialization script from [.ptrInitScript] is executed on it.
;
; Returns:
;    CF: 0 - database was open successfully
;      eax = 0 - Existing database was open successfuly.
;      eax = 1 - New database was created and init script was executed successfully.

;      eax = 2 - New database was created but init script exits with error.

;    CF: 1 - the database could not be open. (error)
;-------------------------------------------------------------------
proc OpenOrCreate, .ptrFileName, .ptrDatabase, .ptrInitScript
   .hSQL dd ?

begin
        push    edi esi ebx

        mov     esi, [.ptrDatabase]

        cinvoke sqliteOpen_v2, [.ptrFileName], esi, SQLITE_OPEN_READWRITE or SQLITE_OPEN_CREATE or SQLITE_OPEN_FULLMUTEX, 0
        test    eax, eax
        jz      .openok

.error:
        stc
        pop     ebx esi edi
        return

.openok:
        xor     ebx, ebx
        lea     eax, [.hSQL]
        cinvoke sqlitePrepare_v2, [esi], sqlCheckEmpty, sqlCheckEmpty.length, eax, 0

        cinvoke sqliteStep, [.hSQL]
        cinvoke sqliteColumnInt, [.hSQL], 0
        push    eax
        cinvoke sqliteFinalize, [.hSQL]
        pop     eax
        test    eax, eax
        jnz     .finish


        inc     ebx
        cinvoke sqliteExec, [esi], [.ptrInitScript], NULL, NULL, NULL
        test    eax, eax
        jz      .finish

        inc     ebx

.finish:

        mov     eax, ebx
        clc
        pop     ebx esi edi
        return








endp

Changes to freshlib/system/Linux/_process_native.asm.

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

.loop1:
        mov     dl, 1
        xchg    byte [ebx+TMutex.value], dl
        test    dl, dl
        jz      .lock_ok

        rep nop                         ; ???
        loop    .loop1

; Set up syscall details
        mov      edx, MUTEX_LOCK_CONTEND
        mov      ecx, FUTEX_WAIT or FUTEX_PRIVATE_FLAG
        xor      esi, esi        ; timeout
        jmp      .check







|







160
161
162
163
164
165
166
167
168
169
170
171
172
173
174

.loop1:
        mov     dl, 1
        xchg    byte [ebx+TMutex.value], dl
        test    dl, dl
        jz      .lock_ok

        pause   ; causes performance problems in the newest Intel Skylake CPU
        loop    .loop1

; Set up syscall details
        mov      edx, MUTEX_LOCK_CONTEND
        mov      ecx, FUTEX_WAIT or FUTEX_PRIVATE_FLAG
        xor      esi, esi        ; timeout
        jmp      .check

Changes to freshlib/system/Linux/_process_posix.asm.

38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

body FreeThreadID
begin
        return
endp



body Exec
begin
;sys_forkd
        return
endp



; MUTEX functions
;_________________________________________________________________________________________


;struct TMutex







<
<
<
<
<
<
<







38
39
40
41
42
43
44







45
46
47
48
49
50
51

body FreeThreadID
begin
        return
endp











; MUTEX functions
;_________________________________________________________________________________________


;struct TMutex

Changes to freshlib/system/Linux/files.asm.

597
598
599
600
601
602
603




























604
605
606
607
608
609
610
        return

.exists:
        clc
        popad
        return
endp

































flushInput = 0
flushOutput = 1







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
        return

.exists:
        clc
        popad
        return
endp




body CreatePipe
.hRead  dd ?
.hWrite dd ?
begin
        push    ebx

        mov     eax, sys_pipe
        lea     ebx, [.hRead]
        int     $80
        neg     eax
        jnz     .err

        clc
        mov     eax, [.hRead]
        mov     edx, [.hWrite]
        pop     ebx
        return

.err:
        stc
        pop     ebx
        return
endp






flushInput = 0
flushOutput = 1

Changes to freshlib/system/Linux/process.asm.

41
42
43
44
45
46
47





48
49
50
51
52
53
54
...
224
225
226
227
228
229
230










231
232
233
234
235
236
237
238






















239
240
241
242
243
244
245
...
267
268
269
270
271
272
273




274


275









































































































































































276
277
278
279
280
281
282

        DebugMsg "Switch to 3GB address space."

        mov     eax, sys_personality
        mov     ebx, PER_LINUX32_3GB
        int     $80






; and restart the process

        mov     eax, [esp+4]          ; argument count
        mov     ebx, [esp+8]          ; the first argument is the name of the program.
        lea     ecx, [esp+8]          ; the arguments list.
        lea     edx, [ecx+4*eax+4]    ; the environment list.

................................................................................
        popad
        return
endp



if used __ExceptionHandler











__ExceptionHandler:

        lea     esi, [esp+8]    ; the start of sig_context structure.

        stdcall FileOpenAccess, "fault.txt", faCreateAlways or faWriteOnly
        mov     ebx, eax























        mov     eax, [esi+sig_context.eip]

        stdcall NumToStr, eax, ntsHex or ntsUnsigned or ntsFixedWidth + 8
        stdcall FileWriteString, ebx, eax
        stdcall FileWriteString, ebx, <txt 13, 10, 13, 10>

        lea     ebp, [esi+sig_context.ebp]
................................................................................
        retn


end if


























































































































































































match =LinuxThreads, LinuxThreads {







>
>
>
>
>







 







>
>
>
>
>
>
>
>
>
>








>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







>
>
>
>

>
>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
...
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
...
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

        DebugMsg "Switch to 3GB address space."

        mov     eax, sys_personality
        mov     ebx, PER_LINUX32_3GB
        int     $80

        OutputValue "sys_personality return EAX:", eax, 10, -1

        test    eax, eax
        js      .finish         ; error set personality; try this way...???

; and restart the process

        mov     eax, [esp+4]          ; argument count
        mov     ebx, [esp+8]          ; the first argument is the name of the program.
        lea     ecx, [esp+8]          ; the arguments list.
        lea     edx, [ecx+4*eax+4]    ; the environment list.

................................................................................
        popad
        return
endp



if used __ExceptionHandler

__reg_names db  'eax', sig_context.eax
            db  'ebx', sig_context.ebx
            db  'ecx', sig_context.ecx
            db  'edx', sig_context.edx
            db  'esi', sig_context.esi
            db  'edi', sig_context.edi
            db  'ebp', sig_context.ebp
            db  'esp', sig_context.esp
            dd  0

__ExceptionHandler:

        lea     esi, [esp+8]    ; the start of sig_context structure.

        stdcall FileOpenAccess, "fault.txt", faCreateAlways or faWriteOnly
        mov     ebx, eax

; Write the registers to the file:

        mov     edi, __reg_names

.reg_loop:
        cmp     byte [edi], 0
        je      .dump_stack

        stdcall FileWrite, ebx, edi, 3
        stdcall FileWrite, ebx, txt " = ", 3

        movzx   eax, byte [edi+3]
        stdcall NumToStr, [esi + eax], ntsHex or ntsUnsigned or ntsFixedWidth + 8
        stdcall FileWriteString, ebx, eax
        stdcall FileWriteString, ebx, <txt 13, 10>
        add     edi, 4
        jmp     .reg_loop

; Write the stack backtrace to the file:

.dump_stack:
        stdcall FileWriteString, ebx, <txt 13, 10>
        mov     eax, [esi+sig_context.eip]

        stdcall NumToStr, eax, ntsHex or ntsUnsigned or ntsFixedWidth + 8
        stdcall FileWriteString, ebx, eax
        stdcall FileWriteString, ebx, <txt 13, 10, 13, 10>

        lea     ebp, [esi+sig_context.ebp]
................................................................................
        retn


end if



body Exec2
.pArgs dd ?
begin
        pushad

        stdcall StrSplitArg, [.hCommand]
        mov     [.pArgs], eax

        mov     eax, sys_fork
        int     $80

        test    eax, eax
        jnz     .parent         ; this is the parent process

; here is the child.

        DebugMsg "Child process here!"

        mov     eax, sys_dup2
        mov     ebx, [.hStdIn]
        xor     ecx, ecx
        int     $80

        mov     eax, sys_dup2
        mov     ebx, [.hStdOut]
        inc     ecx
        int     $80

        mov     eax, sys_dup2
        mov     ebx, [.hStdErr]
        inc     ecx
        int     $80

        mov     ecx, [.pArgs]
        mov     ebx, [ecx]                      ; the first argument is the executable name.

        mov     edx, [_ptrEnvironment]
        mov     eax, [edx]                      ; argument count
        lea     edx, [edx + 4*eax + 8]          ; +4 for argc and +4 for the termination 0; edx points to the env table here.

        mov     eax, sys_execve
        int     $80

        OutputValue "Error Exec2: ", eax, 10, -1

        stc
        mov     [esp+4*regEAX], eax
        popad
        return

.parent:
        stdcall FreeMem, [.pArgs]
        bt      eax, 31
        mov     [esp+4*regEAX], eax
        popad
        return
endp



body WaitProcessExit
begin
        pushad
        mov     eax, sys_waitpid
        mov     ebx, [.ProcessID]
        xor     ecx, ecx
        xor     edx, edx
        int     $80
        clc
        popad
        return
endp



; Splits the string on spaces and forms one-memory-block area that contains, zero terminated
; array of pointers to every part of the string.
; The spaces inside double or single quotes are ignored.
;
; The format of the memory block is the same as used in the sys_execve system function.

proc StrSplitArg, .hCommand
begin
        pushad

if defined StrLib & StrLib
        stdcall StrPtr, [.hCommand]
        mov     esi, eax
        mov     ebx, eax
else
        mov     esi, [.hCommand]
        mov     ebx, esi
end if
        xor     ecx, ecx
        xor     edx, edx

; find the maximal possible count of the arguments (edx)
; and the length of the string.

.loop1:
        lodsb
        test    al, al
        jz      .endstr1
        inc     ecx
        cmp     al, ' '
        jne     .loop1
        inc     edx
        jmp     .loop1

.endstr1:
        mov     esi, ebx

        add     edx, 3
        lea     edx, [4*edx + ecx]
        add     edx, 3
        and     edx, $fffffffc

        stdcall GetMem, edx
        mov     ebx, eax

        sub     edx, ecx
        sub     edx, 4
        and     edx, $fffffffc

        lea     edi, [ebx+edx]
        rep movsb

        mov     edi, ebx
        lea     esi, [ebx+edx]  ; start of the string.

.search_loop:
        mov     [edi], esi
        add     edi, 4

.loop_to_space:
        lodsb
        test    al, al
        jz      .end_of_string

        cmp     al, '"'
        je      .skip_quoted

        cmp     al, "'"
        je      .skip_quoted

        cmp     al, ' '
        jne     .next

        cmp     ah, ' '
        je      .loop_to_space

        mov     byte [esi-1], 0
        jmp     .search_loop

.next:
        mov     ah, al
        jmp     .loop_to_space


.skip_quoted:
        mov     ah, al

.loop_quote:
        lodsb
        test    al, al
        jz      .end_of_string

        cmp     al, ah
        jne     .loop_quote
        jmp     .loop_to_space


.end_of_string:
        mov     [esp+4*regEAX], ebx
        popad
        return
endp






match =LinuxThreads, LinuxThreads {

Changes to freshlib/system/Win32/process.asm.

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
        pop     edx ecx eax
        return
endp




body Exec
.startup STARTUPINFO
.process PROCESS_INFORMATION
begin
        push    ecx esi edi

        stdcall utf8ToWideChar, [.hCommand]
        mov     esi, eax
................................................................................

        lea     edi, [.startup]
        mov     ecx, (sizeof.STARTUPINFO + sizeof.PROCESS_INFORMATION)/4
        xor     eax, eax
        rep     stosd

        mov     [.startup.cb], sizeof.STARTUPINFO








        lea     ecx, [.startup]
        lea     edx, [.process]
        invoke  CreateProcessW, 0, esi, 0, 0, FALSE, NORMAL_PRIORITY_CLASS, 0, 0, ecx, edx
        stdcall FreeMem, esi
        test    eax, eax
        jz      .error

        mov     eax, [.process.hProcess]
        mov     edx, [.process.hThread]








|







 







>
>
>
>
>
>
>



|







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
        pop     edx ecx eax
        return
endp




body Exec2
.startup STARTUPINFO
.process PROCESS_INFORMATION
begin
        push    ecx esi edi

        stdcall utf8ToWideChar, [.hCommand]
        mov     esi, eax
................................................................................

        lea     edi, [.startup]
        mov     ecx, (sizeof.STARTUPINFO + sizeof.PROCESS_INFORMATION)/4
        xor     eax, eax
        rep     stosd

        mov     [.startup.cb], sizeof.STARTUPINFO
        mov     [.startup.dwFlags], STARTF_USESTDHANDLES
        mov     eax, [.hStdIn]
        mov     ecx, [.hStdOut]
        mov     edx, [.hStdErr]
        mov     [.startup.hStdInput], eax
        mov     [.startup.hStdOutput], ecx
        mov     [.startup.hStdError], edx

        lea     ecx, [.startup]
        lea     edx, [.process]
        invoke  CreateProcessW, 0, esi, 0, 0, TRUE, NORMAL_PRIORITY_CLASS, 0, 0, ecx, edx
        stdcall FreeMem, esi
        test    eax, eax
        jz      .error

        mov     eax, [.process.hProcess]
        mov     edx, [.process.hThread]

Changes to freshlib/system/files.asm.

80
81
82
83
84
85
86




87
88
89
90
91
92
93
...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
interface GetFileInfo, .hFile, .ptrFileInfo

interface FileExists, .hFileName

interface CreateDirectory, .DirName







;--------------------------------------
; if CF = 0, eax is handle to the file.
; if CF = 1, eax is error code.
;--------------------------------------
proc FileOpen, .filename
begin
................................................................................





;--------------------------------------------------------------------
; Allocates needed memory and loads the entire file in this memory.
; Returns pointer to memory block or NULL if there is error.
; Returns size in ecx
;--------------------------------------------------------------------
proc LoadBinaryFile, .ptrFileName
begin
        push    esi edi ebx

        stdcall FileOpenAccess, [.ptrFileName], faReadOnly
        jc      .error







>
>
>
>







 







|
|







80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
...
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
interface GetFileInfo, .hFile, .ptrFileInfo

interface FileExists, .hFileName

interface CreateDirectory, .DirName


; returns the read handle in eax and write handle in edx

interface CreatePipe


;--------------------------------------
; if CF = 0, eax is handle to the file.
; if CF = 1, eax is error code.
;--------------------------------------
proc FileOpen, .filename
begin
................................................................................





;--------------------------------------------------------------------
; Allocates needed memory and loads the entire file in this memory.
; Returns pointer to memory block or error code.
; Returns size in ecx or NULL if there is error.
;--------------------------------------------------------------------
proc LoadBinaryFile, .ptrFileName
begin
        push    esi edi ebx

        stdcall FileOpenAccess, [.ptrFileName], faReadOnly
        jc      .error

Changes to freshlib/system/network.asm.

146
147
148
149
150
151
152

153
154
155
156
157
158
159
;        OutputValue "SendAll time: ", eax, 10, -1

        clc
        popad
        return

.finish_err:

        mov     [esp+4*regEAX], eax
        popad
        return
endp










>







146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
;        OutputValue "SendAll time: ", eax, 10, -1

        clc
        popad
        return

.finish_err:
        stc
        mov     [esp+4*regEAX], eax
        popad
        return
endp



Changes to freshlib/system/process.asm.

27
28
29
30
31
32
33
34



35
36
37
38
39
40
41
..
43
44
45
46
47
48
49













50
51
52
53
54
55
56
57
58
59
60
61


interface ThreadCreate, .ptrFunction, .ptrArguments

interface FreeThreadID, .ThreadID


interface Exec, .hCommand




interface FreeProcessID, .ProcessID

interface WaitProcessExit, .ProcessID, .timeout



................................................................................

interface WaitForMutex, .ptrMutex, .timeout

interface MutexRelease, .ptrMutex

interface MutexDestroy, .ptrMutex















include '%TargetOS%/process.asm'


if used ThreadCreate
  options.Threads = 1
else
  options.Threads = 0
end if


endmodule







|
>
>
>







 







>
>
>
>
>
>
>
>
>
>
>
>
>










<

27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
..
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


interface ThreadCreate, .ptrFunction, .ptrArguments

interface FreeThreadID, .ThreadID


; Executes the command .hCommand, gets the STDIN from [.hStdIn] file handle,
; Outputs the STDOUT to [.hStdOut] file handle and STDERR to [.hStdErr] handle.

interface Exec2, .hCommand, .hStdIn, .hStdOut, .hStdErr

interface FreeProcessID, .ProcessID

interface WaitProcessExit, .ProcessID, .timeout



................................................................................

interface WaitForMutex, .ptrMutex, .timeout

interface MutexRelease, .ptrMutex

interface MutexDestroy, .ptrMutex


; Simple wrapper on Exec2;
; See Exec2

proc Exec, .hCommand
begin
        stdcall Exec2, [.hCommand], [STDIN], [STDOUT], [STDERR]
        return
endp





include '%TargetOS%/process.asm'


if used ThreadCreate
  options.Threads = 1
else
  options.Threads = 0
end if


endmodule

Added freshlib/test_code/TestBBCode/angry.gif.

cannot compute difference between binary files

Added freshlib/test_code/TestBBCode/anon.svg.





>
>
1
2
<?xml version="1.0" encoding="UTF-8"?>
<svg width="36" height="36" version="1.1" viewBox="0 0 20.135672 20.135672" xml:space="preserve" xmlns="http://www.w3.org/2000/svg"><g id="Anonym__x2F__Guy_Fawkes" transform="scale(.0703)"><path d="m169 34.6c2.69 1.96 8.78 1.77 12.3-2.43 1.98-2.38 19.1-20.3 43.1-20.3 30.8 0 42.3 10.3 42.3 10.3-11-19.8-34.6-22.2-41.5-22.2-33.5 0-52.6 16-57.6 22.7-4.98 6.77-1.02 10.1 1.43 11.9zm94.9 54.9c-4.3 0-4.48 7.02-4.48 8.95-2e-3 12.6-7.11 24.9-13.4 31.3-18.2 18.6-47.9 2.26-47.9 2.26-5.02-2.03 0.445 3 0.445 3 2.86 2.96 11 12.1 27.1 14.1-0.743 0.786-20 20.9-20 20.9h-17.9l-17.9-26.8c-4.76 6.29-8.95 8.95-17.9 8.95-4.91 0-10.5-0.84-14.4-5.17-3.68-4.07-3.5-9.43-3.5-12.7-0.018-13.4 0-47.6 0-53.7v-15.4c0-7.83-8.79-15-8.79-15-10.4-7.16-17.8 8.97-17.8 8.97l-0.114 0.087c-8.58-3.12-17.7-5.02-27.2-5.02-11.7 0-25.4 1.28-35.6 5.8v-6.25c0-64.1 69.2-22.1 78.9-18.6 9.09 3.23 7.25-7.22 5.67-8.96-21.8-24.1-58.9-26.1-58.9-26.1-50.7 0-52.5 49.6-52.5 71.6v46.5c0 56.8 72.4 154 117 162-0.218 0.104 4.78 1.28 6.79 2.74 0.078 0.053 0.209 0 0.28 0.044 2.59 2.16 5.85 3.59 9.51 3.59 3.56 0 6.69-1.38 9.22-3.41 0 0 3.16-1.83 7.81-3.32 5.31-1.41 14.4-6.93 20.1-11.5 22.9-18.4 46.1-46.9 62.2-87.9 0 0 2.16-9.66-0.612-10.2-2.46-0.499-4.1 0.035-10.8 12-25.4 45.7-60.7 76.6-78.7 77.5v-35.8c11.4-1.76 31.7-10.8 45.5-23.3 8.95-8.14-7.11-6.28-9.68-5.24-11 4.42-13.2 10.6-44.7 10.6-30.1 0-48.1-12.7-53.7-17.9h35.8l17.9-17.9 17.9 17.9h35.8l32.6-38.6c27.3-2.42 30.2-34.1 30.2-37.4v-13.4c0-2.11-0.072-8.95-4.48-8.95zm-165 118c24 15.1 36.5 15 44.3 16.2v35.8c-42.4-10.8-44.8-41.5-44.3-52zm14.2-93.9c-6.68 29.5-40.1 29.5-40.1 29.5-19.8 0-28.3-10.2-28.3-37v-39.8c10.2 4.53 23.9 8.38 35.6 8.38 10.3 0 20.2-2.13 29.4-5.76 0.026 0 0.051 9e-3 0.051 9e-3 3.38 0 5.27 2.65 5.27 2.65s5.52 9.08-1.92 42zm63.8-49.2c11.9 6.39 25.1 10.3 39.2 10.3 14.1 0 27.4-3.92 39.3-10.3-11.9-6.39-25.1-10.3-39.3-10.3-14.1 0-27.3 3.92-39.2 10.3z" clip-rule="evenodd" fill="#3f3f3f" fill-rule="evenodd"/></g></svg>

Added freshlib/test_code/TestBBCode/bbcode.asm.































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
; _______________________________________________________________________________________
;|                                                                                       |
;| ..:: Fresh IDE ::..  template project.                                                |
;|_______________________________________________________________________________________|
;
;  Description: FreshLib portable console application.
;
;  Target OS: Any, supported by FreshLib
;
;  Dependencies: FreshLib
;
;  Notes:
;_________________________________________________________________________________________

include "%lib%/freshlib.inc"

@BinaryType console, compact

options.DebugMode = 0
options.ShowSizes = 1

EMOTICONS_PATH equ './'
include "%lib%/freshlib.asm"

include "hash_function.asm"


FILE_READ_CHUNK = 4096

rb 237

start:
        InitializeAll

        or      ecx, -1
        shr     ecx, 3
.hot:
        dec     ecx
        jnz     .hot

        stdcall TextCreate, sizeof.TText
        mov     edx, eax

.read_loop:
        stdcall TextMoveGap, edx, -1
        stdcall TextSetGapSize, edx, FILE_READ_CHUNK
        mov     eax, [edx+TText.GapBegin]
        add     eax, edx

        stdcall FileRead, [STDIN], eax, FILE_READ_CHUNK
        jc      .translate

        test    eax, eax
        jz      .translate

        add     [edx+TText.GapBegin], eax
        jmp     .read_loop

.translate:

        stdcall GetFineTimestamp
        push    eax

        stdcall TranslateBBCode, edx, 0

        stdcall GetFineTimestamp
        pop     ecx
        sub     eax, ecx

        stdcall NumToStr, eax, ntsDec or ntsUnsigned
        push    eax eax

        stdcall FileWriteString, [STDERR], 'TranslateBBCode time: '
        stdcall FileWriteString, [STDERR] ; from the stack
        stdcall StrDel ; from the stack
        stdcall FileWriteString, [STDERR], <txt ' us', 13, 10>


        stdcall TextCompact, edx

;        stdcall FileWrite, [STDOUT], cPrefix, cPrefix.length
;        stdcall FileWriteString, [STDOUT], edx

.finish:
        push    eax
        FinalizeAll
        stdcall TerminateAll ; from the stack

iglobal
  cPrefix file "prefix.tpl"
  .length = $ - cPrefix
          dd 0
endg


Added freshlib/test_code/TestBBCode/bbcode.fpr.

cannot compute difference between binary files

Added freshlib/test_code/TestBBCode/cry.gif.

cannot compute difference between binary files

Added freshlib/test_code/TestBBCode/hash_function.asm.











































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
iglobal
if used tpl_func
tpl_func:
          db 148,  24,  71,  46, 179,   0, 106, 157,  70,   1, 102, 126, 120, 134, 151, 183
          db  47, 103,  92, 201,  62, 156,  13,  10, 254, 218, 248,  28,  85, 185, 245, 112
          db 236, 237, 150,  37, 172,  63, 203, 198, 116, 196,  25, 107, 239,  44,  15, 175
          db  38, 161, 140,  98,  33,  79,  99, 133,   2,  59, 174, 115,  69, 188,  51,  36
          db 229, 143, 231,  84,  22,  78,  89, 166, 104, 145,  34, 225, 180,  12, 230, 205
          db  97,  39,  49, 190, 182, 202,  17, 219, 176, 170,  80,  74,  73, 194,  41,  26
          db 243, 142,  60, 131, 244, 249, 119,  61, 138,  16,  77,  54, 210,  23,  29, 147
          db 204, 110, 130,  93, 213, 223, 146, 216, 123, 247,  90, 124,  75, 135,  57, 128
          db 226,  50, 224, 127, 139, 152, 193, 101, 207, 122,   9,  58, 184, 250,  96,  67
          db 241, 109,  87, 168, 187, 167, 240, 171, 233, 155, 173, 215,  43, 227, 160,   5
          db 246, 129,  20,  14, 100, 209, 137, 169,  68,  40,  42, 165, 121, 113, 200,  95
          db 189, 251,  82,  72, 154, 235, 195, 136, 186,  55, 212,  35,  32, 253,  66, 132
          db 214,  88, 192,   4,  31,  65, 114, 211,  19,  83,  21,   3,   6, 177,  76, 255
          db 252, 199, 141,  56, 105,  11, 117, 163, 220,  27, 222, 234, 178,  52,  64, 221
          db  18, 232, 164,  53, 206, 153, 118,  48, 158, 162, 159, 191, 242, 125, 144,  94
          db 108,  91, 197, 111,  30,   7,  81, 181, 149,  45, 217, 208,  86, 238,   8, 228
end if
endg

Added freshlib/test_code/TestBBCode/lol.gif.

cannot compute difference between binary files

Added freshlib/test_code/TestBBCode/prefix.tpl.





























































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>BBCode translator test page.</title>
  <style>
    body {font-family: sans-serif;}
    h1, h2, h3, h4, h5, h6 {margin: 1rem 0 1rem 0;}:
    h1 {font-size: 2em}
    h2 {font-size: 1.8em}
    h3 {font-size: 1.6em}
    h4 {font-size: 1.4em}
    h5 {font-size: 1.2em}
    h6 {font-size: 1em}
    code {display: inline; font-size: 14px;}
    code.block {display: block; white-space: pre; background: #e0ffff; color: #000080; border-left: 8px solid #008080; padding: 1em 1ch; margin: 1em 2ch; }
    code.asm {background: black; color:lime; }
    table {border: 1px solid red;}
    td, th {border: 1px solid red; padding: 1em 2ch; }
    blockquote { border: 2px solid green; padding: 1em 2ch; }
    header {background-color: yellow; padding: 0 2ch; }
    img {display: block; margin: 1em auto; text-align: center;}
    img.emoticon { display: inline; margin: 0 4px; }
    p {color: green;}
    a.back::after {content: "^";}
    span.underlined {text-decoration: underline;}
  </style>

<body>

Added freshlib/test_code/TestBBCode/readme.txt.









>
>
>
>
1
2
3
4
This project is the code of a parser for BBCode and the test cases.
Later this code should be merged in FreshLib and used in AsmBB forum engine
This way, two different markup languages will be allowed simultaneously.

Added freshlib/test_code/TestBBCode/rofl.gif.

cannot compute difference between binary files

Added freshlib/test_code/TestBBCode/sad.gif.

cannot compute difference between binary files

Added freshlib/test_code/TestBBCode/smile.gif.

cannot compute difference between binary files

Added freshlib/test_code/TestBBCode/test.bbcode.



















































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
[h1]Header 1[/h1]

[h2]Header 2[/h2]

[h3]Header 3[/h3]

[h4]Header 4[/h4]

[h5]Header 5[/h5]

[h6]Header 6[/h6]

<script>alert('Hi!')</script> << This ""MUST"" be escaped & the tags will be displayed.

[b]This paragraph should be formatted with <strong></strong> tags.[/b]

[i]This paragraph should be formatted with <em></em> tags.[/i]

[u]This paragraph should be formatted with <span class="underlined"></span> tags.[/u]

[s]This paragraph should be formatted with <del></del> tags.[/s]

[c]This paragraph should be formatted with <code></code> tags and monospaced font.[/c]

[url="><span style="color: red">]This is a XSS test. It should [b]NOT[/b] be colored in red![/url]

[url=https://board.asm32.info]This is a link <a href=""></a>[/url]

[url]https://board.asm32.info[/url]

A little bit strange, but valid:

[img=anon.svg[/img]

Standard image definition:

[img]anon.svg[/img]

An image with alt text:

[img=Anonymous user picture]anon.svg[/img]

Missing image alt text displayed and properly encoded:

[img=>>>>>>Anonymous user picture<<<<<<<]someimage.svg[/img]


[quote]Block quote
with paragraphs.

Another paragraph.
And it is multiline
paragraph actually.

[/quote]

[quote=Author name]<<Block quote with author name.>>[/quote]

This is multiline paragraph
and should be formatted as such.
All text should be grouped in one text block.

[quote=Anonymous]
<<Block quote with author name. and nested block quote with formatting>>
[quote=johnfound]
Here is my [b]super asm[/b] code!
[code=asm]
        stdcall LoadBinaryFile, "prefix.tpl"
        jc      .prefix_ok
        push    eax
        stdcall FileWrite, [STDOUT], eax, ecx
        pop     eax
.prefix_ok:
[/code]
[/quote]

Your [i]code[/i] [b]sucks![/b]

[/quote]




[code]
This is a block of code formatted with <code></code> tags.

        [b]This tag should not be formatted[/b], because in [code] tag.

        stdcall LoadBinaryFile, "prefix.tpl"
        jc      .prefix_ok
        push    eax
        stdcall FileWrite, [STDOUT], eax, ecx
        pop     eax
.prefix_ok:
[/code]

[code=asm]
This is a block of code formatted with <code class="asm"></code> tags.
        stdcall LoadBinaryFile, "prefix.tpl"
        jc      .prefix_ok
        push    eax
        stdcall FileWrite, [STDOUT], eax, ecx
        pop     eax
.prefix_ok:

[/code]

[size=48px]Text [color=#ff00ff]with[/color] different size.[/size]

[color=#ff00ff]Text with different color.[/color]

Unordered list 1:

[list]
[*] Item 1
[*] Item 2

[*] Item 3
[/list]

Unordered list 2:

[ul]
[*] Item 1
[*] Item 2
[*] Item 3
Item 3 as well!

Not an item?

[/ul]

Nested ordered lists:

[ol]
[*] Item 1
[*] Item 2 has subitems.
  [ol=a]
     [*] Item 1

     [*] Item 2
     [*] Item 3
     [*] Item 4
   [/ol]

[*] Item 3

[/ol]


[table]
   [tr]
      [td] 1 [/td]
      [td] 2 [/td]
      [td] 3 [/td]
   [/tr]
   [tr]
      [td] 4 [/td]
      [td] 5 [/td]
      [td] 6 [/td]
   [/tr]
   [tr]
      [td] 7 [/td]
      [td] 8 [/td]
      [td] 9 [/td]
   [/tr]
[/table]

[:)] [:D] [:lol:] [;)] [:P] [:(] [:`(] [>:(]


[email]email@email.com[/email]

[email=email@email.com]My email[/email]

[spoiler]
  Vader is Luke's father!
[/spoiler]


[b][i][u]Multi tags[/u][/i][/b]

[b][i][u]Wrong multi tags[/i][/b][/u]

[/asdf]<test for bad closing tag.>

[/<script>alert('hi!')</script>]XSS test for invalid closing tags!

[<script>alert('hi!')</script>=<script>alert('hi!')</script>]XSS test for invalid tags!


[b=some extra attribute ignore!]<<Not needed attributes should be simply deleted/ignored, but the text should be properly formatted>>[/b]

Not finished tag:

[[[[[[[[[b=<script>alert('Hi!')</script>


The following is a not finished tag at the end of the file:

[<script>alert('Hi!')</script>

Added freshlib/test_code/TestBBCode/tongue.gif.

cannot compute difference between binary files

Added freshlib/test_code/TestBBCode/wink.gif.

cannot compute difference between binary files

Added freshlib/test_code/TestGapBuffers.asm.





























































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
; _______________________________________________________________________________________
;|                                                                                       |
;| ..:: Fresh IDE ::..  template project.                                                |
;|_______________________________________________________________________________________|
;
;  Description: FreshLib portable console application.
;
;  Target OS: Any, supported by FreshLib
;
;  Dependencies: FreshLib
;
;  Notes:
;_________________________________________________________________________________________

include "%lib%/freshlib.inc"

@BinaryType console, compact

include "%lib%/freshlib.asm"

start:
        InitializeAll

        stdcall TextCreate, sizeof.TText
        mov     edx, eax

        stdcall TextAddString, edx, 0, "This is test"
        stdcall TextAddString, edx, 8, "simply a small, simple but reliable "

        stdcall TextMoveGap, edx, -100000
        stdcall TextMoveGap, edx, +100000

        stdcall TextMoveGap, edx, 4
        sub     [edx+TText.GapBegin], 4

        stdcall TextAddString, edx, 0, txt "Katakana"

        stdcall TextMoveGap, edx, -1
        sub     [edx+TText.GapBegin], 4

        stdcall TextAddString, edx, [edx+TText.GapBegin], "alphabet."

;        int3
        stdcall TextCompact, edx

        push    eax

        stdcall FileWrite, [STDERR], edx, eax
        stdcall FileWriteString, [STDERR], <txt "<<<<<<<", 13, 10, "Text length: ">

        pop     eax
        stdcall NumToStr, eax, ntsDec or ntsUnsigned

        stdcall FileWriteString, [STDERR], eax
        stdcall FileWriteString, [STDERR], <txt 13, 10>

        stdcall NumToStr, [edx+TText.GapEnd], ntsDec
        push    eax

        stdcall NumToStr, [edx+TText.GapBegin], ntsDec
        push    eax

        stdcall NumToStr, [edx+TText.Length], ntsDec
        push    eax

        stdcall FileWriteString, [STDERR], "TText length: "
        stdcall FileWriteString, [STDERR] ; from the stack

        stdcall FileWriteString, [STDERR], <txt 13, 10, "Gap begin: ">
        stdcall FileWriteString, [STDERR] ; from the stack

        stdcall FileWriteString, [STDERR], <txt 13, 10, "Gap end: ">
        stdcall FileWriteString, [STDERR] ; from the stack

        stdcall FileWriteString, [STDERR], <txt 13, 10>

        FinalizeAll
        stdcall TerminateAll, 0

Added freshlib/test_code/TestGapBuffers.fpr.

cannot compute difference between binary files

Changes to freshlib/test_code/TestMarkdown/TestMarkdown.asm.

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
include "%lib%/freshlib.asm"


uglobal
  Result TMarkdownResults
endg








start:
        InitializeAll

; place your code here.

        stdcall ReadTheInputSlow

        stdcall TranslateMarkdown, edi, 0, 0, Result



        stdcall FileWriteString, [STDOUT], [Result.hContent]


        FinalizeAll
        stdcall TerminateAll, 0









>
>
>
>
>









|

>
>







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
include "%lib%/freshlib.asm"


uglobal
  Result TMarkdownResults
endg

iglobal
  cHeader file "../TestBBCode/prefix.tpl"
          dd 0
endg



start:
        InitializeAll

; place your code here.

        stdcall ReadTheInputSlow

        stdcall TranslateMarkdown2, edi, 0, TRUE, Result, 0

        stdcall FileWriteString, [STDOUT], cHeader
        stdcall FileWriteString, [STDOUT], [Result.hIndex]
        stdcall FileWriteString, [STDOUT], [Result.hContent]


        FinalizeAll
        stdcall TerminateAll, 0


Changes to freshlib/test_code/TestMarkdown/TestMarkdown.fpr.

cannot compute difference between binary files

Deleted freshlib/test_code/TestMarkdown/image.png.

cannot compute difference between binary files

Changes to freshlib/test_code/TestMarkdown/test.markdown.

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
# Header 1

This is a test file for the markdown parser.


## Header 1.1

Here are some image:

           [!img1][Some alt text]

           [!image.png]

And here, some object:

        [$128x48:test.swf][With inline address.]

        [$Flash1][This is simple demonstration of Fresh IDE power.]

        [$Flash2][Broken link processing]

        [$test.swf][There should be no size, but the animation should appear.]

        [$x480test.swf][Broken3]

## Header 1.2












Another paragraph.






















Some more text to test the sub-chapters.

### Header 1.2.1


















































































[$Flash1] 64x48:test.swf
[$Flash2] 64:test.swf
[!img1] image.png







|

|


|

|



|

|
|
|
|
|
|
|
|
|
|
|

|

>
>
>
>
>
>
>
>
>
>
>


>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>


<
>

>

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>




|







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
# Test of the MiniMagAsm markup.

This is a test file for the MiniMagAsm kind of markdown parser.


## Header 1

Here are some images:

           [!img1][Some alt text]

           [!../TestBBCode/anon.svg]

;And here, some object:
;
;        [$128x48:test.swf][With inline address.]
;
;        [$Flash1][This is simple demonstration of Fresh IDE power.]
;
;        [$Flash2][Broken link processing]
;
;        [$test.swf][There should be no size, but the animation should appear.]
;
;        [$x480test.swf][Broken3]

## Code block without syntax specified

;begin
        stdcall LoadBinaryFile, "prefix.tpl"
        jc      .prefix_ok
        push    eax
        stdcall FileWrite, [STDOUT], eax, ecx
        pop     eax
.prefix_ok:

;end


Another paragraph.

;quote Anonymous
<<Block quote with author name. and nested block quote with formatting>>
;quote johnfound
Here is my *super asm* code! (Code block with language specified.)

;begin asm
        stdcall LoadBinaryFile, "prefix.tpl"
        jc      .prefix_ok
        push    eax
        stdcall FileWrite, [STDOUT], eax, ecx
        pop     eax
.prefix_ok:

;end

;end

Your /code/ *sucks!*

;end

Some more text to test the sub-chapters.


### Bold and italic.

Another chapter.

   *This is bold*

   /This is italic/

#### Level 4

;ulist
* List item 1

* List item 2

* List item 3
;end

;olist
* List item 1

* List item 2
;ulist
* Subitem 1

* Subitem 2

* Subitem 3
;end

* List item 3
;end

##### Level 5

Table formatting:

;table
1

2

3

;-------------------

4

5

6

;---------------------

7

8

9

;end

###### Level 6

  The maximal level of H tags.

;spoiler Spoiler
  Vader is Luke's father!
;end


### Underlined and Strikethrough.

  _This is underlined_

  -Deleted text-

### One more header.

 This is inline `code formatting`, displayed with monospaced font.


[$Flash1] 64x48:test.swf
[$Flash2] 64:test.swf
[!img1] ../TestBBCode/anon.svg







Changes to freshlib/test_code/TestPThreads.asm.

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
..
49
50
51
52
53
54
55
56
57
58
;
;  Notes:
;_________________________________________________________________________________________

include "%lib%/freshlib.inc"

@BinaryType console



include "%lib%/freshlib.asm"


; include your includes here.

iglobal
  Stop dd 0
endg

start:
        InitializeAll











        stdcall ThreadCreate, TestThread, 0

.wait:
        stdcall Sleep, 100
        cmp     [Stop], 0
        je      .wait



        FinalizeAll
        stdcall TerminateAll, 0


proc TestThread, .arg
begin
        mov     esi, esp
................................................................................
        loop    .touch

        mov     [Stop], 1
        mov     esp, esi
        cret
endp


@AllImportEmbeded       ; or @AllImportSection
@AllDataEmbeded         ; or @AllDataSection








>
>

>










>
>
>
>
>
>
>
>
>
>







>
>







 







<
<
<
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
..
64
65
66
67
68
69
70



;
;  Notes:
;_________________________________________________________________________________________

include "%lib%/freshlib.inc"

@BinaryType console

options.DebugMode = 0

include "%lib%/freshlib.asm"


; include your includes here.

iglobal
  Stop dd 0
endg

start:
        InitializeAll


        stdcall FileOpenAccess, "./TestPThreads.asm", faReadOnly
        mov     ebx, eax

        stdcall FileOpenAccess, "./out.txt", faWriteOnly or faCreateAlways
        mov     edi, eax

        stdcall Exec2, '/usr/bin/grep "stdcall Exec2"', ebx, edi, edi   ; Runs in WINE as well, but will not in the original Windows.
        jmp     .finish

        stdcall ThreadCreate, TestThread, 0

.wait:
        stdcall Sleep, 100
        cmp     [Stop], 0
        je      .wait


.finish:
        FinalizeAll
        stdcall TerminateAll, 0


proc TestThread, .arg
begin
        mov     esi, esp
................................................................................
        loop    .touch

        mov     [Stop], 1
        mov     esp, esi
        cret
endp




Changes to freshlib/test_code/TestPThreads.fpr.

cannot compute difference between binary files

Changes to freshlib/test_code/TestStrLib.asm.

12
13
14
15
16
17
18

19
20
21
22
23
24
25
..
38
39
40
41
42
43
44
45

46



47
48
49
50




















51
52
53
54
55
56
57
...
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
;  Notes:
;_________________________________________________________________________________________
include "%lib%/freshlib.inc"

@BinaryType console

options.DisableMMX = 0


include "%lib%/freshlib.asm"

; include your includes here.

MIN_STR_LEN = 1
MAX_STR_LEN = 1500
................................................................................
             rb 6
  cLowerCase db "THIS is TEST case string. /\"
             dd 0
             db $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff
  cLowerCase2 db "THISISTH"
              dd 0
             db $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff


endg





start:
        InitializeAll






















        xor     ebx, ebx

.all_loop:

        stdcall NumToStr, ebx, ntsHex or ntsUnsigned or ntsFixedWidth + 8
................................................................................

        dec     ecx
        cmp     ecx, MIN_STR_LEN
        jae     .output_loop

.finish:
        FinalizeAll
        stdcall Terminate, 0




proc TestLoop, .min, .max, .missalignment, .offsResult, .proc
begin
        pushad







>







 








>

>
>
>




>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







 







|







12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
..
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
...
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
;  Notes:
;_________________________________________________________________________________________
include "%lib%/freshlib.inc"

@BinaryType console

options.DisableMMX = 0
options.DebugMode = 1

include "%lib%/freshlib.asm"

; include your includes here.

MIN_STR_LEN = 1
MAX_STR_LEN = 1500
................................................................................
             rb 6
  cLowerCase db "THIS is TEST case string. /\"
             dd 0
             db $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff
  cLowerCase2 db "THISISTH"
              dd 0
             db $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff, $ff


endg

TestPosHeap text  'saasoidhjaasdfsijoa;aijdfasi;ojaryiaejnkjfvnarfui;adnv'
TestPosNeedle text 'b'


start:
        InitializeAll

; Test StrPos procedure


        stdcall StrDupMem, TestPosHeap
        mov     esi, eax

        stdcall StrDupMem, TestPosNeedle
        mov     ebx, eax

        stdcall StrPos, esi, txt 'sdaoluhfas;iodfhjas;oidjf;sioajf;oasijf;ioasj;iodjfs;aiojd;ioj;dofija;iojf;oiasjd;fioajs;iojfas;iojf;saoij_'

        OutputValue "Found StrPos: ", eax, 16, 8

        jmp     .finish







        xor     ebx, ebx

.all_loop:

        stdcall NumToStr, ebx, ntsHex or ntsUnsigned or ntsFixedWidth + 8
................................................................................

        dec     ecx
        cmp     ecx, MIN_STR_LEN
        jae     .output_loop

.finish:
        FinalizeAll
        stdcall TerminateAll, 0




proc TestLoop, .min, .max, .missalignment, .offsResult, .proc
begin
        pushad

Changes to freshlib/test_code/md5_test/MD5_test.fpr.

cannot compute difference between binary files

Changes to freshlib/test_code/md5_test/md5_test.asm.

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



include "%lib%/freshlib.inc"
@BinaryType console

include "%lib%/freshlib.asm"

GlobalAPI equ ascii

start:
        int3
        InitializeAll

        stdcall GetCmdArguments
        mov     esi, eax

        stdcall FileWriteString, [STDOUT], [esi+TArray.array+4]
        stdcall FileWriteString, [STDOUT], cSeparator

        stdcall StrMD5, [esi+TArray.array+4]
        stdcall FileWriteString, [STDOUT], eax



cSeparator text '>>>'



        FinalizeAll
        stdcall Terminate, 0









@AllImportEmbeded
@AllDataEmbeded






|

|


<





|
|

|
|
>
>

<
>
>

<
|
>
>
>

>
>

>

<
<
>
>
>
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
include "%lib%/freshlib.inc"
@BinaryType console

options.DebugMode = 0

include "%lib%/freshlib.asm"

start:

        InitializeAll

        stdcall GetCmdArguments
        mov     esi, eax

        cmp     [esi+TArray.count], 2
        jne     .finish

        stdcall LoadBinaryFile, [esi+TArray.array+4]
        lea     edx, [ecx+1024]
        stdcall ResizeMem, eax, edx
        mov     edi, eax


        stdcall DataSHA1, edi, ecx
        push    eax


        stdcall StrSHA1, eax
        stdcall StrLCase, eax
        stdcall FreeMem; from the stack
        push    eax

        stdcall FileWriteString, [STDOUT], eax
        stdcall StrDel ; from the stack

        stdcall FileWriteString, [STDOUT], <txt 13, 10>



.finish:
        FinalizeAll
        stdcall TerminateAll, 0

Changes to freshlib/test_code0/TestDeflate.fpr.

cannot compute difference between binary files

Changes to freshlib/test_code0/TestText.asm.

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
...
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

include "%lib%/freshlib.asm"


iglobal
TemplateMainForm:
        ObjTemplate  tfParent or tfEnd, TForm, frmMain, \
                     x, 100,        \
                     y, 50,         \
                     width, 1024,    \
                     height, 600,   \
                     Visible, TRUE, \
                     OnDestroy, FormOnDestroy,  \
                     Caption, 'Fresh portable Win32/Linux application test.'

          ObjTemplate  tfChild, TButton, btnChild1,          \
                     x, 48,         \
                     y, 48,         \
                     width, 80,     \
                     height, 48,    \
                     TextAlign, dtfAlignLeft or dtfAlignMiddle or dtfCRLF or dtfWordWrap,\
                     Caption, 'Button BRAVO',        \
                     Visible, TRUE, \
                     OnClick, Button1Click

          ObjTemplate  tfChild or tfEnd, TButton, btnChild2,          \
                     x, 48,         \
                     y, 100,         \
                     width, 80,     \
                     height, 48,    \
                     TextAlign, dtfAlignLeft or dtfAlignMiddle or dtfCRLF or dtfWordWrap,\
                     Caption, 'Button 2',        \
                     Visible, TRUE, \
                     OnClick, Button2Click
endg


rb 423




start:
        InitializeAll


        create  [pApplication], TApplication

        stdcall CreateFromTemplate, TemplateMainForm, 0
        set     [pApplication], TApplication:MainWindow, frmMain



        stdcall Run

        FinalizeAll
        stdcall TerminateAll, 0


................................................................................
.width dd ?
.height dd ?
.y      dd ?
.start  dd ?
begin
        pushad

        DebugMsg 'Button2 clicked'

        stdcall FontCreate4, 'Ubuntu', 16, 900, ffUnderline
        mov     [.font], eax

        stdcall CreateImage, 1024, 600
        mov     esi, eax

        mov     edi, [frmMain]
        stdcall AllocateContext, [edi+TWindow.handle]
        mov     edi, eax

        stdcall DrawSolidRect, esi, 0, 0, 1024, 600,  $ffd0d0d8
        stdcall DrawSolidRect, esi, 320, 0, 320, 480, $ff0000ff

        stdcall GetFontMetrics, [.font]
        mov     [.height], eax


        stdcall GetTimestamp
        mov     [.start], eax

        mov     edx, 7
        mov     ecx, -100

        mov     ecx, 1000
        mov     [.y], 5

.loop:
; 85 characters text
;        stdcall DrawString2, esi, 'uon ravo s s smpe ex, spaye y reeype rary usng reeype cace.', -1, 150, [.y], [.font], $ff000000
        stdcall DrawString2, esi, 'Button BRAVO This is simple text, displayed by FreeType library using FreeType cache.', -1, 150, [.y], [.font], $ff000000

        mov     [.width], eax

        mov     edx, ebx
        add     ebx, [.y]
        neg     edx
;        stdcall DrawSolidRect, esi, 150, ebx, eax, 1, $ffff0000
;        stdcall DrawSolidRect, esi, 150, [.y], eax, 1, $ffff0000

;        stdcall GetTextBounds2, 'Button BRAVO This is simple text, displayed by FreeType library using FreeType cache.', -1, [.font]

        mov     eax, [.height]
        add     [.y], eax

        dec     ecx
        jnz     .loop

        stdcall GetTimestamp
        sub     eax, [.start]

        stdcall NumToStr, eax, ntsUnsigned or ntsDec
        push    eax

        stdcall StrPtr, eax
        stdcall DrawString2, esi, eax, -1, 40, 500, [.font], $ff000000
        stdcall StrDel ; from the stack

        stdcall NumToStr, [.width], ntsDec or ntsSigned
        push    eax

        stdcall StrPtr, eax
        stdcall DrawString2, esi, eax, -1, 40, 520, [.font], $ff000000
        stdcall StrDel ; from the stack


        stdcall NumToStr, [.height], ntsDec or ntsUnsigned
        push    eax

        stdcall StrPtr, eax
        stdcall DrawString2, esi, eax, -1, 40, 540, [.font], $ff000000
        stdcall StrDel ; from the stack




        stdcall DrawImage, edi, esi, 0, 0

        stdcall FontDestroy2, [.font]

        popad
        return
endp








|
|
|
|
|
|
|


|
|
|
|
|
|
|
|


|
|
|
|
|
|
|
|










>
>




>
>







 







|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|




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
...
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

include "%lib%/freshlib.asm"


iglobal
TemplateMainForm:
        ObjTemplate  tfParent or tfEnd, TForm, frmMain, \
                     x = 100,        \
                     y = 50,         \
                     width = 1024,    \
                     height = 600,   \
                     Visible = FALSE, \
                     OnDestroy = FormOnDestroy,  \
                     Caption = 'Fresh portable Win32/Linux application test.'

          ObjTemplate  tfChild, TButton, btnChild1,          \
                     x = 48,         \
                     y = 48,         \
                     width = 80,     \
                     height = 48,    \
                     TextAlign = dtfAlignLeft or dtfAlignMiddle or dtfCRLF or dtfWordWrap,\
                     Caption = 'Button BRAVO',        \
                     Visible = TRUE, \
                     OnClick = Button1Click

          ObjTemplate  tfChild or tfEnd, TButton, btnChild2,          \
                     x = 48,         \
                     y = 100,         \
                     width = 80,     \
                     height = 48,    \
                     TextAlign = dtfAlignLeft or dtfAlignMiddle or dtfCRLF or dtfWordWrap,\
                     Caption = 'Button 2',        \
                     Visible = TRUE, \
                     OnClick = Button2Click
endg


rb 423




start:
        InitializeAll

        call    GUI.Init
        create  [pApplication], TApplication

        stdcall CreateFromTemplate, TemplateMainForm, 0
        set     [pApplication], TApplication:MainWindow, frmMain

        set     [frmMain], TForm:Visible, TRUE

        stdcall Run

        FinalizeAll
        stdcall TerminateAll, 0


................................................................................
.width dd ?
.height dd ?
.y      dd ?
.start  dd ?
begin
        pushad

;        DebugMsg 'Button2 clicked'
;
;        stdcall FontCreate, 'Ubuntu', 16, 900, ffUnderline
;        mov     [.font], eax
;
;        stdcall CreateImage, 1024, 600
;        mov     esi, eax
;
;        mov     edi, [frmMain]
;        stdcall AllocateContext, [edi+TWindow.handle]
;        mov     edi, eax
;
;        stdcall DrawSolidRect, esi, 0, 0, 1024, 600,  $ffd0d0d8
;        stdcall DrawSolidRect, esi, 320, 0, 320, 480, $ff0000ff
;
;        stdcall GetFontMetrics, [.font]
;        mov     [.height], eax
;
;
;        stdcall GetTimestamp
;        mov     [.start], eax
;
;        mov     edx, 7
;        mov     ecx, -100
;
;        mov     ecx, 1000
;        mov     [.y], 5
;
;.loop:
;; 85 characters text
;;        stdcall DrawString2, esi, 'uon ravo s s smpe ex, spaye y reeype rary usng reeype cace.', -1, 150, [.y], [.font], $ff000000
;        stdcall DrawString2, esi, 'Button BRAVO This is simple text, displayed by FreeType library using FreeType cache.', -1, 150, [.y], [.font], $ff000000
;
;        mov     [.width], eax
;
;        mov     edx, ebx
;        add     ebx, [.y]
;        neg     edx
;;        stdcall DrawSolidRect, esi, 150, ebx, eax, 1, $ffff0000
;;        stdcall DrawSolidRect, esi, 150, [.y], eax, 1, $ffff0000
;
;;        stdcall GetTextBounds2, 'Button BRAVO This is simple text, displayed by FreeType library using FreeType cache.', -1, [.font]
;
;        mov     eax, [.height]
;        add     [.y], eax
;
;        dec     ecx
;        jnz     .loop
;
;        stdcall GetTimestamp
;        sub     eax, [.start]
;
;        stdcall NumToStr, eax, ntsUnsigned or ntsDec
;        push    eax
;
;        stdcall StrPtr, eax
;        stdcall DrawString2, esi, eax, -1, 40, 500, [.font], $ff000000
;        stdcall StrDel ; from the stack
;
;        stdcall NumToStr, [.width], ntsDec or ntsSigned
;        push    eax
;
;        stdcall StrPtr, eax
;        stdcall DrawString2, esi, eax, -1, 40, 520, [.font], $ff000000
;        stdcall StrDel ; from the stack
;
;
;        stdcall NumToStr, [.height], ntsDec or ntsUnsigned
;        push    eax
;
;        stdcall StrPtr, eax
;        stdcall DrawString2, esi, eax, -1, 40, 540, [.font], $ff000000
;        stdcall StrDel ; from the stack
;
;
;
;
;        stdcall DrawImage, edi, esi, 0, 0
;
;        stdcall FontDestroy2, [.font]
;
        popad
        return
endp

Changes to freshlib/test_code0/TestText.fpr.

cannot compute difference between binary files

Changes to freshlib/test_code0/ThemesConfigBuild.fpr.

cannot compute difference between binary files

Added freshlib/test_code0/_test_images/svg/Red_sphere.svg.







































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
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
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<!-- Created with Inkscape (http://www.inkscape.org/) -->

<svg
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:cc="http://creativecommons.org/ns#"
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:svg="http://www.w3.org/2000/svg"
   xmlns="http://www.w3.org/2000/svg"
   xmlns:xlink="http://www.w3.org/1999/xlink"
   xmlns:sodipodi="http://sodipodi.sourceforge.net/DTD/sodipodi-0.dtd"
   xmlns:inkscape="http://www.inkscape.org/namespaces/inkscape"
   version="1.0"
   width="128"
   height="128"
   id="svg2"
   sodipodi:docname="Red_sphere.svg"
   inkscape:export-filename="/work/asmwork/FreshFs/FreshLibDev/freshlib/test_code0/_test_images/ball.png"
   inkscape:export-xdpi="18"
   inkscape:export-ydpi="18"
   inkscape:version="0.92.0 r">
  <metadata
     id="metadata23">
    <rdf:RDF>
      <cc:Work
         rdf:about="">
        <dc:format>image/svg+xml</dc:format>
        <dc:type
           rdf:resource="http://purl.org/dc/dcmitype/StillImage" />
      </cc:Work>
    </rdf:RDF>
  </metadata>
  <sodipodi:namedview
     pagecolor="#ffffff"
     bordercolor="#666666"
     borderopacity="1"
     objecttolerance="10"
     gridtolerance="10"
     guidetolerance="10"
     inkscape:pageopacity="0"
     inkscape:pageshadow="2"
     inkscape:window-width="640"
     inkscape:window-height="480"
     id="namedview21"
     showgrid="false"
     inkscape:current-layer="svg2" />
  <defs
     id="defs5">
    <linearGradient
       id="linearGradient2816">
      <stop
         style="stop-color:#a20000;stop-opacity:1"
         offset="0"
         id="stop2818" />
      <stop
         style="stop-color:white;stop-opacity:1"
         offset="1"
         id="stop2820" />
    </linearGradient>
    <linearGradient
       id="linearGradient2794">
      <stop
         style="stop-color:#545454;stop-opacity:1"
         offset="0"
         id="stop2796" />
      <stop
         style="stop-color:#545454;stop-opacity:1"
         offset="0.9464286"
         id="stop2804" />
      <stop
         style="stop-color:#c5c5c5;stop-opacity:0"
         offset="1"
         id="stop2798" />
    </linearGradient>
    <linearGradient
       id="linearGradient2760">
      <stop
         style="stop-color:red;stop-opacity:1"
         offset="0"
         id="stop2762" />
      <stop
         style="stop-color:#a20000;stop-opacity:1"
         offset="0.61160713"
         id="stop2768" />
      <stop
         style="stop-color:#f66;stop-opacity:1"
         offset="1"
         id="stop2764" />
    </linearGradient>
    <linearGradient
       x1="6.9292336"
       y1="121.07077"
       x2="6.9292336"
       y2="8.9609833"
       id="linearGradient2766"
       xlink:href="#linearGradient2760"
       gradientUnits="userSpaceOnUse" />
    <radialGradient
       cx="62.984127"
       cy="65.015877"
       r="54.179893"
       fx="62.984127"
       fy="65.015877"
       id="radialGradient2812"
       xlink:href="#linearGradient2794"
       gradientUnits="userSpaceOnUse" />
    <linearGradient
       x1="31.5"
       y1="63"
       x2="31.5"
       y2="12"
       id="linearGradient2822"
       xlink:href="#linearGradient2816"
       gradientUnits="userSpaceOnUse" />
  </defs>
  <path
     d="M 117.16402 65.015877 A 54.179893 54.179893 0 1 1  8.8042336,65.015877 A 54.179893 54.179893 0 1 1  117.16402 65.015877 z"
     transform="translate(5.724864,4.031744)"
     style="opacity:1;fill:url(#radialGradient2812);fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:3.75;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
     id="path2790" />
  <path
     d="M 117.16402 65.015877 A 54.179893 54.179893 0 1 1  8.8042336,65.015877 A 54.179893 54.179893 0 1 1  117.16402 65.015877 z"
     transform="translate(1.69312,0)"
     style="opacity:1;fill:url(#linearGradient2766);fill-opacity:1;fill-rule:nonzero;stroke:#740000;stroke-width:3.75;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
     id="path1873" />
  <path
     d="M 95.5 37.5 A 32 25.5 0 1 1  31.5,37.5 A 32 25.5 0 1 1  95.5 37.5 z"
     transform="translate(1,2)"
     style="opacity:1;fill:url(#linearGradient2822);fill-opacity:1;fill-rule:nonzero;stroke:none;stroke-width:3.75;stroke-linecap:butt;stroke-linejoin:round;stroke-miterlimit:4;stroke-dasharray:none;stroke-dashoffset:0;stroke-opacity:1"
     id="path2814" />
</svg>

Changes to freshlib/test_code0/theme_flat.cfg.

24
25
26
27
28
29
30

31
32
33
34
35
36
37
clDialogTxt = $FF000000
clSplitter = $FF34495E
clProgressBk = $FFECF0F1
clProgressBar = $FF1ABC9C
progressBorder = 3
clScrollBk = $3FBDC3C7, $A0BDC3C7
clScrollSlider = $7F7F8C8D, $FF1ABC9C

borderScroll = 0
scrollWidth = 12
minSliderHeight = 24
clLabelBk = $0
clLabelTxt = $FF000000
clTreeViewBack = $FFECF0F1
clTreeViewText = $FF000000







>







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
clDialogTxt = $FF000000
clSplitter = $FF34495E
clProgressBk = $FFECF0F1
clProgressBar = $FF1ABC9C
progressBorder = 3
clScrollBk = $3FBDC3C7, $A0BDC3C7
clScrollSlider = $7F7F8C8D, $FF1ABC9C
clScrollSliderOvf = $3FF39C12, $FFF39C12
borderScroll = 0
scrollWidth = 12
minSliderHeight = 24
clLabelBk = $0
clLabelTxt = $FF000000
clTreeViewBack = $FFECF0F1
clTreeViewText = $FF000000

Changes to freshlib/test_code0/theme_win.cfg.

24
25
26
27
28
29
30

31
32
33
34
35
36
37
clDialogTxt = $FF000000
clSplitter = $FF606060
clProgressBk = $FFD4D0C8
clProgressBar = $FF0A246C
progressBorder = 2
clScrollBk = $80D4D0C8, $D0D4D0C8
clScrollSlider = $C0808080, $FF808080

borderScroll = 1
scrollWidth = 12
minSliderHeight = 24
clLabelBk = $FFFFFF
clLabelTxt = $FF000000
clTreeViewBack = $FFFFFFFF
clTreeViewText = $FF000000







>







24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
clDialogTxt = $FF000000
clSplitter = $FF606060
clProgressBk = $FFD4D0C8
clProgressBar = $FF0A246C
progressBorder = 2
clScrollBk = $80D4D0C8, $D0D4D0C8
clScrollSlider = $C0808080, $FF808080
clScrollSliderOvf = $C0FF8080, $FFFF8080
borderScroll = 1
scrollWidth = 12
minSliderHeight = 24
clLabelBk = $FFFFFF
clLabelTxt = $FF000000
clTreeViewBack = $FFFFFFFF
clTreeViewText = $FF000000