Java Code Examples for com.sun.jna.Pointer#getByteArray()

The following examples show how to use com.sun.jna.Pointer#getByteArray() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractSocket.java    From jnanomsg with Apache License 2.0 6 votes vote down vote up
public synchronized byte[] recvBytes(final EnumSet<SocketFlag> flagSet)
  throws IOException {

  final PointerByReference ptrBuff = new PointerByReference();

  int flags = 0;
  if (flagSet.contains(SocketFlag.NN_DONTWAIT)){
    flags |= SocketFlag.NN_DONTWAIT.value();
  }

  final int rc = nn_recv(this.fd, ptrBuff, NN_MSG, flags);

  if (rc < 0) {
    Nanomsg.handleError(rc);
  }

  final Pointer result = ptrBuff.getValue();
  final byte[] bytesResult = result.getByteArray(0, rc);

  return bytesResult;
}
 
Example 2
Source File: TcpSocket.java    From unidbg with Apache License 2.0 6 votes vote down vote up
@Override
protected int connect_ipv4(Pointer addr, int addrlen) {
    if (log.isDebugEnabled()) {
        byte[] data = addr.getByteArray(0, addrlen);
        Inspector.inspect(data, "addr");
    }

    int sa_family = addr.getShort(0);
    if (sa_family != AF_INET) {
        throw new AbstractMethodError("sa_family=" + sa_family);
    }

    try {
        int port = Short.reverseBytes(addr.getShort(2)) & 0xffff;
        InetSocketAddress address = new InetSocketAddress(InetAddress.getByAddress(addr.getByteArray(4, 4)), port);
        socket.connect(address);
        outputStream = socket.getOutputStream();
        inputStream = socket.getInputStream();
        return 0;
    } catch (IOException e) {
        log.debug("connect ipv4 failed", e);
        emulator.getMemory().setErrno(UnixEmulator.ECONNREFUSED);
        return -1;
    }
}
 
Example 3
Source File: Advapi32Util.java    From jpexs-decompiler with GNU General Public License v3.0 6 votes vote down vote up
public EventLogRecord(Pointer pevlr) {
    _record = new EVENTLOGRECORD(pevlr);
    _source = pevlr.getString(_record.size(), true);
    // data
    if (_record.DataLength.intValue() > 0) {
        _data = pevlr.getByteArray(_record.DataOffset.intValue(),
                _record.DataLength.intValue());
    }
    // strings
    if (_record.NumStrings.intValue() > 0) {
        ArrayList<String> strings = new ArrayList<>();
        int count = _record.NumStrings.intValue();
        long offset = _record.StringOffset.intValue();
        while (count > 0) {
            String s = pevlr.getString(offset, true);
            strings.add(s);
            offset += s.length() * Native.WCHAR_SIZE;
            offset += Native.WCHAR_SIZE;
            count--;
        }
        _strings = strings.toArray(new String[strings.size()]);
    }
}
 
Example 4
Source File: UdpSocket.java    From unidbg with Apache License 2.0 6 votes vote down vote up
@Override
protected int connect_ipv6(Pointer addr, int addrlen) {
    if (log.isDebugEnabled()) {
        byte[] data = addr.getByteArray(0, addrlen);
        Inspector.inspect(data, "addr");
    }

    int sa_family = addr.getShort(0);
    if (sa_family != AF_INET6) {
        throw new AbstractMethodError("sa_family=" + sa_family);
    }

    try {
        int port = Short.reverseBytes(addr.getShort(2)) & 0xffff;
        InetSocketAddress address = new InetSocketAddress(InetAddress.getByAddress(addr.getByteArray(4, 16)), port);
        datagramSocket.connect(address);
        return 0;
    } catch (IOException e) {
        log.debug("connect ipv6 failed", e);
        emulator.getMemory().setErrno(UnixEmulator.ECONNREFUSED);
        return -1;
    }
}
 
