forked from colinta/SublimeStringEncode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstring_encode.py
More file actions
510 lines (380 loc) · 13.9 KB
/
string_encode.py
File metadata and controls
510 lines (380 loc) · 13.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
# coding: utf8
import base64
import codecs
import gzip
import hashlib
import json
import re
import sublime
import sublime_plugin
import sys
from urllib.parse import quote_plus
from urllib.parse import unquote_plus
from .lib.html import entities as html_entities
from .lib.html import escape as html_escape
from .lib.html import unescape as html_unescape
__all__ = [
"StringEncodePaste",
"Gzip64EncodeCommand",
"Gzip64DecodeCommand",
"UnicodeEscapeCommand",
"HtmlEscapeCommand",
"HtmlEntitizeCommand",
"HtmlDeentitizeCommand",
"CssEscapeCommand",
"CssUnescapeCommand",
"SafeHtmlEntitizeCommand",
"SafeHtmlDeentitizeCommand",
"XmlEntitizeCommand",
"XmlDeentitizeCommand",
"JsonEscapeCommand",
"JsonUnescapeCommand",
"UrlEncodeCommand",
"UrlDecodeCommand",
"Base16EncodeCommand",
"Base16DecodeCommand",
"Base32EncodeCommand",
"Base32DecodeCommand",
"Base64EncodeCommand",
"Base64DecodeCommand",
"Md5EncodeCommand",
"Sha1EncodeCommand",
"Sha384EncodeCommand",
"Sha256EncodeCommand",
"Sha512EncodeCommand",
"EscapeRegexCommand",
"EscapeLikeCommand",
"HexDecCommand",
"DecHexCommand",
"UnicodeHexCommand",
"HexUnicodeCommand",
]
xml_entities = {"\"": """, "'": "'", "<": "<", ">": ">" }
def pad64(value):
mod = len(value) % 4
if mod == 3:
return value + '='
elif mod == 2:
return value + '=='
return value
class StringEncodePaste(sublime_plugin.WindowCommand):
def run(self, **kwargs):
items = [
('Html Escpae', 'html_escape'),
('Html Entitize', 'html_entitize'),
('Html Deentitize', 'html_deentitize'),
('Unicode Escape', 'unicode_escape'),
('Css Escape', 'css_escape'),
('Css Unescape', 'css_unescape'),
('Safe Html Entitize', 'safe_html_entitize'),
('Safe Html Deentitize', 'safe_html_deentitize'),
('Xml Entitize', 'xml_entitize'),
('Xml Deentitize', 'xml_deentitize'),
('Json Escape', 'json_escape'),
('Json Unescape', 'json_unescape'),
('Url Encode', 'url_encode'),
('Url Decode', 'url_decode'),
('Base16 Encode', 'base16_encode'),
('Base16 Decode', 'base16_decode'),
('Base32 Encode', 'base32_encode'),
('Base32 Decode', 'base32_decode'),
('Base64 Encode', 'base64_encode'),
('Base64 Decode', 'base64_decode'),
('Md5 Encode', 'md5_encode'),
('Sha1 Encode', 'sha1_encode'),
('Sha256 Encode', 'sha256_encode'),
('Sha384 Encode', 'sha384_encode'),
('Sha512 Encode', 'sha512_encode'),
('Escape Regex', 'escape_regex'),
('Escape Like', 'escape_like'),
('Hex Dec', 'hex_dec'),
('Dec Hex', 'dec_hex'),
('Unicode Hex', 'unicode_hex'),
('Hex Unicode', 'hex_unicode'),
('Gzip64 Encode', 'gzip64_encode'),
('Gzip64 Decode', 'gzip64_decode'),
]
lines = list(map(lambda line: line[0], items))
commands = list(map(lambda line: line[1], items))
view = self.window.active_view()
if not view:
return
def on_done(item):
if item == -1:
return
view.run_command(commands[item], {'source': 'clipboard'})
self.window.show_quick_panel(lines, on_done)
class StringEncode(sublime_plugin.TextCommand):
def run(self, edit, **kwargs):
regions = self.view.sel()
if kwargs.get('source') == 'clipboard':
del kwargs['source']
text = sublime.get_clipboard()
replacement = self.convert(text, **kwargs)
for region in regions:
if region.empty():
self.view.insert(edit, region.begin(), replacement)
else:
self.view.replace(edit, region, replacement)
return
elif 'source' in kwargs:
self.view.show_popup('Unsupported source {0!r}'.format(kwargs['source']))
return
if any(map(lambda region: region.empty(), regions)):
regions = [sublime.Region(0, self.view.size())]
for region in regions:
text = self.view.substr(region)
replacement = self.convert(text, **kwargs)
self.view.replace(edit, region, replacement)
def convert(self, text):
return text
class Gzip64EncodeCommand(StringEncode):
def convert(self, text):
return str(base64.b64encode(gzip.compress(bytes(text, 'utf-8'))), 'ascii')
class Gzip64DecodeCommand(StringEncode):
def convert(self, text):
return str(gzip.decompress(base64.b64decode(pad64(text))), 'utf-8')
class UnicodeEscapeCommand(StringEncode):
def convert(self, text):
return codecs.decode(text, 'unicode-escape')
class CssEscapeCommand(StringEncode):
def convert(self, text):
ret = ''
for char in text:
if char > '\x7f':
ret += '\\' + hex(ord(char))[2:]
else:
ret += char
return ret
class CssUnescapeCommand(StringEncode):
regexp = re.compile(r'\\[a-fA-F0-9]{1,6}')
def convert(self, text):
for s in set(self.regexp.findall(text)):
text = text.replace(s, chr(int(s[1:], 16)))
return text
class HtmlEscapeCommand(StringEncode):
def convert(self, text):
return html_escape(text)
class HtmlEntitizeCommand(StringEncode):
def convert(self, text):
ret = ''
for char in text:
if char in html_entities:
ret += html_entities[char]
continue
if char > '\x7f':
ret += '&#x' + hex(ord(char))[2:] + ';'
continue
ret += char
return ret
class HtmlDeentitizeCommand(StringEncode):
def convert(self, text):
return html_unescape(text)
class SafeHtmlEntitizeCommand(StringEncode):
def convert(self, text):
ret = ''
for char in text:
if char not in {"<", ">", "&", "'", "\""}:
if char in html_entities:
ret += html_entities[char]
continue
if char > '\x7f':
ret += '&#x' + hex(ord(char))[2:] + ';'
continue
ret += char
return ret
class SafeHtmlDeentitizeCommand(StringEncode):
def convert(self, text):
return html_unescape(text, {'lt;', 'gt;', 'amp;', 'apos;', 'quot;'})
class XmlEntitizeCommand(StringEncode):
def convert(self, text):
ret = ''
for char in text.replace('&', '&'):
if char in xml_entities:
ret += xml_entities[char]
continue
if char > '\x7f':
ret += '&#x' + hex(ord(char))[2:] + ';'
continue
ret += char
return ret
class XmlDeentitizeCommand(StringEncode):
def convert(self, text):
for char, entity in xml_entities.items():
text = text.replace(entity, char)
return text.replace('&', '&')
class JsonEscapeCommand(StringEncode):
def convert(self, text):
return json.dumps(text)
class JsonUnescapeCommand(StringEncode):
def convert(self, text):
if text[:1] == "'" and text[-1:] == "'":
return self.convert(text[1:-1])
if text[:1] != '"' and text[-1:] != '"':
return self.convert('"' + text.replace('"', '\\"') + '"')
return json.loads(text)
class UrlEncodeCommand(StringEncode):
def convert(self, text, old_school=True):
quoted = quote_plus(text)
if old_school:
return quoted.replace("+", "%20")
return quoted
class UrlDecodeCommand(StringEncode):
def convert(self, text):
return unquote_plus(text)
class Base16EncodeCommand(StringEncode):
def convert(self, text):
return str(base64.b16encode(bytes(text, 'utf-8')), 'ascii')
class Base16DecodeCommand(StringEncode):
def convert(self, text):
return str(base64.b16decode(text), 'utf-8')
class Base32EncodeCommand(StringEncode):
def convert(self, text):
return str(base64.b32encode(bytes(text, 'utf-8')), 'ascii')
class Base32DecodeCommand(StringEncode):
def convert(self, text):
return str(base64.b32decode(text), 'utf-8')
class Base64EncodeCommand(StringEncode):
def convert(self, text):
return str(base64.b64encode(bytes(text, 'utf-8')), 'ascii')
class Base64DecodeCommand(StringEncode):
def convert(self, text):
return str(base64.b64decode(pad64(text)), 'utf-8')
class Md5EncodeCommand(StringEncode):
def convert(self, text):
return hashlib.md5(bytes(text, 'utf-8')).hexdigest()
class Sha1EncodeCommand(StringEncode):
def convert(self, text):
return hashlib.sha1(bytes(text, 'utf-8')).hexdigest()
class Sha256EncodeCommand(StringEncode):
def convert(self, text):
return hashlib.sha256(bytes(text, 'utf-8')).hexdigest()
class Sha384EncodeCommand(StringEncode):
def convert(self, text):
return hashlib.sha384(bytes(text, 'utf-8')).hexdigest()
class Sha512EncodeCommand(StringEncode):
def convert(self, text):
return hashlib.sha512(bytes(text, 'utf-8')).hexdigest()
class EscapeRegexCommand(StringEncode):
regex = re.compile(r'(?<!\\)([?\\*.+^$()\[\]\{\}\|])')
def convert(self, text):
return self.regex.sub(r'\\\1', text)
class EscapeLikeCommand(StringEncode):
regex = re.compile(r'(?<!\\)([%_])')
def convert(self, text):
return self.regex.sub(r'\\\1', text)
class HexDecCommand(StringEncode):
def convert(self, text):
return str(int(text, 16))
class DecHexCommand(StringEncode):
def convert(self, text):
return hex(int(text))
class UnicodeHexCommand(StringEncode):
def convert(self, text):
hex_text = u''
text_bytes = bytes(text, 'utf-16')
if text_bytes[0:2] == b'\xff\xfe':
endian = 'little'
text_bytes = text_bytes[2:]
elif text_bytes[0:2] == b'\xfe\xff':
endian = 'big'
text_bytes = text_bytes[2:]
char_index = 0
for c in text_bytes:
if char_index == 0:
c1 = c
char_index += 1
elif char_index == 1:
c2 = c
if endian == 'little':
c1, c2 = c2, c1
tmp = (c1 << 8) + c2
if tmp < 0x80:
hex_text += chr(tmp)
char_index = 0
elif tmp >= 0xd800 and tmp <= 0xdbff:
char_index += 1
else:
hex_text += '\\u' + '{0:04x}'.format(tmp)
char_index = 0
elif char_index == 2:
c3 = c
char_index += 1
elif char_index == 3:
c4 = c
if endian == 'little':
c3, c4 = c4, c3
tmp1 = ((c1 << 8) + c2) - 0xd800
tmp2 = ((c3 << 8) + c4) - 0xdc00
tmp = (tmp1 * 0x400) + tmp2 + 0x10000
hex_text += '\\U' + '{0:08x}'.format(tmp)
char_index = 0
return hex_text
class HexUnicodeCommand(StringEncode):
def convert(self, text):
uni_text = text
endian = sys.byteorder
r = re.compile(r'\\u([0-9a-fA-F]{2})([0-9a-fA-F]{2})')
rr = r.search(uni_text)
while rr:
first_byte = int(rr.group(1), 16)
if first_byte >= 0xd8 and first_byte <= 0xdf:
# Surrogate pair
pass
else:
if endian == 'little':
b1 = int(rr.group(2), 16)
b2 = int(rr.group(1), 16)
else:
b1 = int(rr.group(1), 16)
b2 = int(rr.group(2), 16)
ch = bytes([b1, b2]).decode('utf-16')
uni_text = uni_text.replace(rr.group(0), ch)
rr = r.search(uni_text, rr.start(0) + 1)
# Surrogate pair (2 bytes + 2 bytes)
r = re.compile(
r'\\u([0-9a-fA-F]{2})([0-9a-fA-F]{2})\\u([0-9a-fA-F]{2})([0-9a-fA-F]{2})')
rr = r.search(uni_text)
while rr:
if endian == 'little':
b1 = int(rr.group(2), 16)
b2 = int(rr.group(1), 16)
b3 = int(rr.group(4), 16)
b4 = int(rr.group(3), 16)
else:
b1 = int(rr.group(1), 16)
b2 = int(rr.group(2), 16)
b3 = int(rr.group(3), 16)
b4 = int(rr.group(4), 16)
ch = bytes([b1, b2, b3, b4]).decode('utf-16')
uni_text = uni_text.replace(rr.group(0), ch)
rr = r.search(uni_text)
# Surrogate pair (4 bytes)
r = re.compile(
r'\\U([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})')
rr = r.search(uni_text)
while rr:
tmp = (int(rr.group(1), 16) << 24) \
+ (int(rr.group(2), 16) << 16) \
+ (int(rr.group(3), 16) << 8) \
+ (int(rr.group(4), 16))
if (tmp <= 0xffff):
ch = chr(tmp)
else:
tmp -= 0x10000
c1 = 0xd800 + int(tmp / 0x400)
c2 = 0xdc00 + int(tmp % 0x400)
if endian == 'little':
b1 = c1 & 0xff
b2 = c1 >> 8
b3 = c2 & 0xff
b4 = c2 >> 8
else:
b1 = c1 >> 8
b2 = c1 & 0xff
b3 = c2 >> 8
b4 = c2 & 0xff
ch = bytes([b1, b2, b3, b4]).decode('utf-16')
uni_text = uni_text.replace(rr.group(0), ch)
rr = r.search(uni_text)
return uni_text