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

Subversion Repositories bluespec_md6

[/] [bluespec_md6/] [trunk/] [MD6Control/] [test/] [encodeBits.c] - Blame information for rev 7

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 kfleming
#include<stdlib.h>
2
#include<stdio.h>
3
#include<sys/stat.h>
4
#include<assert.h>
5
#include<endian.h>
6
#include"encodeBits.h"
7
#include"md6.h"
8
 
9
 
10
const int requiredWordCount = 1<<20;
11
 
12
 
13
 
14
// reads the first bits bits out of the file 
15
void md6_file(char *filename,
16
              char *outinput,
17
              char *outresult,
18
              char *outsize,
19
               long long bits)
20
 
21
{
22
  FILE *inFile = fopen (filename, "rb");
23
  FILE *outInput = fopen (outinput, "wb");
24
  FILE *outResult = fopen (outresult, "wb");
25
  FILE *outSize = fopen (outsize, "wb");
26
  md6_state st;
27
  uint64_t bytes;
28
  long long bitCounter = bits;
29
  unsigned char data[1024];
30
  int err,i,j,wordCount=0;
31
  long long bitMin;
32
 
33
  md6_word key[md6_k];
34
 
35
  printf("Bits: %d\n", bits);
36
 
37
  for(i=0;i<md6_k;i++) {
38
    key[i]=0;
39
  }
40
 
41
  if (inFile == NULL) {
42
    printf ("%s can't be opened.\n", filename);
43
  }
44
 
45
 
46
  // Use this instead!
47
  //  md6_full_init(st,d,NULL,0,md6_default_L,md6_default_r)
48
  // It is legit to have fewer bytes in the key - should add this option to the HW.
49
  // Perhaps may want to make 512 a parameter to this function
50
  if (err=md6_full_init(&st,512,(char*)key,md6_k*8,md6_default_L,md6_default_r(512))) {
51
      printf("Error code: %d\n",err);
52
  }
53
 
54
  wordCount = 0;
55
  while ((bytes = fread (data, 1, md6_w/8, inFile)) != 0){
56
    char wordBuf[md6_w/8];
57
    int i;
58
 
59
 
60
    //Only swap if this word is full. otherwise, we'll screw up the
61
    //last byte
62
    for( i = 0; i < md6_w/8; i++) {
63
      /*  if((BYTE_ORDER == LITTLE_ENDIAN) && (bitCounter >= md6_w)) {
64
          wordBuf[i] = data[md6_w/8-i-1];
65
        }
66
        else{*/
67
          wordBuf[i] = data[i];
68
     }
69
 
70
 
71
 
72
    // md6_update border case uses wrong endianess
73
    // we may cross a bit boundary here.  In this case we want min of 
74
    // bitCounter/bytes*8
75
    bitMin = (bitCounter<bytes*8)?bitCounter:bytes*8;
76
    if ((err=md6_update(&st, wordBuf, bitMin))) {
77
      printf("Error code: %d\n",err);
78
    }
79
    wordCount++;
80
    bitCounter -= bitMin;
81
    if((bytes != md6_w/8) || (bitCounter <= 0)) {
82
      break;
83
    }
84
  }
85
  // swap the last guy specially
86
  //XXX assert something about bitCounter here
87
  printf("bytes: %d, bits: %d, bitCounter: %d", bytes, bits, bitCounter);
88
  assert(bitCounter == 0);
89
 
90
 
91
  if ((err=md6_final(&st,NULL))) {
92
      printf("Error code: %d\n",err);
93
  } else {
94
    printf("\nCalled md6_final\n");
95
  }
96
 
97
  //Wind back
98
  rewind(inFile);
99
 
100
  for(i=0;i<md6_n;i++) {
101
    data[i]=0;
102
  }
103
 
104
  wordCount = 0;
105
  while ((bytes = fread (data, 1,md6_w/8, inFile))){
106
    char wordBuf[md6_w/8];
107
    int i;
108
 
109
    // Bluespec is big endian
110
    // Really we should make the C code also big endian.
111
    // This has to do with zero padding in the controller.  
112
    // If we fail to feed the right values, we'll basically die.
113
    for( i = 0; i < md6_w/8; i++) {
114
      if(BYTE_ORDER == LITTLE_ENDIAN) {
115
        wordBuf[i] = data[md6_w/8-i-1];
116
      }
117
      else{
118
        wordBuf[i] = data[i];
119
      }
120
    }
121
 
122
 
123
    fprintf(outInput,PR_MD6_WORD "\n", *((md6_word*)wordBuf));
124
    printf("Got %d bytes\n", bytes);
125
    wordCount+=bytes;
126
    if(bytes < (md6_w/8)) {
127
      break;
128
    }
129
    for(i=0;i<md6_n;i++) {
130
      data[i]=0;
131
    }
132
  }
133
 
134
  fprintf(outSize,"%x\n%x\n%x\n%x\n",bits,bits,bits,bits);
135
 
136
  for(; wordCount < requiredWordCount; wordCount++) {
137
    fprintf(outInput,PR_MD6_WORD "\n", 0);
138
  }
139
 
140
  printf("Hashval: %s\n", st.hexhashval);
141
  for (j = 0;j<md6_c;j++) {
142
    char wordBuf[md6_w/8];
143
    int i;
144
 
145
    // Bluespec is big endian
146
    // XXX this code is not deployed
147
    for( i = 0; i < md6_w/8; i++) {
148
      if(BYTE_ORDER != BIG_ENDIAN) {
149
        wordBuf[i] = st.hashval[j*md6_w/8+(md6_w/8-i-1)];
150
      }
151
      else{
152
        wordBuf[i] = st.hashval[j*md6_w/8+i];
153
      }
154
    }
155
 
156
    fprintf(outResult,PR_MD6_WORD "\n",*((md6_word*)wordBuf));
157
  }
158
 
159
  fclose (inFile);
160
  fclose (outInput);
161
  fclose (outResult);
162
  fclose (outSize);
163
}
164
 
165
 

powered by: WebSVN 2.1.0

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