|
19951001
高级用户
    流落街头
积分 570
发帖 272
注册 2005-10-17 来自 北京
状态 离线
|
『楼 主』:
二进制方式处理文件工具 STR (2020.11.16 更新)
[已使用 LLM 解释]
STR
File: A File to be Processed data. //文件
Start: Offset to Process data. //文件中处理数据的起始位置 (文件的开头位置为0)
Bytes: Bytes to Process. //要处理的字节数量 (如果输入0,则表示从Start开始到文件末尾)
/C: Copy Bytes from File to dstFile. //从源文件向目标文件复制数据
/I: Insert Bytes to dstFile from File. //从源文件向目标文件插入数据,插入数据后目标文件增加 Bytes个字节
dstFile: A File to Write Result data. //目标文件
Address: Offset to Write Result data. //目标文件中要复制或插入数据的位置 (如果目标文件不存在,该值被忽略)
STR
/D: Delete Bytes from File & Write Result to dstFile. //从源文件删除数据并结果写入目标文件
STR
/V: View File In Hex Style. //十六进制方式查看文件
/A: Show ASCII Value. //显示ASCII码
/P: Pause Echo Screen. //满屏暂停
STR :
Bytes: Must be 0. //必须为0
/F: Find String in File. //在文件中查找字符串
/Hex: Hex Style. //十六进制方式
/Asc: ASCII Style. //ASCII码方式
Value: Hex_Digital/ASCII String (40 Characters Maximum). //字符传真的值,最长40个字符 (如果是十六进制串长度应为双数)
/I: Ignore Case. //忽略大小写
/A: Process All Value in File. //在文件中查找所有满足要求的字符串(默认查找1次)
STR :
Bytes: Must be 0. //必须为0
/E: Edit File. //编辑文件
STR
Bytes: Must be 0. //必须为0
/R: Relace SrcString with DstString. //字符常替换
SrcString: :. //源串,可以使用十六进制或ASCII码串
DstString: :. //目标串,可以使用十六进制或ASCII码串
/A: Process All SrcString in File. //在文件中查找所有源字符串(默认替换1次)
说明:
1 使用 /C 和 /I 命令时,如果目的文件不存在,Adress 的值将被忽略,直接执行将源文件从 Start 开始的 Bytes 个字节写入到新创建的目的文件中
2 使用 /C 和 /I 命令时,如果目的文件存在,并且不输入 Address,则Address 默认为0,也就是文件的起始位置。
3 目前处理的单个文件大小不超过 2G
例子:
str 1.exe 0x100 0x10 /d 2.bin 执行将文件1.exe 从位置256开始的16个字节删除,并将结果写入到2.bin,文件2.bin的长度将比1.exe长度小16
str 1.exe 0x100 0x10 /c 2.bin 20 执行将文件1.exe 从位置256开始的16个字节写入到2.bin的20位置,也就是文件2.bin从位置20开始的16字节被覆盖
str 1.exe 0x100 0x10 /i 2.bin 20 执行将文件1.exe 从位置256开始的16个字节插入到2.bin的20位置,结果文件2.bin的长度将增加16字节
str 1.exe 0x100 0 /v /p /a 从文件偏移位置256开始在屏幕上显示文件内容,如果Bytes输入为0,则表示从Start开始至文件末尾。
str 1.exe 0x100 0 /e /hex:11223344 将文件偏移位置256开始的4个字节修改为,0x11,0x22,0x33,0x44。
str 1.exe 0x100 0 /e /asc:1234 将文件偏移位置256开始的4个字节修改为,1234, 也就是0x31,0x32,0x33,0x34
查找替换算法使用的是 KMP 算法,不重复计数,如文件 1.txt 的内容为10个字符0:
0000000000
如执行命令: str 1.txt 0 0 /f /asc:0000 /a
则执行结果为:
Find string At:
0 0x0
4 0x4
不会是:
Find string At:
0 0x0
1 0x1
2 0x2
3 0x3
4 0x4
5 0x5
6 0x6
压缩包中包含DOS版本和Win32版本
2008.12.14 修正替换字符串功能中存在的问题
2008.12.19 修正19楼存在问题
2009.2.15 测试版本在50楼
Last edited by 19951001 on 2020-11-16 at 17:01 ]
STR
File: A File to be Processed data. //File: A file to process data
Start: Offset to Process data. //Start: Offset to process data (the beginning of the file is 0)
Bytes: Bytes to Process. //Bytes: Bytes to process (if 0 is entered, it means from Start to the end of the file)
/C: Copy Bytes from File to dstFile. ///C: Copy bytes from the file to the destination file
/I: Insert Bytes to dstFile from File. ///I: Insert bytes from the file into the destination file, and the destination file will increase by Bytes bytes after insertion
dstFile: A File to Write Result data. //dstFile: A file to write result data
Address: Offset to Write Result data. //Address: Offset to write result data (if the destination file does not exist, this value is ignored)
STR
/D: Delete Bytes from File & Write Result to dstFile. ///D: Delete bytes from the file and write the result to the destination file
STR
/V: View File In Hex Style. ///V: View the file in hexadecimal style
/A: Show ASCII Value. ///A: Show ASCII value
/P: Pause Echo Screen. ///P: Pause the echo screen
STR :
Bytes: Must be 0. //Bytes: Must be 0
/F: Find String in File. ///F: Find string in the file
/Hex: Hex Style. ///Hex: Hexadecimal style
/Asc: ASCII Style. ///Asc: ASCII style
Value: Hex_Digital/ASCII String (40 Characters Maximum). //Value: Hexadecimal number/ASCII string (maximum 40 characters) (if it is a hexadecimal string, the length should be even)
/I: Ignore Case. ///I: Ignore case
/A: Process All Value in File. ///A: Process all values in the file (default to find once)
STR :
Bytes: Must be 0. //Bytes: Must be 0
/E: Edit File. ///E: Edit the file
STR
Bytes: Must be 0. //Bytes: Must be 0
/R: Relace SrcString with DstString. ///R: Replace SrcString with DstString
SrcString: :. //SrcString: :
DstString: :. //DstString: :
/A: Process All SrcString in File. ///A: Process all SrcString in the file (default to replace once)
Instructions:
1 When using the /C and /I commands, if the destination file does not exist, the value of Address will be ignored, and directly execute to write the Bytes bytes from Start in the source file to the newly created destination file
2 When using the /C and /I commands, if the destination file exists and Address is not entered, Address defaults to 0, which is the starting position of the file.
3 The size of a single file currently processed does not exceed 2G
Examples:
str 1.exe 0x100 0x10 /d 2.bin Execute to delete 16 bytes from position 256 in file 1.exe and write the result to 2.bin. The length of file 2.bin will be 16 bytes smaller than that of 1.exe
str 1.exe 0x100 0x10 /c 2.bin 20 Execute to write 16 bytes from position 256 in file 1.exe to position 20 of 2.bin, that is, 16 bytes starting from position 20 in file 2.bin will be overwritten
str 1.exe 0x100 0x10 /i 2.bin 20 Execute to insert 16 bytes from position 256 in file 1.exe into position 20 of 2.bin, and the length of the result file 2.bin will increase by 16 bytes
str 1.exe 0x100 0 /v /p /a Display the file content on the screen starting from file offset position 256. If Bytes is entered as 0, it means from Start to the end of the file.
str 1.exe 0x100 0 /e /hex:11223344 Modify 4 bytes starting from file offset position 256 to 0x11, 0x22, 0x33, 0x44.
str 1.exe 0x100 0 /e /asc:1234 Modify 4 bytes starting from file offset position 256 to 1234, that is, 0x31, 0x32, 0x33, 0x34
The search and replace algorithm uses the KMP algorithm, and does not count repeatedly. For example, the content of file 1.txt is 10 characters 0:
0000000000
If the command is executed: str 1.txt 0 0 /f /asc:0000 /a
Then the execution result is:
Find string At:
0 0x0
4 0x4
It will not be:
Find string At:
0 0x0
1 0x1
2 0x2
3 0x3
4 0x4
5 0x5
6 0x6
The compressed package contains DOS version and Win32 version
Corrected the problem in the replace string function on 2008.12.14
Corrected the problem on floor 19 on 2008.12.19
Test version on 2009.2.15 on floor 50
Last edited by 19951001 on 2020-11-16 at 17:01 ]
LLM 解释
技术背景与功能实现解析
STR 工具是一个针对 DOS 与 Win32 环境设计的命令行二进制文件处理器。在 2000 年代中后期的 DOS 技术社区中,这类工具常用于补丁制作、汉化修改、自动化脚本处理以及对二进制数据流的精确控制。
1. 核心操作模式:复制、插入与删除
该工具通过区分 /C (Copy)、/I (Insert) 和 /D (Delete) 三种模式,解决了二进制文件编辑中常见的对齐与位移问题:
-
覆盖模式 (/C):对应于传统的随机读写(Random Access)操作,使用
fseek 定位后直接 fwrite,目标文件长度仅在写入位置超过原文件末尾时才会增加。
-
插入模式 (/I):这在低级语言实现中较为复杂。程序需要先将目标文件从
Address 处起的后续数据向后移动 Bytes 个字节(通常在内存中缓存或通过临时文件交换),然后再写入源数据。
-
删除模式 (/D):实质是文件的“裁剪”操作,将
Start 之前和 Start + Bytes 之后的数据拼接并写入新文件。
2. 文件大小限制与偏移量处理
帖子提到“处理单个文件大小不超过 2G”。这是由于工具在 32 位环境下开发,使用了符号化的 32 位整数(signed long)来表示文件指针偏移量。在 C 标准库的 fseek 和 ftell 函数中,偏移量上限为 $2^{31}-1$ 字节,即 2,147,483,647 字节(2GB)。若要处理超过 2GB 的文件,在 DOS 环境下需依赖编译器特定的 64 位 I/O 函数(如 _fseeki64)或直接调用中断/系统 API(如 DOS 的 INT 21h, AX=4200h 虽然也是 32 位,但某些扩展环境支持更大寻址)。
3. 搜索算法:KMP 与不重复计数
作者明确指出使用了 KMP (Knuth-Morris-Pratt) 算法。
-
算法优势:KMP 算法在处理二进制搜索时具有 $O(m+n)$ 的时间复杂度,通过预处理模式串的“失配函数”(Partial Match Table),避免了在匹配失败时回溯主串指针,这对于大文件和磁盘 I/O 密集型操作具有显著的性能优势。
-
非重叠匹配逻辑:示例中提到的
0000 匹配 0000000000 得到 0 和 4 的偏移位置。这符合工程实践中的“消耗性匹配”原则——一旦一个子串被匹配,搜索指针会跳转到该子串末尾之后开始下一次搜索。这与文本替换逻辑一致,防止了因重叠而导致的逻辑混淆或死循环替换。
4. 字符串输入与十六进制转换
工具支持 /Hex 和 /Asc 两种输入方式,这在 DOS 开发中是典型的需求。
-
十六进制处理:程序通常需要将输入的字符串(如
112233)按每两个字符一组转换为一个字节。作者提醒“十六进制串长度应为双数”,是为了确保字节对齐,防止在处理末尾孤立字符时产生歧义。
-
大小写忽略 (/I):在二进制搜索中,
/I 通常仅对 /Asc 模式有效。它在内部实现时,会将主串和模式串的字节统一转换为大写(或小写)后再进行比较。
5. 环境兼容性:DOS 与 Win32
提供 DOS 版本通常意味着程序采用了 DJGPP(支持 32 位保护模式的 DPMI 环境)或 Turbo C/Borland C++(16 位实模式)进行编译。考虑到 2GB 文件的处理能力,该 DOS 版本极大概率是基于 DPMI 扩展的 32 位 DOS 程序,否则在 16 位实模式下,处理超过 64KB 的内存缓冲区或 2GB 的文件定位会面临显著的段寻址限制。
6. 异常处理与边界条件
-
目标文件不存在:工具在
/C 或 /I 模式下若找不到 dstFile 会自动创建,此时 Address 失去偏移意义,等同于文件提取。
-
十六进制查看 (/V):这是典型的 Hex Dump 实现,通常以 16 字节为一行,左侧显示十六进制码,右侧显示可打印的 ASCII 字符,不可打印字符则以点号
. 代替。
该工具是典型的 DOS 末期至 Windows 普及期过渡阶段的技术产物,强调对底层数据的直接操纵,而不依赖重型的十六进制编辑器界面,适合在批处理脚本中实现自动化部署。
附件
1: str.rar (2008-12-19 22:27, 18.38 KiB, 下载附件所需积分 1 点
,下载次数: 168)
附件
2: strWin32.rar (2020-11-16 17:00, 20.34 KiB,下载次数: 7)
|
|
2008-4-26 21:45 |
|
|
cchessbd
高级用户
   
