arzmq-sys 0.5.2

Low-level bindings to the zeromq library
Documentation
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/*********************************************************************
 *
 * AUTHORIZATION TO USE AND DISTRIBUTE
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that: 
 *
 * (1) source code distributions retain this paragraph in its entirety, 
 *  
 * (2) distributions including binary code include this paragraph in
 *     its entirety in the documentation or other materials provided 
 *     with the distribution, and 
 *
 * (3) all advertising materials mentioning features or use of this 
 *     software display the following acknowledgment:
 * 
 *      "This product includes software written and developed 
 *       by Brian Adamson and Joe Macker of the Naval Research 
 *       Laboratory (NRL)." 
 *         
 *  The name of NRL, the name(s) of NRL  employee(s), or any entity
 *  of the United States Government may not be used to endorse or
 *  promote  products derived from this software, nor does the 
 *  inclusion of the NRL written and developed software  directly or
 *  indirectly suggest NRL or United States  Government endorsement
 *  of this product.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
 ********************************************************************/
 

#include <stdlib.h>
#include <string.h>
        
#include "normEncoderMDP.h"
#include "galois.h"  // for Galois math routines

#ifdef SIMULATE
#include "normMessage.h"
#endif // SIMULATE

NormEncoderMDP::NormEncoderMDP()
    : npar(0), vector_size(0), 
      gen_poly(NULL), scratch(NULL)
{

}  // end NormEncoderMDP::NormEncoderMDP()

NormEncoderMDP::~NormEncoderMDP()
{
	if (gen_poly) Destroy();
}

bool NormEncoderMDP::Init(unsigned int numData, unsigned int numParity, UINT16 vecSizeMax)
{
    // Debugging assertions
    ASSERT((numData + numParity) <= 255);
    if ((numData + numParity) > 255) return false;  // (TBD) printout error message
    if (gen_poly) Destroy();    
    npar = numParity;
    
#ifdef SIMULATE
    vecSizeMax = MIN(SIM_PAYLOAD_MAX, vecSizeMax);
#endif // SIMULATE
    
    vector_size = vecSizeMax;    
    // Create gen_poly polynomial 
    if(!CreateGeneratorPolynomial())
    {
	    PLOG(PL_FATAL, "NormEncoderMDP: Error creating gen_poly polynomial!\n");
	    return false;
    }    
    // Allocate scratch space for encoding
    if(!(scratch = new unsigned char[vecSizeMax]))
    {
	    PLOG(PL_FATAL, "NormEncoderMDP: Error allocating memory for encoder scratch space: %s\n",
                    GetErrorString());
	    Destroy();
	    return false;
    }
	return true;
}  // end NormEncoderMDP::Init()

// Free memory allocated for encoder state (Encoder must be re-inited before use)
void NormEncoderMDP::Destroy()
{
	if(NULL != scratch)
    {
		delete[] scratch;
		scratch = NULL;
    }
    if (NULL != gen_poly) 
    {
	    delete[] gen_poly;
	    gen_poly = NULL;
    }
}  // end NormEncoderMDP::Destroy()


bool NormEncoderMDP::CreateGeneratorPolynomial()
{
    unsigned char *tp, *tp1, *tp2;
    int degree = 2*npar;    
    if(gen_poly) delete[] gen_poly;
    
    if(!(gen_poly = new unsigned char[npar+1]))
    {
	    PLOG(PL_FATAL, "NormEncoderMDP: Error allocating memory for gen_poly polynomial: %s\n",
                 GetErrorString());
	    return false;
    }    
    /* Allocate memory for temporary polynomial arrays */
    if(!(tp = new unsigned char[2*degree]))
    {
	    PLOG(PL_FATAL, "NormEncoderMDP: Error allocating memory while computing gen_poly: %s\n",
                GetErrorString());
        delete[] gen_poly;
	    return false;
    }    
    if(!(tp1 = new unsigned char[2*degree]))
    {
	    delete[] tp;
	    delete[] gen_poly;
	    PLOG(PL_FATAL, "NormEncoderMDP: Error allocating memory while computing gen_poly: %s\n",
                GetErrorString());
	    return false;
    }    
    if(!(tp2 = new unsigned char[2*degree]))
    {
	    delete[] tp1;
	    delete[] tp;
	    delete[] gen_poly;
	    PLOG(PL_FATAL, "NormEncoderMDP: Error allocating memory while computing gen_poly: %s\n",
                GetErrorString());
	    return false;
    }
    // multiply (x + a^n) for n = 1 to npar 
    memset(tp1, 0, degree*sizeof(unsigned char));
    tp1[0] = 1;    
    for (unsigned int n = 1; n <= npar; n++)
    {
	    memset(tp, 0, degree*sizeof(unsigned char));
	    tp[0] = gexp(n);  // set up x+a^n 
	    tp[1] = 1;
	    // Polynomial multiplication 
	    memset(gen_poly, 0, (npar+1)*sizeof(unsigned char));	
	    for (int i = 0; i < degree; i++)
	    {
	        memset(&tp2[degree], 0, degree*sizeof(unsigned char));
            //unsigned int j;
	        // Scale tp2 by p1[i] 
            int j;
	        for(j=0; j<degree; j++) tp2[j]=gmult(tp1[j], tp[i]);
	        // Mult(shift) tp2 right by i 
	        for (j = (degree*2)-1; j >= i; j--) 
                tp2[j] = tp2[j-i];
	        memset(tp2, 0, i*sizeof(unsigned char));
	        // Add into partial product 
	        for(unsigned int x=0; x < (npar+1); x++) gen_poly[x] ^= tp2[x];
	    }    
	    memcpy(tp1, gen_poly, (npar+1)*sizeof(unsigned char));
	    memset(&tp1[npar+1], 0, (2*degree)-(npar+1));
    }    
    delete[] tp2;
    delete[] tp1;
    delete[] tp; 
    return true;  
}  // end NormEncoderMDP::CreateGeneratorPolynomial()



