Skip to content

Instantly share code, notes, and snippets.

@bitRAKE
Last active May 14, 2022 19:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bitRAKE/412278b4de811193fb9ec1d6e77eec26 to your computer and use it in GitHub Desktop.
Save bitRAKE/412278b4de811193fb9ec1d6e77eec26 to your computer and use it in GitHub Desktop.
Bit vectors are a native type on x86 ...
; Create static bit vector:
; + ignore value set duplicates
; + invert value set when bit length is negative
struc(name) BITVECTOR bits,values&
local result,offset,char,inverted,_bits
virtual
offset = $
db values ; string or byte array
result = 0
while offset < $
load char:1 from offset
offset = offset + 1
result = result or (1 shl char)
end while
end virtual
if bits < 0
_bits = -bits
inverted = (1 shl _bits) - 1
else
_bits = bits
inverted = 0
end if
if result shr _bits
err 'value exceeds bit vector size'
end if
name: emit (_bits + 7) shr 3 : (result xor inverted)
end struc
align 32
InvalidFileNameChars BITVECTOR -256,"!#$%&'()-@^_`{}~+,.;=[]0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
FileNameValid__RSI:
xor eax,eax
.more:
lodsb
test eax,eax ; CF=0
jz .done
bt [InvalidFileNameChars],eax
jnc .more
.done:
retn
; CF: file name has invalid characters
FileNameValid__RSI__ECX: ; ECX > 0
.more:
dec ecx ; requres previous CF result on exit
js .done
movzx eax,byte[rsi+rcx]
bt [InvalidFileNameChars],eax
jnc .more
.done:
retn
; CF: file name has invalid characters
macro ValidateFileName reg0,reg1
local more,done
more: dec reg32.#reg1 ; requres previous CF result on exit
js done
movzx eax,byte[reg0+reg1]
bt [InvalidFileNameChars],eax
jnc more
done:
end macro
align 32
WSA0: BITVECTOR 20 shl 3,0x09,0x0A,0x0B,0x0C,0x0D,0x20
WS2000: BITVECTOR 12 shl 3,0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x28,0x29,0x2F,0x5F
; fast path the byte-like whitespace
UTF32_Whitespace:
cmp eax,0xA0 ; U+00A0 non-breaking space
jnc .high
bt [WSA0],eax ; [00-9F] ; 20 bytes
retn
.high:
jz .space
cmp eax,0x2000
jc .notspace
cmp eax,0x2060
jnc .cjk
bt [WS2000 - (0x2000 shr 3)],eax ; [2000-205F] ; 12 bytes
retn
.space:
stc
retn
.cjk:
; jz .space
cmp eax,0x3000
jz .space
.notspace:
clc
retn
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment