MCPcopy Create free account
hub / github.com/qilingframework/qiling / __alloc_strings_array

Function __alloc_strings_array

qiling/os/windows/dlls/msvcrt.py:25–54  ·  view source on GitHub ↗

Allocate and populate an array of strings and return its address.

(ql: Qiling, items: Sequence[str], *, wide: bool)

Source from the content-addressed store, hash-verified

23 pass
24
25def __alloc_strings_array(ql: Qiling, items: Sequence[str], *, wide: bool) -> int:
26 '''Allocate and populate an array of strings and return its address.
27 '''
28
29 enc = 'utf-16le' if wide else 'utf-8'
30
31 nitems = len(items)
32
33 # allocate room for pointers to items and a trailing null pointer
34 p_array = ql.os.heap.alloc((nitems + 1) * ql.arch.pointersize)
35
36 # encode all arguments into bytes
37 items_bytes = [f'{item}\x00'.encode(enc) for item in items]
38
39 # allocate room for the items
40 p_items_bytes = ql.os.heap.alloc(sum(len(a) for a in items_bytes))
41
42 for i, item in enumerate(items_bytes):
43 # write argument data
44 ql.mem.write(p_items_bytes, item)
45
46 # write pointer to argument data into the argv array
47 ql.mem.write_ptr(p_array + (i * ql.arch.pointersize), p_items_bytes)
48
49 p_items_bytes += len(item)
50
51 # write trailing null pointer
52 ql.mem.write_ptr(p_array + (nitems * ql.arch.pointersize), 0)
53
54 return p_array
55
56def __getmainargs(ql: Qiling, params, wide: bool) -> int:
57 argc = len(ql.argv)

Callers 1

__getmainargsFunction · 0.85

Calls 3

write_ptrMethod · 0.80
allocMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected