⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 profile.c

📁 Linux下的类似softice的调试工具
💻 C
字号:
/******************************************************************************//*                                                                            *//*               Profile (C) by Gerhard W. Gruber in Vienna 2003              *//*                          All rights reserved                               *//*                                                                            *//******************************************************************************//****************************************************************************** * * PROJECT: Profile helper module * $Source: /cvsroot/pice/pice/module/profile.c,v $ * $Revision: 1.1 $ * $Date: 2004/02/17 23:12:26 $ * $Author: lightweave $ * $Name:  $ * * $Log: profile.c,v $ * Revision 1.1  2004/02/17 23:12:26  lightweave * New configuration handler for reading config files. See CHANGES.txt for * details. * * *****************************************************************************//*************************************************************************** *                                                                         * *   This program is free software; you can redistribute it and/or modify  * *   it under the terms of the GNU General Public License as published by  * *   the Free Software Foundation; either version 2 of the License, or     * *   (at your option) any later version.                                   * *                                                                         * ***************************************************************************/#ifdef __KERNEL__#include "remods.h"#include <linux/fs.h>   /* struct file, filp_open, etc. *///#include <linux/syscalls.h>   /* sys_read, sys_lseek, etc. */#include "retypes.h"#include "heap.h"#include "debug.h"/*#else#include <malloc.h>#include <stdlib.h>#include <stdio.h>#include <string.h>#include <fcntl.h>*/#endif#ifdef _WINDOWS_#include <io.h>#endif#include "utils.h"#include "profile.h"#define MAX_BUFFER		1024#ifdef __KERNEL__int errno;#endifLONG ReadLine(PROFILE_HANDLE *h, char *Buffer, LONG Lines, LONG MaxLen, BYTE TabExpand){	LONG rc = -1, i, x = 0;	UBYTE *ptr = Buffer;	UWORD tablen;	ENTER_FUNC();	if(h == NULL || Buffer == NULL)	{		errno = EBADF;		goto Quit;	}	if(h->BufferIndex >= h->BufferLen)	{		 Buffer[0] = 0;		 goto Quit;	}	// If the number of lines equal 0 or the maximum linelength equal 0	// we are finished.	if(Lines == 0 || MaxLen == 0)	{		rc = 0;		goto Quit;	}	DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "Reading %lu lines\n", Lines);	memset(Buffer, 0, MaxLen);	i = 0;	while(x < MaxLen && h->BufferIndex < h->BufferLen && i < Lines)	{		 // 0x0A = LF End of line		 if(h->Buffer[h->BufferIndex] == '\n')		// 0x0A = LF is ignored		 {			  if(x > 0 && ptr[x-1] == '\r')				   ptr[x-1] = 0;			  else				   ptr[x] = 0;			  i++;		 }		 else	     if(h->Buffer[h->BufferIndex] != '\r')		 {			  if(TabExpand && h->Buffer[h->BufferIndex] == '\t')			  {				   // translate tabs to blanks				   tablen = TabExpand - (x % TabExpand);				   x += sprintf(&ptr[x], "%*.*s", tablen, tablen, "")-1;			  }			  else			  {				   ptr[x] = h->Buffer[h->BufferIndex];				   x++;			  }		 }		 		 h->BufferIndex++;	}		DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "Line [%s]\n", Buffer);	rc = i;Quit:	DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "rc: %lu\n", rc);	LEAVE_FUNC();	return(rc);}ULONG AddArrayEntry(ULONG *Entries, ULONG New, void **p[]){	ULONG rc = -1;	ULONG l;	void **m;	ENTER_FUNC();	if(p == NULL || Entries == NULL)		goto Quit;	l = (sizeof(void **)*New);	m = *p;	if(m == NULL)	{		if((m = (void **)malloc(l)) == NULL)			goto Quit;	}	else	{		if((m = (void **)realloc(m, l + (sizeof(void *)*(*Entries)))) == NULL)			goto Quit;	}	memset(&m[*Entries], 0, l);	rc = *Entries + New;	*Entries = rc;	*p = m;Quit:	LEAVE_FUNC();	return(rc);}/**********************************************//* ---===<* Handle related functions *>===--- *//**********************************************/PROFILE_HANDLE *CreateHandle(void){	PROFILE_HANDLE *rc;	ENTER_FUNC();	if((rc = (PROFILE_HANDLE *)malloc(sizeof(PROFILE_HANDLE))) == NULL)		goto Quit;	rc->Buffer = NULL;	rc->BufferLen = 0;	rc->BufferIndex = 0;	rc->Path = NULL;	rc->FileHandle = BAD_FILE;	rc->Section = NULL;	rc->Sections = 0L;	rc->Case = FALSE;Quit:	LEAVE_FUNC();	return(rc);}PROFILE_HANDLE *DestroyHandle(PROFILE_HANDLE *h){	ULONG i, n;	ENTER_FUNC();	if(!h)		goto Quit;	if(h->Path)		free(h->Path);	if(h->FileHandle != BAD_FILE)		fclose(h->FileHandle);	if(h->Buffer != NULL)		 free(h->Buffer);	h->BufferLen = 0;	h->BufferIndex = 0;	if(h->Section)	{		n = h->Sections;		for(i = 0; i < n; i++)			DestroySection(h->Section[i]);		free(h->Section);	}	free(h);Quit:	LEAVE_FUNC();	return(NULL);}PROFILE_HANDLE *OpenProfile(char *Path, BOOLEAN CaseSensitive, BOOLEAN bCreate){	PROFILE_HANDLE *rc = NULL, *h = NULL;	ENTER_FUNC();	if(Path == NULL)		goto Quit;	if((h = CreateHandle()) == NULL)		goto Quit;	if((h->Path = StrAlloc(Path)) == NULL)		goto Quit;	DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "Path: %s:%s\n", Path, h->Path);	if(ProfOpenFile(h, bCreate) == FALSE)		goto Quit;	DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "Profile opened\n");	h->Case = CaseSensitive;	ParseFilebuffer(h);	fclose(h->FileHandle);	h->FileHandle = BAD_FILE;	rc = h;Quit:	/* In case something went wrong we destroy the structure. */	if(h != NULL && rc == NULL)		rc = DestroyHandle(h);	LEAVE_FUNC();	return(rc);}BOOLEAN ProfOpenFile(PROFILE_HANDLE *h, BOOLEAN bCreate){	BOOLEAN rc = FALSE;	ENTER_FUNC();	if(h == NULL)		goto Quit;	DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "opening (%s)\n", h->Path);	if((h->FileHandle = fopen(h->Path, O_RDONLY, 0600)) == BAD_FILE)	{		DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "fopen(%s) failed\n", h->Path);		goto Quit;	}	h->BufferLen = h->FileHandle->f_dentry->d_inode->i_size;	DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "Filelen %lu\n", h->BufferLen);	if((h->Buffer = PICE_HeapAlloc(h->BufferLen)) == NULL)	{		DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "Allocating %lu bytes files\n", h->BufferLen);		goto Quit;	}	h->BufferIndex = 0;	DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "Reading file %lu\n", h->BufferLen);	if(fread(h->Buffer, sizeof(char), h->BufferLen, h->FileHandle) != (size_t)h->BufferLen)	{		DPRINT(PICE_DEBUG, DBT_PROFILE, DBL_INFO, "Couldn' t read from file!\n");		goto Quit;	}	rc = TRUE;Quit:	if(rc == FALSE)		 DestroyHandle(h);	LEAVE_FUNC();	return(rc);}BOOLEAN ProfCloseFile(PROFILE_HANDLE *h){	BOOLEAN rc = FALSE;	ENTER_FUNC();	if(h == NULL)		goto Quit;	if(h->FileHandle != BAD_FILE)		fclose(h->FileHandle);	h->FileHandle = BAD_FILE;	rc = TRUE;Quit:	LEAVE_FUNC();	return(rc);}void CloseProfile(PROFILE_HANDLE *h){	ENTER_FUNC();	DestroyHandle(h);	LEAVE_FUNC();}BOOLEAN WriteProfile(PROFILE_HANDLE *h){	BOOLEAN rc = FALSE;	ULONG ih, nh, is, ns;	FILE *f = BAD_FILE;	PROFILE_SECTION *s;	PROFILE_MAP *m;	char str[512];	ENTER_FUNC();	if(h == NULL)		goto Quit;	if(h->FileHandle == BAD_FILE && ProfOpenFile(h, TRUE) == FALSE)		goto Quit;	if((h->FileHandle = fopen(h->Path, O_RDONLY|O_TRUNC, 0600)) == BAD_FILE)		 goto Quit;		f = h->FileHandle;	nh = h->Sections;	for(ih = 0; ih < nh; ih++)	{		// ignore empty entries.		if((s = h->Section[ih]) == NULL)			continue;				// Check for bufferoverflow		if((strlen(s->SectionName)+10) >= sizeof(str))			goto Quit;		sprintf(str, "[%s]\n", s->SectionName);		if(fwrite(str, sizeof(char), strlen(str), f) != (size_t)strlen(str))			goto Quit;		ns = s->MapEntries;		for(is = 0; is < ns; is++)		{			m = s->MapEntry[is];			// Check for bufferoverflow			if((strlen(m->Key)+strlen(m->Value)+10) >= sizeof(str))				goto Quit;			sprintf(str, "%s=%s\n", m->Key, m->Value);			if(fwrite(str, sizeof(char), strlen(str), f) != (size_t)strlen(str))				 goto Quit;		}	}	rc = TRUE;Quit:	ProfCloseFile(h);	LEAVE_FUNC();	return(rc);}BOOLEAN CompressProfile(PROFILE_HANDLE *h, BOOLEAN ReAlloc){	BOOLEAN rc = FALSE;	ENTER_FUNC();	if(h == NULL)		goto Quit;Quit:	LEAVE_FUNC();	return(rc);}ULONG AddSection(PROFILE_HANDLE *h, char *sn, PROFILE_SECTION **ps){	ULONG rc = -1;	ULONG si;	PROFILE_SECTION *s, *ds;	ENTER_FUNC();	// In case we have a NULL pointer here, we assign a dummypointer	// so we don't have to check always if it is NULL.	if(ps == NULL)		ps = &ds;	// Check if the section already exists.	if((si = FindSection(h, sn, &ds)) != -1)	{		rc = si;		*ps = ds;		goto Quit;	}	if((s = CreateSection(sn)) == NULL)		goto Quit;	rc = AddArrayEntry(&h->Sections, 1, (void ***)&h->Section);	h->Section[h->Sections-1] = s;	*ps = s;Quit:	LEAVE_FUNC();	return(rc);}ULONG RemoveSection(PROFILE_HANDLE *h, char *sn, BOOLEAN ReAlloc){	ULONG rc = -1;	ULONG si;	PROFILE_SECTION *s;	ENTER_FUNC();	if((si = FindSection(h, sn, &s)) == -1)		goto Quit;	DestroySection(s);	h->Section[si] = NULL;	rc = si;Quit:	LEAVE_FUNC();	return(rc);}/**********************************************//* ---===<* Section related functions *>===--- *//**********************************************/PROFILE_SECTION *CreateSection(char *SectionName){	PROFILE_SECTION *rc = NULL, *s = NULL;	ENTER_FUNC();	if(SectionName == NULL || SectionName[0] == 0)		goto Quit;	if((s = (PROFILE_SECTION *)malloc(sizeof(PROFILE_SECTION))) == NULL)		goto Quit;	if((s->SectionName = StrAlloc(SectionName)) == NULL)		goto Quit;	s->MapEntries = 0;	s->MapEntry = NULL;	rc = s;Quit:	if(s != NULL && rc == NULL)		DestroySection(s);	LEAVE_FUNC();	return(rc);}PROFILE_SECTION *DestroySection(PROFILE_SECTION *s){	ULONG i, n;	ENTER_FUNC();	if(s == NULL)		goto Quit;	if(s->SectionName != NULL)		free(s->SectionName);	n = s->MapEntries;	for(i = 0; i < n; i++)		DestroyMap(s->MapEntry[i]);	if(s->MapEntry != NULL)		free(s->MapEntry);	free(s);Quit:	LEAVE_FUNC();	return(NULL);}BOOLEAN RenameSection(PROFILE_SECTION *s, char *sn){	BOOLEAN rc = FALSE;	ENTER_FUNC();	if(s == NULL || sn == NULL || sn[0] == 0)		goto Quit;	if(strlen(sn) == strlen(s->SectionName))		strcpy(s->SectionName, sn);	else	{		free(s->SectionName);		s->SectionName = StrAlloc(sn);	}Quit:	LEAVE_FUNC();	return(rc);}ULONG AddMap(PROFILE_SECTION *s, PROFILE_MAP *m){	ULONG rc = -1;	ENTER_FUNC();	if(s == NULL || m == NULL)		goto Quit;	rc = AddArrayEntry(&s->MapEntries, 1, (void ***)&s->MapEntry);	s->MapEntry[s->MapEntries-1] = m;Quit:	LEAVE_FUNC();	return(rc);}ULONG AddMapEntry(PROFILE_SECTION *s, char *k, char *v, BOOLEAN CaseSensitive, PROFILE_MAP **pm){	ULONG rc = -1, mi;	PROFILE_MAP *m = NULL, *dm;	ENTER_FUNC();	if(s == NULL || k == NULL)		goto Quit;	// In case we have a NULL pointer we assign our dummy pointer	// so we dont'have to check always if it is NULL.	if(pm == NULL)		pm = &dm;	*pm = NULL;	// If the key already exists, we replace the value.	if((mi = FindMap(s, k, CaseSensitive, &dm)) != -1)	{		rc = mi;		*pm = dm;		free(dm->Value);		dm->Value = StrAlloc(v);		goto Quit;	}	if((m = CreateMap(k, v)) == NULL)		goto Quit;	if(AddMap(s, m) == -1)		goto Quit;	*pm = m;	m = NULL;	rc = s->MapEntries;Quit:	if(m != NULL)		DestroyMap(m);	LEAVE_FUNC();	return(rc);}ULONG FindSection(PROFILE_HANDLE *h, char *sn, PROFILE_SECTION **ps){	ULONG rc = -1;	ULONG i, n, v;	PROFILE_SECTION *s;	ENTER_FUNC();	if(h == NULL || sn == NULL)		goto Quit;	n = h->Sections;	for(i = 0; i < n; i++)	{		v = 2;		s = h->Section[i];				if(s == NULL)			continue;		if(h->Case == TRUE)			v = strcmp(s->SectionName, sn);		else			v = stricmp(s->SectionName, sn);		if(v == 0)		{			if(ps != NULL)				*ps = h->Section[i];			rc = i;			break;		}	}Quit:	LEAVE_FUNC();	return(rc);}ULONG FindMap(PROFILE_SECTION *s, char *k, BOOLEAN CaseSensitive, PROFILE_MAP **m){	ULONG rc = -1;	ULONG i, n, v;	ENTER_FUNC();	if(s == NULL || k == NULL)		goto Quit;	n = s->MapEntries;	for(i = 0; i < n; i++)	{		v = 2;		if(CaseSensitive == TRUE)			v = strcmp(s->MapEntry[i]->Key, k);		else			v = stricmp(s->MapEntry[i]->Key, k);		if(v == 0)		{			if(m != NULL)				*m = s->MapEntry[i];			rc = i;			break;		}	}Quit:	LEAVE_FUNC();	return(rc);}/**********************************************//*  ---===<* Map related functions *>===---   *//**********************************************/PROFILE_MAP *CreateMap(char *Key, char *Value){	PROFILE_MAP *rc = NULL, *m = NULL;	ENTER_FUNC();	if(Key == NULL || Key[0] == 0)		goto Quit;	if((m = (PROFILE_MAP *)malloc(sizeof(PROFILE_MAP))) == NULL)		goto Quit;	m->Key = NULL;	m->Value = NULL;	if((m->Key = StrAlloc(Key)) == NULL)		goto Quit;	if(Value != NULL && (m->Value = StrAlloc(Value)) == NULL)		goto Quit;	rc = m;Quit:	if(m != NULL && rc == NULL)		DestroyMap(m);	LEAVE_FUNC();	return(rc);}PROFILE_MAP *DestroyMap(PROFILE_MAP *m){	ENTER_FUNC();	if(m == NULL)		goto Quit;	if(m->Key != NULL)		free(m->Key);	if(m->Value != NULL)		free(m->Value);	free(m);Quit:	LEAVE_FUNC();	return(NULL);}BOOLEAN ParseFilebuffer(PROFILE_HANDLE *h){	BOOLEAN rc = FALSE;	char *buffer = NULL, *str;	long n, b0, b1, e;	PROFILE_SECTION *s = NULL;	ENTER_FUNC();	if(h == NULL || h->FileHandle == BAD_FILE)		goto Quit;	if((buffer = (char *)malloc(257)) == NULL)		 goto Quit;	while((n = ReadLine(h, buffer, 1, 256, 8)) != -1)	{		// EOF?		if(n == 0)			goto Quit;		str = buffer;		n = strlen(str);		str = Strip(str, FALSE+TRUE+1);		// empty line		if(*str == 0)			continue;		b0 = FindChar(str, "[", FALSE, TRUE, '\\');		b1 = FindChar(str, "]", FALSE, TRUE, '\\');		e = FindChar(str, "=", FALSE, TRUE, '\\');		// Invalid formats will be ignored.		// Brackets are in the wrong order ][ or an '=' is on the first position		if((b0 != -1 && b1 != -1 && b0 >= b1) || e == 0)			continue;		// if we have an euqal sign in the line, this indicates a		// key/value pair.		if(e == -1)		{			str = StrStrip(str, FALSE+TRUE+1, "[]");			s = CreateSection(str);			AddArrayEntry(&h->Sections, 1, (void ***)&h->Section);			h->Section[h->Sections-1] = s;		}		else		{			// Key/value pair outside a section. This should only be 			// possible at the start of the file. All other pairs would			// be assigned to the current section, as there is no indicator			// when a section ends, the section only ends when the next one			// starts.			if(s == NULL)				continue;			str[e] = 0;			AddMapEntry(s, str, &str[e+1], FALSE, NULL);		}	}	rc = TRUE;Quit:	if(buffer != NULL)		 free(buffer);	LEAVE_FUNC();	return(rc);}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -