XS.xs

filter: Statementstmt 100% 27 / 27 Branchbran 92.3% 24 / 26 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 96.2% 51 / 53 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/Net/Fix/Checksum/XS.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5  
6 #include "ppport.h"
7  
8 /* Limit on the output message size, including the checksum and \0.
9 This mean we can receive up to 4090 chars incl. \0. */
10 #ifndef FIX_MAX_MSG_LENGTH
11 #define FIX_MAX_MSG_LENGTH 4096
12 #endif
13  
14  
15 MODULE = Net::Fix::Checksum::XS PACKAGE = Net::Fix::Checksum::XS
16  
17 static char *
18 generate_checksum(fixmsg)
19 const char * fixmsg
20 PROTOTYPE: $
21 CODE:
22 char *tmpptr;
23 unsigned int len;
24  
25 char cksum[4]; /* checksum value only */
26 unsigned int idx;
27 unsigned int cks;
28  
29 /* Find and extract the checksum */
30 9 len = strlen(fixmsg);
31 /* Check message is properly terminated */
32 9 ▶ if (fixmsg[len-1] != '\001') XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (fixmsg[len-1] != '\001') XSRETURN_UNDEF; 2 7 9
33  
34 7 ▶ if (tmpptr = strstr(fixmsg, "\00110=")) {
Branch: 1 branch
BranchTrueFalseTotal
if (tmpptr = strstr(fixmsg, "\00110=")) { 5 2 7
35 5 len = tmpptr - fixmsg + 1;
36 }
37  
38 13261 ▶ for (idx = 0, cks = 0; idx < len; cks += (unsigned int) fixmsg[idx++]);
Branch: 1 branch
BranchTrueFalseTotal
for (idx = 0, cks = 0; idx < len; cks += (unsigned int) fixmsg[idx++]); 13254 7 13261
39 7 snprintf(cksum, 4, "%03d", (cks % 256));
40  
41 7 RETVAL = cksum;
42 OUTPUT:
43 RETVAL
44  
45 static char *
46 replace_checksum(fixmsg)
47 const char * fixmsg
48 PROTOTYPE: $
49 CODE:
50 char tmpmsg[FIX_MAX_MSG_LENGTH];
51 char *tmpptr;
52 unsigned int len;
53  
54 unsigned int idx;
55 unsigned int cks;
56  
57 /* First remove any trailing checksum form the string */
58 7 len = strlen(fixmsg);
59 /* Check message is properly terminated */
60 8 ▶ if (fixmsg[len-1] != '\001') XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (fixmsg[len-1] != '\001') XSRETURN_UNDEF; 2 5 7
61  
62 5 ▶ if (tmpptr = strstr(fixmsg, "\00110=")) {
Branch: 1 branch
BranchTrueFalseTotal
if (tmpptr = strstr(fixmsg, "\00110=")) { 3 2 5
63 3 ▶ if (len > FIX_MAX_MSG_LENGTH - 1) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (len > FIX_MAX_MSG_LENGTH - 1) XSRETURN_UNDEF; 0 3 3
64 3 len = tmpptr - fixmsg + 1;
65 3 strncpy(tmpmsg, fixmsg, len);
66 /* tmpmsg[len] = '\0'; // Will be null-padded later anyway */
67 } else {
68 2 ▶ if (len > FIX_MAX_MSG_LENGTH - 8) XSRETURN_UNDEF; /* minus 7 checksum bytes and \0 */
Branch: 1 branch
BranchTrueFalseTotal
if (len > FIX_MAX_MSG_LENGTH - 8) XSRETURN_UNDEF; /* minus 7 checksum bytes and \0 */ 1 1 2
69 1 strncpy(tmpmsg, fixmsg, len + 1);
70 }
71  
72 556 ▶ for (idx = 0, cks = 0; idx < len; cks += (unsigned int) tmpmsg[idx++]);
Branch: 1 branch
BranchTrueFalseTotal
for (idx = 0, cks = 0; idx < len; cks += (unsigned int) tmpmsg[idx++]); 552 4 556
73 4 sprintf(tmpmsg + len, "10=%03d\001", (unsigned int) (cks % 256));
74  
75 4 RETVAL = tmpmsg;
76 OUTPUT:
77 RETVAL
78  
79 static int
80 validate_checksum(fixmsg)
81 const char * fixmsg
82 PROTOTYPE: $
83 CODE:
84 char *tmpptr;
85 int checksum;
86 unsigned int len;
87  
88 unsigned int idx;
89 unsigned int cks;
90  
91 /* Find and extract the checksum */
92 11 len = strlen(fixmsg);
93 11 ▶ if (tmpptr = strstr(fixmsg, "\00110=")) {
Branch: 1 branch
BranchTrueFalseTotal
if (tmpptr = strstr(fixmsg, "\00110=")) { 9 2 11
94 9 len = tmpptr - fixmsg + 1;
95 /* Check and get checksum */
96 9 ▶ if (strspn(tmpptr + 4, "0123456789\001") != 4) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (strspn(tmpptr + 4, "0123456789\001") != 4) XSRETURN_UNDEF; 1 8 9
97 8 checksum = atoi(tmpptr + 4);
98 /* checksum must be in range 0-255) */
99 8 ▶ if (checksum >= 256) XSRETURN_UNDEF;
Branch: 1 branch
BranchTrueFalseTotal
if (checksum >= 256) XSRETURN_UNDEF; 1 7 8
100 } else {
101 2 XSRETURN_UNDEF;
102 }
103  
104 9165 ▶ for (idx = 0, cks = 0; idx < len; cks += (unsigned int) fixmsg[idx++]);
Branch: 1 branch
BranchTrueFalseTotal
for (idx = 0, cks = 0; idx < len; cks += (unsigned int) fixmsg[idx++]); 9158 7 9165
105  
106 7 ▶ RETVAL = ((cks % 256) == checksum);
Branch: 1 branch
BranchTrueFalseTotal
RETVAL = ((cks % 256) == checksum); 0 7 7
107 OUTPUT:
108 RETVAL
109  
↑ Summary blib/lib/Net/Fix/Checksum/XS.pm »