Skip to content

Commit

Permalink
Merge pull request #77 from doronz88/refactor/enum_types
Browse files Browse the repository at this point in the history
refactor: use the correct CF enums
  • Loading branch information
doronz88 authored Feb 19, 2022
2 parents ea16486 + e103552 commit 23b7ca8
Show file tree
Hide file tree
Showing 8 changed files with 53 additions and 14 deletions.
5 changes: 5 additions & 0 deletions src/rpcclient/rpcclient/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import os
import sys
import typing
from enum import Enum
from select import select
from socket import socket

Expand Down Expand Up @@ -124,6 +125,10 @@ def call(self, address: int, argv: typing.List[int] = None) -> Symbol:
free_list = []

for arg in argv:
if isinstance(arg, Enum):
# if it's a python enum, then first get its real value and only then attempt to convert
arg = arg.value

tmp = arg

if isinstance(arg, bool):
Expand Down
17 changes: 10 additions & 7 deletions src/rpcclient/rpcclient/darwin/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from rpcclient.client import Client
from rpcclient.darwin import objective_c_class
from rpcclient.darwin.consts import kCFNumberSInt64Type, kCFNumberDoubleType
from rpcclient.darwin.consts import kCFNumberSInt64Type, kCFNumberDoubleType, CFStringEncoding, kCFAllocatorDefault
from rpcclient.darwin.fs import DarwinFs
from rpcclient.darwin.ioregistry import IORegistry
from rpcclient.darwin.location import Location
Expand All @@ -19,6 +19,7 @@
from rpcclient.darwin.structs import utsname
from rpcclient.darwin.symbol import DarwinSymbol
from rpcclient.exceptions import RpcClientException, MissingLibraryError
from rpcclient.structs.consts import RTLD_NOW

IsaMagic = namedtuple('IsaMagic', 'mask value')
ISA_MAGICS = [
Expand All @@ -37,7 +38,7 @@ def __init__(self, sock, sysname: str, hostname: str, port: int = None):
super().__init__(sock, sysname, hostname, port)
self._dlsym_global_handle = -2 # RTLD_GLOBAL

if 0 == self.dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", 2):
if 0 == self.dlopen("/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation", RTLD_NOW):
raise MissingLibraryError('failed to load CoreFoundation')

self._cf_types = {
Expand Down Expand Up @@ -106,7 +107,8 @@ def cf(self, o: object):
# assuming it's already a cfobject
return o
elif isinstance(o, str):
return self.symbols.CFStringCreateWithCString(0, o, 0)
return self.symbols.CFStringCreateWithCString(kCFAllocatorDefault, o,
CFStringEncoding.kCFStringEncodingMacRoman)
elif isinstance(o, bytes):
return self.symbols.CFDataCreate(0, o, len(o))
elif isinstance(o, bool):
Expand All @@ -117,17 +119,17 @@ def cf(self, o: object):
elif isinstance(o, int):
with self.safe_malloc(8) as buf:
buf[0] = o
return self.symbols.CFNumberCreate(0, kCFNumberSInt64Type, buf)
return self.symbols.CFNumberCreate(kCFAllocatorDefault, kCFNumberSInt64Type, buf)
elif isinstance(o, float):
with self.safe_malloc(8) as buf:
buf.poke(struct.pack('<d', o))
return self.symbols.CFNumberCreate(0, kCFNumberDoubleType, buf)
return self.symbols.CFNumberCreate(kCFAllocatorDefault, kCFNumberDoubleType, buf)
elif isinstance(o, list) or isinstance(o, tuple):
cfvalues = [self.cf(i) for i in o]
with self.safe_malloc(8 * len(cfvalues)) as buf:
for i in range(len(cfvalues)):
buf[i] = cfvalues[i]
return self.symbols.CFArrayCreate(0, buf, len(cfvalues), 0)
return self.symbols.CFArrayCreate(kCFAllocatorDefault, buf, len(cfvalues), 0)
elif isinstance(o, dict):
cfkeys = [self.cf(i) for i in o.keys()]
cfvalues = [self.cf(i) for i in o.values()]
Expand All @@ -137,7 +139,8 @@ def cf(self, o: object):
keys_buf[i] = cfkeys[i]
for i in range(len(cfvalues)):
values_buf[i] = cfvalues[i]
return self.symbols.CFDictionaryCreate(0, keys_buf, values_buf, len(cfvalues), 0, 0, 0)
return self.symbols.CFDictionaryCreate(
kCFAllocatorDefault, keys_buf, values_buf, len(cfvalues), 0, 0, 0)
else:
raise NotImplementedError()

Expand Down
26 changes: 26 additions & 0 deletions src/rpcclient/rpcclient/darwin/consts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from enum import Enum

kCFAllocatorDefault = 0
MACH_PORT_NULL = 0
AVAudioSessionCategoryOptionDefaultToSpeaker = 0x8
Expand Down Expand Up @@ -32,3 +34,27 @@
kIOAudioPlane = 'IOAudio'
kIOFireWirePlane = 'IOFireWire'
kIOUSBPlane = 'IOUSB'


class CFStringEncoding(Enum):
kCFStringEncodingMacRoman = 0
kCFStringEncodingWindowsLatin1 = 0x0500 # ANSI codepage 1252
kCFStringEncodingISOLatin1 = 0x0201 # ISO 8859-1
kCFStringEncodingNextStepLatin = 0x0B01 # NextStep encoding
kCFStringEncodingASCII = 0x0600 # 0..127 (in creating CFString values greater than 0x7F are

# kTextEncodingUnicodeDefault + kTextEncodingDefaultFormat (aka kUnicode16BitFormat)
kCFStringEncodingUnicode = 0x0100

kCFStringEncodingUTF8 = 0x08000100 # kTextEncodingUnicodeDefault + kUnicodeUTF8Format
kCFStringEncodingNonLossyASCII = 0x0BFF # 7bit Unicode variants used by Cocoa & Java

# kTextEncodingUnicodeDefault + kUnicodeUTF16Format (alias of kCFStringEncodingUnicode)
kCFStringEncodingUTF16 = 0x0100

kCFStringEncodingUTF16BE = 0x10000100 # kTextEncodingUnicodeDefault + kUnicodeUTF16BEFormat
kCFStringEncodingUTF16LE = 0x14000100 # kTextEncodingUnicodeDefault + kUnicodeUTF16LEFormat

kCFStringEncodingUTF32 = 0x0c000100 # kTextEncodingUnicodeDefault + kUnicodeUTF32Format
kCFStringEncodingUTF32BE = 0x18000100 # kTextEncodingUnicodeDefault + kUnicodeUTF32BEFormat
kCFStringEncodingUTF32LE = 0x1c000100 # kTextEncodingUnicodeDefault + kUnicodeUTF32LEFormat
3 changes: 2 additions & 1 deletion src/rpcclient/rpcclient/darwin/ioregistry.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ def set(self, properties: Mapping):
self._client.symbols.IORegistryEntrySetCFProperties(self._service, self._client.cf(properties))

def get(self, key: str):
return self._client.symbols.IORegistryEntryCreateCFProperty(self._service, self._client.cf(key), 0, 0).py
return self._client.symbols.IORegistryEntryCreateCFProperty(self._service, self._client.cf(key),
kCFAllocatorDefault, 0).py

def _deallocate(self):
self._client.symbols.IOObjectRelease(self._service)
Expand Down
3 changes: 2 additions & 1 deletion src/rpcclient/rpcclient/darwin/media.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from rpcclient.darwin.consts import AVAudioSessionCategoryOptionDefaultToSpeaker
from rpcclient.darwin.symbol import DarwinSymbol
from rpcclient.exceptions import BadReturnValueError, MissingLibraryError
from rpcclient.structs.consts import RTLD_NOW


class Recorder:
Expand Down Expand Up @@ -121,7 +122,7 @@ def _load_av_foundation(self):
'/System/Library/Frameworks/AVFoundation.framework/AVFoundation'
]
for option in options:
if self._client.dlopen(option, 2):
if self._client.dlopen(option, RTLD_NOW):
return
raise MissingLibraryError('failed to load AVFAudio')

Expand Down
3 changes: 2 additions & 1 deletion src/rpcclient/rpcclient/darwin/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from rpcclient.exceptions import BadReturnValueError, RpcClientException
from rpcclient.network import Network
from rpcclient.allocated import Allocated
from rpcclient.structs.consts import RTLD_NOW

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -110,7 +111,7 @@ def _load_wifi_library(self):
'/System/Library/PrivateFrameworks/WiFiKit.framework/WiFiKit'
]
for option in options:
if self._client.dlopen(option, 2):
if self._client.dlopen(option, RTLD_NOW):
return
logger.warning('WiFi library isn\'t available')

