| 1 | /***************************************************************************** |
| 2 | |
| 3 | Copyright (c) 2016, 2026, Oracle and/or its affiliates. |
| 4 | |
| 5 | This program is free software; you can redistribute it and/or modify it under |
| 6 | the terms of the GNU General Public License, version 2.0, as published by the |
| 7 | Free Software Foundation. |
| 8 | |
| 9 | This program is designed to work with certain software (including |
| 10 | but not limited to OpenSSL) that is licensed under separate terms, |
| 11 | as designated in a particular file or component or in included license |
| 12 | documentation. The authors of MySQL hereby grant you an additional |
| 13 | permission to link the program and your derivative works with the |
| 14 | separately licensed software that they have either included with |
| 15 | the program or referenced in the documentation. |
| 16 | |
| 17 | This program is distributed in the hope that it will be useful, but WITHOUT |
| 18 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
| 19 | FOR A PARTICULAR PURPOSE. See the GNU General Public License, version 2.0, |
| 20 | for more details. |
| 21 | |
| 22 | You should have received a copy of the GNU General Public License along with |
| 23 | this program; if not, write to the Free Software Foundation, Inc., |
| 24 | 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 25 | |
| 26 | *****************************************************************************/ |
| 27 | #include <string.h> |
| 28 | #include <fstream> |
| 29 | #include <iostream> |
| 30 | #include <map> |
| 31 | #include "lot0types.h" |
| 32 | |
| 33 | namespace lob_data { |
| 34 | |
| 35 | const char *allowed_char = |
| 36 | "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr" |
| 37 | "stuvwxyz1234567890~!@#$%^&*()_-=+"; |
| 38 | |
| 39 | static std::map<lobid_t, byte *> g_data; |
| 40 | |
| 41 | byte *generate_lob(lobid_t *id, ulint size) { |
| 42 | lobid_t new_id = g_data.size(); |
| 43 | const ulint max_j = strlen(allowed_char); |
| 44 | |
| 45 | byte *tmp = new byte[size]; |
| 46 | |
| 47 | ulint count = 10; |
| 48 | for (ulint i = 0, j = 0; i < size; ++i, --count) { |
| 49 | if (count == 0) { |
| 50 | count = 10; |
| 51 | ++j; |
| 52 | if (j == max_j) { |
| 53 | j = 0; |
| 54 | } |
| 55 | } |
| 56 | tmp[i] = allowed_char[j]; |
| 57 | } |
| 58 | *id = new_id; |
| 59 | |
| 60 | g_data.insert(std::pair<lobid_t, byte *>(new_id, tmp)); |
| 61 | return (tmp); |
| 62 | } |
| 63 | |
| 64 | byte *generate_lob(lobid_t *id, char x, ulint size) { |
| 65 | lobid_t new_id = g_data.size(); |
| 66 | byte *tmp = new byte[size]; |
| 67 | |
| 68 | for (ulint i = 0; i < size; ++i) { |
| 69 | tmp[i] = x; |
| 70 | } |
| 71 | |
| 72 | *id = new_id; |
| 73 | g_data.insert(std::pair<lobid_t, byte *>(new_id, tmp)); |
| 74 | return (tmp); |
| 75 | } |
| 76 | |
| 77 | void remove_lob(lobid_t id) { |
| 78 | auto it = g_data.find(id); |
| 79 | |
| 80 | byte *lob = it->second; |
| 81 | delete[] lob; |
| 82 | |
| 83 | if (it != g_data.end()) { |
| 84 | g_data.erase(it); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | std::pair<byte *, ulint> get_lob(lobid_t *id, const char *filename) { |
| 89 | std::ifstream fstrm(filename, std::ios_base::in | std::ios_base::binary); |
| 90 | if (fstrm.fail()) { |
| 91 | return (std::pair<byte *, ulint>(nullptr, 0)); |
| 92 | } |
| 93 | fstrm.seekg(0, std::ios_base::end); |
| 94 | size_t file_size = fstrm.tellg(); |
| 95 | fstrm.seekg(0, std::ios_base::beg); |
| 96 | byte *tmp = new byte[file_size]; |
| 97 | fstrm.read((char *)tmp, file_size); |
| 98 | fstrm.close(); |
| 99 | lobid_t new_id = g_data.size(); |
| 100 | *id = new_id; |
| 101 | g_data.insert(std::pair<lobid_t, byte *>(new_id, tmp)); |
| 102 | return (std::pair<byte *, ulint>(tmp, file_size)); |
| 103 | } |
| 104 | |
| 105 | } // namespace lob_data |