积分 525
发帖 222
注册 2006-8-28
状态 离线
|
『第 2 楼』:
使用 LLM 解释/回答一下
可能这个东西我用不到,但是我需要一个DOS下查找替换文件中的某个字符串的工具……
不知道 19950101兄能否帮忙写一个?
比如: XXX /rN file "12345,d" "54321,g"
查找到才替换,或替换一次就退出。
替换所有 /r N次 /rN
此文件可能会有 3~10 M
Last edited by cchessbd on 2008-4-28 at 05:39 PM ]
Maybe I won't need this thing, but I need a tool to find and replace a certain string in a file under DOS...
I wonder if brother 19950101 can help write one?
For example: XXX /rN file "12345,d" "54321,g"
Only replace when found, or exit after replacing once. Replace all /r N times /rN
This file may be 3~10 M
Last edited by cchessbd on 2008-4-28 at 05:39 PM ]
|
|
2008-4-28 17:29 |
|
|
19951001
高级用户
    流落街头
积分 570
发帖 272
注册 2005-10-17 来自 北京
状态 离线
|
『第 3 楼』:
使用 LLM 解释/回答一下
Originally posted by cchessbd at 2008-4-28 17:29:
可能这个东西我用不到,但是我需要一个DOS下查找替换文件中的某个字符串的工具……
不知道 19950101兄能否帮忙写一个?
比如: XXX /rN file "123 ...
只需要对文本文件处理吗?
Originally posted by cchessbd at 2008-4-28 17:29:
Maybe I don't need this thing, but I need a tool to find and replace a certain string in a file under DOS...
I wonder if brother 19950101 can help write one?
For example: XXX /rN file "123 ...
Just for text files?
|
|
2008-5-7 22:52 |
|
|
19951001
高级用户
    流落街头
