This source file includes following definitions.
- pmix_bcopy_csum_partial
- pmix_bcopy_uicsum_partial
- pmix_csum_partial
- pmix_uicsum_partial
- pmix_initialize_crc_table
- pmix_bcopy_uicrc_partial
- pmix_uicrc_partial
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 #include <src/include/pmix_config.h>
22
23
24 #ifdef HAVE_STDIO_H
25 #include <stdio.h>
26 #endif
27 #ifdef HAVE_STDLIB_H
28 #include <stdlib.h>
29 #endif
30 #ifdef HAVE_STRINGS_H
31 #include <strings.h>
32 #endif
33 #ifdef HAVE_STRING_H
34 #include <string.h>
35 #endif
36 #ifdef HAVE_UNISTD_H
37 #include <unistd.h>
38 #endif
39
40 #include "src/util/crc.h"
41
42
43 #if (PMIX_ALIGNMENT_LONG == 8)
44 #define PMIX_CRC_WORD_MASK_ 0x7
45 #elif (PMIX_ALIGNMENT_LONG == 4)
46 #define PMIX_CRC_WORD_MASK_ 0x3
47 #else
48 #define PMIX_CRC_WORD_MASK_ 0xFFFF
49 #endif
50
51
52 #define WORDALIGNED(v) \
53 (((intptr_t)v & PMIX_CRC_WORD_MASK_) ? false : true)
54
55
56 #define INTALIGNED(v) \
57 (((intptr_t)v & 3) ? false : true)
58
59
60
61
62
63
64
65
66
67
68
69 unsigned long
70 pmix_bcopy_csum_partial (
71 const void * source,
72 void * destination,
73 size_t copylen,
74 size_t csumlen,
75 unsigned long * lastPartialLong,
76 size_t* lastPartialLength
77 )
78 {
79 unsigned long * src = (unsigned long *) source;
80 unsigned long * dest = (unsigned long *) destination;
81 unsigned long csum = 0;
82 size_t csumlenresidue;
83 unsigned long i, temp;
84
85 csumlenresidue = (csumlen > copylen) ? (csumlen - copylen) : 0;
86 temp = *lastPartialLong;
87
88 if (WORDALIGNED(source) && WORDALIGNED(dest)) {
89 if (*lastPartialLength) {
90
91 if (copylen >= (sizeof(unsigned long) - *lastPartialLength)) {
92 memcpy(((char *)&temp + *lastPartialLength), src,
93 (sizeof(unsigned long) - *lastPartialLength));
94 memcpy(dest, ((char *)&temp + *lastPartialLength),
95 (sizeof(unsigned long) - *lastPartialLength));
96 src = (unsigned long *)((char *)src + sizeof(unsigned long) - *lastPartialLength);
97 dest = (unsigned long *)((char *)dest + sizeof(unsigned long) - *lastPartialLength);
98 csum += (temp - *lastPartialLong);
99 copylen -= sizeof(unsigned long) - *lastPartialLength;
100
101 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
102 memcpy(&temp, src, sizeof(temp));
103 src++;
104 csum += temp;
105 memcpy(dest, &temp, sizeof(temp));
106 dest++;
107 }
108 *lastPartialLength = 0;
109 *lastPartialLong = 0;
110 }
111 else {
112 memcpy(((char *)&temp + *lastPartialLength), src, copylen);
113 memcpy(dest, ((char *)&temp + *lastPartialLength), copylen);
114 src = (unsigned long *)((char *)src + copylen);
115 dest = (unsigned long *)((char *)dest + copylen);
116 csum += (temp - *lastPartialLong);
117 *lastPartialLong = temp;
118 *lastPartialLength += copylen;
119 copylen = 0;
120 }
121 }
122 else {
123 size_t numLongs = copylen/sizeof(unsigned long);
124 for(i = 0; i < numLongs; i++) {
125 csum += *src;
126 *dest++ = *src++;
127 }
128 *lastPartialLong = 0;
129 *lastPartialLength = 0;
130 if (WORDALIGNED(copylen) && (csumlenresidue == 0)) {
131 return(csum);
132 }
133 else {
134 copylen -= i * sizeof(unsigned long);
135 }
136 }
137 } else if (WORDALIGNED(source)) {
138 if (*lastPartialLength) {
139
140 if (copylen >= (sizeof(unsigned long) - *lastPartialLength)) {
141 memcpy(((char *)&temp + *lastPartialLength), src,
142 (sizeof(unsigned long) - *lastPartialLength));
143 memcpy(dest, ((char *)&temp + *lastPartialLength),
144 (sizeof(unsigned long) - *lastPartialLength));
145 src = (unsigned long *)((char *)src + sizeof(unsigned long) - *lastPartialLength);
146 dest = (unsigned long *)((char *)dest + sizeof(unsigned long) - *lastPartialLength);
147 csum += (temp - *lastPartialLong);
148 copylen -= sizeof(unsigned long) - *lastPartialLength;
149
150 if (WORDALIGNED(dest)) {
151 size_t numLongs = copylen/sizeof(unsigned long);
152 for(i = 0; i < numLongs; i++) {
153 memcpy(&temp, src, sizeof(temp));
154 src++;
155 csum += temp;
156 *dest++ = temp;
157 }
158 copylen -= i * sizeof(unsigned long);
159 }
160 else {
161 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
162 memcpy(&temp, src, sizeof(temp));
163 src++;
164 csum += temp;
165 memcpy(dest, &temp, sizeof(temp));
166 dest++;
167 }
168 }
169 *lastPartialLong = 0;
170 *lastPartialLength = 0;
171 }
172 else {
173 memcpy(((char *)&temp + *lastPartialLength), src, copylen);
174 memcpy(dest, ((char *)&temp + *lastPartialLength), copylen);
175 src = (unsigned long *)((char *)src + copylen);
176 dest = (unsigned long *)((char *)dest + copylen);
177 csum += (temp - *lastPartialLong);
178 *lastPartialLong = temp;
179 *lastPartialLength += copylen;
180 copylen = 0;
181 }
182 }
183 else {
184 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
185 temp = *src++;
186 csum += temp;
187 memcpy(dest, &temp, sizeof(temp));
188 dest++;
189 }
190 *lastPartialLong = 0;
191 *lastPartialLength = 0;
192 }
193 } else if (WORDALIGNED(dest)) {
194 if (*lastPartialLength) {
195
196 if (copylen >= (sizeof(unsigned long) - *lastPartialLength)) {
197 memcpy(((char *)&temp + *lastPartialLength), src,
198 (sizeof(unsigned long) - *lastPartialLength));
199 memcpy(dest, ((char *)&temp + *lastPartialLength),
200 (sizeof(unsigned long) - *lastPartialLength));
201 src = (unsigned long *)((char *)src + sizeof(unsigned long) - *lastPartialLength);
202 dest = (unsigned long *)((char *)dest + sizeof(unsigned long) - *lastPartialLength);
203 csum += (temp - *lastPartialLong);
204 copylen -= sizeof(unsigned long) - *lastPartialLength;
205
206 if (WORDALIGNED(src)) {
207 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
208 temp = *src++;
209 csum += temp;
210 memcpy(dest, &temp, sizeof(temp));
211 dest++;
212 }
213 *lastPartialLong = 0;
214 *lastPartialLength = 0;
215 }
216 else {
217 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
218 memcpy(&temp, src, sizeof(temp));
219 src++;
220 csum += temp;
221 memcpy(dest, &temp, sizeof(temp));
222 dest++;
223 }
224 *lastPartialLength = 0;
225 *lastPartialLong = 0;
226 }
227 }
228 else {
229 memcpy(((char *)&temp + *lastPartialLength), src, copylen);
230 memcpy(dest, ((char *)&temp + *lastPartialLength), copylen);
231 src = (unsigned long *)((char *)src + copylen);
232 dest = (unsigned long *)((char *)dest + copylen);
233 csum += (temp - *lastPartialLong);
234 *lastPartialLong = temp;
235 *lastPartialLength += copylen;
236 copylen = 0;
237 }
238 }
239 else {
240 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
241 memcpy(&temp, src, sizeof(temp));
242 src++;
243 csum += temp;
244 *dest++ = temp;
245 }
246 *lastPartialLength = 0;
247 *lastPartialLong = 0;
248 }
249 } else {
250 if (*lastPartialLength) {
251
252 if (copylen >= (sizeof(unsigned long) - *lastPartialLength)) {
253 memcpy(((char *)&temp + *lastPartialLength), src,
254 (sizeof(unsigned long) - *lastPartialLength));
255 memcpy(dest, ((char *)&temp + *lastPartialLength),
256 (sizeof(unsigned long) - *lastPartialLength));
257 src = (unsigned long *)((char *)src + sizeof(unsigned long) - *lastPartialLength);
258 dest = (unsigned long *)((char *)dest + sizeof(unsigned long) - *lastPartialLength);
259 csum += (temp - *lastPartialLong);
260 copylen -= sizeof(unsigned long) - *lastPartialLength;
261
262 if (WORDALIGNED(src) && WORDALIGNED(dest)) {
263 size_t numLongs = copylen/sizeof(unsigned long);
264 for(i = 0; i < numLongs; i++) {
265 csum += *src;
266 *dest++ = *src++;
267 }
268 copylen -= i * sizeof(unsigned long);
269 }
270 else {
271 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
272 memcpy(&temp, src, sizeof(temp));
273 src++;
274 csum += temp;
275 memcpy(dest, &temp, sizeof(temp));
276 dest++;
277 }
278 }
279 *lastPartialLong = 0;
280 *lastPartialLength = 0;
281 }
282 else {
283 memcpy(((char *)&temp + *lastPartialLength), src, copylen);
284 memcpy(dest, ((char *)&temp + *lastPartialLength), copylen);
285 src = (unsigned long *)((char *)src + copylen);
286 dest = (unsigned long *)((char *)dest + copylen);
287 csum += (temp - *lastPartialLong);
288 *lastPartialLong = temp;
289 *lastPartialLength += copylen;
290 copylen = 0;
291 }
292 }
293 else {
294 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
295 memcpy(&temp, src, sizeof(temp));
296 src++;
297 csum += temp;
298 memcpy(dest, &temp, sizeof(temp));
299 dest++;
300 }
301 *lastPartialLength = 0;
302 *lastPartialLong = 0;
303 }
304 }
305
306
307 if ((copylen != 0) && (csumlenresidue == 0)) {
308 temp = *lastPartialLong;
309 if (*lastPartialLength) {
310 if (copylen >= (sizeof(unsigned long) - *lastPartialLength)) {
311
312 unsigned long copytemp = 0;
313 memcpy(©temp, src, copylen);
314 memcpy(dest, ©temp, copylen);
315
316 memcpy(((char *)&temp + *lastPartialLength), src,
317 (sizeof(unsigned long) - *lastPartialLength));
318
319
320
321 csum += (temp - *lastPartialLong);
322 copylen -= sizeof(unsigned long) - *lastPartialLength;
323 src = (unsigned long *)((char *)src + sizeof(unsigned long) - *lastPartialLength);
324 *lastPartialLength = copylen;
325
326 temp = 0;
327 if (copylen) {
328 memcpy(&temp, src, copylen);
329 }
330
331 csum += temp;
332 *lastPartialLong = temp;
333 }
334 else {
335
336 unsigned long copytemp = 0;
337 memcpy(©temp, src, copylen);
338 memcpy(dest, ©temp, copylen);
339
340 memcpy(((char *)&temp + *lastPartialLength), src,
341 copylen);
342
343
344
345 csum += temp - *lastPartialLong;
346 *lastPartialLong = temp;
347 *lastPartialLength += copylen;
348 }
349 }
350 else {
351
352 memcpy(&temp, src, copylen);
353 csum += temp;
354 memcpy(dest, &temp, copylen);
355 *lastPartialLong = temp;
356 *lastPartialLength = copylen;
357
358 }
359 }
360 else if (csumlenresidue != 0) {
361 if (copylen != 0) {
362 temp = 0;
363 memcpy(&temp, src, copylen);
364 memcpy(dest, &temp, copylen);
365 }
366 if (csumlenresidue < (sizeof(unsigned long) - copylen - *lastPartialLength)) {
367 temp = *lastPartialLong;
368 memcpy(((char *)&temp + *lastPartialLength), src, (copylen + csumlenresidue));
369
370
371 csum += temp - *lastPartialLong;
372 src++;
373 *lastPartialLong = temp;
374 *lastPartialLength += copylen + csumlenresidue;
375 csumlenresidue = 0;
376 }
377 else {
378
379
380 temp = *lastPartialLong;
381 memcpy(((char *)&temp + *lastPartialLength), src,
382 (sizeof(unsigned long) - *lastPartialLength));
383
384
385 csum += temp - *lastPartialLong;
386 src = (unsigned long *)((char *)src + sizeof(unsigned long) - *lastPartialLength);
387 csumlenresidue -= sizeof(unsigned long) - *lastPartialLength - copylen;
388 *lastPartialLength = 0;
389 *lastPartialLong = 0;
390 }
391 if (WORDALIGNED(src)) {
392 for (i = 0; i < csumlenresidue/sizeof(unsigned long); i++) {
393 csum += *src++;
394 }
395 }
396 else {
397 for (i = 0; i < csumlenresidue/sizeof(unsigned long); i++) {
398 memcpy(&temp, src, sizeof(temp));
399 csum += temp;
400 src++;
401 }
402 }
403 csumlenresidue -= i * sizeof(unsigned long);
404 if (csumlenresidue) {
405 temp = 0;
406 memcpy(&temp, src, csumlenresidue);
407 csum += temp;
408 *lastPartialLong = temp;
409 *lastPartialLength = csumlenresidue;
410 }
411 }
412
413 return csum;
414 }
415
416 unsigned int
417 pmix_bcopy_uicsum_partial (
418 const void * source,
419 void * destination,
420 size_t copylen,
421 size_t csumlen,
422 unsigned int* lastPartialInt,
423 size_t* lastPartialLength
424 )
425 {
426 unsigned int * src = (unsigned int *) source;
427 unsigned int * dest = (unsigned int *) destination;
428 unsigned int csum = 0;
429 size_t csumlenresidue;
430 unsigned long i;
431 unsigned int temp;
432
433 csumlenresidue = (csumlen > copylen) ? (csumlen - copylen) : 0;
434 temp = *lastPartialInt;
435
436 if (INTALIGNED(source) && INTALIGNED(dest)) {
437 if (*lastPartialLength) {
438
439 if (copylen >= (sizeof(unsigned int) - *lastPartialLength)) {
440 memcpy(((char *)&temp + *lastPartialLength), src,
441 (sizeof(unsigned int) - *lastPartialLength));
442 memcpy(dest, ((char *)&temp + *lastPartialLength),
443 (sizeof(unsigned int) - *lastPartialLength));
444 src = (unsigned int *)((char *)src + sizeof(unsigned int) - *lastPartialLength);
445 dest = (unsigned int *)((char *)dest + sizeof(unsigned int) - *lastPartialLength);
446 csum += (temp - *lastPartialInt);
447 copylen -= sizeof(unsigned int) - *lastPartialLength;
448
449 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
450 memcpy(&temp, src, sizeof(temp));
451 src++;
452 csum += temp;
453 memcpy(dest, &temp, sizeof(temp));
454 dest++;
455 }
456 *lastPartialLength = 0;
457 *lastPartialInt = 0;
458 }
459 else {
460 memcpy(((char *)&temp + *lastPartialLength), src, copylen);
461 memcpy(dest, ((char *)&temp + *lastPartialLength), copylen);
462 src = (unsigned int *)((char *)src + copylen);
463 dest = (unsigned int *)((char *)dest + copylen);
464 csum += (temp - *lastPartialInt);
465 *lastPartialInt = temp;
466 *lastPartialLength += copylen;
467 copylen = 0;
468 }
469 }
470 else {
471 size_t numLongs = copylen/sizeof(unsigned int);
472 for(i = 0; i < numLongs; i++) {
473 csum += *src;
474 *dest++ = *src++;
475 }
476 *lastPartialInt = 0;
477 *lastPartialLength = 0;
478 if (INTALIGNED(copylen) && (csumlenresidue == 0)) {
479 return(csum);
480 }
481 else {
482 copylen -= i * sizeof(unsigned int);
483 }
484 }
485 } else if (INTALIGNED(source)) {
486 if (*lastPartialLength) {
487
488 if (copylen >= (sizeof(unsigned int) - *lastPartialLength)) {
489 memcpy(((char *)&temp + *lastPartialLength), src,
490 (sizeof(unsigned int) - *lastPartialLength));
491 memcpy(dest, ((char *)&temp + *lastPartialLength),
492 (sizeof(unsigned int) - *lastPartialLength));
493 src = (unsigned int *)((char *)src + sizeof(unsigned int) - *lastPartialLength);
494 dest = (unsigned int *)((char *)dest + sizeof(unsigned int) - *lastPartialLength);
495 csum += (temp - *lastPartialInt);
496 copylen -= sizeof(unsigned int) - *lastPartialLength;
497
498 if (INTALIGNED(dest)) {
499 size_t numLongs = copylen/sizeof(unsigned int);
500 for(i = 0; i < numLongs; i++) {
501 memcpy(&temp, src, sizeof(temp));
502 src++;
503 csum += temp;
504 *dest++ = temp;
505 }
506 copylen -= i * sizeof(unsigned int);
507 }
508 else {
509 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
510 memcpy(&temp, src, sizeof(temp));
511 src++;
512 csum += temp;
513 memcpy(dest, &temp, sizeof(temp));
514 dest++;
515 }
516 }
517 *lastPartialInt = 0;
518 *lastPartialLength = 0;
519 }
520 else {
521 memcpy(((char *)&temp + *lastPartialLength), src, copylen);
522 memcpy(dest, ((char *)&temp + *lastPartialLength), copylen);
523 src = (unsigned int *)((char *)src + copylen);
524 dest = (unsigned int *)((char *)dest + copylen);
525 csum += (temp - *lastPartialInt);
526 *lastPartialInt = temp;
527 *lastPartialLength += copylen;
528 copylen = 0;
529 }
530 }
531 else {
532 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
533 temp = *src++;
534 csum += temp;
535 memcpy(dest, &temp, sizeof(temp));
536 dest++;
537 }
538 *lastPartialInt = 0;
539 *lastPartialLength = 0;
540 }
541 } else if (INTALIGNED(dest)) {
542 if (*lastPartialLength) {
543
544 if (copylen >= (sizeof(unsigned int) - *lastPartialLength)) {
545 memcpy(((char *)&temp + *lastPartialLength), src,
546 (sizeof(unsigned int) - *lastPartialLength));
547 memcpy(dest, ((char *)&temp + *lastPartialLength),
548 (sizeof(unsigned int) - *lastPartialLength));
549 src = (unsigned int *)((char *)src + sizeof(unsigned int) - *lastPartialLength);
550 dest = (unsigned int *)((char *)dest + sizeof(unsigned int) - *lastPartialLength);
551 csum += (temp - *lastPartialInt);
552 copylen -= sizeof(unsigned int) - *lastPartialLength;
553
554 if (INTALIGNED(src)) {
555 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
556 temp = *src++;
557 csum += temp;
558 memcpy(dest, &temp, sizeof(temp));
559 dest++;
560 }
561 *lastPartialInt = 0;
562 *lastPartialLength = 0;
563 }
564 else {
565 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
566 memcpy(&temp, src, sizeof(temp));
567 src++;
568 csum += temp;
569 memcpy(dest, &temp, sizeof(temp));
570 dest++;
571 }
572 *lastPartialLength = 0;
573 *lastPartialInt = 0;
574 }
575 }
576 else {
577 memcpy(((char *)&temp + *lastPartialLength), src, copylen);
578 memcpy(dest, ((char *)&temp + *lastPartialLength), copylen);
579 src = (unsigned int *)((char *)src + copylen);
580 dest = (unsigned int *)((char *)dest + copylen);
581 csum += (temp - *lastPartialInt);
582 *lastPartialInt = temp;
583 *lastPartialLength += copylen;
584 copylen = 0;
585 }
586 }
587 else {
588 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
589 memcpy(&temp, src, sizeof(temp));
590 src++;
591 csum += temp;
592 *dest++ = temp;
593 }
594 *lastPartialLength = 0;
595 *lastPartialInt = 0;
596 }
597 } else {
598 if (*lastPartialLength) {
599
600 if (copylen >= (sizeof(unsigned int) - *lastPartialLength)) {
601 memcpy(((char *)&temp + *lastPartialLength), src,
602 (sizeof(unsigned int) - *lastPartialLength));
603 memcpy(dest, ((char *)&temp + *lastPartialLength),
604 (sizeof(unsigned int) - *lastPartialLength));
605 src = (unsigned int *)((char *)src + sizeof(unsigned int) - *lastPartialLength);
606 dest = (unsigned int *)((char *)dest + sizeof(unsigned int) - *lastPartialLength);
607 csum += (temp - *lastPartialInt);
608 copylen -= sizeof(unsigned int) - *lastPartialLength;
609
610 if (INTALIGNED(src) && INTALIGNED(dest)) {
611 size_t numLongs = copylen/sizeof(unsigned int);
612 for(i = 0; i < numLongs; i++) {
613 csum += *src;
614 *dest++ = *src++;
615 }
616 copylen -= i * sizeof(unsigned int);
617 }
618 else {
619 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
620 memcpy(&temp, src, sizeof(temp));
621 src++;
622 csum += temp;
623 memcpy(dest, &temp, sizeof(temp));
624 dest++;
625 }
626 }
627 *lastPartialInt = 0;
628 *lastPartialLength = 0;
629 }
630 else {
631 memcpy(((char *)&temp + *lastPartialLength), src, copylen);
632 memcpy(dest, ((char *)&temp + *lastPartialLength), copylen);
633 src = (unsigned int *)((char *)src + copylen);
634 dest = (unsigned int *)((char *)dest + copylen);
635 csum += (temp - *lastPartialInt);
636 *lastPartialInt = temp;
637 *lastPartialLength += copylen;
638 copylen = 0;
639 }
640 }
641 else {
642 for( ;copylen >= sizeof(*src); copylen -= sizeof(*src)) {
643 memcpy(&temp, src, sizeof(temp));
644 src++;
645 csum += temp;
646 memcpy(dest, &temp, sizeof(temp));
647 dest++;
648 }
649 *lastPartialLength = 0;
650 *lastPartialInt = 0;
651 }
652 }
653
654
655 if ((copylen != 0) && (csumlenresidue == 0)) {
656 temp = *lastPartialInt;
657 if (*lastPartialLength) {
658 if (copylen >= (sizeof(unsigned int) - *lastPartialLength)) {
659
660 unsigned int copytemp = 0;
661 memcpy(©temp, src, copylen);
662 memcpy(dest, ©temp, copylen);
663
664 memcpy(((char *)&temp + *lastPartialLength), src,
665 (sizeof(unsigned int) - *lastPartialLength));
666
667
668
669 csum += (temp - *lastPartialInt);
670 copylen -= sizeof(unsigned int) - *lastPartialLength;
671 src = (unsigned int *)((char *)src + sizeof(unsigned int) - *lastPartialLength);
672 *lastPartialLength = copylen;
673
674 temp = 0;
675 if (copylen) {
676 memcpy(&temp, src, copylen);
677 }
678
679 csum += temp;
680 *lastPartialInt = temp;
681 }
682 else {
683
684 unsigned int copytemp = 0;
685 memcpy(©temp, src, copylen);
686 memcpy(dest, ©temp, copylen);
687
688 memcpy(((char *)&temp + *lastPartialLength), src,
689 copylen);
690
691
692
693 csum += temp - *lastPartialInt;
694 *lastPartialInt = temp;
695 *lastPartialLength += copylen;
696 }
697 }
698 else {
699
700 memcpy(&temp, src, copylen);
701 csum += temp;
702 memcpy(dest, &temp, copylen);
703 *lastPartialInt = temp;
704 *lastPartialLength = copylen;
705
706 }
707 }
708 else if (csumlenresidue != 0) {
709 if (copylen != 0) {
710 temp = 0;
711 memcpy(&temp, src, copylen);
712 memcpy(dest, &temp, copylen);
713 }
714 if (csumlenresidue < (sizeof(unsigned int) - copylen - *lastPartialLength)) {
715 temp = *lastPartialInt;
716 memcpy(((char *)&temp + *lastPartialLength), src, (copylen + csumlenresidue));
717
718
719
720 csum += temp - *lastPartialInt;
721 src++;
722 *lastPartialInt = temp;
723 *lastPartialLength += copylen + csumlenresidue;
724 csumlenresidue = 0;
725 }
726 else {
727
728
729
730 temp = *lastPartialInt;
731 memcpy(((char *)&temp + *lastPartialLength), src,
732 (sizeof(unsigned int) - *lastPartialLength));
733
734
735
736 csum += temp - *lastPartialInt;
737 src = (unsigned int *)((char *)src + sizeof(unsigned int) - *lastPartialLength);
738 csumlenresidue -= sizeof(unsigned int) - *lastPartialLength - copylen;
739 *lastPartialLength = 0;
740 *lastPartialInt = 0;
741 }
742 if (INTALIGNED(src)) {
743 for (i = 0; i < csumlenresidue/sizeof(unsigned int); i++) {
744 csum += *src++;
745 }
746 }
747 else {
748 for (i = 0; i < csumlenresidue/sizeof(unsigned int); i++) {
749 memcpy(&temp, src, sizeof(temp));
750 csum += temp;
751 src++;
752 }
753 }
754 csumlenresidue -= i * sizeof(unsigned int);
755 if (csumlenresidue) {
756 temp = 0;
757 memcpy(&temp, src, csumlenresidue);
758 csum += temp;
759 *lastPartialInt = temp;
760 *lastPartialLength = csumlenresidue;
761 }
762 }
763
764 return csum;
765 }
766
767
768
769
770
771
772
773 unsigned long
774 pmix_csum_partial (
775 const void * source,
776 size_t csumlen,
777 unsigned long* lastPartialLong,
778 size_t* lastPartialLength
779 )
780 {
781 unsigned long * src = (unsigned long *) source;
782 unsigned long csum = 0;
783 unsigned long i, temp;
784
785
786
787 temp = *lastPartialLong;
788
789 if (WORDALIGNED(source)) {
790 if (*lastPartialLength) {
791
792 if (csumlen >= (sizeof(unsigned long) - *lastPartialLength)) {
793 memcpy(((char *)&temp + *lastPartialLength), src,
794 (sizeof(unsigned long) - *lastPartialLength));
795 src = (unsigned long *)((char *)src + sizeof(unsigned long) - *lastPartialLength);
796 csum += (temp - *lastPartialLong);
797 csumlen -= sizeof(unsigned long) - *lastPartialLength;
798
799 for(i = 0; i < csumlen/sizeof(unsigned long); i++) {
800 memcpy(&temp, src, sizeof(temp));
801 csum += temp;
802 src++;
803 }
804 csumlen -= i * sizeof(unsigned long);
805 *lastPartialLong = 0;
806 *lastPartialLength = 0;
807 }
808 else {
809 memcpy(((char *)&temp + *lastPartialLength), src, csumlen);
810 src = (unsigned long *)((char *)src + csumlen);
811 csum += (temp - *lastPartialLong);
812 *lastPartialLong = temp;
813 *lastPartialLength += csumlen;
814 csumlen = 0;
815 }
816 }
817 else {
818 size_t numLongs = csumlen/sizeof(unsigned long);
819 for(i = 0; i < numLongs; i++) {
820 csum += *src++;
821 }
822 *lastPartialLong = 0;
823 *lastPartialLength = 0;
824 if (WORDALIGNED(csumlen)) {
825 return(csum);
826 }
827 else {
828 csumlen -= i * sizeof(unsigned long);
829 }
830 }
831 } else {
832 if (*lastPartialLength) {
833
834 if (csumlen >= (sizeof(unsigned long) - *lastPartialLength)) {
835 memcpy(((char *)&temp + *lastPartialLength), src,
836 (sizeof(unsigned long) - *lastPartialLength));
837 src = (unsigned long *)((char *)src + sizeof(unsigned long) - *lastPartialLength);
838 csum += (temp - *lastPartialLong);
839 csumlen -= sizeof(unsigned long) - *lastPartialLength;
840
841 if (WORDALIGNED(src)) {
842 for(i = 0; i < csumlen/sizeof(unsigned long); i++) {
843 csum += *src++;
844 }
845 csumlen -= i * sizeof(unsigned long);
846 *lastPartialLong = 0;
847 *lastPartialLength = 0;
848 }
849 else {
850 for(i = 0; i < csumlen/sizeof(unsigned long); i++) {
851 memcpy(&temp, src, sizeof(temp));
852 csum += temp;
853 src++;
854 }
855 csumlen -= i * sizeof(unsigned long);
856 *lastPartialLong = 0;
857 *lastPartialLength = 0;
858 }
859 }
860 else {
861 memcpy(((char *)&temp + *lastPartialLength), src, csumlen);
862 src = (unsigned long *)((char *)src + csumlen);
863 csum += (temp - *lastPartialLong);
864 *lastPartialLong = temp;
865 *lastPartialLength += csumlen;
866 csumlen = 0;
867 }
868 }
869 else {
870 for( ;csumlen >= sizeof(*src); csumlen -= sizeof(*src)) {
871 memcpy(&temp, src, sizeof(temp));
872 src++;
873 csum += temp;
874 }
875 *lastPartialLength = 0;
876 *lastPartialLong = 0;
877 }
878 }
879
880
881 if (csumlen != 0) {
882 temp = *lastPartialLong;
883 if (*lastPartialLength) {
884 if (csumlen >= (sizeof(unsigned long) - *lastPartialLength)) {
885
886 memcpy(((char *)&temp + *lastPartialLength), src,
887 (sizeof(unsigned long) - *lastPartialLength));
888 csum += (temp - *lastPartialLong);
889 csumlen -= sizeof(unsigned long) - *lastPartialLength;
890 src = (unsigned long *)((char *)src + sizeof(unsigned long) - *lastPartialLength);
891 *lastPartialLength = csumlen;
892
893 temp = 0;
894 if (csumlen) {
895 memcpy(&temp, src, csumlen);
896 }
897
898 csum += temp;
899 *lastPartialLong = temp;
900 }
901 else {
902
903 memcpy(((char *)&temp + *lastPartialLength), src,
904 csumlen);
905 csum += (temp - *lastPartialLong);
906 *lastPartialLong = temp;
907 *lastPartialLength += csumlen;
908 }
909 }
910 else {
911
912 memcpy(&temp, src, csumlen);
913 csum += temp;
914 *lastPartialLong = temp;
915 *lastPartialLength = csumlen;
916
917 }
918 }
919
920 return csum;
921 }
922
923 unsigned int
924 pmix_uicsum_partial (
925 const void * source,
926 size_t csumlen,
927 unsigned int* lastPartialInt,
928 size_t* lastPartialLength
929 )
930 {
931 unsigned int * src = (unsigned int *) source;
932 unsigned int csum = 0;
933 unsigned int temp;
934 unsigned long i;
935
936
937 temp = *lastPartialInt;
938
939 if (INTALIGNED(source)) {
940 if (*lastPartialLength) {
941
942 if (csumlen >= (sizeof(unsigned int) - *lastPartialLength)) {
943 memcpy(((char *)&temp + *lastPartialLength), src,
944 (sizeof(unsigned int) - *lastPartialLength));
945 src = (unsigned int *)((char *)src + sizeof(unsigned int) - *lastPartialLength);
946 csum += (temp - *lastPartialInt);
947 csumlen -= sizeof(unsigned int) - *lastPartialLength;
948
949 for(i = 0; i < csumlen/sizeof(unsigned int); i++) {
950 memcpy(&temp, src, sizeof(temp));
951 csum += temp;
952 src++;
953 }
954 csumlen -= i * sizeof(unsigned int);
955 *lastPartialInt = 0;
956 *lastPartialLength = 0;
957 }
958 else {
959 memcpy(((char *)&temp + *lastPartialLength), src, csumlen);
960 src = (unsigned int *)((char *)src + csumlen);
961 csum += (temp - *lastPartialInt);
962 *lastPartialInt = temp;
963 *lastPartialLength += csumlen;
964 csumlen = 0;
965 }
966 }
967 else {
968 size_t numLongs = csumlen/sizeof(unsigned int);
969 for(i = 0; i < numLongs; i++) {
970 csum += *src++;
971 }
972 *lastPartialInt = 0;
973 *lastPartialLength = 0;
974 if (INTALIGNED(csumlen)) {
975 return(csum);
976 }
977 else {
978 csumlen -= i * sizeof(unsigned int);
979 }
980 }
981 } else {
982 if (*lastPartialLength) {
983
984 if (csumlen >= (sizeof(unsigned int) - *lastPartialLength)) {
985 memcpy(((char *)&temp + *lastPartialLength), src,
986 (sizeof(unsigned int) - *lastPartialLength));
987 src = (unsigned int *)((char *)src + sizeof(unsigned int) - *lastPartialLength);
988 csum += (temp - *lastPartialInt);
989 csumlen -= sizeof(unsigned int) - *lastPartialLength;
990
991 if (INTALIGNED(src)) {
992 for(i = 0; i < csumlen/sizeof(unsigned int); i++) {
993 csum += *src++;
994 }
995 csumlen -= i * sizeof(unsigned int);
996 *lastPartialInt = 0;
997 *lastPartialLength = 0;
998 }
999 else {
1000 for(i = 0; i < csumlen/sizeof(unsigned int); i++) {
1001 memcpy(&temp, src, sizeof(temp));
1002 csum += temp;
1003 src++;
1004 }
1005 csumlen -= i * sizeof(unsigned int);
1006 *lastPartialInt = 0;
1007 *lastPartialLength = 0;
1008 }
1009 }
1010 else {
1011 memcpy(((char *)&temp + *lastPartialLength), src, csumlen);
1012 src = (unsigned int *)((char *)src + csumlen);
1013 csum += (temp - *lastPartialInt);
1014 *lastPartialInt = temp;
1015 *lastPartialLength += csumlen;
1016 csumlen = 0;
1017 }
1018 }
1019 else {
1020 for( ;csumlen >= sizeof(*src); csumlen -= sizeof(*src)) {
1021 memcpy(&temp, src, sizeof(temp));
1022 src++;
1023 csum += temp;
1024 }
1025 *lastPartialLength = 0;
1026 *lastPartialInt = 0;
1027 }
1028 }
1029
1030
1031 if (csumlen != 0) {
1032 temp = *lastPartialInt;
1033 if (*lastPartialLength) {
1034 if (csumlen >= (sizeof(unsigned int) - *lastPartialLength)) {
1035
1036 memcpy(((char *)&temp + *lastPartialLength), src,
1037 (sizeof(unsigned int) - *lastPartialLength));
1038 csum += (temp - *lastPartialInt);
1039 csumlen -= sizeof(unsigned int) - *lastPartialLength;
1040 src = (unsigned int *)((char *)src + sizeof(unsigned int) - *lastPartialLength);
1041 *lastPartialLength = csumlen;
1042
1043 temp = 0;
1044 if (csumlen) {
1045 memcpy(&temp, src, csumlen);
1046 }
1047
1048 csum += temp;
1049 *lastPartialInt = temp;
1050 }
1051 else {
1052
1053 memcpy(((char *)&temp + *lastPartialLength), src,
1054 csumlen);
1055 csum += (temp - *lastPartialInt);
1056 *lastPartialInt = temp;
1057 *lastPartialLength += csumlen;
1058 }
1059 }
1060 else {
1061
1062 memcpy(&temp, src, csumlen);
1063 csum += temp;
1064 *lastPartialInt = temp;
1065 *lastPartialLength = csumlen;
1066
1067 }
1068 }
1069
1070 return csum;
1071 }
1072
1073
1074
1075 static bool _pmix_crc_table_initialized = false;
1076 static unsigned int _pmix_crc_table[256];
1077
1078
1079
1080
1081
1082 void pmix_initialize_crc_table(void)
1083 {
1084 register int i,j;
1085 register unsigned int crc_accum;
1086
1087 for (i = 0; i < 256; i++) {
1088 crc_accum = (i << 24);
1089 for (j = 0; j < 8; j++) {
1090 if (crc_accum & 0x80000000)
1091 crc_accum = (crc_accum << 1) ^ CRC_POLYNOMIAL;
1092 else
1093 crc_accum = (crc_accum << 1);
1094 }
1095 _pmix_crc_table[i] = crc_accum;
1096 }
1097
1098
1099 _pmix_crc_table_initialized = true;
1100 return;
1101 }
1102
1103 unsigned int pmix_bcopy_uicrc_partial(
1104 const void * source,
1105 void * destination,
1106 size_t copylen,
1107 size_t crclen,
1108 unsigned int partial_crc)
1109 {
1110 size_t crclenresidue = (crclen > copylen) ? (crclen - copylen) : 0;
1111 register int i, j;
1112 register unsigned char t;
1113 unsigned int tmp;
1114
1115 if (!_pmix_crc_table_initialized) {
1116 pmix_initialize_crc_table();
1117 }
1118
1119 if (INTALIGNED(source) && INTALIGNED(destination)) {
1120 register unsigned int * src = (unsigned int *)source;
1121 register unsigned int * dst = (unsigned int *)destination;
1122 register unsigned char *ts, *td;
1123
1124 while (copylen >= sizeof(unsigned int)) {
1125 tmp = *src++;
1126 *dst++ = tmp;
1127 ts = (unsigned char *)&tmp;
1128 for (j = 0; j < (int)sizeof(unsigned int); j++) {
1129 i = ((partial_crc >> 24) ^ *ts++) & 0xff;
1130 partial_crc = (partial_crc << 8) ^ _pmix_crc_table[i];
1131 }
1132 copylen -= sizeof(unsigned int);
1133 }
1134 ts = (unsigned char *)src;
1135 td = (unsigned char *)dst;
1136
1137 while (copylen--) {
1138 t = *ts++;
1139 *td++ = t;
1140 i = ((partial_crc >> 24) ^ t) & 0xff;
1141 partial_crc = (partial_crc << 8) ^ _pmix_crc_table[i];
1142 }
1143
1144 while (crclenresidue--) {
1145 i = ((partial_crc >> 24) ^ *ts++) & 0xff;
1146 partial_crc = (partial_crc << 8) ^ _pmix_crc_table[i];
1147 }
1148 }
1149 else {
1150 register unsigned char * src = (unsigned char *)source;
1151 register unsigned char * dst = (unsigned char *)destination;
1152 while (copylen--) {
1153 t = *src++;
1154 *dst++ = t;
1155 i = ((partial_crc >> 24) ^ t) & 0xff;
1156 partial_crc = (partial_crc << 8) ^ _pmix_crc_table[i];
1157 }
1158 while (crclenresidue--) {
1159 i = ((partial_crc >> 24) ^ *src++) & 0xff;
1160 partial_crc = (partial_crc << 8) ^ _pmix_crc_table[i];
1161 }
1162 }
1163
1164 return partial_crc;
1165 }
1166
1167
1168 unsigned int pmix_uicrc_partial(
1169 const void * source, size_t crclen, unsigned int partial_crc)
1170 {
1171 register int i, j;
1172 register unsigned char * t;
1173 unsigned int tmp;
1174
1175 if (!_pmix_crc_table_initialized) {
1176 pmix_initialize_crc_table();
1177 }
1178
1179 if (INTALIGNED(source)) {
1180 register unsigned int * src = (unsigned int *)source;
1181 while (crclen >= sizeof(unsigned int)) {
1182 tmp = *src++;
1183 t = (unsigned char *)&tmp;
1184 for (j = 0; j < (int)sizeof(unsigned int); j++) {
1185 i = ((partial_crc >> 24) ^ *t++) & 0xff;
1186 partial_crc = (partial_crc << 8) ^ _pmix_crc_table[i];
1187 }
1188 crclen -= sizeof(unsigned int);
1189 }
1190 t = (unsigned char *)src;
1191 while (crclen--) {
1192 i = ((partial_crc >> 24) ^ *t++) & 0xff;
1193 partial_crc = (partial_crc << 8) ^ _pmix_crc_table[i];
1194 }
1195 }
1196 else {
1197 register unsigned char * src = (unsigned char *)source;
1198 while (crclen--) {
1199 i = ((partial_crc >> 24) ^ *src++) & 0xff;
1200 partial_crc = (partial_crc << 8) ^ _pmix_crc_table[i];
1201 }
1202 }
1203
1204 return partial_crc;
1205 }