https://wiki.skullsecurity.org/index.php?title=Util.Buffer&feed=atom&action=historyUtil.Buffer - Revision history2024-03-29T11:40:42ZRevision history for this page on the wikiMediaWiki 1.36.1https://wiki.skullsecurity.org/index.php?title=Util.Buffer&diff=583&oldid=prevRon at 13:30, 29 February 20082008-02-29T13:30:07Z<p></p>
<table style="background-color: #fff; color: #202122;" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 13:30, 29 February 2008</td>
</tr><tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l1">Line 1:</td>
<td colspan="2" class="diff-lineno">Line 1:</td></tr>
<tr><td class="diff-marker" data-marker="−"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #ffe49c; vertical-align: top; white-space: pre-wrap;"><div>package util;</div></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"><pre></ins>package util;</div></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>/** Buffer.java</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>/** Buffer.java</div></td></tr>
<tr><td colspan="2" class="diff-lineno" id="mw-diff-left-l458">Line 458:</td>
<td colspan="2" class="diff-lineno">Line 458:</td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><br/></td></tr>
<tr><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>}</div></td><td class="diff-marker"></td><td style="background-color: #f8f9fa; color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #eaecf0; vertical-align: top; white-space: pre-wrap;"><div>}</div></td></tr>
<tr><td colspan="2"></td><td class="diff-marker" data-marker="+"></td><td style="color: #202122; font-size: 88%; border-style: solid; border-width: 1px 1px 1px 4px; border-radius: 0.33em; border-color: #a3d3ff; vertical-align: top; white-space: pre-wrap;"><div><ins style="font-weight: bold; text-decoration: none;"></pre></ins></div></td></tr>
</table>Ronhttps://wiki.skullsecurity.org/index.php?title=Util.Buffer&diff=582&oldid=prevRon: New page: package util;
/** Buffer.java
* This class makes it easy for the programmer to create an array of bytes
* of a specific format. It'll support these major types:<BR>
* DWord - 4 bytes<...2008-02-29T13:29:50Z<p>New page: package util; /** Buffer.java * This class makes it easy for the programmer to create an array of bytes * of a specific format. It'll support these major types:<BR> * DWord - 4 bytes<...</p>
<p><b>New page</b></p><div>package util;<br />
<br />
/** Buffer.java<br />
* This class makes it easy for the programmer to create an array of bytes<br />
* of a specific format. It'll support these major types:<BR><br />
* DWord - 4 bytes<BR><br />
* Word - 2 bytes<BR><br />
* Byte - 1 byte<BR><br />
* NTString - Any number of bytes, interpreted as characters, terminated by a null.<BR><br />
* <P><br />
* This is a pretty standard buffer in terms of everything else. I've used it extensively<br />
* and have complete confidence in it.<br />
*<br />
* @author Ron<br />
*/<br />
<br />
public class Buffer<br />
{<br />
/** The starting length of the buffer */<br />
public final int defaultLength = 32;<br />
<br />
/** The actual buffer which will hold the data */<br />
protected byte[] buffer;<br />
<br />
/** The current length of the buffer */<br />
private int currentLength;<br />
/** The maximum length of the buffer */<br />
private int maxLength;<br />
<br />
/** Initializes the variables */<br />
public Buffer()<br />
{<br />
buffer = new byte[defaultLength];<br />
currentLength = 0;<br />
maxLength = defaultLength;<br />
}<br />
<br />
/** Initialize the buffer to an array of bytes.<br />
* @param b The initial value.<br />
*/<br />
public Buffer(byte[] b)<br />
{<br />
this();<br />
addBytes(b);<br />
}<br />
<br />
/** Initializes the buffer to another buffer. Note that it makes a copy, it doesn't share<br />
* the data.<br />
* @param b The original buffer.<br />
*/<br />
public Buffer(Buffer b)<br />
{<br />
this();<br />
addBytes(b.getBytes());<br />
}<br />
<br />
public Buffer(char []c)<br />
{<br />
this();<br />
for(int i = 0; i < c.length; i++)<br />
addByte((byte) c[i]);<br />
}<br />
<br />
<br />
/** Returns the size of the buffer */<br />
public int size()<br />
{<br />
return currentLength;<br />
}<br />
<br />
/** Returns the entire buffer as an array of bytes. <br />
* @return The entire buffer as an array of bytes.<br />
*/<br />
public byte[] getBytes()<br />
{<br />
byte[] ret = new byte[currentLength];<br />
<br />
System.arraycopy(buffer, 0, ret, 0, currentLength);<br />
<br />
return ret;<br />
}<br />
<br />
/** Ensures that there is enough length to store the specified number of bytes. If there<br />
* isn't, enough extra room is allocated so we can.<br />
*<br />
* @param bytes The number of bytes we'return adding to it.<br />
*/<br />
private void verifyLength(int bytes)<br />
{<br />
// If we already have enough, just return<br />
if((currentLength + bytes) <= maxLength)<br />
return;<br />
<br />
while((currentLength + bytes) > maxLength)<br />
{<br />
maxLength = maxLength * 2;<br />
}<br />
<br />
// Create a new buffer<br />
byte[] newBuffer = new byte[maxLength];<br />
<br />
// Copy the old buffer into the new buffer<br />
System.arraycopy(buffer, 0, newBuffer, 0, currentLength); <br />
<br />
// Set the current buffer to the new buffer<br />
buffer = newBuffer;<br />
}<br />
<br />
/** Shifts all data back by the requested number of bytes, and returns the<br />
* bytes that were shifted off. <br />
*<br />
* @param number The number of bytes to pull off the beginning.<br />
* @return The bytes that were pulled off.<br />
*/<br />
protected byte[] remove(int number)<br />
{<br />
byte[] ret = new byte[number];<br />
System.arraycopy(buffer, 0, ret, 0, number);<br />
System.arraycopy(buffer, number, buffer, 0, currentLength - number);<br />
currentLength -= number;<br />
<br />
return ret;<br />
}<br />
<br />
/** Adds a single byte to the end of the buffer .<br />
* @param b The byte to add.<br />
*/<br />
public void addByte(byte b)<br />
{<br />
verifyLength(1);<br />
<br />
buffer[currentLength++] = b;<br />
}<br />
<br />
/** Removes a single byte from the beginning of the buffer.<br />
* @return The byte that was removed.<br />
* @throws IndexOutOfBoundsException If there isn't enough room in the buffer to accomidate the<br />
* requested removal.<br />
*/<br />
public byte removeByte() throws IndexOutOfBoundsException<br />
{<br />
if(currentLength == 0)<br />
throw new IndexOutOfBoundsException("Attempted to remove data from the buffer that wasn't there.");<br />
return remove(1)[0];<br />
}<br />
<br />
/** Returns the byte at a specific location.<br />
* @param index The location to get the byte at.<br />
* @return The byte at location "index".<br />
*/<br />
public byte byteAt(int index)<br />
{<br />
return buffer[index];<br />
}<br />
<br />
/** Adds a word to the buffer (2 bytes, little endian).<br />
* @param w The word to add.<br />
*/<br />
public void addWord(short w)<br />
{<br />
addByte((byte)((w & 0x00FF) >> 0));<br />
addByte((byte)((w & 0xFF00) >> 8));<br />
}<br />
<br />
/** Removes and returns a single word (2 bytes).<br />
* @return The word that was removed.<br />
* @throws IndexOutOfBoundsException If there isn't enough room in the buffer to accomidate the<br />
* requested removal.<br />
*/<br />
public short removeWord() throws IndexOutOfBoundsException<br />
{<br />
int ret = ((removeByte() << 0) & 0x000000FF) |<br />
((removeByte() << 8) & 0x0000FF00);<br />
<br />
return (short)(ret & 0x0000FFFF);<br />
}<br />
<br />
/** Adds a dword to the buffer (4 bytes, little endian).<br />
* @param d The dword to add.<br />
*/<br />
public void addDWord(int d)<br />
{<br />
addByte((byte)((d & 0x000000FF) >> 0));<br />
addByte((byte)((d & 0x0000FF00) >> 8));<br />
addByte((byte)((d & 0x00FF0000) >> 16));<br />
addByte((byte)((d & 0xFF000000) >> 24));<br />
<br />
}<br />
<br />
public void addArray(int []a)<br />
{<br />
for(int i = 0; i < a.length; i++)<br />
addDWord(a[i]);<br />
}<br />
<br />
/** Removes and returns a single dword (4 bytes).<br />
* @return The DWord that was removed.<br />
* @throws IndexOutOfBoundsException If there isn't enough room in the buffer to accomidate the<br />
* requested removal.<br />
*/<br />
public int removeDWord() throws IndexOutOfBoundsException<br />
{<br />
return ((removeByte() << 0) & 0x000000FF) |<br />
((removeByte() << 8) & 0x0000FF00) |<br />
((removeByte() << 16) & 0x00FF0000) |<br />
((removeByte() << 24) & 0xFF000000);<br />
}<br />
<br />
/** Adds a QWord to the buffer (8 bytes, little endian)<br />
* @param l The value to add.<br />
*/<br />
public void addLong(long l)<br />
{<br />
addByte((byte)((l & 0x00000000000000FFl) >> 0l));<br />
addByte((byte)((l & 0x000000000000FF00l) >> 8l));<br />
addByte((byte)((l & 0x0000000000FF0000l) >> 16l));<br />
addByte((byte)((l & 0x00000000FF000000l) >> 24l));<br />
addByte((byte)((l & 0x000000FF00000000l) >> 32l));<br />
addByte((byte)((l & 0x0000FF0000000000l) >> 40l));<br />
addByte((byte)((l & 0x00FF000000000000l) >> 48l));<br />
addByte((byte)((l & 0xFF00000000000000l) >> 56l));<br />
<br />
}<br />
<br />
/** Removes and returns a single dword (8 bytes).<br />
* @return The long at the beginning of the buffer.<br />
* @throws IndexOutOfBoundsException If there isn't enough room in the buffer to accomidate the<br />
* requested removal.<br />
*/<br />
public long removeLong() throws IndexOutOfBoundsException<br />
{<br />
return (((long)removeByte() << 0L) & 0x00000000000000FFL) |<br />
(((long)removeByte() << 8L) & 0x000000000000FF00L) |<br />
(((long)removeByte() << 16L) & 0x0000000000FF0000L) |<br />
(((long)removeByte() << 24L) & 0x00000000FF000000L) |<br />
(((long)removeByte() << 32L) & 0x000000FF00000000L) |<br />
(((long)removeByte() << 40L) & 0x0000FF0000000000L) |<br />
(((long)removeByte() << 48L) & 0x00FF000000000000L) |<br />
(((long)removeByte() << 56L) & 0xFF00000000000000L);<br />
}<br />
<br />
/** Adds a non-null-terminated string to the buffer<br />
* @param s The string to add to the buffer.<br />
*/<br />
public void addString(String s)<br />
{<br />
for(int i = 0; i < s.length(); i++)<br />
{<br />
addByte((byte) s.charAt(i));<br />
}<br />
}<br />
<br />
/** Removes 'i' bytes from the buffer, and returns them as a string<br />
* @param i The number of bytes to remove.<br />
* @return The bytes removed as a String.<br />
* @throws IndexOutOfBoundsException If there isn't enough room in the buffer to accomidate the<br />
* requested removal.<br />
*/<br />
public String removeString(int i) throws IndexOutOfBoundsException<br />
{<br />
StringBuffer s = new StringBuffer(i + 1);<br />
<br />
for(int j = 0; j < i; j++)<br />
{<br />
s.append((char) removeByte());<br />
}<br />
<br />
return s.toString();<br />
}<br />
<br />
/** Adds a null-terminated string to the buffer, including the null<br />
* @param s The string to add, without the null terminator.<br />
*/<br />
public void addNTString(String s)<br />
{<br />
addString(s);<br />
addByte((byte) 0x00);<br />
}<br />
<br />
/** Removes and returns a null-terminated string, without the null<br />
* @return The first null-terminatred string in the buffer, without the null.<br />
* @throws IndexOutOfBoundsException If the buffer ended before finding a Null.<br />
*/<br />
public String removeNTString()<br />
{<br />
StringBuffer s = new StringBuffer();<br />
<br />
byte b = removeByte();<br />
<br />
while(b != (byte) 0x00)<br />
{<br />
s.append((char) (b & 0x000000FF));<br />
b = removeByte();<br />
}<br />
<br />
return s.toString();<br />
}<br />
<br />
/** Adds an array of bytes to the buffer<br />
* @param b The bytes to add to the buffer.<br />
*/<br />
public void addBytes(byte[] b)<br />
{<br />
for(int i = 0; i < b.length; i++)<br />
addByte(b[i]);<br />
}<br />
<br />
/** Removes 'i' bytes from the array and returns them as an array of bytes.<br />
* @param i The number of bytes to remove.<br />
* @return The bytes that were removed.<br />
* @throws IndexOutOfBoundsException If there isn't enough room in the buffer to accomidate the<br />
* requested removal.<br />
*/<br />
public byte[] removeBytes(int i) throws IndexOutOfBoundsException<br />
{<br />
return remove(i); <br />
}<br />
<br />
/** Adds another buffer to the end of the buffer. All it actually does is add the bytes<br />
* of the source to the current buffer.<br />
* @param b The buffer to add.<br />
*/<br />
public void addBuffer(Buffer b)<br />
{<br />
addBytes(b.getBytes());<br />
}<br />
<br />
/** Quicly add a byte to the buffer.<br />
* @param b The byte to add.<br />
*/<br />
public void add(byte b)<br />
{<br />
addByte(b);<br />
}<br />
<br />
/** Quickly add a short to the buffer.<br />
* @param s The short to add.<br />
*/<br />
public void add(short s)<br />
{<br />
addWord(s);<br />
}<br />
<br />
/** Quickly add an int to the buffer.<br />
* @param i The inteter to add.<br />
*/<br />
public void add(int i)<br />
{<br />
addDWord(i);<br />
}<br />
<br />
public void add(int []i)<br />
{<br />
addArray(i);<br />
}<br />
<br />
/** Quickly add a long to the buffer.<br />
* @param l The long to add.<br />
*/<br />
public void add(long l)<br />
{<br />
addLong(l);<br />
}<br />
<br />
/** Quickly add an array of bytes to the buffer.<br />
* @param b The array of bytes.<br />
*/<br />
public void add(byte[] b)<br />
{<br />
addBytes(b);<br />
}<br />
<br />
/** Quickly append another buffer to the current buffer.<br />
* @param b The source buffer.<br />
*/<br />
public void add(Buffer b)<br />
{<br />
addBuffer(b);<br />
}<br />
<br />
/** Gets the buffer, formatted in a pretty way.<br />
* @return The formatted string. It might look something like this:<BR><br />
*<PRE><br />
* 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F ................<br />
* 69 68 67 66 65 64 63 62 61 61 6A 6B 6C 6D 6E 00 ihgfedcbaajklmn.<br />
* 41 00 A.<br />
* Length: 34<br />
*</PRE><br />
*/<br />
public String toString()<br />
{<br />
StringBuffer returnString = new StringBuffer( (currentLength * 3) + // The hex<br />
(currentLength) + // The ascii<br />
(currentLength / 4) + // The tabs/\n's<br />
30 ); // The text<br />
<br />
//returnString.append("Buffer contents:\n");<br />
int i, j; // Loop variables<br />
for(i = 0; i < currentLength; i++)<br />
{<br />
if((i != 0) && (i % 16 == 0))<br />
{<br />
// If it's a multiple of 16 and i isn't null, show the ascii<br />
returnString.append('\t');<br />
for(j = i - 16; j < i; j++)<br />
{<br />
if(buffer[j] < 0x20 || buffer[j] > 0x7F)<br />
returnString.append('.');<br />
else<br />
returnString.append((char)buffer[j]);<br />
}<br />
// Add a linefeed after the string<br />
returnString.append("\n");<br />
}<br />
<br />
returnString.append(Integer.toString((buffer[i] & 0xF0) >> 4, 16) +<br />
Integer.toString((buffer[i] & 0x0F) >> 0, 16));<br />
returnString.append(' ');<br />
}<br />
<br />
// Add padding spaces if it's not a multiple of 16<br />
if(i != 0 && i % 16 != 0)<br />
{<br />
for(j = 0; j < ((16 - (i % 16)) * 3); j++)<br />
{<br />
returnString.append(' ');<br />
}<br />
}<br />
// Add the tab for alignment<br />
returnString.append('\t');<br />
<br />
// Add final chararacters at right, after padding<br />
<br />
// If it was at the end of a line, print out the full line<br />
if(i > 0 && (i % 16) == 0)<br />
{<br />
j = i - 16;<br />
} <br />
else<br />
{<br />
j = (i - (i % 16));<br />
}<br />
<br />
for(; i >= 0 && j < i; j++)<br />
{<br />
if(buffer[j] < 0x20 || buffer[j] > 0x7F)<br />
returnString.append('.');<br />
else<br />
returnString.append((char) buffer[j]);<br />
}<br />
<br />
// Finally, tidy it all up with a newline<br />
returnString.append('\n');<br />
returnString.append("Length: " + currentLength + '\n');<br />
<br />
return returnString.toString();<br />
}<br />
<br />
}</div>Ron