How can I handle packed data from Perl/PHP in C++? - php

I got a problem implementing a PHP programm in C++. It is about the PHP/Perl function unpack. I don't know how to do the follwing in C++ (no problem in reading a file... but how do i unpack("C*") the read contents).
<?php
$file = fopen("bitmaskt.dat", "rb");
//create the data stream
$matrix_x = unpack("C*", fread($file, 286));
$matrix_y = unpack("C*", fread($file, 286));
$mask_data = unpack("C*", fread($file, 286));
$reed_ecc_codewords = ord(fread($file, 1));
$reed_blockorder = unpack("C*", fread($file, 128));
fclose($file);
?>
Currently, I'm very hopeless solving this problem on my own - I'm searching for days, all I found are questions... Is there any free unpack() c++ implementation out there? :-(

Perl's documentation for pack covers the templates used for pack and unpack.
Say you generated bitmaskt.dat with
#! /usr/bin/perl
use warnings;
use strict;
open my $fh, ">", "bitmaskt.dat" or die "$0: open: $!";
my #data = (42) x 286;
print $fh pack("C*" => #data);
print $fh pack("C*" => #data);
print $fh pack("C*" => #data);
print $fh pack("C" => 7);
print $fh pack("C*" => (1) x 128);
close $fh or warn "$0: close";
You might read it with
#include <fstream>
#include <iostream>
#include <iterator>
#include <vector>
typedef unsigned char datum_t;
typedef std::vector<datum_t> buf_t;
std::istream &read_data(std::istream &in, buf_t &buf, size_t n)
{
std::istreambuf_iterator<char> it(in.rdbuf()), eos;
while (it != eos && n-- != 0)
buf.push_back(static_cast<datum_t>(*it++));
return in;
}
For example:
int main()
{
std::ifstream bm("bitmaskt.dat", std::ifstream::binary | std::ifstream::in);
struct {
buf_t buf;
size_t len;
std::string name;
} sections[] = {
{ buf_t(), 286, "matrix_x" },
{ buf_t(), 286, "matrix_y" },
{ buf_t(), 286, "mask_data" },
{ buf_t(), 1, "reed_ecc_codewords" },
{ buf_t(), 128, "reed_blockorder" },
};
const int n = sizeof(sections) / sizeof(sections[0]);
for (int i = 0; n - i > 0; i++) {
if (!read_data(bm, sections[i].buf, sections[i].len)) {
std::cerr << "Read " << sections[i].name << " failed" << std::endl;
return 1;
}
}
const int codeword = 3;
std::cout << (unsigned int) sections[codeword].buf[0] << '\n';
return 0;
}
Output:
7

I don't know about any general implementation of unpack for C++, but that doesn't seem to be the thing you need anyway.
if matrix_x is defined somewhere as unsigned char matrix_x[286] and you have an opened input stream inFile
then what you need to do is inFile.get(matrix_x, 286). This reads 286 bytes from the input and places them in the array pointed to by matrix_x.

Related

OPENSSL Blowfish CBC encryption differs from PHP to C++

I am trying to encrypt and decrypt a communication between a C++ library and a PHP server using OPENSSL library in both of them. I want to use the Blowfish CBC algorithm but it seems that the results are different between the C++ code and the PHP code. The C++ code is taken from here:
This is the PHP code:
<?php
function strtohex($x)
{
$s='';
foreach (str_split($x) as $c) $s.=sprintf("%02X",ord($c));
return($s);
}
$encryptedMessage = openssl_encrypt("input", "BF-CBC", "123456", 0, "initvect");
echo $encryptedMessage;
echo "\n";
echo strtohex($encryptedMessage);
The PHP output is this:
x9jDa2WMwvQ=
78396A446132574D7776513D
This is the c++ code:
bool do_encrypt(const char *in, unsigned char *out, int *outlen, unsigned char *key, unsigned char *iv)
{
int buflen, tmplen;
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), nullptr, key, iv);
if (!EVP_EncryptUpdate(&ctx, out, &buflen, (unsigned char*)in, strlen(in)))
{
return false;
}
if (!EVP_EncryptFinal_ex(&ctx, out + buflen, &tmplen))
{
return false;
}
buflen += tmplen;
*outlen = buflen;
EVP_CIPHER_CTX_cleanup(&ctx);
return true;
}
unsigned char output[2048] = { 0 };
int outLen;
auto result = do_encrypt("input", output, &outLen, (unsigned char*)"123456", (unsigned char*)"initvect");
BIGNUM *outputStr = BN_new();
BN_bin2bn(output, outLen, outputStr);
cout << base64_encode(output, outLen) << "\n";
cout << BN_bn2hex(outputStr) << "\n";
The C++ output is this:
EfRhhWqGmSQ=
11F461856A869924
Can someone please help me understand what I'm doing wrong? Any help will be very much appreciated.
Thanks!
Edit 1:
I managed to fix the C++ code after jww's answer and it worked well. I was missing the EVP_CIPHER_CTX_set_key_length However, I couldn't make the PHP code return the same thing and eventually we decided to move to AES and it now works flawlessly. Thanks!
For your OpenSSL code, I believe you need to call EVP_CIPHER_CTX_set_key_length to tell the library the key is only 6 bytes.
Let me throw Crypto++ into the arena below. OpenSSL and Crypto++ will converge on the right answer once you add the missing EVP_CIPHER_CTX_set_key_length OpenSSL call. The right answer is 32CEBA7E046431EB (in hex).
I don't know what's going on with PHP:
x9jDa2WMwvQ=
78396A446132574D7776513D
Considering x is ASCII 0x78, 9 is ASCII 0x39, I'm guessing you hex encoded the Base64 string.
$ cat test.cxx
#include "blowfish.h"
#include "modes.h"
#include "channels.h"
#include "filters.h"
#include "base64.h"
#include "hex.h"
using namespace CryptoPP;
#include <iostream>
#include <string>
using namespace std;
int main(int argc, char* argv[])
{
const byte key[] = "123456"; // 6
const byte iv[] = "initvect"; // 8
CBC_Mode<Blowfish>::Encryption encryptor;
encryptor.SetKeyWithIV(key, 6, iv, 8);
string r1, r2;
ChannelSwitch chsw;
Base64Encoder e1(new StringSink(r1));
HexEncoder e2(new StringSink(r2));
chsw.AddDefaultRoute(e1);
chsw.AddDefaultRoute(e2);
string msg = "input";
StringSource ss(msg, true,
new StreamTransformationFilter(encryptor,
new Redirector(chsw)));
cout << r1 << endl;
cout << r2 << endl;
return 0;
}
The test program results in:
$ ./test.exe
Ms66fgRkMes=
32CEBA7E046431EB
Here's the OpenSSL portion of things. Notice EVP_EncryptInit_ex is called twice. First, EVP_EncryptInit_ex is called to set the block cipher EVP_bf_cbc. The key length is set with EVP_CIPHER_CTX_set_key_length. Then second, EVP_EncryptInit_ex is called to set the key and iv.
#include <openssl/evp.h>
#include <iostream>
#include <iomanip>
#include <stdexcept>
using namespace std;
typedef unsigned char byte;
int main()
{
EVP_CIPHER_CTX ctx;
EVP_CIPHER_CTX_init(&ctx);
int rc;
const byte key[] = "123456"; // 6
const byte iv[] = "initvect"; // 8
rc = EVP_EncryptInit_ex(&ctx, EVP_bf_cbc(), NULL, 0, 0);
if (rc != 1)
throw runtime_error("EVP_EncryptInit_ex failed");
rc = EVP_CIPHER_CTX_set_key_length(&ctx, 6);
if (rc != 1)
throw runtime_error("EVP_CIPHER_CTX_set_key_length failed");
rc = EVP_EncryptInit_ex(&ctx, NULL, NULL, key, iv);
if (rc != 1)
throw runtime_error("EVP_EncryptInit_ex failed");
const byte msg[] = "input";
byte buf[32];
int len1 = sizeof(buf), len2 = sizeof(buf);
rc = EVP_EncryptUpdate(&ctx, buf, &len1, msg, 5);
if (rc != 1)
throw runtime_error("EVP_EncryptUpdate failed");
rc = EVP_EncryptFinal_ex(&ctx, buf+len1, &len2);
if (rc != 1)
throw runtime_error("EVP_EncryptFinal_ex failed");
for(unsigned int i=0; i<len1+len2; i++)
cout << std::hex << setw(2) << setfill('0') << (int)buf[i];
cout << endl;
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}

ZLIB inflate give 'data error' in PHP

I've got a file that has zlib deflated blocks of 4096 bytes. I'm able to inflate at least 1 block of 4096 bytes with C++, using Minzip's inflate implementation, without garbled text or data error.
I'm using the following C++ implementation to inflate the data:
#define DEC_BUFFER_LEN 20000
int main(int argc, char* argv[]) {
FILE *file = fopen("unpackme.3di", "rb");
char *buffer = new char[4096];
std::fstream outputFile;
outputFile.open("output.txt", std::ios_base::out | std::ios_base::trunc | std::ios_base::binary);
// Data zit nu in de buffer
char *decbuffer = new char[DEC_BUFFER_LEN];
mz_streamp streampie = new mz_stream();
streampie->zalloc = Z_NULL;
streampie->zfree = Z_NULL;
streampie->opaque = Z_NULL;
streampie->avail_in = Z_NULL;
streampie->next_in = Z_NULL;
if (inflateInit(streampie) != Z_OK)
return -1;
fread(buffer, 1, 4096, file);
streampie->next_in = (Byte *)&buffer[0];
streampie->avail_in = 4096;
streampie->next_out = (Byte *)&decbuffer[0];
streampie->avail_out = DEC_BUFFER_LEN;
streampie->total_out = 0;
int res = inflate(streampie, Z_NO_FLUSH);
if (res != Z_OK && res != Z_STREAM_END) {
std::cout << "Error: " << streampie->msg << std::endl;
return;
}
outputFile.write(decbuffer, streampie->total_out); // Write data to file
fclose(file);
inflateEnd(streampie);
outputFile.flush();
outputFile.close();
getchar();
return 0;
}
and I'm using the following PHP implementation:
function Unpack3DI($inputFilename) {
$handle = fopen($inputFilename, 'rb');
if ($handle === false) return null;
$data = gzinflate(fread($handle, 4096));
return $data;
}
var_dump(Unpack3DI('unpackme.3di'));
Result:
Warning: gzinflate() [function.gzinflate]: data error in /var/www/html/3di.php on line 9
bool(false)
The issue was that I used the wrong function. I had to use gzuncompress instead of gzinflate.
Also, pushing the whole file in gzuncompress did the job very well actually, as zlib checks if there are remaining blocks to be uncompressed.
More information about the Zlib methods in PHP are answered in this answer to "Which compression method to use in PHP?".

c++ equivalent for php shell_exec

What will be the C++ equivalemt command for below mentioned php command:
$command = shell_exec("sqlldr {$connect_string} control={$ctl_file_name} log={$log_file_name}");
So based on your comments a solution that would work would be to use popen(3):
#include <cstdio>
#include <iostream>
#include <string>
int main()
{
// Set file names based on your input etc... just using dummies below
std::string
ctrlFileName = "file1",
logFileName = "file2",
cmd = "sqlldr usr/pwd#LT45 control=" + ctrlFileName + " log=" + logFileName ;
std::cout << "Executing Command: " << cmd << std::endl ;
FILE* pipe = popen(cmd.c_str(), "r");
if (pipe == NULL)
{
return -1;
}
char buffer[128];
std::string result = "";
while(!feof(pipe))
{
if(fgets(buffer, 128, pipe) != NULL)
{
result += buffer;
}
}
std::cout << "Results: " << std::endl << result << std::endl ;
pclose(pipe);
}
Try forkpty, you get a file descriptor which you can use to read from the other pseudoterminal.

Sending binary commands from PHP to Arduino powered thermal printer

I am having some fun playing around with an Arduino (Uno rev 3) and a thermal printer (this model https://www.sparkfun.com/products/10438). The Arduino makes a request every 10 seconds to my local machine (via an Ethernet shield) and stores the response (if 200) on an SD card. It then prints this out using this library https://github.com/adafruit/Adafruit-Thermal-Printer-Library .
So far I have it correctly polling, storing and printing basic text but now I'm trying to use some of the more advanced commands (underline, inverse etc). My ultimate goal is to send images down and handle all of the rendering on the server ala http://printer.gofreerange.com/ .
The problem is that the commands I am sending are been outputted as text characters. Some commands work (line feed), but others are garbled. I have attached both the Arduino code and the basic PHP script it is calling. Any help?
Arduino:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
#include <SoftwareSerial.h>
#include "Adafruit_Thermal.h"
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
const char host[] = "192.168.1.100";
char cacheFilename[] = "TMP";
const byte printer_RX_Pin = 8; // this is the green wire
const byte printer_TX_Pin = 9; // this is the yellow wire
const byte SD_Pin = 4; // the SD Card SPI pin
bool downloadWaiting = false;
bool statusOk = false;
unsigned long content_length = 0;
EthernetClient client;
Adafruit_Thermal printer(printer_RX_Pin, printer_TX_Pin);
void die(unsigned int times) {
while(true);
}
void checkForDownload() {
Serial.println("checkForDownload");
content_length = 0;
statusOk = false;
unsigned long length = 0;
if (SD.exists(cacheFilename)) {
if (!SD.remove(cacheFilename)) {
die(4);
}
}
File cache = SD.open(cacheFilename, FILE_WRITE);
if(client.connect(host, 80)) {
client.println("GET /printer.php HTTP/1.1");
client.print("Host: "); client.println(host);
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
bool parsingHeader = true;
while(client.connected()) {
while(client.available()) {
if (parsingHeader) {
client.find((char*)"HTTP/1.1 ");
char statusCode[] = "000";
client.readBytes(statusCode, 3);
statusOk = (strcmp(statusCode, "200") == 0);
client.find((char*)"Content-Length: ");
char c;
while (isdigit(c = client.read())) {
content_length = (content_length * 10) + (c - '0');
}
client.find((char*)"\n\r\n");
parsingHeader = false;
} else {
if(length < content_length) {
cache.write((byte)client.read());
length++;
} else {
client.read();
}
}
}
}
client.stop();
cache.seek(0);
if (statusOk && content_length > 0 && (content_length == length) && (content_length == cache.size())) {
downloadWaiting = true;
}
} else {
client.stop();
}
cache.close();
}
void printFromDownload() {
Serial.println("printFromDownload");
File cache = SD.open(cacheFilename);
byte b;
while (content_length--) {
printer.write((byte)cache.read());
}
printer.feed();
cache.close();
downloadWaiting = false;
}
void setup(){
pinMode(SD_Pin, OUTPUT);
if (!SD.begin(SD_Pin)) {
die(2);
}
if (Ethernet.begin(mac) == 0) {
die(3);
}
Serial.begin(9600);
printer.begin(255);
delay(1000);
}
void loop() {
if (downloadWaiting) {
printFromDownload();
delay(5000);
} else {
checkForDownload();
if (!downloadWaiting) {
delay(10000);
}
}
}
PHP:
<?php
ob_start();
// Turn on Inverse mode
// Doesn't work
echo pack('S', 29);
echo pack('S', 66);
echo pack('S', 1);
$string = 'Testing 1, 2, 3';
foreach(str_split($string) as $char) {
echo pack('S', ord($char)); // works
}
// Turn off Inverse mode
echo pack('S', 29);
echo pack('S', 66);
echo pack('S', 0);
// Line feed
echo pack('S', 10); // works
$content = ob_get_clean();
$length = strlen($content);
header("Content-Length: $length");
echo $content;
It seems that you can't print bitmap data directly with printer.write(). The printer expects some special bytes to turn on bitmap printing mode as you can see in the printBitmap() method. (writeBytes(18, 42, chunkHeight, rowBytesClipped))
void Adafruit_Thermal::printBitmap(
int w, int h, const uint8_t *bitmap, bool fromProgMem) {
int rowBytes, rowBytesClipped, rowStart, chunkHeight, x, y, i;
rowBytes = (w + 7) / 8; // Round up to next byte boundary
rowBytesClipped = (rowBytes >= 48) ? 48 : rowBytes; // 384 pixels max width
for(i=rowStart=0; rowStart < h; rowStart += 255) {
// Issue up to 255 rows at a time:
chunkHeight = h - rowStart;
if(chunkHeight > 255) chunkHeight = 255;
writeBytes(18, 42, chunkHeight, rowBytesClipped);
for(y=0; y < chunkHeight; y++) {
for(x=0; x < rowBytesClipped; x++, i++) {
PRINTER_PRINT(fromProgMem ? pgm_read_byte(bitmap + i) : *(bitmap+i));
}
i += rowBytes - rowBytesClipped;
}
timeoutSet(chunkHeight * dotPrintTime);
}
prevByte = '\n';
}
Your sketch will need to understand the data coming from the PHP and know when to send individual characters as bytes with printer.write() and when to send bytes as an image with printer.printBitmap(). This way the printer is receiving the proper commands to prep it for printing the appropriate data. You will need to construct some metadata around what you want to print in PHP and send that to the Arduino. A JSON format might look like this:
{"reciept": [
{
"type": "text",
"style": "bold",
"value": "Thank you for your purchase"
},
{
"type": "bitmap",
"pos": "center",
"value": ".... binary data ..."
}
]}
Now your Arduino sketch will understand when to send bytes individually as text and when to send a lot of data as a bitmap.
A more compact format might use line feeds as a break between segments:
F|bold
T|Thank you for shopping with us\r
P|Center
B|...binary data (with \r escaped)... \r
Or, you can send the amount of data with each segment to avoid escaping binary data much like the Content-Length header of HTTP
F4|boldT32|Thank you for shopping with us\rP6|CenterB3000|...binary data...

How to read PHP's -gzcompress- data from c++

This is my c++ code:
#include <QtCore/QCoreApplication>
#include <QtCore/QDebug>
#include <QFile>
#include <QByteArray>
QByteArray UnZip (QString zipfilename)
{
QFile infile(zipfilename);
infile.open(QIODevice::ReadOnly);
//QByteArray uncompressedData = infile.readAll();
QByteArray uncompressedData = qUncompress(infile.readAll());
infile.close();
return uncompressedData;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
//QString path = "/Users/davide/Desktop/fh8RkbUf";
QString path = "/Users/davide/Desktop/test";
QByteArray data = UnZip(path);
qDebug() << "message";
return a.exec();
}
; that returns qUncompress: Z_DATA_ERROR: Input data is corrupted
However, data should be zlib compressed and php's gzuncompress($data) works fine.
Also, cat file | uncompress returns errors.
The code php side is
$data = gzcompress($data, 6);
$success = file_put_contents($file, $data);

Categories