if (!noData) { length = length + len; rtnValue = (char *)realloc(rtnValue, sizeof(char) * length); strncat(rtnValue, buffer, len); } } else {  rtnValue = (char *)malloc(sizeof(char) * length);  if (!rtnValue) return NULL;  strcpy(rtnValue, value);  if (!noData) { length = length + lenShort; rtnValue = (char *)realloc(rtnValue, sizeof(char) * length); strncat(rtnValue, buffer, lenShort); }
if (!noData) { length = length + bufLen; rtnValue = (char *)realloc(rtnValue, sizeof(char) * (length+1)); strncat(rtnValue, buffer, bufLen);
java_lang_String * getRegElement(JRIEnv *env,								 void *RegDataBuf,								 long itemNum,								 BOOL extendLen){	unsigned short lenShort;	unsigned long len;	java_lang_String *data = NULL;	char *buffer = (char *)RegDataBuf;	long position = RegDataLength;	char *buf = (char *)malloc(sizeof(char) * RegDataLength);	char value[100];	char *rtnValue = NULL;	BOOL noData = FALSE;		if (buf) {		while (itemNum>0 && position>0) {			if (extendLen == TRUE) {				memcpy(buf, buffer, sizeof(len));				len = ntohl(*((long *)buf));				buffer+=sizeof(len);				position-=sizeof(len);				if ((unsigned long) position<len) {					free(buf);					break;				}				buffer+=len;				position-=len;			} else {				memcpy(buf, buffer, sizeof(lenShort));				lenShort = ntohs(*((short *)buf));				buffer+=sizeof(lenShort);				position-=sizeof(lenShort);				if ((unsigned short) position<lenShort) {					free(buf);					break;				}				buffer+=lenShort;				position-=lenShort;			}			if (position<0) {				free(buf);				break;			}						if (extendLen == TRUE) {				memcpy(buf, buffer, sizeof(len));				len = ntohl(*((long *)buf));				buffer+=sizeof(len);				position-=sizeof(len);				if ((unsigned long) position<len) {					free(buf);					break;				}				buffer+=len;				position-=len;			} else {				memcpy(buf, buffer, sizeof(lenShort));				lenShort = ntohs(*((short *)buf));				buffer+=sizeof(lenShort);				position-=sizeof(lenShort);				if ((unsigned short) position<lenShort) {					free(buf);					break;				}				buffer+=lenShort;				position-=lenShort;			}			if (position<0) {				free(buf);				break;			}						--itemNum;		}				if (!itemNum && position>0) {			if (extendLen == TRUE) {				memcpy(buf, buffer, sizeof(len));				len = ntohl(*((long *)buf));				buffer+=sizeof(len);				position-=sizeof(len);				if ((unsigned long) position<len) {					free(buf);					return (data);				}			} else {				memcpy(buf, buffer, sizeof(lenShort));				lenShort = ntohs(*((short *)buf));				buffer+=sizeof(lenShort);				position-=sizeof(lenShort);				if ((unsigned short) position<lenShort) {					free(buf);					return (data);				}			}									if (extendLen == TRUE) {				sprintf(value, "%.*s=", (int)len, buffer);				buffer+=len;				position-=len;			} else {				sprintf(value, "%.*s=", (int)lenShort, buffer);				buffer+=lenShort;				position-=lenShort;			}			if(position<0) {				free(buf);				return (data);			}						if (extendLen == TRUE) {				memcpy(buf, buffer, sizeof(len));				len = ntohl(*((long *)buf));				buffer+=sizeof(len);				position-=sizeof(len);				if ((unsigned long) position<len) {					free(buf);					return (data);				}			} else {				memcpy(buf, buffer, sizeof(lenShort));				lenShort = ntohs(*((short *)buf));				buffer+=sizeof(lenShort);				position-=sizeof(lenShort);				if ((unsigned short) position<lenShort) {					free(buf);					return (data);				}			}						if (strcmp(value, "DOMAIN_NAME=") == 0) {				// gets the name of ISP for icon file name				int  pos;				pos = strcspn(buffer, ".");				strncpy(IconName, buffer, pos);			}			BOOL binDataExists = FALSE;			if (extendLen == TRUE) {				if (len != 0)					binDataExists = TRUE;			} else {				if (lenShort != 0)					binDataExists = TRUE;			}			if (((strcmp(value, "ICON=") == 0) ||				 (strcmp(value, "WIN_ANIMATION_20=") == 0) || 				 (strcmp(value, "WIN_ANIMATION_40=") == 0) ||				 (strcmp(value, "LCK_FILE=") == 0))				 && binDataExists) {				noData=TRUE;				// save the icon data to a file#ifdef WIN32				SECURITY_ATTRIBUTES secAttrib;				memset(&secAttrib, 0, sizeof(SECURITY_ATTRIBUTES));				secAttrib.nLength = sizeof(SECURITY_ATTRIBUTES);				secAttrib.lpSecurityDescriptor = NULL;				secAttrib.bInheritHandle = FALSE;				char file[MAX_PATH];				char profileDir[MAX_PATH];#else				char file[_MAX_PATH];				char profileDir[_MAX_PATH];#endif				GetProfileDirectory((char *)&profileDir);				strcpy(file, profileDir);				if (strcmp(value, "ICON=") == 0) {					// construct the icon file name#ifdef WIN32					strcat(file, IconName);#else					strncat(file, IconName, 8);			// win16 limitation: 8.3 file format#endif					strcat(file, ".ico");				} else if (strcmp(value, "WIN_ANIMATION_20=") == 0) {					strcat(file, "small.bmp");				} else if (strcmp(value, "WIN_ANIMATION_40=") == 0) {					strcat(file, "large.bmp");				} else if (strcmp(value, "LCK_FILE=") == 0) {					strcat(file, "profile.cfg");						}#ifdef WIN32				HANDLE hfile = CreateFile (file, GENERIC_READ|GENERIC_WRITE, FILE_SHARE_READ|FILE_SHARE_WRITE,										   &secAttrib, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);				if (hfile != INVALID_HANDLE_VALUE) {#else				HFILE hfile = _lcreat(file, 0);												if (HFILE_ERROR != hfile) {#endif					DWORD bytesWritten = 0;					if (extendLen == TRUE) {						for (unsigned long i=0; i<len; i++)#ifdef WIN32							WriteFile(hfile, &buffer[i], 1, &bytesWritten, NULL);#else							bytesWritten = _lwrite(hfile, &buffer[i], 1);#endif					} else {						for (short i=0; i< (short) lenShort; i++) #ifdef WIN32													WriteFile(hfile, &buffer[i], 1, &bytesWritten, NULL);#else							bytesWritten = _lwrite(hfile, &buffer[i], 1);#endif					} 										// close file#ifdef WIN32										CloseHandle(hfile);#else					_lclose(hfile);#endif					// save icon filename					strcpy(IconFile, file);				} else {					strcpy(IconFile, "\0");				}			}			// long length = 1 + strlen(value);  // 1 extra for the null char -- WRONG!			long length = strlen(value);			if (extendLen == TRUE) {				rtnValue = (char *)malloc(sizeof(char) * length);				if (!rtnValue)					return NULL;								strcpy(rtnValue, value);								if (!noData) {					length = length + len;					rtnValue = (char *)realloc(rtnValue, sizeof(char) * length);					strncat(rtnValue, buffer, len);				}			} else {				rtnValue = (char *)malloc(sizeof(char) * length);				if (!rtnValue)					return NULL;				strcpy(rtnValue, value);				if (!noData) {					length = length + lenShort;					rtnValue = (char *)realloc(rtnValue, sizeof(char) * length);					strncat(rtnValue, buffer, lenShort);				}			}						data = JRI_NewStringPlatform(env, rtnValue, length, NULL, 0);			if (rtnValue) {				free(rtnValue);				rtnValue = NULL;			}		}		free(buf);	}		return (data);}