积分 570
发帖 272
注册 2005-10-17 来自 北京
状态 离线
|
|
2008-11-14 21:20 |
|
|
本是
银牌会员
    
积分 2221
发帖 789
注册 2005-1-27
状态 离线
|
『第 5 楼』:
使用 LLM 解释/回答一下
好工具,但.rar 解压时报错:文件"WIN"头被损坏!
请重新上传!!!
Good tool, but an error occurred when extracting the .rar file: The "WIN" header of the file is corrupted! Please re-upload!
|

my major is english----my love is dos----my teacher is the buddha----my friends--how about U |
|
2008-11-15 00:01 |
|
|
雨露
管理员
          DOS非常爱好者
积分 6215
发帖 2601
注册 2006-1-20
状态 离线
|
『第 6 楼』:
使用 LLM 解释/回答一下
报错后win32文件夹是空的!
After the error, the win32 folder is empty!
|

|
|
2008-11-15 08:07 |
|
|
本是
银牌会员
    
积分 2221
发帖 789
注册 2005-1-27
状态 离线
|
『第 7 楼』:
使用 LLM 解释/回答一下
文件WIN在根目录。
The file WIN is in the root directory.
|

my major is english----my love is dos----my teacher is the buddha----my friends--how about U |
|
2008-11-15 11:13 |
|
|
19951001
高级用户
    流落街头