Expand Down
8 changes: 4 additions & 4 deletions src/rpcclient/rpcclient/darwin/symbol.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import time
from typing import List, Mapping

from rpcclient.darwin.consts import kCFNumberSInt64Type, kCFNumberDoubleType
from rpcclient.darwin.consts import kCFNumberSInt64Type, kCFNumberDoubleType, CFStringEncoding
from rpcclient.exceptions import CfSerializationError, UnrecognizedSelectorError
from rpcclient.symbol import Symbol

Expand Down Expand Up @@ -30,17 +30,17 @@ def _decode_cfnull(self) -> None:
return None

def _decode_cfstr(self) -> str:
ptr = self._client.symbols.CFStringGetCStringPtr(self, 0)
ptr = self._client.symbols.CFStringGetCStringPtr(self, CFStringEncoding.kCFStringEncodingMacRoman)
if ptr:
return ptr.peek_str()

with self._client.safe_malloc(4096) as buf:
if not self._client.symbols.CFStringGetCString(self, buf, 4096, 0):
if not self._client.symbols.CFStringGetCString(self, buf, 4096, CFStringEncoding.kCFStringEncodingMacRoman):
raise CfSerializationError('CFStringGetCString failed')
return buf.peek_str()

def _decode_cfbool(self) -> bool:
return bool(self._client.symbols.CFBooleanGetValue(self, 0))
return bool(self._client.symbols.CFBooleanGetValue(self))

def _decode_cfnumber(self) -> int:
with self._client.safe_malloc(200) as buf:
Expand Down
2 changes: 2 additions & 0 deletions src/rpcclient/rpcclient/structs/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,5 @@
DT_LNK = 10
DT_SOCK = 12
DT_WHT = 14

RTLD_NOW = 2

0 comments on commit 23b7ca8

Please sign in to comment.