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

The following examples show how to use com.sun.jna.Pointer#getShort() . 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: 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 2
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 3
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 4
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 5
Source File: TcpSocket.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(8, 16)), port);
        socket.connect(address);
        outputStream = socket.getOutputStream();
        inputStream = socket.getInputStream();
        return 0;
    } catch (IOException e) {
        log.debug("connect ipv6 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
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 7
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 8
Source File: TcpSocket.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(8, 16)), port);
        socket.connect(address);
        outputStream = socket.getOutputStream();
        inputStream = socket.getInputStream();
        return 0;
    } catch (IOException e) {
        log.debug("connect ipv6 failed", e);
        emulator.getMemory().setErrno(UnixEmulator.ECONNREFUSED);
        return -1;
    }
}
 
Example 9
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 10
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 11
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 12
Source File: ItemDecoder.java    From domino-jna with Apache License 2.0 5 votes vote down vote up
public static List<Object> decodeTextListValue(Pointer ptr, boolean convertStringsLazily) {
	//read a text list item value
	int listCountAsInt = ptr.getShort(0) & 0xffff;
	
	List<Object> listValues = new ArrayList<Object>(listCountAsInt);
	
	Memory retTextPointer = new Memory(Native.POINTER_SIZE);
	ShortByReference retTextLength = new ShortByReference();
	
	for (short l=0; l<listCountAsInt; l++) {
		short result = NotesNativeAPI.get().ListGetText(ptr, false, l, retTextPointer, retTextLength);
		NotesErrorUtils.checkResult(result);
		
		//retTextPointer[0] points to the list entry text
		Pointer pointerToTextInMem = retTextPointer.getPointer(0);
		int retTextLengthAsInt = retTextLength.getValue() & 0xffff;
		
		if (retTextLengthAsInt==0) {
			listValues.add("");
		}
		else {
			if (convertStringsLazily) {
				byte[] stringDataArr = new byte[retTextLengthAsInt];
				pointerToTextInMem.read(0, stringDataArr, 0, retTextLengthAsInt);

				LMBCSString lmbcsString = new LMBCSString(stringDataArr);
				listValues.add(lmbcsString);
			}
			else {
				String currListEntry = NotesStringUtils.fromLMBCS(pointerToTextInMem, (short) retTextLengthAsInt);
				listValues.add(currListEntry);
			}
		}
	}
	
	return listValues;
}
 
Example 13
Source File: SearchMatchDecoder.java    From domino-jna with Apache License 2.0 5 votes vote down vote up
/**
 * Decodes the SEARCH_MATCH structure starting at the specified memory address
 * 
 * @param ptr memory pointer
 * @return object with search match data
 */
public static NotesSearch.ISearchMatch decodeSearchMatch(Pointer ptr) {
	int gid_file_innards0 = ptr.getInt(0);
	int gid_file_innards1 = ptr.getInt(4);
	
	int gid_note_innards0 = ptr.getInt(8);
	int gid_note_innards1 = ptr.getInt(12);
	
	int noteId = ptr.getInt(16);
	
	int oid_file_innards0 = ptr.getInt(20);
	int oid_file_innards1 = ptr.getInt(24);
	
	int oid_note_innards0 = ptr.getInt(28);
	int oid_note_innards1 = ptr.getInt(32);
	int seq = ptr.getInt(36);
	int seqTimeInnards0 = ptr.getInt(40);
	int seqTimeInnards1 = ptr.getInt(44);
	
	short noteClass = ptr.getShort(48);
	byte seRetFlags = ptr.getByte(50);
	byte privileges = ptr.getByte(51);
	short summaryLength = ptr.getShort(52);
	
	SearchMatchImpl match = new SearchMatchImpl();
	match.setGIDFileInnards(new int[] {gid_file_innards0, gid_file_innards1});
	match.setGIDNoteInnards(new int[] {gid_note_innards0, gid_note_innards1});
	match.setNoteId(noteId);
	match.setOIDFileInnards(new int[] {oid_file_innards0, oid_file_innards1});
	match.setOIDNoteInnards(new int[] {oid_note_innards0, oid_note_innards1});
	match.setSeq(seq);
	match.setSeqTimeInnards(new int[] {seqTimeInnards0, seqTimeInnards1});
	match.setNoteClass(noteClass);
	match.setSeRetFlags(seRetFlags);
	match.setPrivileges(privileges);
	match.setSummaryLength(summaryLength);
	return match;
}
 
