Public Class BinaryCookieReader1
Private mybyte() As Byte
Private outCookie As String = ""
Public Shared Function ReadFile(ByVal fileName As String) As Byte()
Dim fs As New FileStream(fileName, FileMode.Open)
Dim buffer As Byte() = New Byte(fs.Length - 1) {}
Try
fs.Read(buffer, 0, buffer.Length)
fs.Seek(0, SeekOrigin.Begin)
Return buffer
Catch
Return buffer
Finally
If fs IsNot Nothing Then
fs.Close()
End If
End Try
End Function
Public Function ReadInt32(ByRef myByte As Byte(), ByVal startIndex As Integer, Optional ByVal useLeEndian As Boolean = False) As Integer
Dim read4Byte(3) As Byte
If useLeEndian = True Then
read4Byte(0) = myByte(startIndex + 3)
read4Byte(1) = myByte(startIndex + 2)
read4Byte(2) = myByte(startIndex + 1)
read4Byte(3) = myByte(startIndex + 0)
ReadInt32 = CInt("&H" + BitConverter.ToString(read4Byte, 0, 4).Replace("-", ""))
Else
ReadInt32 = CInt("&H" + BitConverter.ToString(myByte, startIndex, 4).Replace("-", ""))
End If
End Function
Public Function ReadCookies(ByRef mybyte As Byte(), ByVal startIndex As Integer) As String
'除了
'1. First 4 bytes in the cookie is the size of the cookie.
'2. The next 4 bytes are unknown (may be related to cookies flags).
'3. The next four bytes are the cookie flags. This is an integer value (1=Secure, 4=HttpOnly, 5= Secure+HttpOnly).
'4. The next 4 bytes are unknown.
'5. The next 4 bytes is an integer specifying the start of the url field in bytes from the start of the cookie record.
'6. The next 4 bytes is an integer specifying the start of the name field in bytes from the start of the cookie record.
'7. The next 4 bytes is an integer specifying the start of the path field in bytes from the start of the cookie record.
'8. The next 4 bytes is an integer specifying the start of the value field in bytes from the start of the cookie record.
'9. The next 8 bytes represents the end of the cookie and it is always 0×0000000000000000.
'10. The next 8 bytes are the cookie expiration date. Date is in Mac epoch format (Mac absolute time). Mac epoch format starts from Jan 2001.
'11. The next 8 bytes are the cookie creation date.
'12. Next to that, the cookie contains the actual cookie domain, name, path & value. The order is not specific and they can appear in any order.
ReadCookies = ""
Dim CookieLength As Integer = ReadInt32(mybyte, startIndex, True)
Dim CookieFlag As Integer = ReadInt32(mybyte, startIndex + 8, True)
Dim CookieUrlOffset As Integer = ReadInt32(mybyte, startIndex + 16, True)
Dim CookieNameOffset As Integer = ReadInt32(mybyte, startIndex + 20, True)
Dim CookiePathOffset As Integer = ReadInt32(mybyte, startIndex + 24, True)
Dim CookieValueOffset As Integer = ReadInt32(mybyte, startIndex + 28, True)
'end of cookie + 8 0000000000000000
'Dim ExpirationDate As Long ' end of cookie+8
'dim CreationDate as long '+8
Dim CookieUrl As String = Encoding.ASCII.GetString(mybyte, startIndex + CookieUrlOffset, CookieNameOffset - CookieUrlOffset - 1)
Dim CookieName As String = Encoding.ASCII.GetString(mybyte, startIndex + CookieNameOffset, CookiePathOffset - CookieNameOffset - 1)
Dim CookiePath As String = Encoding.ASCII.GetString(mybyte, startIndex + CookiePathOffset, CookieValueOffset - CookiePathOffset - 1)
Dim CookieValue As String = Encoding.ASCII.GetString(mybyte, startIndex + CookieValueOffset, CookieLength - CookieValueOffset - 1)
Public Function ReadPage(ByRef mybyte As Byte(), ByVal startIndex As Integer)
'Page(Format)
'1. Every page starts with a 4 byte page header: 0×00000100.
'2. Next four bytes is an integer specifying the number of cookies in the page.
'3. Following that, a 4 byte integer for each cookie, represents the cookie offset. Offset specifies the start of the cookie in bytes from the start of the page.
'4. Next to that, the page contains the actual cookie contents. Each cookie is of variable length. Cookie format is explained below.
'5. Page ends with a 4 byte value and it is always 0×00000000.
Dim PageHeader As Integer = ReadInt32(mybyte, startIndex) '头固定为 0x00000100
Dim NumberOfCookies As Integer = BitConverter.ToInt32(mybyte, startIndex + 4) 'COOKIE 数量
'Dim NumberOfCookies As Integer = ReadInt32(mybyte, startIndex + 4) 'COOKIE 数量
Dim NumberOfCookiesStartIndex As Integer = startIndex + 8
Dim CookieOffset() As Integer
ReDim CookieOffset(NumberOfCookies - 1)
For I As Integer = 0 To NumberOfCookies - 1
CookieOffset(I) = BitConverter.ToInt32(mybyte, NumberOfCookiesStartIndex + I * 4)
ReadCookies(mybyte, startIndex + CookieOffset(I))
Next
End Function
Private LengthPage_StartIndex As Integer = 8
Private Page_StartIndex As Integer = -1
Public Sub New(ByVal cookiefile As String)
'FileHead NoOfpages Page1-N PageHeader BE Endian
'PageFormat & Cookie Format use LE Endian
mybyte = ReadFile(cookiefile)
Dim FileHead As String = Encoding.ASCII.GetString(mybyte, 0, 4) ' //0-3
Dim NumberOfPages As Integer = ReadInt32(mybyte, 4)
'开始位置就是第9位,索引-1 就是8
Dim LengthOfPages() As Integer
ReDim LengthOfPages(NumberOfPages - 1)
For I As Integer = 0 To NumberOfPages - 1
LengthOfPages(I) = ReadInt32(mybyte, LengthPage_StartIndex + 4 * I)
Next
outCookie = ""
Dim nextStartIndex As Integer = 8 + NumberOfPages * 4
For I As Integer = 0 To NumberOfPages - 1
ReadPage(mybyte, nextStartIndex)
nextStartIndex = nextStartIndex + LengthOfPages(I)
Next
MsgBox(outCookie)