send length + data at the same time

This commit is contained in:
Alba Mendez
2023-03-14 17:03:35 +01:00
parent 38709f3ad8
commit 76e267968d
2 changed files with 10 additions and 10 deletions

View File

@@ -179,13 +179,12 @@ class VPCDWorker extends AsyncTask<VPCDWorker.VPCDWorkerParams, Void, Void> {
private void sendToVPCD(byte[] data) throws IOException { private void sendToVPCD(byte[] data) throws IOException {
/* convert length to network byte order. /* convert length to network byte order.
Note that Java always uses network byte order internally. */ Note that Java always uses network byte order internally. */
byte[] length = new byte[2]; byte[] packet = new byte[2 + data.length];
length[0] = (byte) (data.length >> 8); packet[0] = (byte) (data.length >> 8);
length[1] = (byte) (data.length & 0xff); packet[1] = (byte) (data.length & 0xff);
outputStream.write(length); System.arraycopy(data, 0, packet, 2, data.length);
outputStream.write(data, 0, data.length);
outputStream.write(packet);
outputStream.flush(); outputStream.flush();
} }

View File

@@ -219,6 +219,7 @@ static ssize_t sendToVICC(struct vicc_ctx *ctx, size_t length, const unsigned ch
{ {
ssize_t r; ssize_t r;
uint16_t size; uint16_t size;
char *sendBuffer;
if (!ctx || length > 0xFFFF) { if (!ctx || length > 0xFFFF) {
errno = EINVAL; errno = EINVAL;
@@ -226,11 +227,11 @@ static ssize_t sendToVICC(struct vicc_ctx *ctx, size_t length, const unsigned ch
} }
/* send size of message on 2 bytes */ /* send size of message on 2 bytes */
sendBuffer = (char *) alloca(length + 2);
size = htons((uint16_t) length); size = htons((uint16_t) length);
r = sendall(ctx->client_sock, (void *) &size, sizeof size); memcpy(sendBuffer, &size, 2);
if (r == sizeof size) memcpy(sendBuffer + 2, buffer, length);
/* send message */ r = sendall(ctx->client_sock, sendBuffer, length + 2);
r = sendall(ctx->client_sock, buffer, length);
if (r < 0) if (r < 0)
vicc_eject(ctx); vicc_eject(ctx);