This Raku expression converts color codes from RGB to HEX:
raku -e 'my #array = (0, 255, 0), { #^a «+» (25.5, -25.5, 0) } ... ( * ~~ (255, 0, 0 ) );
say #array.map: "#" ~ *.fmt: "%02X"'
(#00 FF 00 #19 E5 00 #33 CC 00 #4C B2 00 #66 99 00 #7F 7F 00 #99 66 00 #B2 4C 00 #CC 33 00 #E5 19 00 #FF 00 00)
Adding q{} to the expression removes spaces:
raku -e 'my #array = (0, 255, 0), { #^a «+» (25.5, -25.5, 0) } ... ( * ~~ (255, 0, 0 ) );
say #array.map: "#" ~ *.fmt: "%02X", q{}'
(#00FF00 #19E500 #33CC00 #4CB200 #669900 #7F7F00 #996600 #B24C00 #CC3300 #E51900 #FF0000)
I have not been able to figure out why adding 'q{}' to the expression removes spaces. I would aprreciate any hint on this issue. Thanks.
Per the doc for .fmt applied to a List, the routine's signature is:
method fmt($format = '%s', $separator = ' ' --> Str:D)
▲▲▲ - default separator
So, by default, each three element List like (0, 255, 0) will turn into three formatted elements separated by a space when you don't specify the separator, but unseparated when you specify a null string as the specifier.
q{} is a string (using the Q lang) that specifies a null string (ie it's the same as '').
Hence the result you're seeing.
Related
I have this code on esp32 which get the return of command from an obd adapter
if (receive(buffer, sizeof(buffer)) > 0) {
char *p = buffer;
Serial.print("3 - p:");
Serial.println(p);
Serial.print("31 - size of p : ");
Serial.println(strlen(p));
p = strstr(p, "43 ");
Serial.print("32 - p:");
Serial.println(p);
}
My goal is to substract the first 3 chars "43 " from the buffer
And this is what I get in serial
3 - p:43 01 33 03 01 00 00
>
31 - size of p : 23
32 - p:43 01 33 03 01 00 00
>
33 - size of p : 23
How should I proceed to substract the first "43 " from the buffer ?
Thanks
If you just add an offset to the buffer pointer, so you're in effect skipping three characters, you should be fine:
if (receive(buffer, sizeof(buffer)) > 0) {
char *p = buffer+3;
Serial.print("3 - p:");
Serial.println(p);
}
This should print
3 - p:01 33 03 01 00 00
Is this what you want?
You can use strstr to check if buffer really contains (or starts with) "43 "
If it does, the returned pointer is set to the begin of "43 "
p+3 would point to the characters following "43 ", if this is what you call "subtracting".
If there's no "43 " in buffer, p is NULL, and p+3 points anywhere. Have fun.
I am uploading a file using whatwg-fetch
When I am sending the file to the server everything seems to works without problem, I have the following headers
Content-Length: 6941
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryUZ3MAI9SPh2fjdl3
And the code
export function addImage(image, name) {
return (dispatch, getState) => {
const { user } = getState();
let data = new FormData();
data.append('image', image);
data.append('name', name);
return fetch(generateUrl(`/image?token=${user.token}`), {
method: 'POST',
body: data
})
.then(response => {
return response.json()
.then(json => {
if (!response.ok) return console.error(json);
dispatch(getImages())
});
})
}
}
Then I am receiving the code on the server
router.post('/', securityController.middleware('post_image'), multer().single('image'), (req, res, next) => {
fs.writeFileSync('test.jpeg', req.file.buffer)
imageDataProvider.addImage(req.body.name, req.file)
.then(image => res.json(image))
.catch(next)
})
I have two tests there, one is uploading to s3 and the other one is writing it into test.jpeg
But the uploaded image is corrupted, this is the image I am receiving
{ fieldname: 'image',
originalname: 'download.jpeg',
encoding: '7bit',
mimetype: 'image/jpeg',
buffer:
<Buffer c3 bf c3 98 c3 bf c3 a0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 c3 bf c3 9b 00 c2 84 00 09 06 07 12 12 10 15 0f 10 10 15 10 0f 0f 0f 0f 10 0f ... >,
size: 9783 }
I do not understand why the size is different. The image is corrupted on both s3 and local storage
The server is a serverless (I am havinf the same issue running with serverless offline or directly on lambda)
JPEG files are starting with hex
FF D8 FF E0 00 10 4A 46 49 46 00
it seems, that your "buffer"-string was converted from ISO-8859-1 to UTF-8, because:
FF (ISO-1) = C3 BF (UTF-8)
D8 (ISO-1) = C3 98 (UTF-8)
FF (ISO-1) = C3 BF (UTF-8)
E0 (ISO-1) = C3 A0 (UTF-8)
that's the reason why the size is now different and the jpeg file is corrupted.
disable the CharacterSet converting to UTF-8 (use original binary data)
I need to decompress hex values and convert those to string.
Actual problem is that i'm not able to figure out how to decompress hex values
Hex do not contain any headers,
If i copy hex codes to CyberChef i'm able to decompress those and have original string
In CyberChef only Raw Inflate operation is needed
So i'm hoping help how to do raw inflate in R
I have tried memDecompress using all options without success (i.e gzip etc)
UPDATE:
Here is a sample from hex:
e3 0e 71 0d 0e f1 54 c8 cb 2f 52 30 02 00
which i'm able to convert using CyberChef to string
".TESTI nor 2"
RLdata<- sqlQuery(connection, ..... AS Varbinary(max) AS NOTEShort ......
> RLdata$NOTEshort[4268]
[[1]]
[1] e3 0e 71 0d 0e f1 54 c8 cb 2f 52 30 02 00
> unlist(RLdata$NOTEshort[4268])
[1] e3 0e 71 0d 0e f1 54 c8 cb 2f 52 30 02 00
> memDecompress(unlist(RLdata$NOTEshort[4268]),type = "gzip", asChar = TRUE)
Error in memDecompress(unlist(RLdata$NOTEshort[4268]), type = "gzip", :
internal error -3 in memDecompress(2)
> memDecompress(unlist(RLdata$NOTEshort[4268]),type = "unknown", asChar = TRUE)
[1] "ã\016q\r\016ñTÈË/R0\002"
Warning message:
In memDecompress(unlist(RLdata$NOTEshort[4268]), type = "unknown", :
unknown compression, assuming none
If you convert it into Base64 and then decode it back to Hex I think it decompresses to original, but may have been changed by a bug fix. It used to do this a couple of years back but I haven't used CyberChef in a while, sorry
Had to do this using python3. Zlib.decompress() did the trick.
Link to python solution
Read Dynamics NAV Table Metadata with SQL
How can we decrypt message (if possible) having part of original message, part of encoded text, IV and key? Assume, we have only one block.
Example:
IV = 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
original text =
__ __ __ 00 00 00 00 00 00 00 00 00 00 00 00 00
encripted text = XX YY ZZ __ __ __ __ __ __ __ __ __ __ __ __ __
(we don't know __ bytes, but we know key)
Additionally, known byte count from original and encriptrd texts will always be 16
Edit:
All previous blocks (encrypted) are known. Initial chaining mode is CBC, but we can decode all previous blocks with given key and IV. This would be the last one.
This is why we can assume, that we have only one block and chaining is ECB.
In order to decrypt a portion of CBC mode encrypted data the prior encrypted block is needed. See CBC mode.
On encryption it is the previous block's encrypted data that is xor'ed with the plain text. In the case of the first block it is the IV that is xor'ed with the data.
I'm not sure if this is the right place to ask this question. If not, i will remove it...
Anyway, i have made driver for enc28j60 for LPC1788 and I'm trying to send UDP message to it from iOS. Sending fails.
Communication starts by iOS sending the ARP request. On LPC i make response and send it to iOS MAC address. I have read RFC for ARP and created packet by the specification, but for some reason i dont see the response in network sniffer.
That would for sure indicate that my response routine does not work, if there was not this case when i send udp message from OSX (nc -u xxx.xxx.xxx.xxx port) that comes correctly to LPC.
The difference that i observed is that OSX sends the request which is 42bytes long and gets 60bytes long response with last 18bytes of 0's (padding bytes), and iOS sends the 60byte long request with last 4bytes as 90 eb 58 96.
I tried to respond with all padding zeros, and with copying those 4 bytes, but neither approach worked.
OSX ARP(req, res) and UDP package is visible in sniffer.
iOS ARP req is visible in sniffer and nothing else.
Any idea ?
EDIT:
IOS code:
CFSocketRef sock = CFSocketCreate(kCFAllocatorDefault, PF_INET, SOCK_DGRAM, IPPROTO_UDP, 0, NULL, NULL);
if ( sock == NULL) {
NSLog(#"CfSocketCreate Failed");
}else{
struct sockaddr_in remoteAddress;
memset(&remoteAddress, 0, sizeof(remoteAddress));
remoteAddress.sin_family = AF_INET;
remoteAddress.sin_len = sizeof(remoteAddress);
remoteAddress.sin_port = htons(1200);
remoteAddress.sin_addr.s_addr = inet_addr("192.168.5.8");
char data[] = "test";
NSData *message_data = [NSData dataWithBytes:&data length:4];
NSData* remoteAddressData = [NSData dataWithBytes:&remoteAddress length:sizeof(remoteAddress)];
CFSocketError er = CFSocketSendData(sock, (__bridge CFDataRef)(remoteAddressData), (__bridge CFDataRef)message_data, 0);
}
LPC code:
void make_arp_response (uint8_t *buffer)
{
uint16_t pos = sizeof (ethernet.source)+sizeof (ethernet.dest);
uint8_t type[2];
type[0] = ARP_TYPE>>8;
type[1] = (uint8_t)ARP_TYPE;
memcpy (buffer+pos, &type, sizeof (ethernet.type)); pos = sizeof(ethernet); //set ethernet type
arp.hwType = ((0x01)<<8);
memcpy (buffer+pos, &arp.hwType, sizeof (arp.hwType)); pos += sizeof(arp.hwType);
arp.protocol[0] = 0x08;
arp.protocol[1] = 0x00;
memcpy (buffer+pos, &arp.protocol, sizeof (arp.protocol)); pos += sizeof(arp.protocol);
memcpy (buffer+pos, &arp.macLen, sizeof (arp.macLen)); pos += sizeof(arp.macLen);
memcpy (buffer+pos, &arp.ipLen, sizeof (arp.ipLen)); pos += sizeof(arp.ipLen);
arp.opCode = (0x02) << 8;
memcpy (buffer+pos, &arp.opCode, sizeof (arp.opCode)); pos += sizeof(arp.opCode);
memcpy (buffer+pos, &macaddr, sizeof (arp.sourceMac)); pos += sizeof(arp.sourceMac);
memcpy (buffer+pos, &arp.destIP, sizeof (arp.sourceIp)); pos += sizeof(arp.sourceIp);
memcpy (buffer+pos, &arp.sourceMac, sizeof (arp.destMac)); pos += sizeof(arp.destMac);
memcpy (buffer+pos, &arp.sourceIp, sizeof (arp.destIP));
enc28j60_PacketSend(sizeof (ethernet)+sizeof (arp),buf);
}
UPDATE:
It seems that this is not just an iOS problem, but problem with Wifi clients. I have tried to send UDP from windows laptop over wifi and it also sends the ARP request but response and UDP packet got lost somehow. It seems that my ruter have some problems with my LPC :)
I have also tried to connect Arduino to the same enc28j60 board instead of LPC, and UDP communication works.
Library for arduino is found somewhere on the NET. ARP response in arduino code looks the same to me.
UPDATE 2:
Arp request:
ff ff ff ff ff ff 85 29 fb 0d c2 71 08 06 00 01
08 00 06 04 00 01 85 29 fb 0d c2 71 c0 a8 05 28
00 00 00 00 00 00 c0 a8 05 08 00 00 00 00 00 00
00 00 00 00 00 00 00 00 25 a9 c3
Arp response:
85 29 fb 0d c2 71 7b c3 53 a6 9c 55 08 06 00 01
08 00 06 04 00 02 7b c3 53 a6 9c 55 c0 a8 05 08
85 29 fb 0d c2 71 c0 a8 05 28
This response is dumped from LPC just after sending it on wire. I didnt saw it in sniffer.