Related
I try to geneate CRC4 lookup tables in C++ for the known CRC algorithms CRC-4/INTERLAKEN and CRC-4/ITU. The CRC definitions are as follows:
width=4 poly=0x3 init=0xf refin=false refout=false xorout=0xf check=0xb residue=0x2 name="CRC-4/INTERLAKEN"
width=4 poly=0x3 init=0x0 refin=true refout=true xorout=0x0 check=0x7 residue=0x0 name="CRC-4/G-704"
I've started adapting the code found from the answer here to generate lookup table to compare with tables found in the answer found here. If the bit order does not matter for this code, besides polynomial which is the same what else I need to consider? How different would the codes for INTERLAKEN and ITU algorithms be?
The code:
#include <iomanip>
#include <iostream>
void make_crc_table(unsigned long crcTable[])
{
unsigned long POLYNOMIAL = 0x3;
unsigned long remainder;
unsigned char b = 0;
do
{
remainder = b;
for (unsigned long bit = 8; bit > 0; --bit)
{
if (remainder & 1)
remainder = (remainder >> 1) ^ POLYNOMIAL;
else
remainder = (remainder >> 1);
}
crcTable[(size_t)b] = remainder;
} while (0 != ++b);
}
int main()
{
unsigned long crcTable[256];
make_crc_table(crcTable);
// Print the CRC table
for (size_t i = 0; i < 256; i++)
{
std::cout << "0x";
std::cout << std::setfill('0') << std::setw(2) << std::hex << crcTable[i];
if (i % 16 == 15)
std::cout << "," << std::endl;
else
std::cout << ", ";
}
return 0;
}
The output:
0x00, 0x02, 0x03, 0x01, 0x01, 0x03, 0x02, 0x00, 0x02, 0x00, 0x01, 0x03, 0x03, 0x01, 0x00, 0x02,
0x03, 0x01, 0x00, 0x02, 0x02, 0x00, 0x01, 0x03, 0x01, 0x03, 0x02, 0x00, 0x00, 0x02, 0x03, 0x01,
0x01, 0x03, 0x02, 0x00, 0x00, 0x02, 0x03, 0x01, 0x03, 0x01, 0x00, 0x02, 0x02, 0x00, 0x01, 0x03,
0x02, 0x00, 0x01, 0x03, 0x03, 0x01, 0x00, 0x02, 0x00, 0x02, 0x03, 0x01, 0x01, 0x03, 0x02, 0x00,
0x02, 0x00, 0x01, 0x03, 0x03, 0x01, 0x00, 0x02, 0x00, 0x02, 0x03, 0x01, 0x01, 0x03, 0x02, 0x00,
0x01, 0x03, 0x02, 0x00, 0x00, 0x02, 0x03, 0x01, 0x03, 0x01, 0x00, 0x02, 0x02, 0x00, 0x01, 0x03,
0x03, 0x01, 0x00, 0x02, 0x02, 0x00, 0x01, 0x03, 0x01, 0x03, 0x02, 0x00, 0x00, 0x02, 0x03, 0x01,
0x00, 0x02, 0x03, 0x01, 0x01, 0x03, 0x02, 0x00, 0x02, 0x00, 0x01, 0x03, 0x03, 0x01, 0x00, 0x02,
0x03, 0x01, 0x00, 0x02, 0x02, 0x00, 0x01, 0x03, 0x01, 0x03, 0x02, 0x00, 0x00, 0x02, 0x03, 0x01,
0x00, 0x02, 0x03, 0x01, 0x01, 0x03, 0x02, 0x00, 0x02, 0x00, 0x01, 0x03, 0x03, 0x01, 0x00, 0x02,
0x02, 0x00, 0x01, 0x03, 0x03, 0x01, 0x00, 0x02, 0x00, 0x02, 0x03, 0x01, 0x01, 0x03, 0x02, 0x00,
0x01, 0x03, 0x02, 0x00, 0x00, 0x02, 0x03, 0x01, 0x03, 0x01, 0x00, 0x02, 0x02, 0x00, 0x01, 0x03,
0x01, 0x03, 0x02, 0x00, 0x00, 0x02, 0x03, 0x01, 0x03, 0x01, 0x00, 0x02, 0x02, 0x00, 0x01, 0x03,
0x02, 0x00, 0x01, 0x03, 0x03, 0x01, 0x00, 0x02, 0x00, 0x02, 0x03, 0x01, 0x01, 0x03, 0x02, 0x00,
0x00, 0x02, 0x03, 0x01, 0x01, 0x03, 0x02, 0x00, 0x02, 0x00, 0x01, 0x03, 0x03, 0x01, 0x00, 0x02,
0x03, 0x01, 0x00, 0x02, 0x02, 0x00, 0x01, 0x03, 0x01, 0x03, 0x02, 0x00, 0x00, 0x02, 0x03, 0x01,
Original question ends here.
Update after rcgldr's answer:
#include <iostream>
#include <bitset>
#include <iomanip>
void make_crc_table(unsigned int crcTable[])
{
unsigned char POLYNOMIAL = 0xc;
unsigned char remainder;
unsigned char b = 0;
do
{
remainder = b;
for (int bit = 8; bit > 0; --bit)
{
if (remainder & 0x80)
remainder = (remainder << 1) ^ POLYNOMIAL;
else
remainder = (remainder << 1);
}
crcTable[(size_t)b] = remainder;
} while (0 != ++b);
}
int main()
{
unsigned int crcTable[256];
make_crc_table(crcTable);
for (size_t i = 0; i < 256; i++)
{
std::cout << "0x";
std::cout << std::setfill('0') << std::setw(2) << std::hex << (crcTable[i]);
if (i % 16 == 15)
std::cout << "," << std::endl;
else
std::cout << ", ";
}
return 0;
}
Output of the code:
0x00, 0x0c, 0x18, 0x14, 0x30, 0x3c, 0x28, 0x24, 0x60, 0x6c, 0x78, 0x74, 0x50, 0x5c, 0x48, 0x44,
0xc0, 0xcc, 0xd8, 0xd4, 0xf0, 0xfc, 0xe8, 0xe4, 0xa0, 0xac, 0xb8, 0xb4, 0x90, 0x9c, 0x88, 0x84,
0x8c, 0x80, 0x94, 0x98, 0xbc, 0xb0, 0xa4, 0xa8, 0xec, 0xe0, 0xf4, 0xf8, 0xdc, 0xd0, 0xc4, 0xc8,
0x4c, 0x40, 0x54, 0x58, 0x7c, 0x70, 0x64, 0x68, 0x2c, 0x20, 0x34, 0x38, 0x1c, 0x10, 0x04, 0x08,
0x14, 0x18, 0x0c, 0x00, 0x24, 0x28, 0x3c, 0x30, 0x74, 0x78, 0x6c, 0x60, 0x44, 0x48, 0x5c, 0x50,
0xd4, 0xd8, 0xcc, 0xc0, 0xe4, 0xe8, 0xfc, 0xf0, 0xb4, 0xb8, 0xac, 0xa0, 0x84, 0x88, 0x9c, 0x90,
0x98, 0x94, 0x80, 0x8c, 0xa8, 0xa4, 0xb0, 0xbc, 0xf8, 0xf4, 0xe0, 0xec, 0xc8, 0xc4, 0xd0, 0xdc,
0x58, 0x54, 0x40, 0x4c, 0x68, 0x64, 0x70, 0x7c, 0x38, 0x34, 0x20, 0x2c, 0x08, 0x04, 0x10, 0x1c,
0x28, 0x24, 0x30, 0x3c, 0x18, 0x14, 0x00, 0x0c, 0x48, 0x44, 0x50, 0x5c, 0x78, 0x74, 0x60, 0x6c,
0xe8, 0xe4, 0xf0, 0xfc, 0xd8, 0xd4, 0xc0, 0xcc, 0x88, 0x84, 0x90, 0x9c, 0xb8, 0xb4, 0xa0, 0xac,
0xa4, 0xa8, 0xbc, 0xb0, 0x94, 0x98, 0x8c, 0x80, 0xc4, 0xc8, 0xdc, 0xd0, 0xf4, 0xf8, 0xec, 0xe0,
0x64, 0x68, 0x7c, 0x70, 0x54, 0x58, 0x4c, 0x40, 0x04, 0x08, 0x1c, 0x10, 0x34, 0x38, 0x2c, 0x20,
0x3c, 0x30, 0x24, 0x28, 0x0c, 0x00, 0x14, 0x18, 0x5c, 0x50, 0x44, 0x48, 0x6c, 0x60, 0x74, 0x78,
0xfc, 0xf0, 0xe4, 0xe8, 0xcc, 0xc0, 0xd4, 0xd8, 0x9c, 0x90, 0x84, 0x88, 0xac, 0xa0, 0xb4, 0xb8,
0xb0, 0xbc, 0xa8, 0xa4, 0x80, 0x8c, 0x98, 0x94, 0xd0, 0xdc, 0xc8, 0xc4, 0xe0, 0xec, 0xf8, 0xf4,
0x70, 0x7c, 0x68, 0x64, 0x40, 0x4c, 0x58, 0x54, 0x10, 0x1c, 0x08, 0x04, 0x20, 0x2c, 0x38, 0x34,
The bit order matters. The question's code is using reflected input and output. That means the polynomial should be bit reversed from 0x03 to 0x0c. To confirm this, table entry [0x80] should be 0x0c.
For the Interlaken table, the code should look like:
void make_crc_table(unsigned char crcTable[])
{
unsigned char POLYNOMIAL = 0x30;
unsigned char remainder;
unsigned char b = 0;
do
{
remainder = b;
for (int bit = 8; bit > 0; --bit)
{
if (remainder & 0x80)
remainder = (remainder << 1) ^ POLYNOMIAL;
else
remainder = (remainder << 1);
}
crcTable[(size_t)b] = remainder;
} while (0 != ++b);
}
Note that the CRC will be in the upper 4 bits of a byte. When done, the code will need to return (crc>>4)^0x0f.
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
For a product I am co-designing I want to display bitmap icons on an LCD screen. Inspired by among others this tutorial I created a separate C-file with the byte array of the icon I am trying to display.
When I try to display the icon directly it seems to work. But when I try to do it indirectly - which is going to be nessecary eventually - it fails.
What really baffles me and what I seek to understand is; when I print out the array of the garbage icon (CHAR ARRAY) to the serial monitor and compare it to the byte array it is suppose to represent they match correctly.
But when I do the same for the image that is displayed correctly (ICON ARRAY), what I print to the serial monitor does not match the byte array it is displaying, the information I printed is not even consistent... How can this be ?!
I have been cracking my head over this for a week now so any help would be greatly appreciated.
The main file:
// --------------------------------------------------------------------------------------
// | This program is made for an arduino mega 2560 used in conjunction with the |
// | (chinese) OPEN-SMART 3.2 inch TFT LCD Shield ILI9327 |
// | This version of the program is a slimmed down one to clearly convey the problem |
// | I am currenlty having drawing pictures on this setup |
// --------------------------------------------------------------------------------------
// This program is based on the library obtained from
// https://drive.google.com/open?id=0B6uNNXJ2z4CxYktCQlViUkI1Sms
// There is also a demo available on YouTube
// https://www.youtube.com/watch?v=JQHGYnKeC0M&t=2s
#include <Adafruit_GFX.h> // Core graphics library
#include <MCUFRIEND_kbv.h>
MCUFRIEND_kbv tft;
// -------------------------------------------------------------------------------------
// | Variable declaration |
// -------------------------------------------------------------------------------------
// Set colors
#define BLACK 0x0000
#define BACKGROUND 0x07FF
// No idea with this does tbh
uint16_t g_identifier;
// Spacing variables
uint8_t cube_size = 75;
uint8_t padding = 3;
uint8_t ix1 = 5+2 * padding;
uint8_t ix2 = 5+3 * padding + cube_size;
uint8_t ix3 = 5+4 * padding + 2 * cube_size;
uint8_t ix4 = 5+5 * padding + 3 * cube_size;
uint16_t ix5 = 5+(6 * padding) + 4 * cube_size;
uint8_t iy1 = 5+1 * padding;
uint8_t iy2 = 5+3 * padding + cube_size;
uint8_t iy3 = 5+4 * padding + 2 * cube_size;
//Progmem Icon init
extern uint8_t icon_from_progmem[];
// -------------------------------------------------------------------------------------
// | Functions |
// -------------------------------------------------------------------------------------
// Standard drawbitmap function
void drawBitmap(int16_t x, int16_t y, const uint8_t bitmap[], int16_t w, int16_t h, uint16_t color) {
int16_t byteWidth = (w + 7) / 8; // Bitmap scanline pad = whole byte
uint8_t byte = 0;
for (int16_t j = 0; j < h; j++, y++) {
for (int16_t i = 0; i < w; i++) {
if (i & 7) byte <<= 1;
else byte = pgm_read_byte(&bitmap[j * byteWidth + i / 8]);
if (byte & 0x80) tft.drawPixel(x + i, y, color);
}
}
}
// -------------------------------------------------------------------------------------
// | Setup |
// -------------------------------------------------------------------------------------
// I just copied this part to get the screen to work
void setup(void) {
Serial.begin(9600);
Serial.println(F("TFT LCD test"));
// tft.reset(); //we can't read ID on 9341 until begin()
g_identifier = tft.readID(); //
Serial.print("ID = 0x");
Serial.println(g_identifier, HEX);
if (g_identifier == 0x00D3 || g_identifier == 0xD3D3) g_identifier = 0x9481; // write-only shield
if (g_identifier == 0xFFFF) g_identifier = 0x9341; // serial
// g_identifier = 0x9329; // force ID
tft.begin(g_identifier); //to enable ILI9327 driver code
tft.setRotation(3); //Change orientation (landscape and portrait)
}
// -----------------------------------------------------------------------------------
// | Loop |
// -----------------------------------------------------------------------------------
void loop(void) {
tft.fillScreen(BACKGROUND); //
unsigned char myCharArray[512];
byte myChar;
for (int k = 0; k < 512; k++) {
// Create myCharArray from icon_from_progmem
myChar = pgm_read_byte(icon_from_progmem + k);
myCharArray[k] = char(myChar);
// Print data from myCharArry to the serial monitor
Serial.print("CHAR ARRAY: ");
Serial.print(k);
Serial.print(" ");
Serial.println(myChar);
}
// Visual separtion string
Serial.println("------------------------------------------------------------------");
// Print data from icon_from_progmem to serial monitor
for (int k=0; k<512; k++){
Serial.print("ICONS ARRAY: ");
Serial.print(k);
Serial.print(" ");
Serial.println(icon_from_progmem[k]);
}
// Draw the two icons on the screen
drawBitmap(ix2, iy1, myCharArray, 64, 64, BLACK);
drawBitmap(ix4, iy1, icon_from_progmem, 64, 64, BLACK);
delay(600000);
} // end of void loop
The separate file for the bitmap:
#include <avr/pgmspace.h>
// 'PlafLamp', 64x64px
const unsigned char icon_from_progmem [] PROGMEM = {
0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x07, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x7f, 0xfe, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xff, 0xff, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x0f, 0xff, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xff, 0xff, 0xf8, 0x00, 0x00,
0x00, 0x00, 0x7f, 0xc0, 0x03, 0xfe, 0x00, 0x00, 0x00, 0x00, 0xfe, 0x00, 0x00, 0x7f, 0x00, 0x00,
0x00, 0x01, 0xfc, 0x00, 0x00, 0x3f, 0x80, 0x00, 0x00, 0x03, 0xf0, 0x00, 0x00, 0x0f, 0xc0, 0x00,
0x00, 0x07, 0xe0, 0x00, 0x00, 0x07, 0xe0, 0x00, 0x00, 0x0f, 0xc0, 0x00, 0x00, 0x03, 0xf0, 0x00,
0x00, 0x0f, 0x80, 0x00, 0x00, 0x01, 0xf0, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x00, 0x00, 0xf8, 0x00,
0x00, 0x1e, 0x00, 0x00, 0x00, 0x00, 0x78, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x00, 0x00, 0x7c, 0x00,
0x00, 0x3c, 0x00, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00, 0x7c, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00,
0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00, 0x00, 0x78, 0x00, 0x00, 0x00, 0x00, 0x1e, 0x00,
0x00, 0xf8, 0x00, 0x00, 0x00, 0x00, 0x1f, 0x00, 0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00,
0x00, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x0f, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00,
0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xfe, 0x00,
0x00, 0x00, 0x07, 0xc0, 0x03, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x03, 0xc0, 0x00, 0x00,
0x00, 0x00, 0x03, 0xe0, 0x07, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x01, 0xf8, 0x1f, 0x80, 0x00, 0x00,
0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x3f, 0xfc, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x1f, 0xf8, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x1c, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x00, 0x3e, 0x00, 0x00, 0x7c, 0x00, 0x00,
0x00, 0x00, 0x7e, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x00, 0x00, 0xfc, 0x01, 0x80, 0x3f, 0x00, 0x00,
0x00, 0x01, 0xf8, 0x03, 0xc0, 0x1f, 0x80, 0x00, 0x00, 0x03, 0xf0, 0x03, 0xc0, 0x0f, 0xc0, 0x00,
0x00, 0x07, 0xe0, 0x03, 0xc0, 0x07, 0xe0, 0x00, 0x00, 0x07, 0xc0, 0x03, 0xc0, 0x03, 0xe0, 0x00,
0x00, 0x07, 0x80, 0x03, 0xc0, 0x01, 0xe0, 0x00, 0x00, 0x03, 0x00, 0x03, 0xc0, 0x00, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x80, 0x00, 0x00, 0x00
};
What is displayed on the screen
The image on the left is the CHAR ARRAY and the image on the right is the ICON ARRAY.
in drawBitmap() you are iterating through bytes via pgm_read_byte()
In the avr-libc docs it says
Read a byte from the program space with a 16-bit (near) address.
Note:
The address is a byte address. The address is in the program space.
Your array that is a copy of the bitmap, "myCharArray", is not in program space so pgm_read_byte() does not operate correctly.
Below is the data i've got from wireshark and not able to crack it.
char peer0_13[] = {
0x02, 0xb6, 0x8d, 0x7c, 0x7a, 0x56, 0x2f, 0x06,
0xde, 0x6c, 0xd9, 0x55, 0x78, 0xd3, 0x12, 0xf9,
0xe5, 0x4a, 0x57, 0x02, 0xdc, 0xb7, 0x1c, 0x75,
0x5b, 0x9c, 0x51, 0x51, 0x3e, 0x2a, 0x3a, 0x9a,
0xca, 0x50, 0xeb, 0x40, 0x28, 0x05, 0x00, 0xa0,
0x14, 0x02, 0x80, 0x50, 0x0a, 0x01, 0x40, 0x28,
0x05, 0x00, 0xa0, 0x14, 0x02, 0x80, 0x50, 0x0a,
0x01, 0x40, 0x28, 0x05, 0x00, 0xa0, 0x14, 0x02,
0x80, 0x50, 0x0a, 0x01, 0x40, 0x28, 0x05, 0x00,
0xa0, 0x14, 0x02, 0x80, 0x50, 0x0a, 0x01, 0x40,
0x28, 0x05, 0x00, 0xa0, 0x14, 0x02, 0x80, 0x50,
0x0a, 0x01, 0x40, 0x28, 0x05, 0x00, 0xa0, 0x14,
0x02, 0x80, 0x50, 0x0a, 0x01, 0x40, 0x28, 0x05,
0x00, 0xa0, 0x14, 0x02, 0x80, 0x50, 0x0a, 0x01,
0x40, 0x28, 0x05, 0x00, 0xa0, 0x14, 0x02, 0x80,
0x50, 0x0a, 0x01, 0x40, 0x28, 0x05, 0x00, 0xa0,
0x14, 0x02, 0x80, 0x50, 0x0a, 0x01, 0x40, 0x28,
0x05, 0x00, 0xa0, 0x14, 0x02, 0x80, 0x50, 0x0a,
0x01, 0x40, 0x28, 0x05, 0x00, 0xa0, 0x14, 0x02,
0x80, 0x50, 0x0a, 0x01, 0x40, 0x28, 0x05, 0x00,
0xa0, 0x3f, 0xff, 0xd9 };
I am not getting that how to convert this data into plain text / string so that it is easy to read.
Here are some manual tricks i've applied to get in plain text.
(First solution is not working properly, as it gives me garbage values).
#1.
for (int i = 0; i <= sizeof (peer0_13); i++)
{
printf ("%c",peer0_13[i]);
}
#2.
cout << peer0_13 << endl ;
#3.
printf ("%s\n", peer0_13);
Please help to convert this data into normal string.
Any help would be grateful. :)
You can not convert the above char vector into String since I see 0x00 at the end of the line before the last. char[] is a String but if you need to copy its data into a normal string then you have to copy all of the chars to an empty string one by one using a loop, but the 0x00 value will truncate the string.
Note that in your first loop you need to change the condition to i < sizeof (peer0_13) and eleminate the equal sign which raises an index out of bound error.
If you meant to read the char array as a String that a human could understand and you need to just print it out, You have to exclude the null characters (0x00) while copying it into a String or erase null character from your array and simply use %s with a printf.
I want to convert my C#(managed) decryption method into Android NDK, C/C++ (NO JAVA)
I see there was crypto. on the JAVA side but I want to keep away from any JNI, and I also see there's mcrypt and crypt++ but cant find a compiled lib for android.
Here an example in C# which I want to translate, to c/c++
public byte[] DecryptBytes(byte[] encryptedBytes)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
RijndaelCipher.Mode = CipherMode.CBC;
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(Bytes32_KEY, Bytes16_IV);
MemoryStream memoryStream = new MemoryStream(encryptedBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
byte[] plainBytes = new byte[encryptedBytes.Length];
int DecryptedCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length);
memoryStream.Close();
cryptoStream.Close();
return plainBytes;
};
UPDATE
So the best I've found so far is to use openSSL AES, I have downloaded a pre-compiled lib for Android, I'm just struggling to get it work with the example some already posted as working here is the c code example
void test_enc(){
int keylength = 256;
// // 256bit KEY
uint8_t key[32] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F,
0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F};
//128bit IV
uint8_t iv[16] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F};
//input data
uint8_t input[64] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07};
size_t inputslength = 10;
int x;
uint8_t *aes_key = key;
uint8_t *aes_input = input;
uint8_t *iv_enc = iv;
uint8_t *iv_dec = iv;
// buffers for encryption and decryption
const size_t encslength = ((inputslength + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
uint8_t *enc_out = (uint8_t*)malloc(sizeof(uint8_t) *encslength);
uint8_t *dec_out = (uint8_t*)malloc(sizeof(uint8_t) *inputslength);
memset(enc_out, 0, encslength);
memset(dec_out, 0, inputslength);
// so i can do with this aes-cbc-128 aes-cbc-192 aes-cbc-256
AES_KEY enc_key, dec_key;
AES_set_encrypt_key(aes_key, keylength, &enc_key);
AES_cbc_encrypt(input, enc_out, inputslength, &enc_key, iv_enc, AES_ENCRYPT);
AES_set_decrypt_key(aes_key, keylength, &dec_key);
AES_cbc_encrypt(enc_out, dec_out, encslength, &dec_key, iv_dec, AES_DECRYPT);
LOGI("Before:");
for(x=0;x<inputslength;x++)
LOGI("%02x, ", input[x]);
LOGI("Encrypted:");
for(x=0;x<encslength;x++)
LOGI("%02x, ", enc_out[x]);
LOGI("Decrypted:");
for(x=0;x<encslength;x++)
LOGI("%02x, ", dec_out[x]);
};
The encrypted bytes aren't the same as the c# and then the decrypt doesn't go back to the input, where have I gone wrong ?
SOLVED:
Issue is it appears the array which held the IV gets altered after the encryption so you need to reset back before you decrypt for the result
Using the pre-built OpenSSL for Android you can find here OpenSSL-for-Android-Prebuilt
and the code above just remember to set the IV before each call to AES_cbc_encrypt.
OpenSSL is quite a big library (if you care about APK size)
You can use Tiny AES in C (can be used with C++)
I have added it to Android project (CMake)
https://github.com/anonym24/Android-Tiny-AES-NDK
I have three array, like this, that contains my bitmap images:
static unsigned char __attribute__ ((progmem)) impostazioni_logo[]={
0x00, 0x02, 0x7E, 0x02, 0x00, 0x00, 0x78, 0x10, 0x08, 0x08, 0x08, 0x70, 0x10, 0x08, 0x08, 0x08,
0x70, 0x00, 0x00, 0x78, 0x10, 0x08, 0x08, 0x08, 0x10, 0x60, 0x00, 0x00, 0x60, 0x10, 0x08, 0x08,
0x08, 0x10, 0x60, 0x00, 0x00, 0x30, 0x48, 0x48, 0x08, 0x08, 0x10, 0x00, 0x00, 0x08, 0x7E, 0x08,
0x08, 0x08, 0x00, 0x00, 0x50, 0x48, 0x48, 0x48, 0x70, 0x00, 0x00, 0x08, 0x08, 0x08, 0x48, 0x28,
0x18, 0x00, 0x00, 0x7A, 0x00, 0x00, 0x60, 0x10, 0x08, 0x08, 0x08, 0x10, 0x60, 0x00, 0x00, 0x78,
0x10, 0x08, 0x08, 0x08, 0x08, 0x70, 0x00, 0x00, 0x7A, 0x00, 0x00, 0x08, 0x0F, 0x08, 0x00, 0x00,
0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x7F, 0x08, 0x08,
0x08, 0x08, 0x04, 0x03, 0x00, 0x00, 0x03, 0x04, 0x08, 0x08, 0x08, 0x04, 0x03, 0x00, 0x00, 0x04,
0x08, 0x08, 0x09, 0x09, 0x06, 0x00, 0x00, 0x00, 0x07, 0x08, 0x08, 0x08, 0x00, 0x07, 0x08, 0x08,
0x08, 0x04, 0x0F, 0x00, 0x00, 0x0C, 0x0A, 0x09, 0x08, 0x08, 0x08, 0x00, 0x00, 0x0F, 0x00, 0x00,
0x03, 0x04, 0x08, 0x08, 0x08, 0x04, 0x03, 0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x0F,
0x00, 0x00, 0x0F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
Now i want a function return the correct array to display on lcd by passing the page parameter.
unsigned char logo(int page){
char buffer[32];
switch(page){
case 1:
for(int i=0;i<sizeof(impostazioni_logo);i++){
strcpy_P(buffer,pgm_read_byte(&(impostazioni_logo[i]))); //<==pgm_read_byte comes from here:http://www.arduino.cc/en/Reference/PROGMEM
}
break;
}
return buffer;
}
it doesn't work. Compiler tell me something wrong about conversion.
EDIT:
caller is a simply function that draw the right image. Images can be different for different pages. Number of pages are near 20:
void drawLogo(){
glcd.drawbitmap(15,1, logo(), 90,16); //display lcd library for ST7565
}
There are a few issues with this code:
return type for logo is unsigned char while you are returning char *
pgm_read_byte supposedly returns a byte, so you could simply do buffer[i]=pgm_read_byte(...)
buffer that you are trying to return is allocated on the stack and will not exist after function returns.
You should probably be using strlcpy_P instead.
Update:
1. Assuming you have a fixed number of pages. Try creating a bitmap per page, like:
static unsigned char __attribute__ ((progmem)) impostazioni_logo_page1[]={..}
2. return a pointer to each pages' logo:
unsigned char* logo(int page)
{
switch(page)
{
case 1:
return impostazioni_logo_page1;
break;
}
return NULL;
}
If you like to have all bitmaps in a single array, calculate an offset in the array and return that instead:
int offset = page_num*page_size_in_chars;
return &impostazioni_logo_all_pages[offset];
Update 2: Another option to manage pages:
static unsigned char* pages[] = { impostazioni_logo_page1, impostazioni_logo_page2, ... }
...
glcd.drawbitmap(15,1, pages[page_index], 90,16);