Guest User

Untitled

a guest
May 26th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.75 KB | None | 0 0
  1.  
  2. # old way:
  3. func = dlfunc lib, "foo", "vilf"
  4.  
  5. Limited by the string. New types aren't available, and aliasing types doesn't work. Structures are difficult too.
  6.  
  7.  
  8. # New way:
  9. constants:
  10.  
  11. c_void = c void
  12. c_char = c int8
  13. c_wchar = c_sint16 or c_int32 # system dependent
  14. c_byte = c_sint8
  15. c_ubyte = c_uint8
  16. c_short = c_sint16
  17. c_ushort = c_uint16
  18. c_int = c_long = c_sint32
  19. c_uint = c_ulong = c_uint32
  20. c_longlong = c_sint64 # __int64 or long long int/long
  21. c_ulonglong = c_uint64 # unsigned __int64 or unsigned long long int/long
  22. c_float # float
  23. c_double # double float
  24. c_longdouble # long double float system dependent
  25.  
  26. # connivence consts:
  27. c_char_p # char * (NUL terminated) string or None
  28. c_wchar_p # wchar_t * (NUL terminated) unicode or None
  29. c_void_p # void * int/long or None
  30.  
  31. # functions:
  32. PMC pointer(PMC type_constant) # finds the pointer value of a constant type
  33. PMC byref(PMC ref_value) # finds makes a reference for function calls
  34. PMC array(PMC type_constant, INT size) # creates a new array of type_constant type of size length
  35. PMC newstruct 'name'
  36. INT sizeof(PMC type or value)
  37. PMC cast(PMC value, PMC type_constant)
  38.  
  39. # Example:
  40.  
  41. # C Example:
  42.  
  43. struct in_addr
  44. {
  45. unsigned long s_addr;
  46. };
  47.  
  48. struct sockaddr_in
  49. {
  50. short sin_family; /* must be AF_INET */
  51. u_short sin_port;
  52. struct in_addr sin_addr;
  53. char sin_zero[8]; /* Not used, must be zero */
  54. };
  55.  
  56. pin.sin_family = AF_INET;
  57. pin.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
  58. pin.sin_port = htons(PORT);
  59.  
  60. /* grab an Internet domain socket */
  61. if ((sd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
  62. perror("socket");
  63. exit(1);
  64. }
  65.  
  66. /* connect to PORT on HOST */
  67. if (connect(sd,(struct sockaddr *) &pin, sizeof(pin)) == -1) {
  68. perror("connect");
  69. exit(1);
  70. }
  71.  
  72.  
  73. # Potential parrot equivalent:
  74.  
  75.  
  76. $P0 = newstruct 'in_addr'
  77. $P0['s_addr'] = c_ulong
  78.  
  79. $P1 = newstruct 'sockaddr_in'
  80. $P1['sin_family'] = c_short
  81. $P1['sin_port'] = c_ushort
  82. $P1['sin_addr'] = $P0
  83. $P1['sin_zero'] = array(c_char, 8)
  84.  
  85. $P2 = new $P1
  86. $P2['sin_family'] = AF_INET # constant defined somewhere else
  87. $P3 = pointer('in_addr')
  88. $P4 = cast($P123, $P3) # $P123 given defined somewhere else
  89. $P2['sin_addr';'s_addr'] = $P4['s_addr']
  90.  
  91. $P2['sin_port'] = 1234
  92. # return, #param1 ... n
  93. $P5 = dlfunc lib, 'socket', c_int , c_int, c_int, c_int
  94.  
  95. $P6 = $P5(AF_INET, SOCK_STREAM, 0) # constants defined elsewhere
  96.  
  97. # connect signature: int connect(int, struct sockaddr *address, socklen_t address_len); socklen_t = int (assuming)
  98. $P7 = dlfunc lib, 'connect', c_int, c_int, c_void_p, c_int)
  99.  
  100. $P8 = pointer($P2)
  101. $P9 = byref($P2)
  102. $I1 = sizeof($P2)
  103.  
  104. $P7($P6, $P9, $I1) # invoke connect(int, struct sockaddr *address, socklen_t address_len)
Add Comment
Please, Sign In to add comment