@@ -82,7 +82,7 @@ SEXP rnng_serial(SEXP mode) {
8282SEXP nano_encode (SEXP object ) {
8383
8484 R_xlen_t xlen = Rf_xlength (object );
85- unsigned char * buf ;
85+ void * buf ;
8686 size_t sz ;
8787 SEXP out ;
8888
@@ -105,25 +105,25 @@ SEXP nano_encode(SEXP object) {
105105 } else {
106106 switch (TYPEOF (object )) {
107107 case REALSXP :
108- buf = ( unsigned char * ) REAL (object );
108+ buf = REAL (object );
109109 sz = xlen * sizeof (double );
110110 out = Rf_allocVector (RAWSXP , sz );
111111 memcpy (RAW (out ), buf , sz );
112112 break ;
113113 case INTSXP :
114- buf = ( unsigned char * ) INTEGER (object );
114+ buf = INTEGER (object );
115115 sz = xlen * sizeof (int );
116116 out = Rf_allocVector (RAWSXP , sz );
117117 memcpy (RAW (out ), buf , sz );
118118 break ;
119119 case LGLSXP :
120- buf = ( unsigned char * ) LOGICAL (object );
120+ buf = LOGICAL (object );
121121 sz = xlen * sizeof (int );
122122 out = Rf_allocVector (RAWSXP , sz );
123123 memcpy (RAW (out ), buf , sz );
124124 break ;
125125 case CPLXSXP :
126- buf = ( unsigned char * ) COMPLEX (object );
126+ buf = COMPLEX (object );
127127 sz = xlen * (sizeof (double ) + sizeof (double ));
128128 out = Rf_allocVector (RAWSXP , sz );
129129 memcpy (RAW (out ), buf , sz );
@@ -478,7 +478,7 @@ SEXP rnng_matchargs(SEXP mode) {
478478
479479}
480480
481- SEXP nano_decode (unsigned char * buf , size_t sz , const int mod , const int kpr ) {
481+ SEXP nano_decode (void * buf , size_t sz , const int mod , const int kpr ) {
482482
483483 int tryErr = 0 ;
484484 SEXP raw , data ;
@@ -491,7 +491,6 @@ SEXP nano_decode(unsigned char *buf, size_t sz, const int mod, const int kpr) {
491491 data = R_tryEval (expr , R_BaseEnv , & tryErr );
492492 if (tryErr ) {
493493 data = raw ;
494- raw = R_NilValue ;
495494 }
496495 UNPROTECT (2 );
497496 } else if (mod == 2 ) {
@@ -506,26 +505,62 @@ SEXP nano_decode(unsigned char *buf, size_t sz, const int mod, const int kpr) {
506505 SETLENGTH (data , m );
507506 UNPROTECT (1 );
508507 } else {
508+ size_t size ;
509509 switch (mod ) {
510510 case 3 :
511- data = Rf_allocVector (CPLXSXP , sz / (sizeof (double ) + sizeof (double )));
512- memcpy (COMPLEX (data ), buf , sz );
511+ size = sizeof (double ) + sizeof (double );
512+ if (sz % size == 0 ) {
513+ data = Rf_allocVector (CPLXSXP , sz / size );
514+ memcpy (COMPLEX (data ), buf , sz );
515+ } else {
516+ Rf_warning ("received data could not be converted to complex" );
517+ data = Rf_allocVector (RAWSXP , sz );
518+ memcpy (RAW (data ), buf , sz );
519+ }
513520 break ;
514521 case 4 :
515- data = Rf_allocVector (REALSXP , sz / sizeof (double ));
516- memcpy (REAL (data ), buf , sz );
522+ size = sizeof (double );
523+ if (sz % size == 0 ) {
524+ data = Rf_allocVector (REALSXP , sz / size );
525+ memcpy (REAL (data ), buf , sz );
526+ } else {
527+ Rf_warning ("received data could not be converted to double" );
528+ data = Rf_allocVector (RAWSXP , sz );
529+ memcpy (RAW (data ), buf , sz );
530+ }
517531 break ;
518532 case 5 :
519- data = Rf_allocVector (INTSXP , sz / sizeof (int ));
520- memcpy (INTEGER (data ), buf , sz );
533+ size = sizeof (int );
534+ if (sz % size == 0 ) {
535+ data = Rf_allocVector (INTSXP , sz / size );
536+ memcpy (INTEGER (data ), buf , sz );
537+ } else {
538+ Rf_warning ("received data could not be converted to integer" );
539+ data = Rf_allocVector (RAWSXP , sz );
540+ memcpy (RAW (data ), buf , sz );
541+ }
521542 break ;
522543 case 6 :
523- data = Rf_allocVector (LGLSXP , sz / sizeof (int ));
524- memcpy (LOGICAL (data ), buf , sz );
544+ size = sizeof (int );
545+ if (sz % size == 0 ) {
546+ data = Rf_allocVector (LGLSXP , sz / size );
547+ memcpy (LOGICAL (data ), buf , sz );
548+ } else {
549+ Rf_warning ("received data could not be converted to logical" );
550+ data = Rf_allocVector (RAWSXP , sz );
551+ memcpy (RAW (data ), buf , sz );
552+ }
525553 break ;
526554 case 7 :
527- data = Rf_allocVector (REALSXP , sz / sizeof (double ));
528- memcpy (REAL (data ), buf , sz );
555+ size = sizeof (double );
556+ if (sz % size == 0 ) {
557+ data = Rf_allocVector (REALSXP , sz / size );
558+ memcpy (REAL (data ), buf , sz );
559+ } else {
560+ Rf_warning ("received data could not be converted to numeric" );
561+ data = Rf_allocVector (RAWSXP , sz );
562+ memcpy (RAW (data ), buf , sz );
563+ }
529564 break ;
530565 case 8 :
531566 data = Rf_allocVector (RAWSXP , sz );
@@ -606,7 +641,7 @@ void context_finalizer(SEXP xptr) {
606641
607642SEXP rnng_protocol_open (SEXP protocol ) {
608643
609- const int pro = * INTEGER (protocol );
644+ const int pro = INTEGER (protocol )[ 0 ] ;
610645 nng_socket * sock ;
611646 char * pname ;
612647 int xc ;
@@ -944,7 +979,7 @@ SEXP rnng_send(SEXP socket, SEXP data, SEXP block, SEXP echo) {
944979 nng_socket * sock = (nng_socket * ) R_ExternalPtrAddr (socket );
945980
946981 const nng_duration blk = (nng_duration ) Rf_asInteger (block );
947- const int ech = * LOGICAL (echo );
982+ const int ech = LOGICAL (echo )[ 0 ] ;
948983 int xc ;
949984 nng_msg * msgp ;
950985 nng_aio * aiop ;
@@ -993,19 +1028,19 @@ SEXP rnng_recv(SEXP socket, SEXP mode, SEXP block, SEXP keep) {
9931028 nng_socket * sock = (nng_socket * ) R_ExternalPtrAddr (socket );
9941029
9951030 mode = rnng_matcharg (mode );
996- const int mod = * INTEGER (mode ), kpr = * LOGICAL (keep );
1031+ const int mod = INTEGER (mode )[ 0 ] , kpr = LOGICAL (keep )[ 0 ] ;
9971032 int xc ;
9981033 void * buf ;
9991034 size_t sz ;
10001035 nng_aio * aiop ;
10011036 SEXP res ;
10021037
10031038 if (TYPEOF (block ) == LGLSXP ) {
1004- const int blk = * LOGICAL (block );
1039+ const int blk = LOGICAL (block )[ 0 ] ;
10051040 xc = blk ? nng_recv (* sock , & buf , & sz , 1u ): nng_recv (* sock , & buf , & sz , 3u );
10061041 if (xc )
10071042 return mk_error (xc );
1008- res = nano_decode (( unsigned char * ) buf , sz , mod , kpr );
1043+ res = nano_decode (buf , sz , mod , kpr );
10091044 nng_free (buf , sz );
10101045
10111046 } else {
@@ -1024,7 +1059,7 @@ SEXP rnng_recv(SEXP socket, SEXP mode, SEXP block, SEXP keep) {
10241059 nng_msg * msgp = nng_aio_get_msg (aiop );
10251060 buf = nng_msg_body (msgp );
10261061 sz = nng_msg_len (msgp );
1027- res = nano_decode (( unsigned char * ) buf , sz , mod , kpr );
1062+ res = nano_decode (buf , sz , mod , kpr );
10281063 nng_msg_free (msgp );
10291064 nng_aio_free (aiop );
10301065 }
@@ -1039,14 +1074,14 @@ SEXP rnng_ctx_send(SEXP context, SEXP data, SEXP timeout, SEXP echo) {
10391074 error_return ("'con' is not a valid Context" );
10401075 nng_ctx * ctxp = (nng_ctx * ) R_ExternalPtrAddr (context );
10411076
1042- const int ech = * LOGICAL (echo );
1077+ const int ech = LOGICAL (echo )[ 0 ] ;
10431078 nng_duration dur ;
10441079 int xc ;
10451080 nng_msg * msgp ;
10461081 nng_aio * aiop ;
10471082
10481083 if (TYPEOF (timeout ) == LGLSXP ) {
1049- const int blk = * LOGICAL (timeout );
1084+ const int blk = LOGICAL (timeout )[ 0 ] ;
10501085 dur = blk ? -2 : 0 ;
10511086 } else {
10521087 dur = (nng_duration ) Rf_asInteger (timeout );
@@ -1089,14 +1124,14 @@ SEXP rnng_ctx_recv(SEXP context, SEXP mode, SEXP timeout, SEXP keep) {
10891124 nng_duration dur ;
10901125
10911126 mode = rnng_matcharg (mode );
1092- const int mod = * INTEGER (mode ), kpr = * LOGICAL (keep );
1127+ const int mod = INTEGER (mode )[ 0 ] , kpr = LOGICAL (keep )[ 0 ] ;
10931128 int xc ;
10941129 void * buf ;
10951130 size_t sz ;
10961131 SEXP res ;
10971132
10981133 if (TYPEOF (timeout ) == LGLSXP ) {
1099- const int blk = * LOGICAL (timeout );
1134+ const int blk = LOGICAL (timeout )[ 0 ] ;
11001135 dur = blk ? -2 : 0 ;
11011136 } else {
11021137 dur = (nng_duration ) Rf_asInteger (timeout );
@@ -1118,7 +1153,7 @@ SEXP rnng_ctx_recv(SEXP context, SEXP mode, SEXP timeout, SEXP keep) {
11181153 nng_msg * msgp = nng_aio_get_msg (aiop );
11191154 buf = nng_msg_body (msgp );
11201155 sz = nng_msg_len (msgp );
1121- res = nano_decode (( unsigned char * ) buf , sz , mod , kpr );
1156+ res = nano_decode (buf , sz , mod , kpr );
11221157 nng_msg_free (msgp );
11231158 nng_aio_free (aiop );
11241159
@@ -1132,14 +1167,14 @@ SEXP rnng_stream_send(SEXP stream, SEXP data, SEXP timeout, SEXP echo) {
11321167 error_return ("'con' is not a valid Stream" );
11331168 nng_stream * sp = (nng_stream * ) R_ExternalPtrAddr (stream );
11341169
1135- const int ech = * LOGICAL (echo );
1170+ const int ech = LOGICAL (echo )[ 0 ] ;
11361171 nng_duration dur ;
11371172 int xc ;
11381173 nng_iov iov ;
11391174 nng_aio * aiop ;
11401175
11411176 if (TYPEOF (timeout ) == LGLSXP ) {
1142- const int blk = * LOGICAL (timeout );
1177+ const int blk = LOGICAL (timeout )[ 0 ] ;
11431178 dur = blk ? -2 : 0 ;
11441179 } else {
11451180 dur = (nng_duration ) Rf_asInteger (timeout );
@@ -1148,7 +1183,7 @@ SEXP rnng_stream_send(SEXP stream, SEXP data, SEXP timeout, SEXP echo) {
11481183 const R_xlen_t xlen = Rf_xlength (enc );
11491184 unsigned char * dp = RAW (enc );
11501185
1151- const int frames = * LOGICAL (Rf_getAttrib (stream , nano_TextframesSymbol ));
1186+ const int frames = LOGICAL (Rf_getAttrib (stream , nano_TextframesSymbol ))[ 0 ] ;
11521187 iov .iov_len = frames == 1 ? xlen - 1 : xlen ;
11531188 iov .iov_buf = dp ;
11541189
@@ -1186,7 +1221,7 @@ SEXP rnng_stream_recv(SEXP stream, SEXP mode, SEXP timeout, SEXP keep, SEXP byte
11861221 nng_stream * sp = (nng_stream * ) R_ExternalPtrAddr (stream );
11871222
11881223 mode = rnng_matchargs (mode );
1189- const int mod = * INTEGER (mode ), kpr = * LOGICAL (keep );
1224+ const int mod = INTEGER (mode )[ 0 ] , kpr = LOGICAL (keep )[ 0 ] ;
11901225 const size_t xlen = (size_t ) Rf_asInteger (bytes );
11911226 nng_duration dur ;
11921227 int xc ;
@@ -1196,7 +1231,7 @@ SEXP rnng_stream_recv(SEXP stream, SEXP mode, SEXP timeout, SEXP keep, SEXP byte
11961231 SEXP res ;
11971232
11981233 if (TYPEOF (timeout ) == LGLSXP ) {
1199- const int blk = * LOGICAL (timeout );
1234+ const int blk = LOGICAL (timeout )[ 0 ] ;
12001235 dur = blk ? -2 : 0 ;
12011236 } else {
12021237 dur = (nng_duration ) Rf_asInteger (timeout );
@@ -1230,7 +1265,7 @@ SEXP rnng_stream_recv(SEXP stream, SEXP mode, SEXP timeout, SEXP keep, SEXP byte
12301265 }
12311266
12321267 sz = nng_aio_count (aiop );
1233- res = nano_decode (( unsigned char * ) iov .iov_buf , sz , mod , kpr );
1268+ res = nano_decode (iov .iov_buf , sz , mod , kpr );
12341269 nng_aio_free (aiop );
12351270 R_Free (iov .iov_buf );
12361271
0 commit comments