积分 570
发帖 272
注册 2005-10-17 来自 北京
状态 离线
|
『第 8 楼』:
使用 LLM 解释/回答一下
已经修复破损的压缩包
The damaged compressed package has been repaired
|
|
2008-11-15 11:42 |
|
|
chishingchan
银牌会员
    
积分 1282
发帖 538
注册 2002-11-2
状态 离线
|
『第 9 楼』:
使用 LLM 解释/回答一下
因需修改二进制文件,这种小软件我以前寻找过,只找到纯DOS下的FH,现在这个应该不错。
Because I need to modify binary files, I have looked for such small software before and only found FH under pure DOS. Now this one should be good.
|
|
2008-11-24 21:25 |
|
|
goli2008
中级用户
  
积分 252
发帖 97
注册 2006-9-17
状态 离线
|
『第 10 楼』:
使用 LLM 解释/回答一下
坚决支持,!!
找了好久,请楼主将标题改改,可好,方便其他人搜索,谢谢!
Firmly support,!!
Searched for a long time, please the landlord change the title, okay, convenient for others to search, thank you!
|
|
2008-12-13 11:07 |
|
|
chenall
银牌会员
    
积分 1276
发帖 469
注册 2002-12-23 来自 福建泉州
状态 离线
|
|
2008-12-13 18:10 |
|
|
chenall
银牌会员
    
