This repository was archived by the owner on Mar 21, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfile.cpp
More file actions
132 lines (109 loc) · 3.51 KB
/
file.cpp
File metadata and controls
132 lines (109 loc) · 3.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
/*
Copyright (c) 2015 Piotr Stolarz
scriptext: Various scripting utilities WinDbg extension
Distributed under the GNU General Public License (the License)
see accompanying file LICENSE for details.
This software is distributed WITHOUT ANY WARRANTY; without even the
implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the License for more information.
*/
#include "common.h"
#include "file.h"
/* exported; see header for details */
BOOL file_open(
const char *pc_file, const char *pc_mode, const char *pc_prnm)
{
BOOL ret=FALSE;
FILE *fh=NULL;
IDebugRegisters2 *DebugRegisters=NULL;
if ((get_client()->QueryInterface(
__uuidof(IDebugRegisters2), (void **)&DebugRegisters))!=S_OK)
goto finish;
fh = fopen(pc_file, pc_mode);
if (!fh) err_dbgprintf("File opening error: %s\n", strerror(errno));
DEBUG_VALUE fh_val;
fh_val.Type = DEBUG_VALUE_INT64;
fh_val.I64 = (sizeof(fh)<8 ? DEBUG_EXTEND64(fh) : (ULONG64)fh);
fh_val.Nat = FALSE;
ULONG pr_i;
if (DebugRegisters->GetPseudoIndexByName(pc_prnm, &pr_i)==S_OK)
if (DebugRegisters->SetPseudoValues(
DEBUG_REGSRC_DEBUGGEE, 1, NULL, pr_i, &fh_val)==S_OK)
ret=TRUE;
if (!ret) err_dbgprintf("Pseudo-reg access error\n");
finish:
if (fh && !ret) fclose(fh);
if (DebugRegisters) DebugRegisters->Release();
return ret;
}
/* exported; see header for details */
BOOL file_wrtstr(FILE *fh, const char *pc_in)
{
BOOL ret=FALSE;
IDebugControl2 *DebugControl=NULL;
char buf[3072]; /* default buffer */
char *pc_ebuf=NULL; /* extra buffer */
if (get_client()->QueryInterface(
__uuidof(IDebugControl2), (void **)&DebugControl)!=S_OK) goto finish;
strncpy(buf, pc_in, sizeof(buf));
buf[sizeof(buf)-1]=0;
char *pc_str;
if (buf[0]=='[')
{
/* alias name */
char *pc_aname = &buf[1];
stresc(pc_aname, ']', NULL);
if (!*pc_aname) goto finish;
pc_str = pc_aname+strlen(pc_aname)+1;
size_t rem_buf_sz = pc_str-&buf[0];
/* get alias val size */
ULONG aval_sz;
if (DebugControl->GetTextReplacement(
pc_aname, 0, NULL, 0, NULL, NULL, 0, &aval_sz)!=S_OK) goto finish;
/* value read */
aval_sz = RNDUP_DW(aval_sz+1);
if (aval_sz > rem_buf_sz) {
if (!(pc_str=pc_ebuf=(char*)malloc(aval_sz))) goto finish;
}
if (DebugControl->GetTextReplacement(
pc_aname, 0, NULL, 0, NULL, pc_str, aval_sz, NULL)!=S_OK)
goto finish;
} else
if (buf[0]=='\'' || buf[0]=='"') {
/* apostrophed string */
pc_str = &buf[1];
stresc(pc_str, buf[0], NULL);
} else {
/* string */
pc_str=buf;
stresc(pc_str);
}
if (!*pc_str) goto finish;
size_t cb = strlen(pc_str);
if (fwrite(pc_str, 1, cb, fh)==cb) ret=TRUE;
else err_dbgprintf("File write error\n");
fflush(fh);
finish:
if (pc_ebuf) free(pc_ebuf);
if (DebugControl) DebugControl->Release();
return ret;
}
/* exported; see header for details */
void file_rdln(FILE *fh)
{
char buf[0x100+1];
for (int i=0, c; (c=fgetc(fh))!=EOF; i++) {
if (i>=sizeof(buf)-1) {
/* flush the read buffer */
i=0;
buf[sizeof(buf)-1]=0;
dbgprintf("%s", buf);
}
if (c!='\n') buf[i]=(char)c;
else {
buf[i]=0;
dbgprintf("%s\n", buf);
break;
}
}
}