httpͨѶ¼°ä¯ÀÀÆ÷ÖеÄHTML±àÂë¡¢URL±àÂë¡¢base64±àÂ뼰תÒå
Ŀ¼
httpͨѶ¼°ä¯ÀÀÆ÷ÖеÄHTML±àÂë¡¢URL±àÂë¡¢base64±àÂ뼰תÒå
1¡¢html±àÂëµÄÌØÊâ×Ö·û
1.1¡¢HTML ʵÌå
1.2¡¢HTML×Ö·ûʵÌå,Êý¾ÝµÄ±í´ï
2¡¢URL±àÂëµÄÌØÊâ×Ö·û
2.1¡¢URLÖ§³ÖµÄ×Ö·û ? ? ? ?
2.2¡¢ASCII×Ö·û±àÂë±í
3¡¢Base64±àÂëµÄÌØÊâ×Ö·û
3.1¡¢base64ÔÚhttpºÍhtmlÖÐÓÐÐí¶àʹÓó¡¾°:
3.2¡¢base64×Ö·û±àÂë±í
3.3¡¢base64×Ö½Ú¹æÔò¼°±àÂë²½Öè
1¡¢html±àÂëµÄÌØÊâ×Ö·û
? ? ? ? HTML encoding character
? ? ? ? HTML ºÍ XHTML Óñê×¼µÄ 7 ±ÈÌØ ASCII ´úÂëÔÚÍøÂçÉÏ´«ÊäÊý¾Ý¡£
1.1¡¢HTML ʵÌå
? ? ? ? HTMLÓïÑÔ Öеġ°±£Áô×Ö¡±,¼´Ä³Ð©Ô¤Áô×Ö·û¡£ ? ? ? ? ±ÈÈç:²»ÄÜÔÚ³ý¡°ÔªËØ¡±ÒâÍâµÄλÖÃ,ʹÓÃСÓÚºÅ(<)ºÍ´óÓÚºÅ(>),ä¯ÀÀÆ÷»áÎó½âΪËüÃÇÊDZêÇ©¡£ ? ? ? ? ÈôÓÐÐèÒªÈÃä¯ÀÀÆ÷ÕýÈ·µØÏÔʾԤÁô×Ö·û,ÎÒÃDZØÐëÔÚ HTML Ô´´úÂëÖÐʹÓÃ×Ö·ûʵÌå(character entities)¡£
1.2¡¢HTML×Ö·ûʵÌå,Êý¾ÝµÄ±í´ï
1¡¢&ºÅ?¡°¼Ó¡± ÌØÊâ×Ö·ûµÄ¡°Ô¼¶¨ÊµÌåÃû³Æ¡±? ¡°¼Ó¡± ;ºÅ,±ÈÈç·ûºÅ:? ?< ,±í´ïΪ:? ? <
×¢Òâ,ʵÌåÃû³Æ:´óСдÃô¸Ð¡£»òÕß:
2¡¢&ºÅ?¡°¼Ó¡± #?¡°¼Ó¡± ÌØÊâ×Ö·ûµÄASCIIÂë? ¡°¼Ó¡± ;ºÅ,±ÈÈç·ûºÅ:? ?< ,±í´ïΪ:? ? <
? ? ? ? html±£Áô×ÖµÄ×Ö·ûʵÌå±í:
ÏÔʾ½á¹û | ÃèÊö | ʵÌåÃû³Æ | ʵÌå±àºÅ |
---|
| ¿Õ¸ñ | |   | < | СÓںŠ| < | < | > | ´óÓںŠ| > | > | & | ºÍºÅ | & | & | " | ÒýºÅ | " | " | ' | ƲºÅ? | ' (IE²»Ö§³Ö) | ' | ¢ | ·Ö | ¢ | ¢ | ¡ê | °÷ | £ | £ | £¤ | ÈÕÔ² | ¥ | ¥ | € | Å·Ôª | € | € | ¡ì | С½Ú | § | § | ? | °æȨ | © | © | ? | ×¢²áÉ̱ê | ® | ® | ? | É̱ê | ™ | ™ | ¡Á | ³ËºÅ | × | × | ¡Â | ³ýºÅ | ÷ | ÷ |
2¡¢URL±àÂëµÄÌØÊâ×Ö·û
? ? ? ? URL encoding?character
2.1¡¢URLÖ§³ÖµÄ×Ö·û ? ? ? ?
? ? ? ? ÓÉÓÚURLÖ»Ö§³ÖÓ¢ÎÄ×Öĸ¡¢Êý×Ö¡¢ºá¸Ü¡¢Ï»®Ïß¡¢¾äµã¡¢²¨ÀËÏß,ÈôÒª±íʾÆäËû×Ö·ûÔòÐèÒª±àÂë¡£ ? ? ? ? ÀýÈç:°Ù·ÖºÅ¡¢ÖÐÎÄ¡£ ? ? ? ? ÓÉÓÚ°Ù·ÖºÅÒ²ÐèÒª±àÂë,Òò´Ë»á³öÏÖijЩÈƹýÎÊÌâ¡£
¢Ù¡¢URL±àÂë»ùÓÚASCIIÂë:
- URLÖÐÎÞÌØÊâ×Ö·û: È«ÊÇASCII¿ÉÏÔʾ×Ö·û
https://www.cpuofbs.com/rest/postdata?A=abc&B=123 https://www.cpuofbs.com/rest/postdata?A=abc&B=123
- URLÖаüº¬ÌØÊâ×Ö·û: ¿Õ¸ñ ¡¢(¶ÙºÅµÈµÈ)
https://www.cpuofbs.com/rest/postdata?A= abc&B¡¢=123 https://www.cpuofbs.com/rest/postdata?A=%20abc&B%E3%80%81=123
https://www.cpuofbs.com/rest/postdata?ѧÉúÐÕÃû=Ó¢ÎÄÃûabc&Éí·ÝÖ¤ºÅsfzh=432345678909876543 https://www.cpuofbs.com/rest/postdata?%E5%AD%A6%E7%94%9F%E5%A7%93%E5%90%8D=%E8%8B%B1%E6%96%87%E5%90%8Dabc&%E8%BA%AB%E4%BB%BD%E8%AF%81%E5%8F%B7sfzh=432345678909876543 ?
¢Ú¡¢ÌØÊâ×Ö·û,Ïȸù¾Ýµ±Ç°Ò³ÃæµÄ±àÂ뷽ʽת»»,±ÈÈç:
<meta charset="utf-8" content-type="text/html" />,
È¡ÆäÊ®Áù½øÖÆÐÎʽ,ÿÁ½Î»Ìí¼Ó1¸ö%
2.2¡¢ASCII×Ö·û±àÂë±í
? ? ? ?HTML ºÍ XHTML Óñê×¼µÄ 7 ±ÈÌØ ASCII ´úÂëÔÚÍøÂçÉÏ´«ÊäÊý¾Ý¡£
2.2.1¡¢¡°32~126¡±Çø¶Î,¿ÉÏÔʽÇø¶Î:
½á¹û | ÃèÊö | ʵÌå±àºÅ |
---|
| space |   | ! | exclamation mark | ! | " | quotation mark | " | # | number sign | # | $ | dollar sign | $ | % | percent sign | % | & | ampersand | & | ' | apostrophe | ' | ( | left parenthesis | ( | ) | right parenthesis | ) | * | asterisk | * | + | plus sign | + | , | comma | , | - | hyphen | - | . | period | . | / | slash | / | 0 | digit 0 | 0 | 1 | digit 1 | 1 | 2 | digit 2 | 2 | 3 | digit 3 | 3 | 4 | digit 4 | 4 | 5 | digit 5 | 5 | 6 | digit 6 | 6 | 7 | digit 7 | 7 | 8 | digit 8 | 8 | 9 | digit 9 | 9 | : | colon | : | ; | semicolon | ; | < | less-than | < | = | equals-to | = | > | greater-than | > | ? | question mark | ? | @ | at sign | @ | A | uppercase A | A | B | uppercase B | B | C | uppercase C | C | D | uppercase D | D | E | uppercase E | E | F | uppercase F | F | G | uppercase G | G | H | uppercase H | H | I | uppercase I | I | J | uppercase J | J | K | uppercase K | K | L | uppercase L | L | M | uppercase M | M | N | uppercase N | N | O | uppercase O | O | P | uppercase P | P | Q | uppercase Q | Q | R | uppercase R | R | S | uppercase S | S | T | uppercase T | T | U | uppercase U | U | V | uppercase V | V | W | uppercase W | W | X | uppercase X | X | Y | uppercase Y | Y | Z | uppercase Z | Z | [ | left square bracket | [ | \ | backslash | \ | ] | right square bracket | ] | ^ | caret | ^ | _ | underscore | _ | ` | grave accent | ` | a | lowercase a | a | b | lowercase b | b | c | lowercase c | c | d | lowercase d | d | e | lowercase e | e | f | lowercase f | f | g | lowercase g | g | h | lowercase h | h | i | lowercase i | i | j | lowercase j | j | k | lowercase k | k | l | lowercase l | l | m | lowercase m | m | n | lowercase n | n | o | lowercase o | o | p | lowercase p | p | q | lowercase q | q | r | lowercase r | r | s | lowercase s | s | t | lowercase t | t | u | lowercase u | u | v | lowercase v | v | w | lowercase w | w | x | lowercase x | x | y | lowercase y | y | z | lowercase z | z | { | left curly brace | { | | | vertical bar | | | } | right curly brace | } | ~ | tilde | ~ |
2.2.2¡¢¡°0~30¼°¶ÀÁ¢µÄ127¡±Çø¶Î,É豸¿ØÖÆÇø¶Î:
? ? ? ?ASCIIÉ豸¿ØÖÆ´úÂë×î³õ±»Éè¼ÆΪÓÃÀ´¿ØÖÆÖîÈç´òÓ¡»úºÍ´Å´øÇý¶¯Æ÷Ö®ÀàµÄÓ²¼þÉ豸¡£ÔÚHTMLÎĵµÖÐÕâЩ´úÂë²»»áÆðÈκÎ×÷ÓÃ:
½á¹û | ÃèÊö | ʵÌå±àºÅ |
---|
NUL | null character | � | SOH | start of header |  | STX | start of text |  | ETX | end of text |  | EOT | end of transmission |  | ENQ | enquiry |  | ACK | acknowledge |  | BEL | bell (ring) |  | BS | backspace |  | HT | horizontal tab | 	 | LF | line feed | | VT | vertical tab |  | FF | form feed |  | CR | carriage return | | SO | shift out |  | SI | shift in |  | DLE | data link escape |  | DC1 | device control 1 |  | DC2 | device control 2 |  | DC3 | device control 3 |  | DC4 | device control 4 |  | NAK | negative acknowledge |  | SYN | synchronize |  | ETB | end transmission block |  | CAN | cancel |  | EM | end of medium |  | SUB | substitute |  | ESC | escape |  | FS | file separator |  | GS | group separator |  | RS | record separator |  | US | unit separator |  | DEL | delete (rubout) |  |
3¡¢Base64±àÂëµÄÌØÊâ×Ö·û
? ? ? ? Base64 encoding character
3.1¡¢base64ÔÚhttpºÍhtmlÖÐÓÐÐí¶àʹÓó¡¾°:
1¡¢±ÈÈçÎÒÃÇÔÚhtmlÖбí´ï1¸öͼƬÎļþ½øÐÐbase64±àÂëµÄsrc×ÊÔ´,¿ÉÒÔ:
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAACCAYAAACZgbYnAAAAE0lEQVQImWP4f4bLly//BwAmVgd1/w11/gAAAABJRU5ErkJggg==
?2¡¢ÔÙ±ÈÈç?¶ÔÎļþ½øÐÐbase64±àÂë,ÎÒÃÇÉÏ´«ºÍÏÂÔس¬´óÎļþ,¿ÉÏȶÔÆä½øÐÐbase64¶þ½øÖƱàÂëÔÙѹËõºó,½øÐÐ,ÕâÑù¿ÉÒÔ¼«´óÌáÉýЧÂʲ¢ÌṩÁË¡°¶Ïµã¡±Ðø´«µÄ¿ÉÄÜ
? ? ? ? °¸Àý:delphiѹËõºóʹÓÃhttpÐÒébase64ÉÏ´«ÏÂÔØ6G³¬´óÎļþ_pulledupµÄ²©¿Í-CSDN²©¿ÍdelphiѹËõºóʹÓÃhttpÐÒébase64ÉÏ´«ÏÂÔØ6G³¬´óÎļþ×¢:·þÎñ¶ËÈí¼þ,ʹÓøßÓ³öÆ·GYRestServerϵÁС£»¶ÓʹÓÃ,¼ÓQQȺ×Éѯ:174483085Ò»¡¢ÖªÊ¶µã:1¡¢Delphi×Ô´øµÄѹËõ½âѹµ¥Ôªsystem.zlib.pasÖкËÐĺ¯ÊýµÄʹÓÃ2¡¢·þÎñ¶ËhttpÐÒéContentType(mime-type)Ïà¹ØÁбíÀàÐ͵Ä×¢²á3¡¢Base64±àÂëµÄ¹æÔò4¡¢ÎªºÎÒª·Ö¿é¶ÏµãÐø´«,²¢Ê¹ÓÃTFileStreamÎļþÁ÷Ìæ´úÄÚ´æÁ÷TMemoryStream5¡¢Buffe..https://blog.csdn.net/pulledup/article/details/121455926
3.2¡¢base64×Ö·û±àÂë±í
base64_°Ù¶È°Ù¿ÆBase64ÊÇÍøÂçÉÏ×î³£¼ûµÄÓÃÓÚ´«Êä8Bit×Ö½ÚÂëµÄ±àÂ뷽ʽ֮һ,Base64¾ÍÊÇÒ»ÖÖ»ùÓÚ64¸ö¿É´òÓ¡×Ö·ûÀ´±íʾ¶þ½øÖÆÊý¾ÝµÄ·½·¨¡£¿É²é¿´RFC2045~RFC2049,ÉÏÃæÓÐMIMEµÄÏêϸ¹æ·¶¡£Base64±àÂëÊÇ´Ó¶þ½øÖƵ½×Ö·ûµÄ¹ý³Ì,¿ÉÓÃÓÚÔÚHTTP»·¾³Ï´«µÝ½Ï³¤µÄ±êʶÐÅÏ¢¡£²ÉÓÃBase64±àÂë¾ßÓв»¿É¶ÁÐÔ,ÐèÒª½âÂëºó²ÅÄÜÔĶÁ¡£Base64ÓÉÓÚÒÔÉÏÓŵ㱻¹ã·ºÓ¦ÓÃÓÚ¼ÆËã»úµÄ¸÷¸öÁìÓò,È»¶øÓÉÓÚÊä³öÄÚÈÝÖаüÀ¨Á½¸öÒÔÉÏ¡°·ûºÅÀࡱ×Ö·û(+, /, =),²»Í¬µÄÓ¦Óó¡¾°ÓÖ·Ö±ðÑÐÖÆÁËBase64µÄ¸÷ÖÖ¡°±äÖÖ¡±¡£ÎªÍ³Ò»ºÍ¹æ·¶»¯Base64µÄÊä³ö,Base62x±»ÊÓΪÎÞ·ûºÅ»¯µÄ¸Ä½ø°æ±¾¡£https://baike.baidu.com/item/base64/8545775?fr=aladdin
? ? ? ? ´óдA~Z ,Сдa~z ,?Êý×Ö0~9, +? \? ,ÒÔ¼°1¸öÌØÊâµÄ¡°×Ö·ûA¡±½áβ±êʶ·û ==?
6λ¶þ½øÖƲ¹ÆëºóµÄ10½øÖÆË÷Òý
|
base64
×Ö·û
| 6λ¶þ½øÖƲ¹ÆëºóµÄ10½øÖÆË÷Òý | base64×Ö·û | 6λ¶þ½øÖƲ¹ÆëºóµÄ10½øÖÆË÷Òý | base64×Ö·û | 6λ¶þ½øÖƲ¹ÆëºóµÄ10½øÖÆË÷Òý | base64×Ö·û |
0
|
A
|
16
|
Q
|
32
|
g
|
48
|
w
|
1
|
B
|
17
|
R
|
33
|
h
|
49
|
x
|
2
|
C
|
18
|
S
|
34
|
i
|
50
|
y
|
3
|
D
|
19
|
T
|
35
|
j
|
51
|
z
|
4
|
E
|
20
|
U
|
36
|
k
|
52
|
0
|
5
|
F
|
21
|
V
|
37
|
l
|
53
|
1
|
6
|
G
|
22
|
W
|
38
|
m
|
54
|
2
|
7
|
H
|
23
|
X
|
39
|
n
|
55
|
3
|
8
|
I
|
24
|
Y
|
40
|
o
|
56
|
4
|
9
|
J
|
25
|
Z
|
41
|
p
|
57
|
5
|
10
|
K
|
26
|
a
|
42
|
q
|
58
|
6
|
11
|
L
|
27
|
b
|
43
|
r
|
59
|
7
|
12
|
M
|
28
|
c
|
44
|
s
|
60
|
8
|
13
|
N
|
29
|
d
|
45
|
t
|
61
|
9
|
14
|
O
|
30
|
e
|
46
|
u
|
62
|
+
|
15
|
P
|
31
|
f
|
47
|
v
|
63
|
/
|
base64×Ö·ûË÷Òý¡¢¶þ½øÖÆ¡¢base64×Ö·û?¶ÔÕÕ±í:
Ë÷Òý | 2½øÖÆ | Char | | Ë÷Òý | 2½øÖÆ | Char | | Ë÷Òý | 2½øÖÆ | Char | | Ë÷Òý | 2½øÖÆ | Char |
---|
0 | 000000 | A | 16 | 010000 | Q | 32 | 100000 | g | 48 | 110000 | w | 1 | 000001 | B | 17 | 010001 | R | 33 | 100001 | h | 49 | 110001 | x | 2 | 000010 | C | 18 | 010010 | S | 34 | 100010 | i | 50 | 110010 | y | 3 | 000011 | D | 19 | 010011 | T | 35 | 100011 | j | 51 | 110011 | z | 4 | 000100 | E | 20 | 010100 | U | 36 | 100100 | k | 52 | 110100 | 0 | 5 | 000101 | F | 21 | 010101 | V | 37 | 100101 | l | 53 | 110101 | 1 | 6 | 000110 | G | 22 | 010110 | W | 38 | 100110 | m | 54 | 110110 | 2 | 7 | 000111 | H | 23 | 010111 | X | 39 | 100111 | n | 55 | 110111 | 3 | 8 | 001000 | I | 24 | 011000 | Y | 40 | 101000 | o | 56 | 111000 | 4 | 9 | 001001 | J | 25 | 011001 | Z | 41 | 101001 | p | 57 | 111001 | 5 | 10 | 001010 | K | 26 | 011010 | a | 42 | 101010 | q | 58 | 111010 | 6 | 11 | 001011 | L | 27 | 011011 | b | 43 | 101011 | r | 59 | 111011 | 7 | 12 | 001100 | M | 28 | 011100 | c | 44 | 101100 | s | 60 | 111100 | 8 | 13 | 001101 | N | 29 | 011101 | d | 45 | 101101 | t | 61 | 111101 | 9 | 14 | 001110 | O | 30 | 011110 | e | 46 | 101110 | u | 62 | 111110 | + | 15 | 001111 | P | 31 | 011111 | f | 47 | 101111 | v | 63 | 111111 | / | Padding Ìî³äλ | = | |
?´úÂë±íʾBase64×Ö·û±í:
Shotgun.Js.Base64 = {
_table: [
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/'
],
3.3¡¢base64×Ö½Ú¹æÔò¼°±àÂë²½Öè
3.3.1¡¢·Çbase64µÄ×Ö·ûÄÚ´æ±à×鷽ʽ
? ? ? ? ͨ³£,¼ÆËã»úµÄÄÚ´æÖÐÓÃ8λ¶þ½øÖƱí´ï1¸ö×Ö·û¡£
? ? ? ? ±ÈÈç×Ö·ûA :0100 0001 :
01000001
3.3.2¡¢base64µÄ×Ö·ûÄÚ´æ±à×鷽ʽ
? ? ? ? ½«Ô±¾1*8=8,¸Ä±àΪ2*(2²¹Æë+6)=16??
? ? ? ? ±ÈÈç×Ö·ûA :
00010000 00010000
²éÕÒASCII±í10½øÖƽá¹û:
16 16
»ò(±ê×¼¸ñʽ):
00010000 00010000 00000000 00000000
16 16?NUL(null)?NUL(null)
? ? ? ? È»ºó,±ÈÕÕbase64×Ö·û±àÂë±í:
16 16
½á¹û:
QQ
»ò(±ê×¼¸ñʽ):
16 16?NUL(null)?NUL(null)? ---------00000000 00000000¸ÕºÃ±È¶Ô³ö2¸ö¡°A¡±ÔòÓÃ==±íʾ:
QQ==
3.3.3¡¢jsÖÐbase64µÄ±àÂëºÍ½âÂë:
// ½â¾ö·½°¸ #1 ¨C ÔÚ±àÂë֮ǰתÒå×Ö·û´®---unescapeºÍescapeÒÑÆúÓà :
function utf8_to_b64(str) {
return window.btoa(unescape(encodeURIComponent(str)));
}; //
function b64_to_utf8(str) {
return decodeURIComponent(escape(window.atob(str)));
}; //
const bin8 = '01000001', // 00010000 00010000
bin16 = '0001000000010000', // 00010000 00010000 ´ú±íQQ
bin32 = '00010000000100000000000000000000'; // 00010000 00010000 00000000 00000000 ´ú±íQQ==
//
//00010000 00010000
let encodedData = window.btoa("A"); // = Base64±àÂë (ȱÏÝ)
console.log(encodedData); //: QQ==
let decodedData = window.atob("QQ=="); // = ASCII±àÂë (ȱÏÝ)
let decodedData_2 = window.atob("QQ=="); // = ASCII±àÂë (ȱÏÝ)
console.log(decodedData); //:
console.log(utf8_to_b64("A")); //: (ȱÏÝ)
console.log(b64_to_utf8("QQ==")); //: ±ØÐë2**nÃÝµÈ //console.log(2 ** 4);//: 2**nÃÝÔËËã
console.log(b64_to_utf8("QQ")); //: (ȱÏÝ)
//
//½â¾ö·½°¸ #2 ¨C ÖØдatob()ºÍbtoa()ʹÓÃTypedArrays ºÍ UTF-8 :
//×¢Òâ:ÒÔÏ´úÂë¶ÔÓÚ´Ó Base64 ×Ö·û´®»ñÈ¡ArrayBufferÒ²ºÜÓÐÓÃ,·´Ö®ÒàÈ»(¼ûÏÂÎÄ):
//
"use strict";
// ×Ö½ÚÊý×éµ½Base64×Ö·û´®½âÂë :
function b64ToUint6(nChr) {
return nChr > 64 && nChr < 91 ?
nChr - 65 :
nChr > 96 && nChr < 123 ?
nChr - 71 :
nChr > 47 && nChr < 58 ?
nChr + 4 :
nChr === 43 ?
62 :
nChr === 47 ?
63 :
0;
}
function base64DecToArr(sBase64, nBlocksSize) {
const sB64Enc = sBase64.replace(/[^A-Za-z0-9+/]/g, "");
const nInLen = sB64Enc.length;
const nOutLen = nBlocksSize ? Math.ceil((nInLen * 3 + 1 >> 2) / nBlocksSize) * nBlocksSize : nInLen * 3 + 1 >> 2;
const taBytes = new Uint8Array(nOutLen);
let nMod3;
let nMod4;
let nUint24 = 0;
let nOutIdx = 0; //let nOutId = 0;
for (let nInIdx = 0; nInIdx < nInLen; nInIdx++) {
nMod4 = nInIdx & 3;
nUint24 |= b64ToUint6(sB64Enc.charCodeAt(nInIdx)) << 6 * (3 - nMod4);
if (nMod4 === 3 || nInLen - nInIdx === 1) {
nMod3 = 0;
while (nMod3 < 3 && nOutIdx < nOutLen) {
taBytes[nOutIdx] = nUint24 >>> (16 >>> nMod3 & 24) & 255;
nMod3++;
nOutIdx++;
}
nUint24 = 0;
}
}
return taBytes;
}
/* Base64×Ö·û´®µ½Êý×é±àÂë : */
function uint6ToB64(nUint6) {
return nUint6 < 26 ?
nUint6 + 65 :
nUint6 < 52 ?
nUint6 + 71 :
nUint6 < 62 ?
nUint6 - 4 :
nUint6 === 62 ?
43 :
nUint6 === 63 ?
47 :
65;
}
function base64EncArr(aBytes) {
let nMod3 = 2;
let sB64Enc = "";
const nLen = aBytes.length;
let nUint24 = 0;
for (let nIdx = 0; nIdx < nLen; nIdx++) {
nMod3 = nIdx % 3;
if (nIdx > 0 && (nIdx * 4 / 3) % 76 === 0) {
sB64Enc += "\r\n";
}
nUint24 |= aBytes[nIdx] << (16 >>> nMod3 & 24);
if (nMod3 === 2 || aBytes.length - nIdx === 1) {
sB64Enc += String.fromCodePoint(uint6ToB64(nUint24 >>> 18 & 63), uint6ToB64(nUint24 >>> 12 & 63), uint6ToB64(nUint24 >>> 6 & 63), uint6ToB64(nUint24 & 63));
nUint24 = 0;
}
}
return sB64Enc.substr(0, sB64Enc.length - 2 + nMod3) + (nMod3 === 2 ? '' : nMod3 === 1 ? '=' : '==');
}
/* UTF-8Êý×éµ½JS×Ö·û´®,·´Ö®ÒàÈ» : */
function UTF8ArrToStr(aBytes) {
let sView = "";
let nPart;
const nLen = aBytes.length;
for (let nIdx = 0; nIdx < nLen; nIdx++) {
nPart = aBytes[nIdx];
sView += String.fromCodePoint(
nPart > 251 && nPart < 254 && nIdx + 5 < nLen ? /* six bytes */
/* (nPart - 252 << 30) may be not so safe in ECMAScript! So¡: */
(nPart - 252) * 1073741824 + (aBytes[++nIdx] - 128 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128 :
nPart > 247 && nPart < 252 && nIdx + 4 < nLen ? /* five bytes */
(nPart - 248 << 24) + (aBytes[++nIdx] - 128 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128 :
nPart > 239 && nPart < 248 && nIdx + 3 < nLen ? /* four bytes */
(nPart - 240 << 18) + (aBytes[++nIdx] - 128 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128 :
nPart > 223 && nPart < 240 && nIdx + 2 < nLen ? /* three bytes */
(nPart - 224 << 12) + (aBytes[++nIdx] - 128 << 6) + aBytes[++nIdx] - 128 :
nPart > 191 && nPart < 224 && nIdx + 1 < nLen ? /* two bytes */
(nPart - 192 << 6) + aBytes[++nIdx] - 128 :
/* nPart < 127 ? */
/* one byte */
nPart
);
}
return sView;
}
function strToUTF8Arr(sDOMStr) {
let aBytes;
let nChr;
const nStrLen = sDOMStr.length;
let nArrLen = 0;
/* mapping¡ */
for (let nMapIdx = 0; nMapIdx < nStrLen; nMapIdx++) {
nChr = sDOMStr.codePointAt(nMapIdx);
if (nChr > 65536) {
nMapIdx++;
}
nArrLen += nChr < 0x80 ? 1 : nChr < 0x800 ? 2 : nChr < 0x10000 ? 3 : nChr < 0x200000 ? 4 : nChr < 0x4000000 ? 5 : 6;
}
aBytes = new Uint8Array(nArrLen);
/* ת¼ : transcription¡ */
let nIdx = 0;
let nChrIdx = 0;
while (nIdx < nArrLen) {
nChr = sDOMStr.codePointAt(nChrIdx);
if (nChr < 128) {
/* one byte */
aBytes[nIdx++] = nChr;
} else if (nChr < 0x800) {
/* two bytes */
aBytes[nIdx++] = 192 + (nChr >>> 6);
aBytes[nIdx++] = 128 + (nChr & 63);
} else if (nChr < 0x10000) {
/* three bytes */
aBytes[nIdx++] = 224 + (nChr >>> 12);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
} else if (nChr < 0x200000) {
/* four bytes */
aBytes[nIdx++] = 240 + (nChr >>> 18);
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
nChrIdx++;
} else if (nChr < 0x4000000) {
/* five bytes */
aBytes[nIdx++] = 248 + (nChr >>> 24);
aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
nChrIdx++;
} else /* if (nChr <= 0x7fffffff) */ {
/* six bytes */
aBytes[nIdx++] = 252 + (nChr >>> 30);
aBytes[nIdx++] = 128 + (nChr >>> 24 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 18 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 12 & 63);
aBytes[nIdx++] = 128 + (nChr >>> 6 & 63);
aBytes[nIdx++] = 128 + (nChr & 63);
nChrIdx++;
}
nChrIdx++;
}
return aBytes;
}
// ²âÊÔ :
const sMyInput = "Base 64 \u2014 ";
const aMyUTF8Input = strToUTF8Arr(sMyInput);
const sMyBase64 = base64EncArr(aMyUTF8Input);
console.log(sMyBase64);
//
const aMyUTF8Output = base64DecToArr(sMyBase64);
const sMyOutput = UTF8ArrToStr(aMyUTF8Output);
console.log(sMyOutput);
//
const sMyInput_ = "\ud869\ude95\u3400\u499b\u2a6c7\udec7";
const aMyUTF8Input_ = strToUTF8Arr(sMyInput);
const sMyBase64_ = base64EncArr(aMyUTF8Input);
console.log(sMyBase64_);
// 👪 \ תÒå·û: (€) € € < < &(&) (") ±íʾΪ" ½«µ¥ÒýºÅ (') ±íʾΪ'
//👨‍👩‍👧‍👦ΪÁ˱í´ï'\u1F468\u200D\u1F469\u200D\u1F467\u200D\u1F466'ʹÓÃ16½øÖÆ»ò10½øÖÆ
//��㐀䦛𪛇�
//𪛇 ; 𣎴 ¹ØÓÚUTF-16²¹³ä×Ö·ûÓëÂëµã :ʹÓÃUCS4¸ßµÍλת»»¼ÆËãºóµÄÂëµã"𣎴"¶ø²»ÊÇUTF-16µÄ¸ßµÍλÂë: ��
//
// ½« Base64 ×Ö·û´®½âÂëΪ Uint8Array »ò ArrayBuffer :
//º¯Êýbase64DecToArr(sBase64[, nBlockSize])·µ»ØÒ»¸öuint8×Ö½ÚÊý×é¡£
//Èç¹ûÄúµÄÄ¿±êÊǹ¹½¨16λ/32λ/64λÔʼÊý¾ÝµÄ»º³åÇø,ÇëʹÓÃnBlockSize²ÎÊý(ÃݵȲÎÊý) : ÕâÊÇuint8Array.bufferËù°üº¬µÄ×Ö½ÚÊý :
//uint8Array.buffer.bytesLengthÊôÐÔ:
//1¡¢ASCII¶þ½øÖÆ×Ö·û(¼´×Ö·û´®ÖеÄÿ¸ö×Ö·û¶¼±»ÊÓΪ¶þ½øÖÆÊý¾Ý8λ1¸ö×Ö½ÚµÄ×Ö·û)
//2¡¢»òUTF-8±àÂë×Ö·û´®Îª1¸ö8λ,UTF-16×Ö·û´®Îª2¸ö8λ,UTF-32×Ö·û´®Îª4¸ö8λ¡£
// : ×Ö·û±àÂë :
//
// "Base 64 \u2014 Mozilla Developer Network"
const myArray = base64DecToArr("QmFzZSA2NCDigJQgTW96aWxsYSBEZXZlbG9wZXIgTmV0d29yaw==");
// "Base 64 \u2014 " // : \u2014×Ö½Ú³¤3 : \uÕ¼1¸ö×Ö½Ú¡¢È«½Ç×Ö·û-Õ¼2¸ö×Ö½Ú
const myBuffer = base64DecToArr("QmFzZSA2NCDigJQgTW96aWxsYSBEZXZlbG9wZXIgTmV0d29yaw==").buffer;
console.log(myBuffer.byteLength); //:
//µ±Äú´Ó·þÎñÆ÷¼ìË÷ÎĵµÊ±,·þÎñÆ÷ͨ³£»áËæÎĵµ·¢ËÍһЩ¸½¼ÓÐÅÏ¢¡£Õâ³ÆΪ HTTP ±êÍ·¡£ÕâÊÇÒ»¸ö¹ØÓÚÎĵµÐÅÏ¢µÄʾÀý,¸ÃÐÅϢͨ¹ý HTTP ±êÍ·ÓëÎĵµÒ»Æð´Ó·þÎñÆ÷´«Êäµ½¿Í»§¶Ëʱ´«µÝ
|