积分 1276
发帖 469
注册 2002-12-23 来自 福建泉州
状态 离线
|
『第 12 楼』:
使用 LLM 解释/回答一下
可不可以再加个功能"字符转义"
比如\0 就是ASCII码00
比如
strw test.bin 0x10 0 /e /asc:test\13\0
会写入test和0X0D和0X00
Can we add a "character escape" function? For example, \0 is ASCII code 00. For example, strw test.bin 0x10 0 /e /asc:test\13\0 will write test and 0X0D and 0X00.
|

QQ:366840202
http://chenall.net |
|
2008-12-13 19:10 |
|
|
19951001
高级用户
    流落街头
积分 570
发帖 272
注册 2005-10-17 来自 北京
状态 离线
|
『第 13 楼』:
使用 LLM 解释/回答一下
Originally posted by chenall at 2008-12-13 19:10:
可不可以再加个功能"字符转义"
比如\0 就是ASCII码00
比如
strw test.bin 0x10 0 /e /asc:test\13\0
会写入test和0X0D和0X00
理论上可以实现,不过我近些日子比较忙,暂时没有时间修改
其实也可以这样实现
strw test.bin 0x10 0 /e /asc:test
strw test.bin 0x14 0 /e /hex:1300
上述两条命令可以实现你要的功能
Last edited by 19951001 on 2008-12-13 at 23:18 ]
Originally posted by chenall at 2008-12-13 19:10:
Can you add another function "character escaping"?
For example, \0 is ASCII code 00
For example
strw test.bin 0x10 0 /e /asc:test\13\0
will write test and 0X0D and 0X00
Theoretically, it can be implemented, but I've been quite busy these days and don't have time to modify it for the moment.
Actually, it can be implemented like this:
strw test.bin 0x10 0 /e /asc:test
strw test.bin 0x14 0 /e /hex:1300
The above two commands can achieve the function you want.
Last edited by 19951001 on 2008-12-13 at 23:18 ]
|
|
2008-12-13 22:28 |
|
|
chenall
银牌会员
    
积分 1276
发帖 469
注册 2002-12-23 来自 福建泉州
状态 离线
|
『第 14 楼』:
使用 LLM 解释/回答一下
嗯,上面是可以实现,只是如果放在批处理里面要计算字符长度会比较麻烦.
另一个问题,不知程序有没有返回errorleve值?
比如查找,成功返回?失败返回?
Well, the above can be achieved, but it will be more troublesome to calculate the character length if it is placed in a batch file.
Another question, I wonder if the program returns an errorlevel value?
For example, for a search, what is returned on success? What is returned on failure?
|

QQ:366840202
http://chenall.net |
|
2008-12-14 09:43 |
|
|
chenall
银牌会员
    
积分 1276
发帖 469
注册 2002-12-23 来自 福建泉州
状态 离线
|
『第 15 楼』:
使用 LLM 解释/回答一下
查找替换功能好像有些问题..
文件A.TXT内容
Exe =$1\TEST
Exe =$1\TEST2
strw a.txt 0 0 /r /asc:$1 /asc:j: /a
得到的结果为乱码,而且丢失字符
正确的结果
Exe =J:\TEST
Exe =J:\TEST2
There seems to be some issues with the find - replace function..
The content of file A.TXT
Exe =$1\TEST
Exe =$1\TEST2
Executing strw a.txt 0 0 /r /asc:$1 /asc:j: /a results in garbled code and loss of characters
The correct result
Exe =J:\TEST
Exe =J:\TEST2
|

QQ:366840202
http://chenall.net |
|
2008-12-14 10:14 |
|