# How to use targ - 2 common examples

## To help you get started, we’ve selected a few targ examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

timnewsham / TriforceFreeBSDSyscallFuzzer / targ / genTempl.py View on Github
``````def genArg(a) :
"""Generate a list of arg values for this argument generator."""
if a.startswith('0x') :	# hex numbers
return [Num(int(a[2:], 16))]
elif a.startswith('0') and a != '0' : # octal numbers
return [Num(int(a[1:], 8))]
elif a.isdigit() : 		# decimal numbers
return [Num(int(a, 10))]
elif a.startswith('32[') :	# vec32
if a[-1] != ']' :
Error("bad vec arg %r" % a)
vec = a[3:-1].split(',')
return [Vec32(*xs) for xs in genArgs(vec)]
elif a[0] == '[' :		# vec64
if a[-1] != ']' :
Error("bad vec arg %r" % a)
vec = a[1:-1].split(',')
return [Vec64(*xs) for xs in genArgs(vec)]
elif a[0] == '{' :          # union of several other arg types
if a[-1] != '}' :
Error("bad union arg %r" % a)
its = a[1:-1].split(';')
res = []
for it in its :
res += genArg(it)
return res
elif a[0] == '"' :		# string literal as buffer
if a[-1] != '"' :
Error("bad string arg %r" % a)
return [StringZ(a[1:-1])]
elif a == 'fd' :		# file descriptor options
# stdfile0 = "/", stdfile1 = tcp socket``````
timnewsham / TriforceFreeBSDSyscallFuzzer / targ / genTempl.py View on Github
``````elif a[0] == '[' :		# vec64
if a[-1] != ']' :
Error("bad vec arg %r" % a)
vec = a[1:-1].split(',')
return [Vec64(*xs) for xs in genArgs(vec)]
elif a[0] == '{' :          # union of several other arg types
if a[-1] != '}' :
Error("bad union arg %r" % a)
its = a[1:-1].split(';')
res = []
for it in its :
res += genArg(it)
return res
elif a[0] == '"' :		# string literal as buffer
if a[-1] != '"' :
Error("bad string arg %r" % a)
return [StringZ(a[1:-1])]
elif a == 'fd' :		# file descriptor options
# stdfile0 = "/", stdfile1 = tcp socket
return [Num(1), File("stuffhere"), StdFile(0), StdFile(1)]
elif a == 'fn' :		# filename options
return [StringZ("/tmp/file9"), StringZ("/"), StringZ("/tmp"), Filename("#!/bin/sh\necho hi\n")]
elif a == 'str' :		# initialized string/buffer options
return [StringZ("testing"), Alloc(256), Filename("stuffhere")]
elif a == 'buf' :		# alloc uninitialized buffer options
return [Alloc(128), Alloc(4096)]
elif a == 'sz' :		# sizes
return [Len()]
elif a == 'pid' :		# proc id options
return [Num(0), Num(0xffffffffffffffff), ChildPid, MyPid] # skip PPid
else :
raise Error("bad arg type %r" % a)``````

## targ

Build a Python CLI for your app, just using type hints and docstrings.

MIT