[Reprinted] the base64 codec program written by Niu Ren in batch

Source: Internet
Author: User
Tags base64 encode

Awesome!

It is estimated that there should be few bugs. Who can optimize it?

 

: Base64.cmd beta -- base64 encoding and decoding 01/28/2007 by 0401
@ Echo off
Setlocal enabledelayedexpansion
Set op =
Set key = abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789 +/=
Set hexstr = 0 1 2 3 4 5 6 7 8 9 A B C D E F
Set D = 0
For % I in (% hexstr %) do set D! D! = % I & set/A D + = 1
If "% ~ 1 "=" "Goto: Help
If "% ~ 1 "= "/? "Goto: Help
For % I In (d e h) Do (
If "% ~ 1 "="/% I "Call: Arg % I % *
If exist "% ~ 1 "(
If exist "% ~ 1/"set error = sorry. base64 encoding is not supported for a directory. & Goto: Error
Set op = encode
Set infile = % ~ Sf1
)
)
If defined error Goto: Error
If defined op Goto: % OP %
Set error = Invalid parameter or file name: % 1. & Goto: Error

: Arge
Set op = encode
: Argd
If not defined OP set op = decode
Set infile = % ~ Sf2
If not defined infile set error = the input file is not specified. & Exit/B
If not exist "% infile %" set error = the specified file cannot be found: % infile %. & Exit/B
Set OUTFILE = % ~ F3
Goto: EOF
: Argh
Set op = Help
Goto: EOF
: Error
Echo % error %
Exit/B

Goto: encode
S file size
L add the 100 h f sub-command to the file to fill in the memory length.
C is only used to calculate the first encoding time.
K judge C1 C2 C3
W each line generates several 4-byte encoding characters. The default value is 18.
If Q is equal to W, a line of encoded strings is output.
N is used to filter out duplicate bytes.
: Encode
> NUL (chcp 437 & graftabl 936)
CLS
For % I in (% infile %) do set S = % ~ Zi
If % S % equ 0 exit/B
If % S % gtr 65535 set error = encoding failed. The maximum file size is 64 KB. & Goto: Error
Set/P = encoding initializing. Please wait. <NUL
Set of = b64enc
Set/P = <NUL> "% ~ Dp0 % of %"
Set/a l = S + 255
Call: D2H % L %
Set L = % hex %
Call: D2H % S %
Echo exit | % comspec %/kprompt F Cs: 100 l % L % 0a $ _ r CX $ _ % hex % $ _ n. t1 $ _ w $ _ f Cs: 100 l % L % 0d $ _ n B. t1 $ _ w $ _ q $ _ | debug> NUL
FC/B % infile % A. T1 | find ":">. T1
FC/B % infile % B. T1 | find ":">. T1
Sort. T1>. T2
Call: time t1
Set n =
Set C = 0
Set K = 0
Set q = 0
Set W = 18
For/F "tokens = 1, 2" % I in (. T2) Do (
If not "! N! "=" % I "(
Set/a d = 0x % J
Set/a k + = 1
If! K! Equ 1 Set C1 =! D!
If! K! Equ 2 set C2 =! D!
If! K! Equ 3 set C3 =! D!
If! K! Equ 3 (
Set/A e1 = "C1> 2!
Set/A e2 = "(C1 & 3) <4) | (c2> 4 )"
Set/A E3 = "(C2 & 15) <2) | (C3> 6 )"
Set/A E4 = "C3 & 63"
Call set b64 =! B64! % Key :~! E1 !, 1% % key :~! E2 !, 1% % key :~! E3 !, 1% % key :~! E4 !, 1% %
Set/a q + = 1
If! Q! Equ! W! (
Echo! B64!> "% ~ Dp0 % of %"
Set b64 =
Set q = 0
If! C! Equ 0 (
Set/a C + = 1
Call: time t2
Set/a t = "s/(w * 3) * (t2-t1)/100"
Echo estimated remaining time! T! Seconds.
)
Set/P = # <NUL
)
Set K = 0
)
)
Set n = % I
)
If! K! Equ 1 (
Set/A e1 = "C1> 2"
Set/A e2 = "(C1 & 3) <4"
Call set b64 =! B64! % Key :~! E1 !, 1% % key :~! E2 !, 1% % =
)
If! K! Equ 2 (
Set/A e1 = "C1> 2"
Set/A e2 = "(C1 & 3) <4) | (c2> 4 )"
Set/A E3 = "(C2 & 15) <2 )"
Call set b64 =! B64! % Key :~! E1 !, 1% % key :~! E2 !, 1% % key :~! E3 !, 1% % =
)
Echo.
If defined b64 echo % b64 %> "% ~ Dp0 % of %"
If defined OUTFILE move/y "% ~ Dp0 % of % "" % OUTFILE %"
Echo encoding is complete.
Del A. t1 B. T1. T1. T2
: Chcp 936> NUL
Exit/B

Goto: Decode
K is used to determine E1 E2 E3 E4
The number of bytes written in each line by the E sub-command. The default value is 24 bytes to generate a debug e sub-command. Can be modified by yourself (3 <= e <= 24)
Hex string after S1 Conversion
M determines whether S1 contains the specified number of bytes.
Q each row has (several bytes * 3) encoded
CX indicates the file size (in bytes). Each operation adds 3 because each 4 bytes is decoded into 3 bytes.
How many offsets does the CS e subcommand write in the CS segment address?
V1 adds 2 bytes to each row (two bytes both press enter and line feed)
V2 rows
3 times of V3 Q to determine the time used to process 1st rows
: Decode
> NUL (chcp 437 & graftabl 936)
CLS
Set/P = decoding <NUL
Set/P = <NUL> dbg. SRC
Set/P V1 = <% infile %
Set/P = % V1 % <NUL>. T1
For % I in (. T1) do set/A Q = % ~ Zi/4 & set V1 = % ~ Zi + 2 & del. T1
For % I in (% infile %) do set/A v2 = % ~ Zi/V1 + 1
Set/A V3 = "Q * 3"
Call: time t1
Set E = 24
Set k = 1
Set M = 1
Set Cx = 0
Set cs = 256
For/F % I in (% infile %) Do (
Set STR = % I
For/L % J in (1, 1, % Q %) Do (
If not defined STR if % j lss % Q % Goto: decend
For/L % K in (0, 1, 3) Do (
For/L % L in (0, 1, 64) Do (
If "! STR :~ % K, 1! "= "! Key :~ % L, 1! "(
If! K! Equ 1 set E1 = % L
If! K! Equ 2 set E2 = % L
If! K! Equ 3 set E3 = % L
If! K! Equ 4 set E4 = % L
Set/a k + = 1
)
)
)
Set/A C1 = "(E1 <2) | (E2> 4 )"
Call: D2H! C1!
Set S1 =! S1! ! Hex!
Set/a C2 = "(E2 & 15) <4) | (E3> 2 )"
Call: D2H! C2!
Set S1 =! S1! ! Hex!
Set/A C3 = "(E3 & 3) <6) | E4"
Call: D2H! C3!
Set S1 =! S1! ! Hex!

Set k = 1
Set/A cx + = 3
Set/a m = Cx "%" E
If! M! Equ 0 (
If! E4! Equ 64 (
Set/a cx-= 1
Set S1 =! S1 :~ 0,-3!
If! E3! Equ 64 (
Set/a cx-= 1
Set S1 =! S1 :~ 0,-3!
)
)
Call: D2H! CS!
Echo e! Hex! ! S1! > Dbg. SRC
Set/a Cs + = E
Set S1 =
)
Set STR =! STR :~ 4!
)
If! CX! Equ % V3 % (
Call: time t2
Set/a TS = t2-t1
Set/a t = "TS * vv2/100"
Echo, total files! V2! Line. It takes about every line! TS! Millisecond, estimated remaining time! T! Seconds.
)
Set/P = # <NUL
)
: Decend
If defined S1 (
If % E4 % equ 64 (
Set/a cx-= 1
Set S1 =! S1 :~ 0,-3!
If % E3 % equ 64 (
Set/a cx-= 1
Set S1 =! S1 :~ 0,-3!
)
)
Call: D2H % CS %
Echo e! Hex! ! S1! > Dbg. SRC
)
Call: D2H % CX %
(Echo R CX
Echo % hex %
Echo n b64dec
Echo W
Echo q)> dbg. SRC
Echo.
Debug <dbg. SRC> NUL
If defined OUTFILE move/y b64dec "% OUTFILE %"
Echo decoding is completed.
Del dbg. SRC
: Chcp 936> NUL
Exit/B

: Time
For/F "tokens = 1-4 delims =:." % I in ("% time %") Do (
Set/A time1 = "% I * 360000"
Set/A time2 = "(1% % j-100) * 6000"
Set/A time3 = "(1% % K-100) * 100"
Set/A time4 = "1% % L-100"
)
Set/A % ~ 1 = time1 + time2 + time3 + time4
Exit/B
: D2H
Set/a d = % 1
Set hex =
If % d % equ 0 set hex = 00 & Exit/B
If % d % gtr 255 (set C = 4) else (set C = 2)
For/L % I in (1, 1, % C %) Do (
Set/a td = D "&" 15
Set/a d ">>=" 4
Call set hex = % d! TD! %! Hex!
)
Exit/B

: Help
For/F "tokens = 1 delims =:" % I in ('findstr/n/C: "= help =" "% ~ F0 "') do more + % I" % ~ F0 "> Con & Goto: EOF
Base64 encode N decode beta by 0401
Usage:
Base64 [/E] filename1 [filename2]
[/D filename1] [filename2]
[/H]
/E base64 encoding operation, processing file size limit 64kb
/D base64 Decoding
Filename1 input file name
Filename2 output file name
/E the default output file name is b64enc.
/D the default output file name is b64dec.
/H help

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.