// Encode data vectors one at a time.  The user of this function
// must keep track of when parity is ready for transmission
// Parity data is written to list of parity vectors supplied by caller
// MUST be called w/ "data" vectors in-order by segmentId (caller's responsibility)
void NormEncoderMDP::Encode(unsigned int /*segmentId*/, const char* data, char** pVec)
{
    int i, j;
    unsigned char *userData, *LSFR1, *LSFR2, *pVec0;
    int npar_minus_one = npar - 1;
    unsigned char* genPoly = &gen_poly[npar_minus_one];
    ASSERT(NULL != scratch);  // Make sure it's been init'd first    
    // Assumes parity vectors are zero-filled at block start !!! 
    // Copy pVec[0] for use in calculations 
    
    UINT16 vecSize = vector_size;
    
    memcpy(scratch, pVec[0], vecSize);
    if (npar > 1)
    {
	    for(i = 0; i < npar_minus_one; i++)
	    {
	        pVec0 = scratch;
	        userData = (unsigned char*) data;
	        LSFR1 = (unsigned char*) pVec[i];
	        LSFR2 = (unsigned char*) pVec[i+1];
	        for(j = 0; j < vecSize; j++)
		        *LSFR1++ = *LSFR2++ ^
			        gmult(*genPoly, (*userData++ ^ *pVec0++));
            genPoly--;
	    }
        
    }    
    pVec0 = scratch;
    userData = (unsigned char*) data;
    LSFR1 = (unsigned char*) pVec[npar_minus_one];
    for(j = 0; j < vecSize; j++)
    	*LSFR1++ = gmult(*genPoly, (*userData++ ^ *pVec0++));
}  // end NormEncoderMDP::Encode()


/********************************************************************************
 *  NormDecoderMDP implementation routines
 */

NormDecoderMDP::NormDecoderMDP()
    : npar(0), vector_size(0), lambda(NULL), 
      s_vec(NULL), o_vec(NULL), scratch(NULL)
{
    
}

NormDecoderMDP::~NormDecoderMDP()
{
    if (NULL != lambda) Destroy();
}

bool NormDecoderMDP::Init(unsigned int numData, unsigned int numParity, UINT16 vecSizeMax)
{ 
    // Debugging assertions
    ASSERT((numData + numParity) <= 255);
    if ((numData + numParity) > 255) return false;
    
#ifdef SIMULATE
    vecSizeMax = MIN(SIM_PAYLOAD_MAX, vecSizeMax);
#endif // SIMUATE  
    
    if (lambda) Destroy();  // Check if already inited ...
    
    npar = numParity;
    vector_size = vecSizeMax;
    
    if(!(lambda = new unsigned char[2*npar]))
    {
	    PLOG(PL_FATAL, "NormDecoderMDP: Error allocating memory for lambda: %s\n",
                GetErrorString());
	    return(false);
    }
    
    /* Allocate memory for s_vec ptr and the syndrome vectors */
    if(!(s_vec = new unsigned char*[npar]))
    {
	    PLOG(PL_FATAL, "NormDecoderMDP: Error allocating memory for s_vec ptr: %s\n",
                GetErrorString());
	    Destroy();
	    return(false);
    }
    unsigned int i;
    for(i=0; i < npar; i++)
    {
	    if(!(s_vec[i] = new unsigned char[vecSizeMax]))
	    {
	        PLOG(PL_FATAL, "NormDecoderMDP: Error allocating memory for new s_vec: %s\n",
                    GetErrorString());
	        Destroy();
	        return(false);
	    }
    }
    
    /* Allocate memory for the o_vec ptr and the Omega vectors */
    if(!(o_vec = new unsigned char*[npar]))
    {
	    PLOG(PL_FATAL, "NormDecoderMDP: Error allocating memory for new o_vec ptr: %s\n",
                GetErrorString());
	    Destroy();
	    return(false);
    }
    
    for(i=0; i < npar; i++)
    {
	    if(!(o_vec[i] = new unsigned char[vecSizeMax]))
	    {
	        PLOG(PL_FATAL, "NormDecoderMDP: Error allocating memory for new o_vec: %s",
                    GetErrorString());
	        Destroy();
	        return(false);
	    }
    }
    
    if (!(scratch = new unsigned char[vecSizeMax]))
    {
        PLOG(PL_FATAL, "NormDecoderMDP: Error allocating memory for scratch space: %s",
                 GetErrorString());  
    }
    memset(scratch, 0, vecSizeMax*sizeof(unsigned char));
    return(true);
}  // end NormDecoderMDP::Init()