Example 5
Source File: UdpSocket.java    From unidbg with Apache License 2.0 6 votes vote down vote up
@Override
protected int connect_ipv4(Pointer addr, int addrlen) {
    if (log.isDebugEnabled()) {
        byte[] data = addr.getByteArray(0, addrlen);
        Inspector.inspect(data, "addr");
    }

    int sa_family = addr.getShort(0);
    if (sa_family != AF_INET) {
        throw new AbstractMethodError("sa_family=" + sa_family);
    }

    try {
        int port = Short.reverseBytes(addr.getShort(2)) & 0xffff;
        InetSocketAddress address = new InetSocketAddress(InetAddress.getByAddress(addr.getByteArray(8, 4)), port);
        datagramSocket.connect(address);
        return 0;
    } catch (IOException e) {
        log.debug("connect ipv4 failed", e);
        emulator.getMemory().setErrno(UnixEmulator.ECONNREFUSED);
        return -1;
    }
}
 
Example 6
Source File: UdpSocket.java    From unidbg with Apache License 2.0 6 votes vote down vote up
@Override
public int sendto(byte[] data, int flags, Pointer dest_addr, int addrlen) {
    if (addrlen != 16) {
        throw new IllegalStateException("addrlen=" + addrlen);
    }

    if (log.isDebugEnabled()) {
        byte[] addr = dest_addr.getByteArray(0, addrlen);
        Inspector.inspect(addr, "addr");
    }

    int sa_family = dest_addr.getInt(0);
    if (sa_family != AF_INET) {
        throw new AbstractMethodError("sa_family=" + sa_family);
    }

    try {
        InetAddress address = InetAddress.getByAddress(dest_addr.getByteArray(4, 4));
        throw new UnsupportedOperationException("address=" + address);
    } catch (IOException e) {
        log.debug("sendto failed", e);
        emulator.getMemory().setErrno(UnixEmulator.EACCES);
        return -1;
    }
}
 
Example 7
Source File: NotesAttachment.java    From domino-jna with Apache License 2.0 6 votes vote down vote up
@Override
public short invoke(Pointer data, int length, Pointer param) {
	if (length==0)
		return 0;
	
	try {
		byte[] dataArr = data.getByteArray(0, length);
		Action action = callback.read(dataArr);
		if (action==Action.Continue) {
			return 0;
		}
		else {
			return INotesErrorConstants.ERR_NSF_INTERRUPT;
		}
	}
	catch (Throwable t) {
		extractError[0] = t;
		return INotesErrorConstants.ERR_NSF_INTERRUPT;
	}
}
 
Example 8
Source File: UdpSocket.java    From unidbg with Apache License 2.0 6 votes vote down vote up
@Override
public int sendto(byte[] data, int flags, Pointer dest_addr, int addrlen) {
    if (addrlen != 16) {
        throw new IllegalStateException("addrlen=" + addrlen);
    }

    if (log.isDebugEnabled()) {
        byte[] addr = dest_addr.getByteArray(0, addrlen);
        Inspector.inspect(addr, "addr");
    }

    int sa_family = dest_addr.getInt(0);
    if (sa_family != AF_INET) {
        throw new AbstractMethodError("sa_family=" + sa_family);
    }

    try {
        InetAddress address = InetAddress.getByAddress(dest_addr.getByteArray(4, 4));
        throw new UnsupportedOperationException("address=" + address);
    } catch (IOException e) {
        log.debug("sendto failed", e);
        emulator.getMemory().setErrno(UnixEmulator.EACCES);
        return -1;
    }
}
 
Example 9
Source File: TcpSocket.java    From unidbg with Apache License 2.0 6 votes vote down vote up
@Override
protected int bind_ipv4(Pointer addr, int addrlen) {
    int sa_family = addr.getShort(0);
    if (sa_family != AF_INET) {
        throw new AbstractMethodError("sa_family=" + sa_family);
    }

    try {
        int port = Short.reverseBytes(addr.getShort(2)) & 0xffff;
        InetSocketAddress address = new InetSocketAddress(InetAddress.getByAddress(addr.getByteArray(4, 4)), port);
        if (log.isDebugEnabled()) {
            byte[] data = addr.getByteArray(0, addrlen);
            Inspector.inspect(data, "address=" + address);
        }
        socket.bind(address);
        return 0;
    } catch (IOException e) {
        log.debug("bind ipv4 failed", e);
        emulator.getMemory().setErrno(UnixEmulator.EADDRINUSE);
        return -1;
    }
}
 
