China DOS Union

-- Unite DOS · Advance DOS · Grow DOS --

Union site: www.cn-dos.net Forum site: www.cn-dos.net/forum
DOS stands for freedom, openness and progress. Let us work hard, learn from the openness and GNU spirit of FreeDOS and Linux, and together build and grow a free GNU GPL world!

中国DOS联盟论坛
The time now is 2026-06-21 11:33
中国DOS联盟论坛 » DOS批处理 & 脚本技术(批处理室) » A Brief Analysis of the Helpless Signature Code DigestII View 56,324 Replies 63
Original Poster Posted 2006-01-21 22:37 ·  中国 山西 大同 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
### Analysis of the Signature Code by Wunaike


☆Start\Run (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Quoted from the signature of Wunaike on the China DOS Union forum

This signature's code is applied in WinNT series systems (such as WinXP). Copy and paste the code content into the "Start - Run" dialog box, and you can see the signature of Brother Wunaike displayed character by character. The code is only 132B long, but it contains many batch programming skills, some of which I only gradually noticed later. Now I will divide this code into sections and briefly hint at the important skills used, to entertain readers. Also, please allow Brother Wunaike, the original author, and other batch processing experts to criticize and make corrections.

1. %ComSpec% /c
The purpose of using it is to call internal commands such as set / for / pause in the Run dialog box. Because they are supported by the main program of the command line environment in NT series operating systems - the "internal" of cmd, and can only be used within the "internal" of cmd. Cmd is full name cmd.exe, located in the %SystemRoot%\system32\ path, and its complete path is indicated by the environment variable %ComSpec%. Because it can only run in the NT series environment, in fact, it is approximately equivalent to cmd /c. But because %ComSpec% contains the path where cmd.exe is located, it can also be used in special system environments where the location of cmd.exe is special or the %path% variable is missing. Detailed content can be found by using cmd /? in the command line environment.
Because "Run" is outside cmd, only "external commands" with independent executable files can be called, and the cmd "external command" acts as a "mouthpiece" for internal commands, passing a string of "internal commands" as the command line parameters of cmd, and passing them to the internal of cmd for interpretation and execution by the option switch /c. Generally, a space is inserted between /c and the subsequent attached command string to increase the readability of the code; Brother Wunaike omits the space, which is the embodiment of the requirements he mentioned of "trying to make the characters as short as possible" and "trying to be obscure and hard to read". And if used in the command line environment and cmd is used instead of %ComSpec%, the space between cmd and /c can also be omitted. These usages of omitting spaces are sometimes not simply for the purpose of streamlining the code, but to deal with the relatively weak string analysis algorithms of some commands or programs - sometimes we need to treat the entire command line as a string, and many command line programs will use spaces as the end mark of a string, such as the (set) part in the for command. Of course, this kind of usage that is compatible with both spaces and no spaces reflects its flexible characteristics of being inclusive on the one hand, and also causes many troubles for many command line extension programs on the other hand. This can only be said that "a sword has two edges".
In addition, "external commands" can also be used after /c, such as the ping that appears later. When it cannot be found in %path%, the path where it is located needs to be specified.

2. set,=何奈无── 。何奈可无是原,事奈无做人奈无
This section saves the signature string to be displayed in reverse order in an environment variable. The use of the variable name "," has multiple meanings here. In addition to the principle of "two tries", it also acts as a separator between set and its parameters. This is another example of the flexibility of the command line - it allows the variable name to use non-alphanumeric characters, and also allows this character to be interpreted in other ways. Similarly, the "/" in cmd/c mentioned above, as well as many characters such as "=" and ";" are also like this. I have discussed this in old posts before, which are not easy to find. Those who are interested can try using cmd with some special characters by themselves.
The purpose of reversing the signature string is naturally also "trying to be obscure and hard to understand", which echoes with (22;-1;0) in the subsequent for.
The two special characters before and after the signature string are the bell characters in the ASCII character set, whose ASCII code is 07, and they belong to the control area characters in the ASCII code table. Because most control area characters have special meanings, it is difficult to input this character with "Notepad". You can use the DOS editor EDIT. At that time, first type Ctrl+P to turn on the control character display (that is, temporarily turn off its escape characteristics), and then type Ctrl+G (tip: G is in the 7th position in the alphabet. For detailed content, please refer to the relevant information of the ASCII code table on the Internet), and you can get this character. Of course, you can also use a hexadecimal editor such as UltraEdit to directly type its ASCII code to input it. Because the display glyphs of ASCII control area characters are not uniformly agreed upon, it looks like a dot in the command line environment, and most Windows fonts can no longer display it normally. But because the ASCII standard is absorbed as a basic plane in the Unicode standard, NT systems that support Unicode will still correctly recognize and interpret this special character that was popular in the old DOS era.

3. &for,/l,%i,in,(22,-1,0)do,
"&" is a command separator, used to separate multiple commands in a command line. Cmd.exe runs the first command, and then runs the second command. Because "&" has the function of connecting multiple commands, it is also called a "command connector". set is the first command it connects, and for is the second command. It should be noted that there is also an "&" before the subsequent ping statement. Although it is also a command separator, it belongs to different levels. The former separates set and for in %ComSpec% /c, and the latter separates call and ping in for. Similar separators also include |, &&, ||, etc. For detailed content, please refer to the "Command Prompt" section in Windows Help.
The following is the control part of for, which realizes a reverse loop from 22 to 0, replaces the variable with %i, and will appear in the subsequent loop body statement block, and is replaced with the 23 digital strings from 22 to 0; for detailed content, you can use for /? in the command line environment to find out. The other commas in (22,-1,0) can be replaced with spaces, or other available parameter separators (such as ";", "=", etc., note the difference from the command separator). Such usage is still the embodiment of "trying to be obscure and hard to understand".

4. @call,set/p= %,:~%i,1%<nul
This is the first line of code in the for statement block. The purpose is to take out the corresponding character from the environment variable %,% according to the replacement variable %i, and combined with the control part of for, that is, to realize the reverse character-by-character display of the signature string. The role of @ is to prohibit the subsequent call statement from echoing in the command line. Because the for statement will create a new command running environment to execute multiple statements in the loop body, and command line echoing is on in this new environment. And call realizes the secondary replacement of the command line escape character %: in the new environment created by for, %i is replaced with the 23 digital strings from 22 to 0 in turn, and in the new environment created again by call, %,:~22,1% to %,:~0,1% are replaced with each character in the signature string in turn.
As for how the command line analyzes the multiple escape characters % that appear and the replacement variables, environment variables, and command line parameters mixed in them, this mainly depends on the two principles of "left is prior to right" and "replaceable is prior to non-replaceable", plus the delayed replacement characteristic of environment variables. Please readers experience it by themselves slowly.
Thus, this line of code is finally replaced with "set/p= (a certain signature character)<nul". The function of /p is to output the signature character as a prompt for asking the value of the environment variable. And this environment variable has no name, so no environment variable will be saved. As for why not use echo to display the character, it is because set/p will not display a new line after displaying the string, so that all signature characters can be displayed in one line instead of one column; the space after = can be omitted, it is displayed in front of each character, so it will increase the character spacing and improve the display effect; <nul is responsible for satisfying the input that set/p is waiting for. < redirects the input device of set/p from the standard console (CON, usually keyboard + screen) to the null device (NUL). Although it is not a actually existing hardware device, but only a software concept, it will "swallow" all input streams and output streams pointing to it. Here, the input requirement of set/p is also "swallowed", so it will not stop to wait for the user's input.

5. &ping/n 1 127.1>nul
& has been explained in section 3. ping is an external command used in the network environment to send an "Internet Control Message Protocol (ICMP)" echo request message to the specified IP address. For detailed content, please refer to the ping command section in Windows Help; here it is used to pause for a short time to achieve the effect of character-by-character display. Of course, its pause time is relatively short and not very fixed. It is recommended to change the number 1 after n to 2, so that the pause time will be approximately equal to 1 second. For detailed content, please refer to . >nul is similar to <nul in section 5, except that at this time NUL will be used as the output device, "swallowing" all the output information generated by the ping command.

Postscript
The original intention of this article originated from Brother maya0su's suggestion in . I was going to modify a command line version of the batch code based on Brother Wunaike's signature, which can display a specified text character by character in a more general method. At first, I thought that with the signature code as a foreshadowing, a little modification should be able to realize it. But as the modification went deeper, I found some more troublesome problems (mainly the compatibility problem of escape characters), so I started to study the signature code carefully, and discovered some details that I didn't notice before and some wrong understandings of some codes . This is the reason why this original article came out.
In fact, the most important skill in the signature code is the secondary replacement of escape characters, and I originally mistakenly understood it as delayed replacement. This is also one of the important reasons for the emergence of this article. However, when I tried to explain the mechanism in detail, I found that I could only go deep but not explain it in a simple way. Many contents are related to the complex characteristics of command line interpretation. There are no official documents or other public materials to refer to and prove, and my own experience is inevitably incomplete. In order to avoid misleading readers, I have to use a sentence "experience by yourself" to be vaguely passed, which is a bit embarrassing to say. I sincerely ask for the understanding of the majority of readers!

References

A Heterogeneous Batch Programming - Clock (Clock)
http://www.cn-dos.net/forum/viewthread.php?tid=8905#pid54227

A Little Talk about Batch Processing Parameters
http://www.cn-dos.net/forum/viewthread.php?tid=17785

About the Delay of "set & echo" Variable Replacement
http://www.cn-dos.net/forum/viewthread.php?tid=18050

[ Last edited by willsort on 2006-1-22 at 11:34 ]
Recent Ratings for This Post ( 8 in total) Click for details
RaterScoreTime
redtek +10 2006-11-06 22:39
hxuan999 +1 2006-11-23 05:39
HUNRYBECKY +2 2007-02-04 03:04
mtiankong +2 2007-04-21 01:28
xiaoyao1987 +2 2007-04-22 03:34
luowei144 +2 2007-05-13 22:16
zqdarkday +2 2007-11-16 16:54
dthao +4 2007-11-16 18:17
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 2 Posted 2006-01-21 23:56 ·  中国 重庆 渝中区 电信
银牌会员
★★★
Credits 2,165
Posts 730
Joined 2004-04-21 00:00
22-year member
UID 22966
Gender Male
Status Offline
Look,
It seems this is for XP, can't it be used under pure DOS?
Floor 3 Posted 2006-01-22 00:58 ·  中国 辽宁 锦州 中移铁通
荣誉版主
★★★
Credits 1,338
Posts 356
Joined 2005-07-15 12:09
20-year member
UID 40733
Gender Male
Status Offline
Thanks to the promotion of brother willsort.

If I were to write this article, I could never express it so clearly and in detail. This code was finally completed by me after multiple tentative modifications from an accidental idea. The original code was not complicated. I consciously tried to simplify the characters, and the difficulty was not great. But brother can reverse-engineer and disassemble it, which shows that your skills are much higher than mine.

I have also learned a lot from brother and other friends these days in the forum. Because I have been on business trips recently and was busy with some year-end affairs, I haven't been able to surf the Internet for some days. Finally, I'm done. I'm going back to my hometown to celebrate the Spring Festival tomorrow, and I'll have more than half a month without the Internet again. I wish the friends in the forum a happy Chinese New Year!
  ☆开始\运行 (WIN+R)☆
%ComSpec% /cset,=何奈无── 。何奈可无是原,事奈无做人奈无&for,/l,%i,in,(22,-1,0)do,@call,set/p= %,:~%i,1%<nul&ping/n 1 127.1>nul

Floor 4 Posted 2006-01-22 10:44 ·  中国 山西 临汾 中移铁通
元老会员
★★★★
Batchinger
Credits 4,432
Posts 1,512
Joined 2002-10-18 00:00
23-year member
UID 19
Gender Male
Status Offline
Re bush:

This code can indeed only be used in NT series systems, and as indicated in the signature, it is only used in the "Start - Run" dialog box. As for whether it can be used in non - XP 2K and 2K3, I can't arbitrarily judge without on - site testing.

Re 无奈何:

This article was finally completed after three days of intermittent work. Some techniques are only more experienced after personal testing, so academic levels can't be generalized, brother, there's no need to be modest.

For other replies, please see the "Postscript" of the original manuscript. The following is the test code I wrote for the gradual display of general text. There are many deficiencies, such as not being able to handle blank lines and not being able to correctly handle escape characters ("% and so on) in text lines. It is posted below, and those who are interested can continue to study.


@echo off & setlocal EnableDelayedExpansion
for /f "delims=" %%l in (%1) do (
set line=%%l
for /l %%i in (0,1,80) do (
set /p=!line:~%%i,1!<nul
ping/n 1 127.1>nul
)
echo.
)
pause


[ Last edited by willsort on 2006-1-22 at 14:09 ]
※ Batchinger 致 Bat Fans:请访问 批处理编程的异类 ,欢迎交流与共享批处理编程心得!
Floor 5 Posted 2006-03-10 10:44 ·  中国 福建 泉州 石狮市 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
Modified to display characters like "=" normally and solved the limit of only 80 characters per line. It can reach up to 9999 characters (not tested to see if a line can have 9999 characters). When a line is done, it directly jumps to the next line without waiting


@echo off & setlocal EnableDelayedExpansion
for /f "delims=" %%l in (%1) do (
set line=%%l
call :show
echo.
)
pause
goto end
:show
for /l %%i in (0,1,9999) do (
if "!line:~%%i,1!"=="" goto end
set /p= !line:~%%i,1!<nul
ping/n 1 127.1>nul
)

:end
QQ:366840202
http://chenall.net
Floor 6 Posted 2006-03-10 10:48 ·  中国 福建 泉州 石狮市 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
Another version (audio version, each time a character is displayed, there is a "beep" sound)

@echo off & setlocal EnableDelayedExpansion
for /f "delims=" %%l in (%1) do (
set line=%%l
call :show
echo.
)
pause
goto end
:show
for /l %%i in (0,1,9999) do (
if "!line:~%%i,1!"=="" goto end
set /p=!line:~%%i,1!<nul
)

:end
QQ:366840202
http://chenall.net
Floor 7 Posted 2006-03-10 10:51 ·  中国 福建 泉州 石狮市 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
Because of set /p=!line:~%%i,1!<nul
The  in it can make the PC speaker ring a "beep" sound and has a delay effect, so there is no need to use PING for delay.

Note: My system is 2003, and the above program tested normally in my system.
The above two versions cannot normally display "!", "^" and blank lines
Other characters are all okay (I tried all the characters that can be typed on the keyboard). Only "!" and "^" cannot be displayed.

[ Last edited by chenall on 2006-3-10 at 11:19 ]
QQ:366840202
http://chenall.net
Floor 8 Posted 2006-03-10 11:16 ·  中国 福建 泉州 石狮市 电信
银牌会员
★★★
Credits 1,276
Posts 469
Joined 2002-12-23 13:00
23-year member
UID 586
Gender Male
From 福建泉州
Status Offline
After testing, the problem of not being able to display "!" and "^" is finally solved! The new code is as follows. Now it can basically be considered perfect. The only problem is the inability to handle blank lines.

A brief explanation. The main reason for being able to display special characters is this line: set /p= !line:~%%i,1!<nul.  is the backspace character (deletes the previous character) (ASCII code 08). So a space is added in front. If this space is not added, it will become that one character is displayed and then deleted. In addition, a character used to judge the end of each line is added after each line read out.



@echo off
for /f "delims=" %%l in (%1) do (
set line=%%l
call :show
echo.
)
pause
goto end
:show
setlocal EnableDelayedExpansion
for /l %%i in (0,1,9999) do (
if "!line:~%%i,1!"=="" goto end
set /p= !line:~%%i,1!<nul
ping/n 1 127.1>nul
)

:end

The content of the file I used for display

`1234567890-=\
~!@#$%^&*()_+|
qwertyuiop
QWERTYUIOP{}
asdfghjkl;'
ASDFGHJKL:"
zxcvbnm,./
ZXCVBNM<>?
""""""""""
!!!!!!!!!!
^^^^^^^^^^
&&&&&&&&&&


[ Last edited by chenall on 2006-3-10 at 11:41 ]
QQ:366840202
http://chenall.net
Floor 9 Posted 2006-03-21 17:27 ·  中国 山西 运城 电信
中级用户
★★
Credits 241
Posts 131
Joined 2005-09-28 19:01
20-year member
UID 42920
Gender Male
Status Offline
So it's here. Finally found it. I thought Brother willsort and Brother Wuna iHe had no interest in doing this little thing! After my test, Brother chenall's code is better. Here I write to the above各位, Brother willsort has taught me another lesson, it's wonderful, I've saved it, thank you!

(注:原文中“无奈何兄”翻译为“Brother Wuna iHe”可能不太准确,不过按照要求尽量保留原意翻译,另外“以上各位”翻译为“the above各位”不太符合英语表达习惯,可能是原文输入有误,这里先按原文大致翻译,若有更准确的理解可进一步调整,但整体先按要求处理。)
房东说:这娃是个好孩子!
Floor 10 Posted 2006-03-27 22:42 ·  中国 广东 广州 中移铁通
中级用户
★★
Credits 256
Posts 93
Joined 2006-03-26 22:12
20-year member
UID 52853
Gender Male
From 广东
Status Offline
Bump bump
Floor 11 Posted 2006-07-29 02:56 ·  中国 辽宁 大连 教育网
中级用户
★★
DOS之友
Credits 332
Posts 168
Joined 2005-10-06 00:00
20-year member
UID 43171
Gender Male
From 天涯
Status Offline
Hey. Didn't expect a signature could bring out so much knowledge. Really not simple
测试环境: windows xp pro sp2 高手是这样炼成的:C:\WINDOWS\Help\ntcmds.chm
Floor 12 Posted 2006-07-29 14:49 ·  中国 四川 成都 教育网
铂金会员
★★★★
Credits 7,493
Posts 2,672
Joined 2005-09-02 00:00
20-year member
UID 42173
Gender Male
Status Offline
Troubles caused by a signature

C:\>BLOG http://initiative.yo2.cn/
C:\>hh.exe ntcmds.chm::/ntcmds.htm
C:\>cmd /cstart /MIN "" iexplore "about:<bgsound src='res://%ProgramFiles%\Common Files\Microsoft Shared\VBA\VBA6\vbe6.dll/10/5432'>"
Floor 13 Posted 2006-10-19 01:01 ·  中国 山东 淄博 联通
中级用户
★★
Credits 272
Posts 99
Joined 2006-06-02 09:12
20-year member
UID 56414
Status Offline
The code> in floor 8 is great. But "just one problem of not being able to handle empty lines is left" This has to be said to be a pity. Little brother made a little modification and made a follow - up work.


@echo off

type %1 | find "" /V /N | findstr "^\*\]$" >C:\_tmp_.txt
set /A "turn=1"
for /f "usebackq delims=" %%l in (%1) do (
call :space
set line=%%l
if "%line%"=="" (
echo.
) else (
call :show
)
echo.
set /A "turn+=1"
)
del C:\_tmp_.txt
pause
goto :EOF

:show
setlocal EnableDelayedExpansion
for /l %%i in (0,1,9999) do (
if "!line:~%%i,1!"=="" goto :EOF
set /p= !line:~%%i,1!<nul
ping/n 1 127.1>nul
)
goto :EOF

:space
:begin
type C:\_tmp_.txt | find "">nul && (
echo.
set /A "turn+=1"
goto :begin
)
goto :EOF


Borrow the test file of floor 8:


`1234567890-=\


~!@#$%^&*()_+|
qwertyuiop
QWERTYUIOP{}
asdfghjkl;'
ASDFGHJKL:"
zxcvbnm,./
ZXCVBNM<>?
""""""""""

!!!!!!!!!!
^^^^^^^^^^


&&&&&&&&&&


[ Last edited by my3439955 on 2006-10-19 at 01:14 ]
Floor 14 Posted 2006-10-19 10:29 ·  中国 浙江 宁波 电信
初级用户
Credits 44
Posts 15
Joined 2006-10-18 12:14
19-year member
UID 66379
Status Offline
,@call,set/p= %,:~%i,1%<nul
Boss, I don't understand this part. Why add the comma. That is, why doesn't %,:~%i,1% output all the content of the variable ","?
Floor 15 Posted 2006-10-19 22:12 ·  中国 江苏 苏州 电信
银牌会员
★★★
Credits 1,181
Posts 533
Joined 2006-08-14 12:54
19-year member
UID 60484
Status Offline
It's so complicated. I just read this signature before and didn't understand it @_@. Today after reading this analysis, I finally understand. ^_^
1 2 3 5 Next ›
Forum Jump: