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 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 0);
97 char ar[] = "*0123456789.,!?_#'\"/\\-:()";
98 for (int i = 0; i < (int) strlen (ar); ++i)
107 switch (currentCharSet)
110 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5); // temporarily changes to the second charset
113 write_one_word_to_string_table (zmb, &w, &numberOfSymbolsInWord, 5); // permanently changes to the zero charset
115 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, c);
126 if (numberOfSymbolsInWord != 0)
128 zmem_putbyte (zmb, (w >> 8) & 255);
129 zmem_putbyte (zmb, w & 255);
130 numberOfSymbolsInWord = 0;
134 if ((zmb->contents[zmb->used_size - 1] & 31) == 0) // if the last symbol is a space or not filled
135 zmb->contents[zmb->used_size - 2] |= 0x80; // then we mark this word as the last word
138 zmem_putbyte(zmb, 0x80); // otherwise we add one more empty word
139 zmem_putbyte(zmb, 0x0); // with 15-th bit set
146 String_table::write_one_word_to_string_table (ZMemblock * zmb, Word * w,
147 int *numberOfSymbolsInWord,
150 int shift = ((2 - *numberOfSymbolsInWord) * 5);
151 *w |= (symbol << shift);
153 (*numberOfSymbolsInWord)++;
155 if (*numberOfSymbolsInWord == 3)
157 zmem_putbyte (zmb, (*w >> 8) & 255);
158 zmem_putbyte (zmb, (*w) & 255);
159 *numberOfSymbolsInWord = 0;