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
|
#include <stdio.h>
#include "ppe2fsp.h"
#include "pk_trace.h"
#define MAX_INPUT_SIZE 0x2040 //8k
#define MAX_OUTPUT_SIZE (4 * MAX_INPUT_SIZE)
char* inbuf[MAX_INPUT_SIZE];
char* outbuf[MAX_OUTPUT_SIZE];
;
int main(int argc, char** argv)
{
FILE* in;
FILE* out;
size_t input_size;
size_t output_size;
size_t bytes_written;
int rc = -1;
do
{
if(argc > 3)
{
fprintf(stderr, "Usage: %s [input file] [output file]\n", argv[0]);
}
if(argc < 3)
{
out = stdout;
}
else
{
//open the output file for writing
out = fopen(argv[2], "w");
if(!out)
{
perror("failed to open file for writing");
break;
}
}
if(argc < 2)
{
in = stdin;
}
else
{
//open the input file for reading
in = fopen(argv[1], "r");
if(!in)
{
perror("failed to open file for reading");
break;
}
}
//read the input stream until we reach EOF or the max size
input_size = fread(inbuf, 1, MAX_INPUT_SIZE, in);
if(!feof(in))
{
if(ferror(in))
{
perror("failed to read input file");
break;
}
else
{
fprintf(stderr, "Input stream exceeds max size of %d bytes. Exiting.\n", MAX_INPUT_SIZE);
break;
}
}
output_size = MAX_OUTPUT_SIZE;
//Actual size of output buffer will be set upon successful completion
rc = ppe2fsp(inbuf, input_size, outbuf, &output_size);
if(rc)
{
fprintf(stderr, "Failed converting ppe trace to fsp trace. rc = %d\n", rc);
if(rc == P2F_INVALID_VERSION)
{
fprintf(stderr, "PPE trace buffer must be version %d.\n", PK_TRACE_VERSION);
}
break;
}
rc = -1;
//operation was successful. Write out the fsp trace data
bytes_written = fwrite(outbuf, 1, output_size, out);
if(bytes_written != output_size)
{
if(ferror(out))
{
perror("Failed to write output stream");
break;
}
fprintf(stderr, "Failure: Only able to write %d of %d bytes to output stream\n", bytes_written, output_size);
break;
}
fclose(in);
fclose(out);
fclose(stderr);
rc = 0;
}while(0);
return rc;
}
|