Example 10
Source File: InlineHook.java    From unidbg with Apache License 2.0 6 votes vote down vote up
public static void simpleArmIntercept(Emulator<?> emulator, long address, InterceptCallback callback) {
    Pointer pointer = UnicornPointer.pointer(emulator, address);
    if (pointer == null) {
        throw new IllegalArgumentException();
    }
    Capstone capstone = null;
    try {
        capstone = new Capstone(Capstone.CS_ARCH_ARM, Capstone.CS_MODE_ARM);
        capstone.setDetail(Capstone.CS_OPT_ON);

        byte[] code = pointer.getByteArray(0, 4);
        Capstone.CsInsn[] insns = capstone.disasm(code, 0, 1);
        if (insns == null || insns.length < 1) {
            throw new IllegalArgumentException("Invalid intercept address: " + pointer);
        }
        Capstone.CsInsn insn = insns[0];
        emulator.getSvcMemory().registerSvc(new ArmIntercept(pointer, callback, insn));
    } finally {
        if (capstone != null) {
            capstone.close();
        }
    }
}
 
Example 11
Source File: ARM64SyscallHandler.java    From unidbg with Apache License 2.0 6 votes vote down vote up
private int write(Emulator<?> emulator, int offset) {
    RegisterContext context = emulator.getContext();
    int fd = context.getIntArg(offset);
    Pointer buffer = context.getPointerArg(offset + 1);
    int count = context.getIntArg(offset + 2);
    byte[] data = buffer.getByteArray(0, count);
    if (log.isDebugEnabled()) {
        log.debug("write fd=" + fd + ", buffer=" + buffer + ", count=" + count);
    }

    FileIO file = fdMap.get(fd);
    if (file == null) {
        emulator.getMemory().setErrno(UnixEmulator.EBADF);
        return -1;
    }
    return file.write(data);
}
 
Example 12
Source File: ARM32SyscallHandler.java    From unidbg with Apache License 2.0 6 votes vote down vote up
private int write(Unicorn u, Emulator<?> emulator) {
    int fd = ((Number) u.reg_read(ArmConst.UC_ARM_REG_R0)).intValue();
    Pointer buffer = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R1);
    int count = ((Number) u.reg_read(ArmConst.UC_ARM_REG_R2)).intValue();
    byte[] data = buffer.getByteArray(0, count);
    if (log.isDebugEnabled()) {
        log.debug("write fd=" + fd + ", buffer=" + buffer + ", count=" + count);
    }

    FileIO file = fdMap.get(fd);
    if (file == null) {
        emulator.getMemory().setErrno(UnixEmulator.EBADF);
        return -1;
    }
    return file.write(data);
}
 
