2 * string_table.cpp -- part of ZilUtils/ZilAsm
4 * Copyright (C) 2019, 2020 Jason Self <j@jxself.org>
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.
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.
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/>
19 * SPDX-License-Identifier: AGPL-3.0-or-later
25 #include "string_table.h"
33 String_table::add_string_to_string_table (const char *str, ZMemblock * zmb)
35 encrypt_string (str, zmb);
41 String_table::encrypt_string (const char *str, ZMemblock * zmb)
44 zmb = zmem_init (strlen (str) * 3);
46 int currentCharSet = 0;
50 int numberOfSymbolsInWord = 0;
51 while (index < (int) strlen (str))
54 if (c >= 'a' && c <= 'z')
56 switch (currentCharSet)
61 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5); // permanently changes to the zero charset
65 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4); // permanently changes to the zero charset
69 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord,
72 else if (c >= 'A' && c <= 'Z')
74 switch (currentCharSet)
77 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4); // temporarily changes to the zero charset
82 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4); // permanently changes to the zero charset
84 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 4); // temporarily changes to the first charset
87 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord,
93 //if (numberOfSymbolsInWord != 0)
95 // zmem_putbyte(zmb, (w >> 8) & 255);
96 // zmem_putbyte(zmb, w & 255);
97 // numberOfSymbolsInWord = 0;
100 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 0);
104 char ar[] = "*0123456789.,!?_#'\"/\\-:()";
105 for (int i = 0; i < (int) strlen (ar); ++i)
114 switch (currentCharSet)
117 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5); // temporarily changes to the second charset
120 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5); // permanently changes to the zero charset
122 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5); // temporarily changes to the second charset
127 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, c);
133 if (numberOfSymbolsInWord != 0)
135 zmem_putbyte (zmb, (w >> 8) & 255);
136 zmem_putbyte (zmb, w & 255);
137 numberOfSymbolsInWord = 0;
141 if ((zmb->contents[zmb->used_size - 1] & 31) == 0) // if the last symbol is a space or not filled
142 zmb->contents[zmb->used_size - 2] |= 0x80; // then we mark this word as the last word
145 zmem_putbyte(zmb, 0x80); // otherwise we add one more empty word
146 zmem_putbyte(zmb, 0x0); // with 15-th bit set
153 String_table::write_one_word_to_string_table (ZMemblock * zmb, Word * w,
154 int *numberOfSymbolsInWord,
157 int shift = ((2 - *numberOfSymbolsInWord) * 5);
158 *w |= (symbol << shift);
160 (*numberOfSymbolsInWord)++;
162 if (*numberOfSymbolsInWord == 3)
164 zmem_putbyte (zmb, (*w >> 8) & 255);
165 zmem_putbyte (zmb, (*w) & 255);
166 *numberOfSymbolsInWord = 0;