http://qs1969.pair.com?node_id=1180095


in reply to Re: Is this a sane/safe way to pass an aref into a C function?
in thread Is this a sane/safe way to pass an aref into a C function?

Hey BrowserUK, thanks for responding.

1) I was casting to int because it was what I had the first time it worked, and hadn't yet done further testing (disclaimer: I am literally learning C as I go through this process).

2) Yep, I have full intention to ensure that each 'byte' will be < 256 && > -1 or so to speak (ie. a proper uint8_t). This is a piece of a much larger scenario, where I'll be tearing apart the bits and manipulating and in many cases ensuring each bit range is within proper bounds (ie.,eg: are bits 5-4 set correctly) and my lack of understanding of a few things likely had me leave out some items. There will be added checks for everything, but I tried to keep the sample small.

3) See this node for the why. Again, I'm just coming into the C/XS/bitwise world, so I may not be explaining myself correctly. Essentially, that node shares that I'm working on something where I need a dynamic number of bytes incoming, without having to change the underlying C code, specifically because in some cases, I'll just want to pass the data straight through, other times, I'll need/want to perform bitwise operations on it.

Really, I summarized this thread into a single problem, where maybe I should have explained it for what it was... looking for a solution to two separate issues while taking up as little time of others as possible ;)

Replies are listed 'Best First'.
Re^3: Is this a sane/safe way to pass an aref into a C function?
by BrowserUk (Patriarch) on Jan 22, 2017 at 23:47 UTC
    I need a dynamic number of bytes incoming, without having to change the underlying C code,

    I'm not quite sure why you think you cannot pass variable length strings in scalars?

    Also, whilst pack is one way to encode unsigned bytes into strings, it is not the only way:

    #! perl -slw use strict; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'END_C', NAME => '_1180095', CLEAN_AFTER_BUILD =>0; int doStuff( SV *sv ) { STRLEN len, i; unsigned char *bytes = SvPVx( sv, len ); printf( "%d : ", len ); // print the hi & lo nybbles, of the unsigned bytes passed, in hex for( i=0; i < len; ++i ) { printf( "%01x %01x\t", ( bytes[ i ] & 0xf0 ) >> 4, bytes[ i ] +& 0x0f ); } printf( "\n" ); return 1; } END_C doStuff( "\x0f\xf0\xaa\x55\x01\x80" ); doStuff( join '', map chr, 0x00 .. 0xff ); doStuff( pack 'C*', 1 .. 10 );

    Output:

    C:\test>1180095.pl 6 : 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0 2 0 3 0 4 0 5 0 6 0 7 0 8 + 0 9 0 a C:\test>

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice.