Important alert: (current site time 5/21/2013 6:11:35 AM EDT)
 

article

Escape machine code bytes to extended chars

Email
Submitted on: 8/27/2012 3:14:42 PM
By: Rde 
Level: Advanced
User Rating: By 3 Users
Compatibility: VB 4.0 (32-bit), VB 5.0, VB 6.0
Views: 21669
author picture
 
     A more compact storage of bytes in string than using Hex. Storing binary data (anything) in a string can be done more compactly using this method than using Hex strings. Any data copied to a byte array can be stored for 8 character per 7 bytes compared with 14 when using a predictable-length Hex string which is 2 characters per byte.
558BEC669C53568B75088B1E8B16C1EB02891E83E2038B750C89168B75108B1E83C321891E8B45145E5B669D8BE55DC21000
–Õ‹ìæœÓÖ©‹õˆ‹ž‹–ëÁ낉žƒâÒƒ‹õŒ‰–‹´õ‹žƒÃ¡…‰ž‹Å”Þۮ杋åݐ€€
Original proof and very cool concept thanks to jeremyxtz :)

This article has accompanying files
 
 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.
				
Option Explicit
Private sCurPath As String
Private sFileName As String
 ''''''''''''''''''''''''''''''''''''''''''
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal ptrMC As Long, ByVal P1 As Long, ByVal P2 As Long, ByVal P3 As Long, ByVal P4 As Long) As Long
 Private abAsmMC() As Byte ' Array to hold machine code
 Const sASMCode As String = "558BECB82312353C8BE55DC21000"
 '
' HexStr2MCode abAsmMC, sASMCode
' lpMC = VarPtr(abAsmMC(0))
' CallWindowProc lpMC, arg1, arg2, arg3, arg4
 '
Private Sub HexStr2MCode(abMCode() As Byte, sHexCode As String) 'Original code RR
Dim i As Long, iSize As Long
iSize = Len(sHexCode)
Debug.Assert Not iSize = 0&
Debug.Assert Not iSize And 1& 'may not be odd (hex pairs)!
iSize = iSize * 0.5
ReDim abMCode(iSize - 1)
Do: abMCode(i) = Val("&H" & Mid$(sHexCode, i + i + 1&, 2&))
i = i + 1&
Loop Until i = iSize
End Sub
 '
' Asm2HexStr "C:\Asm\ASMCode.bin"
 '
Private Sub Asm2HexStr(sBinFile As String) 'Original code UMGEDV GmbH/RR ?
Dim hx As String, s As String
Dim i As Long, j As Long, k As Long
Dim hFile As Long
hFile = FreeFile
On Error Resume Next
 Open sBinFile For Binary Access Read Lock Write As hFile
 If Not Err = 0& Then Exit Sub