void NormDecoderMDP::Destroy()
{
    if (scratch)
    {
        delete[] scratch;
        scratch = NULL;
    }
    if(o_vec)
    {
	    for(unsigned int i=0; i<npar; i++)
	        if (o_vec[i]) delete[] o_vec[i];
	    delete[] o_vec;
        o_vec = NULL;
    }	
    if(s_vec)
    {
	    for(unsigned int i = 0; i < npar; i++)
	        if (s_vec[i]) delete[] s_vec[i];
	    delete[] s_vec;
        s_vec = NULL;
    }
    if (lambda)
    {
        
        delete[] lambda;
        lambda = NULL;
    }
}  // end NormDecoderMDP::Destroy()


// This will crash & burn if (erasureCount > npar)
int NormDecoderMDP::Decode(char** dVec, unsigned int numData, unsigned int erasureCount, unsigned int* erasureLocs)
{
    // Debugging assertions
    ASSERT(NULL != lambda);     
    ASSERT(erasureCount && (erasureCount <= npar));
      
    // (A) Compute syndrome vectors 
    
    // First zero out erasure vectors (NORM provides zero-filled vecs) 
	
	// Then calculate syndrome (based on zero value erasures) 
    unsigned int nvecs = npar + numData;
    UINT16 vecSize = vector_size;
    
    unsigned int i;
    for (i = 0; i < npar; i++)
    {
	    int X = gexp(i+1);
	    unsigned char* synVec = s_vec[i];
        memset(synVec, 0, vecSize*sizeof(char));
        for(unsigned int j = 0; j < nvecs; j++)
        {
	        unsigned char* data = dVec[j] ? (unsigned char*)dVec[j] : scratch;
            unsigned char* S = synVec;
            for (int n = 0; n < vecSize; n++)
	        {
                *S = *data++ ^ gmult(X, *S);
	            S++;
	        }
        }
    }
    
    // (B) Init lambda (the erasure locator polynomial) 
    unsigned int degree = 2*npar;
    unsigned int nvecsMinusOne = nvecs - 1;
    memset(lambda, 0, degree*sizeof(char));
    lambda[0] = 1;
    for (i = 0; i < erasureCount; i++)
    {
	    int X = gexp(nvecsMinusOne - erasureLocs[i]);
	    for(int j = (degree-1); j > 0; j--)
	        lambda[j] = lambda[j] ^ gmult(X, lambda[j-1]);
    }

    // (C) Compute modified Omega using lambda 
    for(i = 0; i < npar; i++)
    {
	    int k = i;
	    memset(o_vec[i], 0, vecSize*sizeof(char));
        //int m = i + 1;
	    for(unsigned int j = 0; j <= i; j++)
	    { 
	        unsigned char* Omega = o_vec[i];
	        unsigned char* S = s_vec[j];
	        int Lk = lambda[k--];
	        for(UINT16 n = 0; n < vecSize; n++)
		        *Omega++ ^= gmult(*S++, Lk);
	    }
    }

    // (D) Finally, fill in the erasures 
    for (i = 0; i < erasureCount; i++)
    {       
        // Only fill _data_ erasures
        if (erasureLocs[i] >= numData) break;//return erasureCount;
        
        // evaluate lambda' (derivative) at alpha^(-i) 
	    // (all odd powers disappear) 
	    unsigned int k = nvecsMinusOne - erasureLocs[i];
	    int denom = 0;
        unsigned int j;
	    for (j = 1; j < degree; j += 2)
	        denom ^= gmult(lambda[j], gexp(((255-k)*(j-1)) % 255));
	    // Invert for use computing error value below 
	    denom = ginv(denom);

	    // Now evaluate Omega at alpha^(-i) (numerator) 
	    unsigned char* eVec = (unsigned char*)dVec[erasureLocs[i]];
	    for (j = 0; j < npar; j++)
	    {
            unsigned char* data = eVec;
	        unsigned char* Omega = o_vec[j];
	        int X = gexp(((255-k)*j) % 255);
	        for(int n = 0; n < vecSize; n++)
		        *data++ ^= gmult(*Omega++, X);
            
	    }

	    // Scale numerator with denominator 
	    unsigned char* data = eVec;
	    for(int n = 0; n < vecSize; n++)
	    {
	        *data = gmult(*data, denom);
	        data++;
	    }
    }
    return erasureCount;
}  // end NormDecoderMDP::Decode()