Example #1
0
void
readfile( void )
{
  VMRef 	v;
  Symbol *	s;
  word		op = 0;
  

  /*
   * Quick Test.
   * Determine if the first byte in the input file is a GHOF
   * directive.  If not then do not bother to parse the rest
   * of the file.
   */

  op = getc( infd );

  if (op >= 0x40)
    {
#if defined __ARM && (defined RS6000 || defined __SUN4)
      
      /* only support AOF -> GHOF conversion when cross linking */
      
      if (op == 0xC3 || op == 0xC5)
	{
	  s_aof *	pAOF;
	  uword		iCodeSize;
	  char *	pTempFileName;
	  
	  
	  trace( "Assuming file is in AOF format" );

	  pTempFileName = malloc( strlen( infile_duplicate ) + 5 /* strlen( ".ghof" ) */ + 1 );

	  if (pTempFileName == NULL)
	    {
	      error( "Out of memory allocating temporary file name" );

	      return;
	    }
	  
	  strcpy( pTempFileName, infile_duplicate );
	  strcat( pTempFileName, ".ghof" );

	  file_trace( "creating GHOF copy of AOF file '%s'", infile_duplicate );
	  
	  pAOF = open_aof( infile_duplicate );

	  if (pAOF == NULL)
	    {
	      error( "Failed to reopen file in AOF mode" );
	      return;
	    }

	  (void)convert_aof( pAOF, pTempFileName, bSharedLib, bTinyModel, bDeviceDriver );

	  /* Open the temporary file again */

	  file_trace( "opening GHOF copy '%s'", pTempFileName );

	  infd = freopen( pTempFileName, "rb", infd );

	  if (infd == NULL)
	    {
	      error( "Unable to reopen temporary file %s", pTempFileName );

	      return;
	    }
	  else
	    file_trace( "copy opened" );

	  free( pTempFileName );

	  /* drop throiugh into normal readfile() code */
	}
      else
#endif /* __ARM and (RS6000 or __SUN4) */
	{
	  error( "Input file is not in GHOF format" );

	  return;
	}
    }
  else
    {
      ungetc( (char)op, infd );
    }
      
  do
    {
      op = rdint();
      
      trace("OP = %x, curloc = %x, codepos = %x",op,curloc, codepos);
      
      switch( op )
	{
	default:
	  error( "Illegal linker directive '%x'", op );
	  break;
	  
	case EOF: return;
	  
	case OBJCODE:
	    {
	      word size = rdint();
	      
	      
	      if (size < 0)
         	error( "Negative sized code directive encountered (%x)", size );
	      
	      trace("CODE %d",size);
	      
	      while( size-- ) genbyte(rdch());
	      
	      break;
	    }
	  
	case OBJBSS:
	    {
	      word size = rdint();
	      
	      
	      if (size < 0)
		error("Negative sized bss defined");
	      
	      trace("BSS %d",size);
	      
	      while( size-- ) genbyte(0L);
	      
	      break;
	    }
	  
	case OBJWORD: 
	  genpatch( OBJWORD );
	  break;
	  
	case OBJSHORT: 
	  genpatch( OBJSHORT );
	  break;
	  
	case OBJBYTE:
	  genpatch( OBJBYTE );
	  break;
	  
	case OBJINIT:
	  geninit();
	  break;
	  
	case OBJMODULE:
	  genmodule(rdint());
	  break;
	  
	case OBJBYTESEX:
	  if( bytesex != 0 ) error("bytesex already set");
	  bytesex = rdint();
	  trace("BYTESEX %d",bytesex);
	  break;
	  
	case OBJREF:
	  v = rdsymb();
	  movesym(v);		/* XXX - all REF symbols are implicitly global */	 
	  refsymbol_nondef(v);
	  break;
	  
	case OBJGLOBAL:
	  v = rdsymb();
	  s = VMAddr(Symbol,v);
	  movesym(v);
	  if (s->referenced)
	    {
	      refsymbol_def(v);
	    }
	  break;
	  
	case OBJLABEL:
	  v = rdsymb();
	  s = VMAddr(Symbol,v);
	  trace("LABEL %s",s->name);
	  if( s->type != S_UNBOUND )
	    {
	      if (!inlib)
		warn( "Duplicate definition of symbol '%s' defined in file '%s'", s->name, s->file_name );
	    }
	  else
	    {
	      if (s->AOFassumedData)
		{
		  error( "(AOF) Symbol '%s' has been assumed to be data in file '%s'",
		       s->name, s->file_name );
		  
		  s->AOFassumedData = FALSE;
		}
	      
	      copycode();
	      
	      s->type      = S_CODESYMB;
	      s->value.v   = codeptr();
	      s->module    = curmod;
	      s->file_name = infile_duplicate;
	      
	      if (s->referenced)
		refsymbol_def(v);
	      
		{
		  int len = strlen( s->name );
		  
		  /* hack to insert correct name for resident libraries */
		  VMlock( v );
		  if (len > 8 &&
		      strcmp( s->name + len - 8, ".library" ) == 0 &&
		      VMAddr( asm_Module, curmod )->id     != -1 )
		    {
		      VMAddr( asm_Module, curmod )->file_name = s->name;
		    }
		  VMunlock( v );
		}
	    }
	  break;
	  
	case OBJDATA:
	case OBJCOMMON:
	    {
	      word size = rdint();
	      
	      
	      if (size < 0)
		error("Negative sized data/common directive encountered");
	      
	      v = rdsymb();
	      
	      s = VMAddr(Symbol,v);
	      
	      trace("%s %d %s",op== OBJDATA ? "DATA" : "COMMON",size,s->name);
	      
	      if( s->type != S_UNBOUND ) 
		{
		  if( s->type != S_COMMSYMB)
		    {
		      if (!inlib)
			warn("Duplicate data definition of symbol '%s' defined in file '%s'",s->name, s->file_name);
		    }
		  else {
		      if( s->value.w < size ) 
			s->value.w = size;
		    }
		}
	      else {
		  s->type = op== OBJDATA ? S_DATASYMB : S_COMMSYMB;
		  s->value.w = size;
		  s->module = curmod;
		  s->file_name = infile_duplicate;
		  if(s->referenced)
		    refsymbol_def(v);
		}
	      (void)newcode(op,0,0,curloc,v);
	      break;
	    }
	  
	case OBJCODETABLE:
	    {
	      if (!smtopt)
		error("CODETABLE directive encountered without split module table mode set");
	      
	      v = rdsymb();
	      s = VMAddr(Symbol,v);
	      
#if 0 /* problems for .MaxCodeP */
	      movesym(v);	/* implicit global directive */
#endif
	      trace("%s %s","CODETABLE",s->name);
	      if ( s->type != S_UNBOUND ) 
		{
		  if (!inlib)
		    warn("Duplicate definition of symbol '%s' defined in file '%s'",
			 s->name, s->file_name);
		}
	      else
		{ 
		  if (s->AOFassumedData)
		    {
		      error( "(AOF) Symbol '%s' has been assumed to be data in file '%s'",
			   s->name, s->file_name );
		      
		      s->AOFassumedData = FALSE;
		    }
		  
		  s->type = S_FUNCSYMB;
		  s->value.w = 4;
		  s->module = curmod;
		  s->file_name = infile_duplicate;
		  
		  if(s->referenced)
		    refsymbol_def(v);
		}
	      (void)newcode(op,0,0,curloc,v);
	      break;
	    }
	  
	}
      
    }
  while( op != EOF );
  
  return;
}
}*/

JNIEXPORT jint JNICALL Java_com_sqq_updateapk_DiffUtils_genDiff
        (JNIEnv *env, jobject cls, jstring old, jstring new, jstring patch){
    int argc = 4;
    char * argv[argc];
    argv[0] = "bsdiff";
    argv[1] = (char*) ((*env)->GetStringUTFChars(env, old, 0));
    argv[2] = (char*) ((*env)->GetStringUTFChars(env, new, 0));
    argv[3] = (char*) ((*env)->GetStringUTFChars(env, patch, 0));

    printf("old apk = %s \n", argv[1]);
    printf("new apk = %s \n", argv[2]);
    printf("patch = %s \n", argv[3]);

    int ret = genpatch(argc, argv);

    printf("genDiff result = %d ", ret);

    (*env)->ReleaseStringUTFChars(env, old, argv[1]);
    (*env)->ReleaseStringUTFChars(env, new, argv[2]);
    (*env)->ReleaseStringUTFChars(env, patch, argv[3]);

    return ret;
}

static void split(off_t *I, off_t *V, off_t start, off_t len, off_t h) {
    off_t i, j, k, x, tmp, jj, kk;

    if (len < 16) {
        for (k = start; k < start + len; k += j) {