Page 1 of 1

Hacking Android (pipes) stdout and stderr redirection

Posted: Mon Mar 04, 2019 7:23 am
by Antonio Linares
https://codelab.wordpress.com/2014/11/0 ... roid-apps/

https://www.bignerdranch.com/blog/hijac ... nd-profit/

I have found this great code from _ Hernández Suárez:
https://groups.google.com/d/msg/harbour ... NBsuvHM6kJ

hb_processOpen() lets us manage pipes from Harbour
Hello.

This is an proof of concept of using an interface to communicate with a Java application using JSON.

----8<----
REQUEST HB_GT_STD_DEFAULT

PROCEDURE Main()
LOCAL hProcess
LOCAL hStdIn
LOCAL hStdOut
LOCAL hStdErr

LOCAL nSize := 1024
LOCAL cBuffer

LOCAL nReadedErr
LOCAL nReadedOut

LOCAL hInput := { => }
LOCAL n
LOCAL hPerson
LOCAL hDecode

LOCAL cInput
LOCAL cError := ""
LOCAL cOutput := ""

hInput[ "id" ] := 1001
hInput[ "list" ] := {}

FOR n := 1 TO 1000
hPerson := { => }
hPerson[ "name" ] := "John"
hPerson[ "surname" ] := "Smith" + Chr( 13 ) + Chr( 10 ) + " and Smith"

AAdd( hInput[ "list" ], hPerson )
NEXT

?
BEGIN SEQUENCE
cInput := hb_JsonEncode( hInput, .f. ) + Chr( 10 )
? "INPUT:", Len( cInput )

hProcess := hb_processOpen( "java.exe -cp JSON.jar;. HJAVA", @hStdIn, @hStdOut, @hStdErr, .t. )
? "WRITE:", FWrite( hStdIn, cInput )

DO WHILE .T.
? "WAITING..."
cBuffer := Space( nSize )
nReadedErr := hb_PRead( hStdErr, @cBuffer, nSize, 0 )

IF nReadedErr > 0
cErr += Left( cBuffer, nReadedErr )
ENDIF

cBuffer := Space( nSize )
nReadedOut := hb_PRead( hStdOut, @cBuffer, nSize, 0 )

IF nReadedOut > 0
cOutput += Left( cBuffer, nReadedOut )
ENDIF

IF nReadedErr < 0 .AND. nReadedOut < 0
EXIT
ENDIF
ENDDO

IF Len( cOutput ) > 0
hDecode := { => }
hb_JsonDecode( cOutput, @hDecode )
? hb_JsonEncode( hDecode, .t. )
ENDIF
IF Len( cError ) > 0
? cError
ENDIF

ALWAYS

hb_processClose( hProcess, .t. ) // kill if needed
? "ret=" + hb_ntos( hb_processValue( hProcess ) ) // wait and return exit code

FClose( hStdIn )
FClose( hStdOut )
FClose( hStdErr )

END SEQUENCE
RETURN
---->8----




Source "HJAVA.java":
----8<----
import java.util.Scanner; // JRE 1.5

import org.json.JSONObject;
import org.json.JSONException;

class HJAVA
{
public static void main( String[] args )
{
Scanner scanner = new Scanner( System.in );
String line = scanner.nextLine();
String result;

try
{
JSONObject json = new JSONObject( line );
json.put( "status", 200 );
json.put( "status_text", "OK" );

resultado = json.toString();
}
catch ( JSONException e )
{
try
{
JSONObject json = new JSONObject();
json.put( "status", 500 );
json.put( "status_text", e.toString() );

result = json.toString();
}
catch ( JSONException ee )
{
result = "";
}
}
System.out.print( result );
System.exit( 0 );
}
}
---->8----

A problem I've seen is when calling hb_ProcessClose() in Windows, if the process executes other processes, they are not finished. We should use the winapi TerminateJobObject and similar.

Sorry for the example so cumbersome, i have little time available and I could not reduce it more. I hope you can get the idea.

Regards.