Example 13
Source File: NDPITiledReader.java    From orbit-image-analysis with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Get the slide overview image
 * 
 * @param imageID Image ID - see {@link #initImage(Path)} / {@link #initTestImage(int, int)}
 * @return Byte array for the jpeg representation of the slide image
 */
public byte[] getSlideData (final int imageID) throws NDPRException, IOException
{
    final int requestID = NDPR.createRequest(RequestType.GetMetaImage);
    try {
        NDPR.setRequestString(requestID, "format", "JPEG");
        NDPR.setRequestString(requestID, "metaimagename", "ndpImage.SlideImage");
        NDPR.setRequestInt(requestID, "imageid", imageID);

        // NOTE This request throws an exception if no such image is
        // present, but we simply want to return 'null' in this case
        try {
            NDPR.executeRequest(requestID);
        } catch (NDPRException exception) {
            switch (exception.getError()) {
            case MetaImageNotPresent:
                return null;
            default:
                throw exception;
            }
        }

        // TODO Create a method to read out that information?
        // final int imageWidth = NDPR.getRequestInt(requestID, "width");
        // final int imageHeight = NDPR.getRequestInt(requestID, "height");
        // final int physicalX = NDPR.getRequestInt(requestID, "physicalx");
        // final int physicalY = NDPR.getRequestInt(requestID, "physicaly");
        // final int physicalWidth = NDPR.getRequestInt(requestID, "physicalwidth");
        // final int physicalHeight = NDPR.getRequestInt(requestID, "physicalheight");

        final Pointer imgBuffer = NDPR.getRequestPointer(requestID, "imagebuffer");
        final int imgBufferSize = NDPR.getRequestInt(requestID, "buffersize");

        return imgBuffer.getByteArray(0, imgBufferSize);
    } finally {
        NDPR.freeRequest(requestID);
    }
}
 
Example 14
Source File: MachOLoader.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private void __NSSetLogCStringFunction(Pointer message, int length, boolean withSysLogBanner) {
    byte[] data = message.getByteArray(0, length);
    String str = new String(data, StandardCharsets.UTF_8);
    if (withSysLogBanner) {
        System.err.println("NSLog: " + str);
    } else {
        System.out.println("NSLog: " + str);
    }
}
 
Example 15
Source File: InlineHook.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private static byte[] getThumbCode(Pointer pointer) {
    short ins = pointer.getShort(0);
    if(ARM.isThumb32(ins)) { // thumb32
        return pointer.getByteArray(0, 4);
    } else {
        return pointer.getByteArray(0, 2);
    }
}
 
Example 16
Source File: JnaDataFileSource.java    From mr4c with Apache License 2.0 5 votes vote down vote up
private void ensureSource() {
	if ( m_delegate!=null ) {
		return;
	}
	long size = getSizeHelper();
	Pointer ptr = getPointer();
	byte[] bytes = ptr.getByteArray(0, (int)size);
	m_delegate = new BytesDataFileSource(bytes);
}
 
Example 17
Source File: UnixSyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
protected final int connect(Emulator<?> emulator, int sockfd, Pointer addr, int addrlen) {
    if (log.isDebugEnabled()) {
        byte[] data = addr.getByteArray(0, addrlen);
        Inspector.inspect(data, "connect sockfd=" + sockfd + ", addr=" + addr + ", addrlen=" + addrlen);
    }

    FileIO file = fdMap.get(sockfd);
    if (file == null) {
        emulator.getMemory().setErrno(UnixEmulator.EBADF);
        return -1;
    }
    return file.connect(addr, addrlen);
}
 
Example 18
Source File: UnixSyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
protected final int bind(Emulator<?> emulator, int sockfd, Pointer addr, int addrlen) {
    if (log.isDebugEnabled()) {
        byte[] data = addr.getByteArray(0, addrlen);
        Inspector.inspect(data, "bind sockfd=" + sockfd + ", addr=" + addr + ", addrlen=" + addrlen);
    }

    FileIO file = fdMap.get(sockfd);
    if (file == null) {
        emulator.getMemory().setErrno(UnixEmulator.EBADF);
        return -1;
    }
    return file.bind(addr, addrlen);
}
 
Example 19
Source File: NSData.java    From unidbg with Apache License 2.0 4 votes vote down vote up
public byte[] getBytes() {
    UnicornPointer pointer = (UnicornPointer) object.call("length");
    int length = (int) (pointer.peer & 0x7fffffff);
    Pointer bytes = getBytesPointer();
    return bytes.getByteArray(0, length);
}
 
Example 20
Source File: Nc4Iosp.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 4 votes vote down vote up
private Array decodeVlen(DataType dt, int pos, ByteBuffer bbuff) throws IOException {
  Array array;
  int n = (int) bbuff.getLong(pos); // Note that this does not increment the buffer position
  long addr = getNativeAddr(pos + com.sun.jna.Native.POINTER_SIZE, bbuff);
  Pointer p = new Pointer(addr);
  Object data;
  switch (dt) {
    case BOOLEAN: /* byte[] */
    case ENUM1:
    case BYTE:
      data = p.getByteArray(0, n);
      break;
    case ENUM2:
    case SHORT: /* short[] */
      data = p.getShortArray(0, n);
      break;
    case ENUM4:
    case INT: /* int[] */
      data = p.getIntArray(0, n);
      break;
    case LONG: /* long[] */
      data = p.getLongArray(0, n);
      break;
    case FLOAT: /* float[] */
      data = p.getFloatArray(0, n);
      break;
    case DOUBLE: /* double[] */
      data = p.getDoubleArray(0, n);
      break;
    case CHAR: /* char[] */
      data = p.getCharArray(0, n);
      break;
    case STRING: /* String[] */
      // For now we need to use p.getString()
      // because p.getStringArray(int,int) does not exist
      // in jna version 3.0.9, but does exist in
      // verssion 4.0 and possibly some intermediate versions
      String[] stringdata = new String[n];
      for (int i = 0; i < n; i++)
        stringdata[i] = p.getString(i * 8);
      data = stringdata;
      break;
    case OPAQUE:
    case STRUCTURE:
    default:
      throw new IllegalStateException();
  }
  array = Array.factory(dt, new int[] {n}, data);
  return array;
}