Added ability to define multiple functions (without parameters) and
[zilutils.git] / zilasm / string_table.cpp
1 /*
2  * string_table.cpp -- part of ZilUtils/ZilAsm
3  *
4  * Copyright (C) 2019 Jason Self <j@jxself.org>
5  *
6  * This program is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU Affero General Public License as
8  * published by the Free Software Foundation, either version 3 of the
9  * License, or (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU Affero General Public License for more details.
15  *
16  * You should have received a copy of the GNU Affero General Public License
17  * along with this program.  If not, see <http://www.gnu.org/licenses/>
18  *
19  * SPDX-License-Identifier: AGPL-3.0-or-later
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include "header.h"
25 #include "string_table.h"
26 #include <string.h>
27 extern "C"
28 {
29 #include <strings.h>
30 }
31
32 unsigned
33 String_table::add_string_to_string_table (const char *str, ZMemblock * zmb)
34 {
35   encrypt_string (str, zmb);
36   return 0;
37 }
38
39
40 ZMemblock *
41 String_table::encrypt_string (const char *str, ZMemblock * zmb)
42 {
43   if (!zmb)
44     zmb = zmem_init (strlen (str) * 3);
45
46   int currentCharSet = 0;
47   Byte currentByte = 0;
48   int index = 0;
49   Word w = 0;
50   int numberOfSymbolsInWord = 0;
51   while (index < (int) strlen (str))
52     {
53       char c = str[index];
54       if (c >= 'a' && c <= 'z')
55         {
56           switch (currentCharSet)
57             {
58             case 0:
59               break;
60             case 1:
61               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5);      // permanently changes to the zero charset
62               currentCharSet = 0;
63               break;
64             case 2:
65               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4);      // permanently changes to the zero charset
66               currentCharSet = 0;
67               break;
68             }
69           write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord,
70                                           c - 'a' + 6);
71         }
72       else if (c >= 'A' && c <= 'Z')
73         {
74           switch (currentCharSet)
75             {
76             case 0:
77               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4);      // temporarily changes to the zero charset
78               break;
79             case 1:
80               break;
81             case 2:
82               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4);      // permanently changes to the zero charset
83               currentCharSet = 0;
84               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4);      // temporarily changes to the first charset
85               break;
86             }
87           write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord,
88                                           c - 'A' + 6);
89
90         }
91       else if (c == ' ')
92         {
93           write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 0);
94         }
95       else
96         {
97           char ar[] = "*0123456789.,!?_#'\"/\\-:()";
98           for (int i = 0; i < (int) strlen (ar); ++i)
99             {
100               if (ar[i] == c)
101                 {
102                   c = i + 7;
103                   break;
104                 }
105             }
106
107           switch (currentCharSet)
108             {
109             case 0:
110               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5);      // temporarily changes to the second charset
111               break;
112             case 1:
113               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5);      // permanently changes to the zero charset
114               currentCharSet = 0;
115               write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5);      // temporarily changes to the second charset
116               break;
117             case 2:
118               break;
119             }
120           write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, c);
121         }
122
123       ++index;
124     }
125
126   if (numberOfSymbolsInWord != 0)
127     {
128       zmem_putbyte (zmb, (w >> 8) & 255);
129       zmem_putbyte (zmb, w & 255);
130       numberOfSymbolsInWord = 0;
131       w = 0;
132     }
133   zmb->contents[zmb->used_size - 2] |= 0x80;
134
135   return zmb;
136 }
137
138 void
139 String_table::write_one_word_to_string_table (ZMemblock * zmb, Word * w,
140                                               int *numberOfSymbolsInWord,
141                                               Byte symbol)
142 {
143   int shift = ((2 - *numberOfSymbolsInWord) * 5);
144   *w |= (symbol << shift);
145
146   (*numberOfSymbolsInWord)++;
147
148   if (*numberOfSymbolsInWord == 3)
149     {
150       zmem_putbyte (zmb, (*w >> 8) & 255);
151       zmem_putbyte (zmb, (*w) & 255);
152       *numberOfSymbolsInWord = 0;
153       (*w) = 0;
154     }
155 }