Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support writing unsigned integers to buffer #691

Merged
merged 3 commits into from
May 26, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/main/java/net/schmizz/sshj/common/Buffer.java
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,23 @@ public long readUInt32()
data[rpos++] & 0x000000ffL;
}

/**
* Writes a uint32 integer
*
* @param uint32
*
* @return this
*/
@SuppressWarnings("unchecked")
public T putUInt32FromInt(int uint32) {
ensureCapacity(4);
data[wpos++] = (byte) (uint32 >> 24);
data[wpos++] = (byte) (uint32 >> 16);
data[wpos++] = (byte) (uint32 >> 8);
data[wpos++] = (byte) uint32;
return (T) this;
}

/**
* Writes a uint32 integer
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ private void gotChannelOpen(SSHPacket buf)
public void sendOpenFailure(int recipient, Reason reason, String message)
throws TransportException {
trans.write(new SSHPacket(Message.CHANNEL_OPEN_FAILURE)
.putUInt32(recipient)
.putUInt32FromInt(recipient)
.putUInt32(reason.getCode())
.putString(message));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ protected void handleRequest(String reqType, SSHPacket buf)
}

protected SSHPacket newBuffer(Message cmd) {
return new SSHPacket(cmd).putUInt32(recipient);
return new SSHPacket(cmd).putUInt32FromInt(recipient);
}

protected void receiveInto(ChannelInputStream stream, SSHPacket buf)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ private void checkWindow()
if (adjustment > 0) {
log.debug("Sending SSH_MSG_CHANNEL_WINDOW_ADJUST to #{} for {} bytes", chan.getRecipient(), adjustment);
trans.write(new SSHPacket(Message.CHANNEL_WINDOW_ADJUST)
.putUInt32(chan.getRecipient()).putUInt32(adjustment));
.putUInt32FromInt(chan.getRecipient()).putUInt32(adjustment));
win.expand(adjustment);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ boolean flush(int bufferSize, boolean canAwaitExpansion) throws TransportExcepti

packet.wpos(headerOffset);
packet.putMessageID(Message.CHANNEL_DATA);
packet.putUInt32(chan.getRecipient());
packet.putUInt32FromInt(chan.getRecipient());
packet.putUInt32(writeNow);
packet.wpos(dataOffset + writeNow);

Expand Down
20 changes: 20 additions & 0 deletions src/test/java/net/schmizz/sshj/common/BufferTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@

public class BufferTest {

@Test
public void testNegativeInteger() throws BufferException {
byte[] negativeInt = new byte[] { (byte) 0xB8,
(byte) 0x4B,
(byte) 0xF4,
(byte) 0x38,
};
PlainBuffer buffer = new PlainBuffer(negativeInt);
assertEquals(buffer.readUInt32AsInt(),-1202981832);

PlainBuffer buff = new PlainBuffer();
buff.ensureCapacity(4);
buff.putUInt32FromInt(-1202981832);
byte[] data = buff.getCompactData();
assertEquals(data[0], (byte) 0xB8);
assertEquals(data[1], (byte) 0x4B);
assertEquals(data[2], (byte) 0xF4);
assertEquals(data[3], (byte) 0x38);
}

// Issue 72: previously, it entered an infinite loop trying to establish the buffer size
@Test
public void shouldThrowOnTooLargeCapacity() {
Expand Down