On Error GoTo 0
j = LOF(hFile)
If Not j = 0& Then
ReDim b(j - 1) As Byte
Get #hFile, , b
Close hFile
hx = Space$(j + j)
k = 1&
Do: Mid$(hx, k&) = Right$("0" & Hex$(b(i)), 2&)
i = i + 1&
k = k + 2&
Loop Until i = j
i = InStrRev(sBinFile, "\") + 1&
j = InStrRev(sBinFile, ".")
s = Mid$(sBinFile, i, j - i)
AsmStr2Const s, hx, 100
'Const sASMCode As String = "558BECB84E61BC008BE55DC21000"
Else
Close hFile
End If
End Sub
Sub AsmStr2Const(sConstName As String, sEscStr As String, Optional ByVal iMaxLineLen As Long = 100)
Dim i As Long, cnt As Long, s As String
' Note - if you get the 'too many continuation lines' message,
' simply set maxlinelen higher, anything up to a little over 900
Debug.Print ""
Debug.Print "' Please do not edit the following machine code"
s = Space$(19 + Len(sConstName))
cnt = Len(sEscStr)
Select Case cnt
Case Is < iMaxLineLen
Debug.Print "Const " & sConstName & " As String = """ & sEscStr & """"
'Const sASMCode As String = "ÆÕ‰åÓÖ׸Ò„€€ßÞÛ‹‰ìݐ€"
Case Is < iMaxLineLen + iMaxLineLen
iMaxLineLen = cnt * 0.5 ' Rounds down - relevant only for extended input
iMaxLineLen = cnt - iMaxLineLen ' Odd length displays extra char on first line
Debug.Print "Const " & sConstName & " As String = """ & Left$(sEscStr, iMaxLineLen) & """ & _"
Debug.Print s & """" & Mid$(sEscStr, iMaxLineLen + 1&) & """"
Case Else
cnt = cnt - iMaxLineLen
Debug.Print "Const " & sConstName & " As String = """ & Left$(sEscStr, iMaxLineLen) & """ & _"
i = iMaxLineLen + 1&
Do While cnt > iMaxLineLen
cnt = cnt - iMaxLineLen
Debug.Print s & """" & Mid$(sEscStr, i, iMaxLineLen) & """ & _"
i = i + iMaxLineLen
Loop
Debug.Print s & """" & Mid$(sEscStr, i) & """"
End Select
MsgBox "Copy code in your debug window"
End Sub

 ' ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
 ' Escape all machine code bytes to extended chars
 ' A more compact storage of bytes in string than using Hex. Storing data (anything)
 ' in a string can be done more compactly using this method than using Hex strings.
 ' Any data copied to a byte array can be stored for 8 character per 7 bytes compared
 ' with 14 when using a predictable-length Hex string which is 2 characters per byte
 ' 558BEC669C53568B75088B1E8B16C1EB02891E83E2038B750C89168B75108B1E83C321891E8B45145E5B669D8BE55DC21000
 ' –Õ‹ìæœÓÖ©‹õˆ‹ž‹–ëÁ낉žƒâÒƒ‹õŒ‰–‹´õ‹žƒÃ¡…‰ž‹Å”Þۮ杋åݐ€€
 ' Original proof and very cool concept thanks to jeremyxtz :)
Private Sub AsmBin2EscStr(sBinFile As String, Optional ByVal iMaxLineLen As Long = 100)
Dim i As Long, sEsc As String
Dim cnt As Long, cmeta As Long
Dim power As Byte, meta As Byte
Dim hFile As Long
hFile = FreeFile
On Error Resume Next
 Open sBinFile For Binary Access Read Lock Write As hFile
 If Not Err = 0& Then Exit Sub
On Error GoTo 0
cnt = LOF(hFile) ' Machine code byte count
If Not cnt = 0& Then
ReDim b(0 To cnt - 1) As Byte
Get #hFile, , b
Close hFile
If (cnt Mod 7) Then i = 1& ' Account for remainder meta byte (set to 1 if needed)
sEsc = Space$(cnt + (cnt \ 7&) + i) ' Byte cnt + meta byte cnt (1 per 7 code bytes) + remainder meta byte
meta = 128 ' Byte MSBit used to escape machine code bytes to extended chars
cmeta = 2& ' Set read pointer one based, skip first meta byte
For i = 0 To UBound(b) ' Bit flag indicates when already an extended char
If b(i) And 128 Then meta = meta Or (2 ^ power)
Mid$(sEsc, i + cmeta) = Chr$(b(i) Or 128)
power = (i + 1) Mod 7' Escape all machine code bytes to extended chars
If power = 0 Then' Write meta bits to first byte of completed byte set
Mid$(sEsc, i + cmeta - 7&) = Chr$(meta)
meta = 128 ' Reset meta byte LSBits for next set of seven (w/ escaped MSBit)
cmeta = cmeta + 1&
End If
Next' Write remainder meta byte (less than 7 meaningful bits)
If power Then Mid$(sEsc, i + cmeta - power - 1&) = Chr$(meta) ' We -1 as i incremented on exit
i = InStrRev(sBinFile, "\") + 1&' Extract the binary objects filename
' Remove the extension and create the Const string
AsmStr2Const Mid$(sBinFile, i, InStrRev(sBinFile, ".") - i), sEsc, iMaxLineLen
Else
Close hFile
End If
End Sub
Sub AsmEscStr2Bin(b() As Byte, sEscStr As String) ' Original code jeremyxtz
Dim cmeta As Long, remainder As Long
Dim c7Bs As Long, i As Long, j As Long
Dim power As Byte, meta As Byte
j = Len(sEscStr)' mbbbbbbbmbbbbbbbmbbbbbbbmbb
i = (j \ 8) ' mbbbbbbbmbbbbbbbmbbbbbbb
remainder = j Mod 8 ' mbb
c7Bs = i * 7' bbbbbbbbbbbbbbbbbbbbb
c7Bs = c7Bs - 1& ' Account for zero based array
If remainder = 0& Then
ReDim b(c7Bs) As Byte ' No remainder bytes (7 is a divisor of cnt)
Else ' Remove remainder meta byte from count
remainder = remainder - 1&
ReDim b(c7Bs + remainder) ' bb
End If
meta = Asc(Mid$(sEscStr, 1&, 1&)) ' Assign bit flags for first set of 7 machine code bytes
cmeta = 2&' Set read pointer, dodge first meta byte
For i = 0 To c7Bs + remainder ' Unexcape all machine code bytes into array
If meta And (2 ^ power) Then ' If already extended then assign it as is
b(i) = Asc(Mid$(sEscStr, i + cmeta, 1&))
Else ' Else assign it with MSBit masked out
b(i) = Asc(Mid$(sEscStr, i + cmeta, 1&)) And (Not 128)
End If
power = (i + 1) Mod 7
If power = 0 Then ' If set complete grab next set of meta bits
cmeta = cmeta + 1&' Skip the meta byte
If i + cmeta > j Then Exit For ' No remainder condition (so no meta byte)
meta = Asc(Mid$(sEscStr, i + cmeta, 1&))
End If
Next
End Sub

Const sHexMCode As String = "558BEC669C53568B75088B1E8B16C1EB02891E83E2038B750C89168B75108B1E83C321891E8B45145E5B669D8BE55DC21000"
Const sEscMCode As String = "–Õ‹ìæœÓÖ©‹õˆ‹ž‹–ëÁ낉žƒâÒƒ‹õŒ‰–‹´õ‹žƒÃ¡…‰ž‹Å”Þۮ杋åݐ€€"

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.
  3. Scan the source code with Minnow's Project Scanner

If you don't have a virus scanner, you can get one at many places on the net including:McAfee.com

 
Terms of Agreement:   
By using this article, you agree to the following terms...   
  1. You may use this article in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this article (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this article from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the article or article's description.


Other 52 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Advanced category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments
8/13/2012 7:14:39 PMDRWUMR2

for those who wants to learn compression , this is good source.

also this is intended for HEX encoded compression purpose, maybe can be modified to accept a series of base-n formats such as octet and maybe base 20 or b-64 =) have you tried other base nunbers RDE? =)
(If this comment was disrespectful, please report it.)

 
8/28/2012 3:18:33 AMRobert Rayment

No vbp file hence CommonDialog control reported missing, I find.
(If this comment was disrespectful, please report it.)

 
8/29/2012 5:47:31 AMRde

Hi Rob

Yer, I neglected to include a vbp


(If this comment was disrespectful, please report it.)

 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)
 

To post feedback, first please login.