/****************************************************************************** * * project name: TI-68k Developer Utilities * file name: ttextract.c * initial date: 13/08/2000 * author: thomas.nussbaumer@gmx.net * description: extracts a part of a file which starts after the starttoken * and ends just before the endtoken * ******************************************************************************/ /* This file is part of TI-68k Developer Utilities. This file is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. As a special exception, UNMODIFIED copies of ttextract may also be redistributed or sold without source code, for any purpose. (The Lesser General Public License restrictions do apply in other respects; for example, they cover modification of the program.) This exception notice must be removed on modified copies of this file. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #include #include #include #include "ttversion.h" #include "revtools.h" #ifdef FILE_REVISION #undef FILE_REVISION #endif #define FILE_REVISION "1.8" //============================================================================= // outputs usage information of this tool //============================================================================= void PrintUsage() { PRINT_ID("TTExtract"); fprintf(USAGE_OUT, "Usage: ttextract [flags] []\n\n"\ " -quiet .... don't output standard messages (unsets -v)\n" \ " -v .... output additional messages (unsets -quiet)\n" \ " -sh .... treat start token as hexcode\n" \ " -eh .... treat end token as hexcode\n\n" \ " Extract part of infile which starts right after the start token.\n"\ " If an endtoken is given it ends just before the end token.\n" \ " NOTE1: If you use -sh or -eh the given tokens have to be plain\n" \ " hexcodes WITHOUT any prefix like \"0x\" or \"0h\"\n" \ " NOTE2: if you want the extracted data to be correct, you have to\n"\ " use -fno-unit-at-a-time, as long as this switch is\n" \ " supported by GCC\n\n"); } //============================================================================= // converts hex digit to number //============================================================================= unsigned char ConvHexDigit(unsigned char c) { c = tolower(c); if (c >= '0' && c <= '9') return c -'0'; return c-'a'+10; } //============================================================================= // find given token in file //============================================================================= long FindToken(FILE* fp, char* token, int treat_as_hex) { int length = strlen(token); int i; int index = 0; int input; int found = 0; if (treat_as_hex) { if (length % 2 == 1) { fprintf(stderr,"ERROR: invalid length (%d) for hextoken (%s)\n",length,token); return -1; } for (i=0;i