Example 14
Source File: LocalUdpSocket.java    From unidbg with Apache License 2.0 5 votes vote down vote up
@Override
public int connect(Pointer addr, int addrlen) {
    short sa_family = addr.getShort(0);
    if (sa_family != AF_LOCAL) {
        throw new UnsupportedOperationException("sa_family=" + sa_family);
    }

    String path = addr.getString(2);
    log.debug("connect sa_family=" + sa_family + ", path=" + path);

    return connect(path);
}
 
Example 15
Source File: ARM64SyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private int ppoll(Emulator<?> emulator) {
    RegisterContext context = emulator.getContext();
    Pointer fds = context.getPointerArg(0);
    int nfds = context.getIntArg(1);
    Pointer tmo_p = context.getPointerArg(2);
    Pointer sigmask = context.getPointerArg(3);
    int count = 0;
    for (int i = 0; i < nfds; i++) {
        Pointer pollfd = fds.share(i * 8);
        int fd = pollfd.getInt(0);
        short events = pollfd.getShort(4); // requested events
        if (log.isDebugEnabled()) {
            log.debug("ppoll fds=" + fds + ", nfds=" + nfds + ", tmo_p=" + tmo_p + ", sigmask=" + sigmask + ", fd=" + fd + ", events=" + events);
        }
        if (fd < 0) {
            pollfd.setShort(6, (short) 0);
        } else {
            short revents = 0;
            if((events & POLLOUT) != 0) {
                revents = POLLOUT;
            } else if ((events & POLLIN) != 0) {
                revents = POLLIN;
            }
            pollfd.setShort(6, revents); // returned events
            count++;
        }
    }
    return count;
}
 
Example 16
Source File: LocalUdpSocket.java    From unidbg with Apache License 2.0 5 votes vote down vote up
@Override
public int connect(Pointer addr, int addrlen) {
    short sa_family = addr.getShort(0);
    if (sa_family != AF_LOCAL) {
        throw new UnsupportedOperationException("sa_family=" + sa_family);
    }

    String path = addr.getString(2);
    log.debug("connect sa_family=" + sa_family + ", path=" + path);

    return connect(path);
}
 
Example 17
Source File: ARM32SyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private int poll(Unicorn u, Emulator<?> emulator) {
    Pointer fds = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R0);
    int nfds = ((Number) u.reg_read(ArmConst.UC_ARM_REG_R1)).intValue();
    int timeout = ((Number) u.reg_read(ArmConst.UC_ARM_REG_R2)).intValue();
    int count = 0;
    for (int i = 0; i < nfds; i++) {
        Pointer pollfd = fds.share(i * 8);
        int fd = pollfd.getInt(0);
        short events = pollfd.getShort(4); // requested events
        if (log.isDebugEnabled()) {
            log.debug("poll fds=" + fds + ", nfds=" + nfds + ", timeout=" + timeout + ", fd=" + fd + ", events=" + events);
        }
        if (fd < 0) {
            pollfd.setShort(6, (short) 0);
        } else {
            short revents = 0;
            if((events & POLLOUT) != 0) {
                revents = POLLOUT;
            } else if ((events & POLLIN) != 0) {
                revents = POLLIN;
            }
            pollfd.setShort(6, revents); // returned events
            count++;
        }
    }
    return count;
}
 
Example 18
Source File: ARM64SyscallHandler.java    From unidbg with Apache License 2.0 5 votes vote down vote up
private int poll(Unicorn u, Emulator<?> emulator) {
    Pointer fds = UnicornPointer.register(emulator, ArmConst.UC_ARM_REG_R0);
    int nfds = ((Number) u.reg_read(ArmConst.UC_ARM_REG_R1)).intValue();
    int timeout = ((Number) u.reg_read(ArmConst.UC_ARM_REG_R2)).intValue();
    int count = 0;
    for (int i = 0; i < nfds; i++) {
        Pointer pollfd = fds.share(i * 8);
        int fd = pollfd.getInt(0);
        short events = pollfd.getShort(4); // requested events
        if (log.isDebugEnabled()) {
            log.debug("poll fds=" + fds + ", nfds=" + nfds + ", timeout=" + timeout + ", fd=" + fd + ", events=" + events);
        }
        if (fd < 0) {
            pollfd.setShort(6, (short) 0);
        } else {
            short revents = 0;
            if((events & POLLOUT) != 0) {
                revents = POLLOUT;
            } else if ((events & POLLIN) != 0) {
                revents = POLLIN;
            }
            pollfd.setShort(6, revents); // returned events
            count++;
        }
    }
    return count;
}
 
