J_Class.c
J_Error_t jParseMethods(J_Class* jc,uint8_t* p,uint32_t* poolLen){
  uint16_t acc;
  int32_t  i,sz;
  uint8_t* start = p;
  J_UtfString str;
  
  *poolLen = 0;
  if(jc->methodsCount>0){
   /* allocate memory for methods */
	jc->methods = (J_ClassMethods*) calloc(jc->methodsCount,sizeof(J_ClassMethods));
	if(jc->methods==NULL) {
	  dbg(DBG_MEMORY,"not enought memory for methods");
	  return ENOMEM;
	}
	
	for(i=0;i<jc->methodsCount;i++){
	 /* get access flags */
	  acc = jc->methods[i].accessFlags = getUint16(p);
      p += 2;

      jc->methods[i].nameIdx = getUint16(p);
	  p += 2;
dbg(DBG_TEMPORARY," -------( '%s' )-------\n",jGetUtfString(jc,jc->methods[i].nameIdx).str);

	 /* Count method parameters */
      jc->methods[i].descrIdx = getUint16(p); 
      sz = countParams(jGetUtfString(jc,jc->methods[i].descrIdx));
      if(sz<0){
		jc->methods[i].paramsCount = 0;
	  } else jc->methods[i].paramsCount = sz;
      p += 2;

	 /* Is it method init ? */
      jc->methods[i].initMethod = 0;
      str = jGetUtfString(jc,jc->methods[i].nameIdx);
	  if((str.str[0]=='<') && (str.str[1]=='i')){
dbg(DBG_TEMPORARY,"method %s is <init>",str.str);
		jc->methods[i].initMethod = 1;
	  }

	 /* Is it return value (not void) ? */
      str = jGetUtfString(jc,jc->methods[i].descrIdx);
	  jc->methods[i].isRetValue = 0;
      if(str.str[str.len-1]!='V')
		jc->methods[i].isRetValue = 1;

     /* how many attribures have its method */
      jc->methods[i].attributesCount = getUint16(p);
      p += 2;

     /* allocate memory for the attributes */
      jc->methods[i].methodsAttr = (J_MethodsAttr*) calloc(jc->methods[i].attributesCount,
														   sizeof(J_MethodsAttr));
      if(jc->methods[i].methodsAttr==NULL){
		dbg(DBG_MEMORY,"not enought memory for methods attrib structure");
		return ENOMEM;
      }

     /* get method attributes */
	 /* If the method is either native or abstract, its method_info 
	  * structure must not have a Code attribute. 
	  */

	 /* Here need to load native functions */
	  if(!(acc & (ACCESS_NATIVE|ACCESS_ABSTRACT))){
		J_MethodsExceptions* ea;
		int j, k;
		
	   /* Check all attributes */
		for(j=0;j<jc->methods[i].attributesCount;j++) {
		  jc->methods[i].methodsAttr[j].attributeNameIdx = getUint16(p);
		  p += 2;

		 /* Is it 'Exceptions' attribute ? /added 21-feb-2005/ */
		  ea = &(jc->methods[i].exceptionsAttr);
		  if(!strncmp(jGetUtfString(jc,jc->methods[i].methodsAttr[j].attributeNameIdx).str,"Exceptions",10)) {
			ea->len = getUint32(p);
			p+=4;
			ea->numberOfExceptions = getUint16(p);
			p+=2;
			ea->exceptionsTable = (uint16_t*) p;
			
/* dbg(DBG_EXCEPTIONS,"Exception attribute: len = %d, number = %d",ea->len,ea->numberOfExceptions); */
/*  {int k; */
/* 			for(k=0;k<ea->numberOfExceptions;k++){ */
/* 			  uint16_t ei = getUint16(p); */
/* 			  p+=2; */
/* dbg(DBG_EXCEPTIONS," (%s)",jGetUtfString(jc,getUint16(jc->consts[ei]+jc->classmem+1)).str); */
/* 			} */
/*  } */
			
		   /* skip exceptions table */
			p += 2 * ea->numberOfExceptions;
			continue;
		  } else {
			ea->numberOfExceptions = 0;
			ea->len = 0;
		  }

		  jc->methods[i].methodsAttr[j].len = getUint32(p);
		  p += 4;

		 /* Get max stack and max locals */
		  jc->methods[i].maxStack = getUint16(p);
		  p += 2;
		
		  jc->methods[i].maxLocals = getUint16(p);
		  p += 2;

		 /* Get code section */
		  jc->methods[i].codeLength = getUint32(p);
		  p += 4;
		  if(jc->methods[i].codeLength>0){
			jc->methods[i].code = p;
		  } else {
			dbg(DBG_TEMPORARY,"No code section enabled in ");
		  }
		  p += jc->methods[i].codeLength;

		 /* Get exception table section */
		  jc->methods[i].excTableLength = getUint16(p);
/* dbg(DBG_EXCEPTIONS,"exceptions table len = %d",jc->methods[i].excTableLength); */
		  p += 2;
		  
/* if(jc->methods[i].excTableLength>0) */
/*   printMemory(p,8 * jc->methods[i].excTableLength); */
 
		  if(jc->methods[i].excTableLength>0) jc->methods[i].excTable = p;
		  p += 8 * jc->methods[i].excTableLength;

		 /* Skip code attributes */
		  sz = getUint16(p); // attributes_count
		  p += 2;
		  for(k=0;k<sz;k++){
			uint16_t t;
			p += 6; // attr_name_index(2) + attr_length(4)
			t = getUint16(p); // line_numb_table_len
			p += 2;
			p += t*4;         // line_table
		  }
		} // for(j=0...
	  }   // if(!NATIVE...
	  
dbg(DBG_TEMPORARY,"maxLocals = %d, maxStack = %d, paramsCount = %d",jc->methods[i].maxLocals,
	jc->methods[i].maxStack,jc->methods[i].paramsCount);
	  
	 /* If method is native load it */
	  if(acc & ACCESS_NATIVE)
		jLoadNativeFunction(jc,i);
	  
	} // for(i=0...
  } // if(jc->methodsCount ... 

 /* And General Attributes section skip ! */
  *poolLen = p - start;

  return EOK;
}
/**
 *  Parse methods from the class file 
 */
