OpenCores
URL https://opencores.org/ocsvn/amber/amber/trunk

Subversion Repositories amber

[/] [amber/] [trunk/] [sw/] [tools/] [amber-pkt2mem.c] - Blame information for rev 62

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 62 csantifort
/*----------------------------------------------------------------
2
//                                                              //
3
//  amber-bin2mem.c                                             //
4
//                                                              //
5
//  This file is part of the Amber project                      //
6
//  http://www.opencores.org/project,amber                      //
7
//                                                              //
8
//  Description                                                 //
9
//  Read in a binary file from tcpdump and write it out in      //
10
//  in Verilog readmem format.                                  //
11
//                                                              //
12
//  Author(s):                                                  //
13
//      - Conor Santifort, csantifort.amber@gmail.com           //
14
//                                                              //
15
//////////////////////////////////////////////////////////////////
16
//                                                              //
17
// Copyright (C) 2010 Authors and OPENCORES.ORG                 //
18
//                                                              //
19
// This source file may be used and distributed without         //
20
// restriction provided that this copyright statement is not    //
21
// removed from the file and that any derivative work contains  //
22
// the original copyright notice and the associated disclaimer. //
23
//                                                              //
24
// This source file is free software; you can redistribute it   //
25
// and/or modify it under the terms of the GNU Lesser General   //
26
// Public License as published by the Free Software Foundation; //
27
// either version 2.1 of the License, or (at your option) any   //
28
// later version.                                               //
29
//                                                              //
30
// This source is distributed in the hope that it will be       //
31
// useful, but WITHOUT ANY WARRANTY; without even the implied   //
32
// warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR      //
33
// PURPOSE.  See the GNU Lesser General Public License for more //
34
// details.                                                     //
35
//                                                              //
36
// You should have received a copy of the GNU Lesser General    //
37
// Public License along with this source; if not, download it   //
38
// from http://www.opencores.org/lgpl.shtml                     //
39
//                                                              //
40
----------------------------------------------------------------*/
41
 
42
 
43
#include <stdio.h>
44
#include <stdlib.h>
45
#include "../boot-loader/boot-loader.h"
46
 
47
/* Function prototypes */
48
int fsize(FILE *f);
49
int conv_hstring(char*, unsigned int*);
50
unsigned int buf_uint (unsigned char *buf);
51
 
52
 
53
int main(int argc,char *argv[])
54
{
55
   FILE *infile;
56
   char filename_mem[80], filename_nopath[80];
57
   unsigned char *buf;
58
   FILE *file_mem;
59
   int infile_size, buf_size, i, j;
60
   int ret;
61
   int buf_pos;
62
   unsigned int incl_len;
63
   unsigned int orig_len;
64
   int mem_pos = 0;
65
 
66
   if (argc<2){
67
      fprintf(stderr, "ERROR: no input file specified. Quitting\n");
68
      exit(1);
69
      }
70
 
71
 
72
   infile=fopen(argv[1],"rb");
73
   if(infile==NULL) {
74
      fprintf(stderr, "ERROR: Can't open %s. Quitting\n", argv[1]);
75
      exit(1);
76
   }
77
   infile_size = fsize(infile);
78
 
79
   buf=(unsigned char*)malloc(infile_size);
80
   buf_size=fread(buf,1,infile_size,infile);
81
   fclose(infile);
82
 
83
   if ( buf_size != infile_size ) {
84
      fprintf(stderr, "%s ERROR: Input %s file length is %d bytes long, buffer read buf_size %d\n",
85
      argv[0], argv[1], infile_size, buf_size);
86
      exit(1);
87
      }
88
 
89
 
90
   if ( infile_size > 0x1000000 ) {
91
      fprintf(stderr, "%s WARNING: Input %s file length is %d bytes long, greater than boot-loader can handle \n",
92
      argv[0], argv[1], infile_size);
93
      }
94
 
95
 
96
   /* Start of file */
97
   buf_pos = 6*4;
98
 
99
   /* Main loop. once per packet */
100
   while (buf_pos < infile_size) {
101
      buf_pos=buf_pos+8;
102
      incl_len = buf_uint(&buf[buf_pos]); buf_pos+=4;
103
      orig_len = buf_uint(&buf[buf_pos]); buf_pos+=4;
104
 
105
      /* error check included packet length */
106
      if (incl_len != orig_len) {
107
         fprintf(stderr, "Warning: the full packet was not captured in the tcpdump file\n");
108
         fprintf(stderr, "incl_len %d != orig_len %d\n", incl_len, orig_len);
109
         //goto error_return;
110
         }
111
 
112
      /* error check max packet length */
113
      if (orig_len > 255) {
114
         fprintf(stderr, "Error: the packet is too long. Max length is 255 bytes\n");
115
         fprintf(stderr, "orig_len %d\n", orig_len);
116
         goto error_return;
117
         }
118
 
119
      /* check if full packet is contained in file */
120
      if (buf_pos + incl_len > infile_size) {
121
         fprintf(stderr, "Error: end of current packet is after the end of the buffer\n");
122
         goto error_return;
123
         }
124
 
125
 
126
      printf("// incl_len %d, orig_len %d\n", incl_len, orig_len);
127
      printf("// ip-sa %d.%d.%d.%d, ip-da %d.%d.%d.%d, protocol %d\n",
128
               buf[buf_pos+26], buf[buf_pos+27], buf[buf_pos+28], buf[buf_pos+29],
129
               buf[buf_pos+30], buf[buf_pos+31], buf[buf_pos+32], buf[buf_pos+33],
130
               buf[buf_pos+23]
131
               );
132
 
133
      /* First byte gives the packet length */
134
      printf("@%04x %02x\n",   mem_pos++, orig_len);
135
      for (i=buf_pos;i<buf_pos+incl_len;i++,mem_pos++) {
136
          printf("@%04x %02x\n", mem_pos, buf[i]);
137
          }
138
 
139
      /* pad out any missing bytes */
140
      if (orig_len > incl_len) {
141
         for (i=incl_len;i<orig_len;i++,mem_pos++) {
142
            printf("@%04x 00\n", mem_pos);
143
            }
144
      }
145
 
146
      /* advance to the start of the next entry */
147
      buf_pos += incl_len;
148
      }
149
 
150
   free(buf);
151
   return 0;
152
 
153
 
154
   error_return:
155
      free(buf);
156
      return 1;
157
}
158
 
159
 
160
 
161
unsigned int buf_uint (unsigned char *buf)
162
{
163
    return buf[3]<<24 | buf[2]<<16 | buf[1]<<8 | buf[0];
164
}
165
 
166
 
167
/* Return the buf_size of a file in bytes */
168
int fsize( FILE *f )
169
{
170
    int end;
171
 
172
    /* Set current position at end of file */
173
    fseek( f, 0, SEEK_END );
174
 
175
    /* File buf_size in bytes */
176
    end = ftell( f );
177
 
178
    /* Set current position at start of file */
179
    fseek( f, 0, SEEK_SET );
180
 
181
    return end;
182
}
183
 
184
 
185
int conv_hstring ( char *string, unsigned int * num)
186
{
187
int pos = 0;
188
*num = 0;
189
 
190
while (((string[pos] >= '0' && string[pos] <= '9') ||
191
       (string[pos] >= 'a' && string[pos] <= 'f')) && pos < 9) {
192
    if (string[pos] >= '0' && string[pos] <= '9')
193
        *num =  (*num << 4) + ( string[pos++] - '0' );
194
    else
195
        *num =  (*num << 4) + ( string[pos++] - 'a' ) + 10;
196
    }
197
 
198
return pos;
199
}
200
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.