Example 19
Source File: NotesLookupResultBufferDecoder.java    From domino-jna with Apache License 2.0 4 votes vote down vote up
/**
	 * Decodes an ITEM_TABLE structure with item names and item values
	 * 
	 * @param bufferPtr pointer to a buffer
	 * @param convertStringsLazily true to delay string conversion until the first use
	 * @param convertNotesTimeDateToCalendar true to convert {@link NotesTimeDate} values to {@link Calendar}
	 * @param decodeAllValues true to decode all values in the buffer
	 * @return data
	 */
	public static IItemTableData decodeItemTable(Pointer bufferPtr,
			boolean convertStringsLazily, boolean convertNotesTimeDateToCalendar, boolean decodeAllValues) {
		int bufferPos = 0;
		NotesItemTableStruct itemTable = NotesItemTableStruct.newInstance(bufferPtr);
		itemTable.read();
		
		//skip item table header
		bufferPos += itemTable.size();

//		typedef struct {
//			   USHORT Length; /*  total length of this buffer */
//			   USHORT Items;  /* number of items in the table */
//			/* now come an array of ITEMs */
//			/* now comes the packed text containing the item names. */
//			} ITEM_TABLE;					
		
		int itemsCount = itemTable.getItemsAsInt();
		int[] itemValueLengths = new int[itemsCount];
		int[] itemNameLengths = new int[itemsCount];
		
		//read ITEM structures for each item
		for (int j=0; j<itemsCount; j++) {
			Pointer itemPtr = bufferPtr.share(bufferPos);
			itemNameLengths[j] = (int) (itemPtr.getShort(0) & 0xffff);
			itemValueLengths[j] = (int) (itemPtr.share(2).getShort(0) & 0xffff);
			
			bufferPos += NotesConstants.tableItemSize;
		}
		
		ItemTableDataImpl data = new ItemTableDataImpl(convertStringsLazily);
		data.setPreferNotesTimeDates(!convertNotesTimeDateToCalendar);
		data.m_totalBufferLength = itemTable.getLengthAsInt();
		data.m_itemsCount = itemsCount;
		
		Pointer itemValuePtr = bufferPtr.share(bufferPos);
		populateItemValueTableData(itemValuePtr, itemsCount, itemNameLengths, itemValueLengths,
				data, convertStringsLazily, convertNotesTimeDateToCalendar, decodeAllValues);
		
		return data;
	}
 
Example 20
Source File: NotesLookupResultBufferDecoder.java    From domino-jna with Apache License 2.0 4 votes vote down vote up
/**
	 * Decodes an ITEM_VALUE_TABLE structure, which contains an ordered list of item values
	 * 
	 * @param bufferPtr pointer to a buffer
	 * @param convertStringsLazily true to delay string conversion until the first use
	 * @param convertNotesTimeDateToCalendar true to convert {@link NotesTimeDate} values to {@link Calendar}
	 * @param decodeAllValues true to decode all values in the buffer
	 * @return item value table data
	 */
	public static IItemValueTableData decodeItemValueTable(Pointer bufferPtr,
			boolean convertStringsLazily, boolean convertNotesTimeDateToCalendar, boolean decodeAllValues) {
		int bufferPos = 0;
		
		//skip item value table header
		bufferPos += NotesConstants.itemValueTableSize;
		
//		The information in a view summary of values is as follows:
//
//			ITEM_VALUE_TABLE containing header information (total length of summary, number of items in summary)
//			WORD containing the length of item #1 (including data type)
//			WORD containing the length of item #2 (including data type)
//			WORD containing the length of item #3 (including data type)
//			...
//			USHORT containing the data type of item #1
//			value of item #1
//			USHORT containing the data type of item #2
//			value of item #2
//			USHORT containing the data type of item #3
//			value of item #3
//			....
		
		int totalBufferLength = bufferPtr.getShort(0) & 0xffff;
		int itemsCount = bufferPtr.getShort(2) & 0xffff;
		
		int[] itemValueLengths = new int[itemsCount];
		//we don't have any item names:
		int[] itemNameLengths = null;
		
		//read all item lengths
		for (int j=0; j<itemsCount; j++) {
			//convert USHORT to int without sign
			itemValueLengths[j] = bufferPtr.getShort(bufferPos) & 0xffff;
			bufferPos += 2;
		}

		ItemValueTableDataImpl data = new ItemValueTableDataImpl(convertStringsLazily);
		data.setPreferNotesTimeDates(!convertNotesTimeDateToCalendar);
		data.m_totalBufferLength = totalBufferLength;
		data.m_itemsCount = itemsCount;

		Pointer itemValuePtr = bufferPtr.share(bufferPos);
		populateItemValueTableData(itemValuePtr, itemsCount, itemNameLengths, itemValueLengths, data,
				convertStringsLazily, convertNotesTimeDateToCalendar, decodeAllValues);

		return data;
	}