Parse methods from the class file
Parse methods from the class file
J_Error_t jParseMethods(J_Class* jc,uint8_t* p,uint32_t* poolLen){
  uint16_t acc;
  int32_t  i,sz;
  uint8_t* start = p;
  J_UtfString str;
  *poolLen = 0;
  if(jc->methodsCount>0){
	jc->methods = (J_ClassMethods*) calloc(jc->methodsCount,sizeof(J_ClassMethods));
	if(jc->methods==NULL) {
	  dbg(DBG_MEMORY,"not enought memory for methods");
	  return ENOMEM;
	}
	for(i=0;i<jc->methodsCount;i++){
	  acc = jc->methods[i].accessFlags = getUint16(p);
      p += 2;
      jc->methods[i].nameIdx = getUint16(p);
	  p += 2;
dbg(DBG_TEMPORARY," -------( '%s' )-------\n",jGetUtfString(jc,jc->methods[i].nameIdx).str);
      jc->methods[i].descrIdx = getUint16(p); 
      sz = countParams(jGetUtfString(jc,jc->methods[i].descrIdx));
      if(sz<0){
		jc->methods[i].paramsCount = 0;
	  } else jc->methods[i].paramsCount = sz;
      p += 2;
      jc->methods[i].initMethod = 0;
      str = jGetUtfString(jc,jc->methods[i].nameIdx);
	  if((str.str[0]=='<') && (str.str[1]=='i')){
dbg(DBG_TEMPORARY,"method %s is <init>",str.str);
		jc->methods[i].initMethod = 1;
	  }
      str = jGetUtfString(jc,jc->methods[i].descrIdx);
	  jc->methods[i].isRetValue = 0;
      if(str.str[str.len-1]!='V')
		jc->methods[i].isRetValue = 1;
      jc->methods[i].attributesCount = getUint16(p);
      p += 2;
      jc->methods[i].methodsAttr = (J_MethodsAttr*) calloc(jc->methods[i].attributesCount,
														   sizeof(J_MethodsAttr));
      if(jc->methods[i].methodsAttr==NULL){
		dbg(DBG_MEMORY,"not enought memory for methods attrib structure");
		return ENOMEM;
      }
	  if(!(acc & (ACCESS_NATIVE|ACCESS_ABSTRACT))){
		J_MethodsExceptions* ea;
		int j, k;
		for(j=0;j<jc->methods[i].attributesCount;j++) {
		  jc->methods[i].methodsAttr[j].attributeNameIdx = getUint16(p);
		  p += 2;
		  ea = &(jc->methods[i].exceptionsAttr);
		  if(!strncmp(jGetUtfString(jc,jc->methods[i].methodsAttr[j].attributeNameIdx).str,"Exceptions",10)) {
			ea->len = getUint32(p);
			p+=4;
			ea->numberOfExceptions = getUint16(p);
			p+=2;
			ea->exceptionsTable = (uint16_t*) p;
			p += 2 * ea->numberOfExceptions;
			continue;
		  } else {
			ea->numberOfExceptions = 0;
			ea->len = 0;
		  }
		  jc->methods[i].methodsAttr[j].len = getUint32(p);
		  p += 4;
		  jc->methods[i].maxStack = getUint16(p);
		  p += 2;
		  jc->methods[i].maxLocals = getUint16(p);
		  p += 2;
		  jc->methods[i].codeLength = getUint32(p);
		  p += 4;
		  if(jc->methods[i].codeLength>0){
			jc->methods[i].code = p;
		  } else {
			dbg(DBG_TEMPORARY,"No code section enabled in ");
		  }
		  p += jc->methods[i].codeLength;
		  jc->methods[i].excTableLength = getUint16(p);
		  p += 2;
		  if(jc->methods[i].excTableLength>0) jc->methods[i].excTable = p;
		  p += 8 * jc->methods[i].excTableLength;
		  sz = getUint16(p); 
		  p += 2;
		  for(k=0;k<sz;k++){
			uint16_t t;
			p += 6; 
			t = getUint16(p); 
			p += 2;
			p += t*4;         
		  }
		} 
	  }   
dbg(DBG_TEMPORARY,"maxLocals = %d, maxStack = %d, paramsCount = %d",jc->methods[i].maxLocals,
	jc->methods[i].maxStack,jc->methods[i].paramsCount);
	  if(acc & ACCESS_NATIVE)
		jLoadNativeFunction(jc,i);
	} 
  } 
  *poolLen = p - start;
  return EOK;
}
J_Error_t jParseMethods ( J_Class * jc , uint8_t * p , uint32_t * poolLen ) { uint16_t acc ; int32_t i , sz ; uint8_t * start = p ; J_UtfString str ; * poolLen = 0 ; if ( jc -> methodsCount > 0 ) { jc -> methods = ( J_ClassMethods * ) calloc ( jc -> methodsCount , sizeof ( J_ClassMethods ) ) ; if ( jc -> methods == NULL ) { dbg ( DBG_MEMORY , " " ) ; return ENOMEM ; } for ( i = 0 ; i < jc -> methodsCount ; i ++ ) { acc = jc -> methods [ i ] . accessFlags = getUint16 ( p ) ; p += 2 ; jc -> methods [ i ] . nameIdx = getUint16 ( p ) ; p += 2 ; dbg ( DBG_TEMPORARY , " \n " , jGetUtfString ( jc , jc -> methods [ i ] . nameIdx ) . str ) ; jc -> methods [ i ] . descrIdx = getUint16 ( p ) ; sz = countParams ( jGetUtfString ( jc , jc -> methods [ i ] . descrIdx ) ) ; if ( sz < 0 ) { jc -> methods [ i ] . paramsCount = 0 ; } else jc -> methods [ i ] . paramsCount = sz ; p += 2 ; jc -> methods [ i ] . initMethod = 0 ; str = jGetUtfString ( jc , jc -> methods [ i ] . nameIdx ) ; if ( ( str . str [ 0 ] == ' ' ) && ( str . str [ 1 ] == ' ' ) ) { dbg ( DBG_TEMPORARY , " " , str . str ) ; jc -> methods [ i ] . initMethod = 1 ; } str = jGetUtfString ( jc , jc -> methods [ i ] . descrIdx ) ; jc -> methods [ i ] . isRetValue = 0 ; if ( str . str [ str . len - 1 ] != ' ' ) jc -> methods [ i ] . isRetValue = 1 ; jc -> methods [ i ] . attributesCount = getUint16 ( p ) ; p += 2 ; jc -> methods [ i ] . methodsAttr = ( J_MethodsAttr * ) calloc ( jc -> methods [ i ] . attributesCount , sizeof ( J_MethodsAttr ) ) ; if ( jc -> methods [ i ] . methodsAttr == NULL ) { dbg ( DBG_MEMORY , " " ) ; return ENOMEM ; } if ( ! ( acc & ( ACCESS_NATIVE | ACCESS_ABSTRACT ) ) ) { J_MethodsExceptions * ea ; int j , k ; for ( j = 0 ; j < jc -> methods [ i ] . attributesCount ; j ++ ) { jc -> methods [ i ] . methodsAttr [ j ] . attributeNameIdx = getUint16 ( p ) ; p += 2 ; ea = & ( jc -> methods [ i ] . exceptionsAttr ) ; if ( ! strncmp ( jGetUtfString ( jc , jc -> methods [ i ] . methodsAttr [ j ] . attributeNameIdx ) . str , " " , 10 ) ) { ea -> len = getUint32 ( p ) ; p += 4 ; ea -> numberOfExceptions = getUint16 ( p ) ; p += 2 ; ea -> exceptionsTable = ( uint16_t * ) p ; p += 2 * ea -> numberOfExceptions ; continue ; } else { ea -> numberOfExceptions = 0 ; ea -> len = 0 ; } jc -> methods [ i ] . methodsAttr [ j ] . len = getUint32 ( p ) ; p += 4 ; jc -> methods [ i ] . maxStack = getUint16 ( p ) ; p += 2 ; jc -> methods [ i ] . maxLocals = getUint16 ( p ) ; p += 2 ; jc -> methods [ i ] . codeLength = getUint32 ( p ) ; p += 4 ; if ( jc -> methods [ i ] . codeLength > 0 ) { jc -> methods [ i ] . code = p ; } else { dbg ( DBG_TEMPORARY , " " ) ; } p += jc -> methods [ i ] . codeLength ; jc -> methods [ i ] . excTableLength = getUint16 ( p ) ; p += 2 ; if ( jc -> methods [ i ] . excTableLength > 0 ) jc -> methods [ i ] . excTable = p ; p += 8 * jc -> methods [ i ] . excTableLength ; sz = getUint16 ( p ) ; p += 2 ; for ( k = 0 ; k < sz ; k ++ ) { uint16_t t ; p += 6 ; t = getUint16 ( p ) ; p += 2 ; p += t * 4 ; } } } dbg ( DBG_TEMPORARY , " " , jc -> methods [ i ] . maxLocals , jc -> methods [ i ] . maxStack , jc -> methods [ i ] . paramsCount ) ; if ( acc & ACCESS_NATIVE ) jLoadNativeFunction ( jc , i ) ; } } * poolLen = p - start ; return EOK ; }
Parse methods from the class file
Parse methods from the class file
/* allocate memory for methods */ /* get access flags */ /* Count method parameters */ /* Is it method init ? */ /* Is it return value (not void) ? */ /* how many attribures have its method */ /* allocate memory for the attributes */ /* get method attributes */ /* If the method is either native or abstract, its method_info 
	  * structure must not have a Code attribute. 
	  */ /* Here need to load native functions */ /* Check all attributes */ /* Is it 'Exceptions' attribute ? /added 21-feb-2005/ */ /* dbg(DBG_EXCEPTIONS,"Exception attribute: len = %d, number = %d",ea->len,ea->numberOfExceptions); */ /*  {int k; */ /* 			for(k=0;k<ea->numberOfExceptions;k++){ */ /* 			  uint16_t ei = getUint16(p); */ /* 			  p+=2; */ /* dbg(DBG_EXCEPTIONS," (%s)",jGetUtfString(jc,getUint16(jc->consts[ei]+jc->classmem+1)).str); */ /* 			} */ /*  } */ /* skip exceptions table */ /* Get max stack and max locals */ /* Get code section */ /* Get exception table section */ /* dbg(DBG_EXCEPTIONS,"exceptions table len = %d",jc->methods[i].excTableLength); */ /* if(jc->methods[i].excTableLength>0) */ /*   printMemory(p,8 * jc->methods[i].excTableLength); */ /* Skip code attributes */ // attributes_count // attr_name_index(2) + attr_length(4) // line_numb_table_len // line_table // for(j=0... // if(!NATIVE... /* If method is native load it */ // for(i=0... // if(jc->methodsCount ...  /* And General Attributes section skip ! */