Last active
July 17, 2026 01:36
-
-
Save darealshinji/f912aae50533734b3237a04c7bdefb66 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| Licensed under the MIT License <http://opensource.org/licenses/MIT>. | |
| SPDX-License-Identifier: MIT | |
| Copyright (c) 2026 Carsten Janssen | |
| Permission is hereby granted, free of charge, to any person obtaining a copy | |
| of this software and associated documentation files (the "Software"), to deal | |
| in the Software without restriction, including without limitation the rights | |
| to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
| copies of the Software, and to permit persons to whom the Software is | |
| furnished to do so, subject to the following conditions: | |
| The above copyright notice and this permission notice shall be included in all | |
| copies or substantial portions of the Software. | |
| THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
| IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
| FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
| AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
| LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
| OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
| SOFTWARE. | |
| **/ | |
| #include <ctype.h> | |
| #include <stdlib.h> | |
| #include <fstream> | |
| #include <iostream> | |
| #include <vector> | |
| static char *esc = NULL; | |
| static void strip_spaces(std::string &str) | |
| { | |
| while (!str.empty() && isspace(str.back())) { | |
| str.pop_back(); | |
| } | |
| while (!str.empty() && isspace(str.front())) { | |
| str.erase(0, 1); | |
| } | |
| } | |
| static bool is_hex(std::string &str, char &ch) | |
| { | |
| if (str.size() == 4 && | |
| str.starts_with("0x") && | |
| isxdigit(str.at(2)) && | |
| isxdigit(str.at(3))) | |
| { | |
| ch = strtoul(str.substr(0, 4).c_str(), NULL, 16); | |
| return true; | |
| } | |
| return false; | |
| } | |
| static bool save_char(char &ch, std::string &s, std::vector<std::string> &vec) | |
| { | |
| switch (ch) | |
| { | |
| case ',': | |
| /* save buffer to vector */ | |
| strip_spaces(s); | |
| vec.push_back(s); | |
| s.clear(); | |
| break; | |
| case ';': | |
| case '#': | |
| /* begin of comment */ | |
| return false; | |
| default: | |
| /* append to buffer */ | |
| s.push_back(ch); | |
| break; | |
| } | |
| return true; | |
| } | |
| static std::string decode(std::string in) | |
| { | |
| static char esc_buffer[256] = {0}; | |
| std::vector<std::string> vec; | |
| std::string s, bytes, out; | |
| bool ascii = true; | |
| bool zero_only = true; | |
| /* ".byte" should be followed by a space */ | |
| if (in.empty() || !isspace(in.front())) { | |
| return {}; | |
| } | |
| /* set escape sequences, https://sourceware.org/binutils/docs/as/Strings.html */ | |
| if (!esc) { | |
| esc = esc_buffer; | |
| esc['\b'] = 'b'; | |
| esc['\f'] = 'f'; | |
| esc['\n'] = 'n'; | |
| esc['\r'] = 'r'; | |
| esc['\t'] = 't'; | |
| esc['\\'] = '\\'; | |
| esc['"'] = '"'; | |
| } | |
| /* delete comment */ | |
| auto pos = in.find_first_of("#;"); | |
| if (pos != std::string::npos) { | |
| in.erase(pos); | |
| } | |
| /* split .byte array */ | |
| for (auto &ch : in) { | |
| if (!save_char(ch, s, vec)) { | |
| break; | |
| } | |
| } | |
| strip_spaces(s); | |
| vec.push_back(s); | |
| /* convert entries */ | |
| for (auto &e : vec) { | |
| char ch; | |
| if (!is_hex(e, ch)) { | |
| return {}; | |
| } | |
| bytes.push_back(ch); | |
| } | |
| /* check if we should use .byte or .ascii */ | |
| for (auto &ch : bytes) { | |
| auto i = static_cast<uint8_t>(ch); | |
| if (ch != 0) { | |
| zero_only = false; | |
| } | |
| if (!isprint(ch) && esc[i] == 0 && ch != 0) { | |
| ascii = false; | |
| break; | |
| } | |
| } | |
| if (zero_only && bytes.size() > 1) { | |
| /* .zero line */ | |
| out = ".zero "; | |
| out += std::to_string(bytes.size()); | |
| } else if (ascii && !zero_only) { | |
| /* .ascii line */ | |
| out = ".ascii \""; | |
| for (auto &ch : bytes) { | |
| auto i = static_cast<uint8_t>(ch); | |
| if (esc[i]) { | |
| out += "\\"; | |
| out += esc[i]; | |
| } else if (ch == 0) { | |
| out += "\\000"; | |
| } else { | |
| out += ch; | |
| } | |
| } | |
| out += '"'; | |
| } else { | |
| /* .byte line */ | |
| out = ".byte "; | |
| for (auto &ch : bytes) { | |
| auto i = static_cast<uint8_t>(ch); | |
| if (esc[i]) { | |
| out += " '\\"; | |
| out += esc[i]; | |
| out += "'"; | |
| } else if (isprint(ch)) { | |
| out += " '"; | |
| out += ch; | |
| out += "'"; | |
| } else { | |
| char buf[8]; | |
| snprintf(buf, sizeof(buf), "%02X", i); | |
| out += " 0x"; | |
| out += buf; | |
| } | |
| out += ','; | |
| } | |
| if (out.ends_with(',')) { | |
| out.pop_back(); | |
| } | |
| } | |
| return out; | |
| } | |
| int main(int argc, char **argv) | |
| { | |
| if (argc < 2) { | |
| return 0; | |
| } | |
| std::ifstream ifs(argv[1]); | |
| if (!ifs.is_open()) { | |
| return 1; | |
| } | |
| for (std::string line; std::getline(ifs, line); ) { | |
| size_t pos = line.find_first_not_of(" \t"); | |
| if (pos != std::string::npos) { | |
| auto sub = line.substr(pos); | |
| if (sub.starts_with(".byte") || sub.starts_with(".BYTE")) { | |
| sub.erase(0, 5); | |
| std::string buf = decode(sub); | |
| if (!buf.empty()) { | |
| std::cout << line.substr(0, pos) << buf << std::endl; | |
| continue; | |
| } | |
| } | |
| } | |
| std::cout << line << std::endl; | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment