How to find 32bit CRC for a file in objective C - objective-c
I want to find out CRC for a bin file in my iOS application. Is there already a function which will accept filename as input and return 32 bit CRC value? Please share me if any code available.
You can use this file I modeled on the ios common crypto library functions:
crc32.h
#ifndef sessionTest_crc32_h
#define sessionTest_crc32_h
// Standard library
#include <stdint.h>
#include <stdio.h>
#define CC_CRC32_DIGEST_LENGTH 4
typedef struct {
uint32_t crc;
} CC_CRC32_CTX;
static uint32_t crc32_tbl[256] =
{
0x00000000L, 0x77073096L, 0xEE0E612CL, 0x990951BAL, 0x076DC419L,
0x706AF48FL, 0xE963A535L, 0x9E6495A3L, 0x0EDB8832L, 0x79DCB8A4L,
0xE0D5E91EL, 0x97D2D988L, 0x09B64C2BL, 0x7EB17CBDL, 0xE7B82D07L,
0x90BF1D91L, 0x1DB71064L, 0x6AB020F2L, 0xF3B97148L, 0x84BE41DEL,
0x1ADAD47DL, 0x6DDDE4EBL, 0xF4D4B551L, 0x83D385C7L, 0x136C9856L,
0x646BA8C0L, 0xFD62F97AL, 0x8A65C9ECL, 0x14015C4FL, 0x63066CD9L,
0xFA0F3D63L, 0x8D080DF5L, 0x3B6E20C8L, 0x4C69105EL, 0xD56041E4L,
0xA2677172L, 0x3C03E4D1L, 0x4B04D447L, 0xD20D85FDL, 0xA50AB56BL,
0x35B5A8FAL, 0x42B2986CL, 0xDBBBC9D6L, 0xACBCF940L, 0x32D86CE3L,
0x45DF5C75L, 0xDCD60DCFL, 0xABD13D59L, 0x26D930ACL, 0x51DE003AL,
0xC8D75180L, 0xBFD06116L, 0x21B4F4B5L, 0x56B3C423L, 0xCFBA9599L,
0xB8BDA50FL, 0x2802B89EL, 0x5F058808L, 0xC60CD9B2L, 0xB10BE924L,
0x2F6F7C87L, 0x58684C11L, 0xC1611DABL, 0xB6662D3DL, 0x76DC4190L,
0x01DB7106L, 0x98D220BCL, 0xEFD5102AL, 0x71B18589L, 0x06B6B51FL,
0x9FBFE4A5L, 0xE8B8D433L, 0x7807C9A2L, 0x0F00F934L, 0x9609A88EL,
0xE10E9818L, 0x7F6A0DBBL, 0x086D3D2DL, 0x91646C97L, 0xE6635C01L,
0x6B6B51F4L, 0x1C6C6162L, 0x856530D8L, 0xF262004EL, 0x6C0695EDL,
0x1B01A57BL, 0x8208F4C1L, 0xF50FC457L, 0x65B0D9C6L, 0x12B7E950L,
0x8BBEB8EAL, 0xFCB9887CL, 0x62DD1DDFL, 0x15DA2D49L, 0x8CD37CF3L,
0xFBD44C65L, 0x4DB26158L, 0x3AB551CEL, 0xA3BC0074L, 0xD4BB30E2L,
0x4ADFA541L, 0x3DD895D7L, 0xA4D1C46DL, 0xD3D6F4FBL, 0x4369E96AL,
0x346ED9FCL, 0xAD678846L, 0xDA60B8D0L, 0x44042D73L, 0x33031DE5L,
0xAA0A4C5FL, 0xDD0D7CC9L, 0x5005713CL, 0x270241AAL, 0xBE0B1010L,
0xC90C2086L, 0x5768B525L, 0x206F85B3L, 0xB966D409L, 0xCE61E49FL,
0x5EDEF90EL, 0x29D9C998L, 0xB0D09822L, 0xC7D7A8B4L, 0x59B33D17L,
0x2EB40D81L, 0xB7BD5C3BL, 0xC0BA6CADL, 0xEDB88320L, 0x9ABFB3B6L,
0x03B6E20CL, 0x74B1D29AL, 0xEAD54739L, 0x9DD277AFL, 0x04DB2615L,
0x73DC1683L, 0xE3630B12L, 0x94643B84L, 0x0D6D6A3EL, 0x7A6A5AA8L,
0xE40ECF0BL, 0x9309FF9DL, 0x0A00AE27L, 0x7D079EB1L, 0xF00F9344L,
0x8708A3D2L, 0x1E01F268L, 0x6906C2FEL, 0xF762575DL, 0x806567CBL,
0x196C3671L, 0x6E6B06E7L, 0xFED41B76L, 0x89D32BE0L, 0x10DA7A5AL,
0x67DD4ACCL, 0xF9B9DF6FL, 0x8EBEEFF9L, 0x17B7BE43L, 0x60B08ED5L,
0xD6D6A3E8L, 0xA1D1937EL, 0x38D8C2C4L, 0x4FDFF252L, 0xD1BB67F1L,
0xA6BC5767L, 0x3FB506DDL, 0x48B2364BL, 0xD80D2BDAL, 0xAF0A1B4CL,
0x36034AF6L, 0x41047A60L, 0xDF60EFC3L, 0xA867DF55L, 0x316E8EEFL,
0x4669BE79L, 0xCB61B38CL, 0xBC66831AL, 0x256FD2A0L, 0x5268E236L,
0xCC0C7795L, 0xBB0B4703L, 0x220216B9L, 0x5505262FL, 0xC5BA3BBEL,
0xB2BD0B28L, 0x2BB45A92L, 0x5CB36A04L, 0xC2D7FFA7L, 0xB5D0CF31L,
0x2CD99E8BL, 0x5BDEAE1DL, 0x9B64C2B0L, 0xEC63F226L, 0x756AA39CL,
0x026D930AL, 0x9C0906A9L, 0xEB0E363FL, 0x72076785L, 0x05005713L,
0x95BF4A82L, 0xE2B87A14L, 0x7BB12BAEL, 0x0CB61B38L, 0x92D28E9BL,
0xE5D5BE0DL, 0x7CDCEFB7L, 0x0BDBDF21L, 0x86D3D2D4L, 0xF1D4E242L,
0x68DDB3F8L, 0x1FDA836EL, 0x81BE16CDL, 0xF6B9265BL, 0x6FB077E1L,
0x18B74777L, 0x88085AE6L, 0xFF0F6A70L, 0x66063BCAL, 0x11010B5CL,
0x8F659EFFL, 0xF862AE69L, 0x616BFFD3L, 0x166CCF45L, 0xA00AE278L,
0xD70DD2EEL, 0x4E048354L, 0x3903B3C2L, 0xA7672661L, 0xD06016F7L,
0x4969474DL, 0x3E6E77DBL, 0xAED16A4AL, 0xD9D65ADCL, 0x40DF0B66L,
0x37D83BF0L, 0xA9BCAE53L, 0xDEBB9EC5L, 0x47B2CF7FL, 0x30B5FFE9L,
0xBDBDF21CL, 0xCABAC28AL, 0x53B39330L, 0x24B4A3A6L, 0xBAD03605L,
0xCDD70693L, 0x54DE5729L, 0x23D967BFL, 0xB3667A2EL, 0xC4614AB8L,
0x5D681B02L, 0x2A6F2B94L, 0xB40BBE37L, 0xC30C8EA1L, 0x5A05DF1BL,
0x2D02EF8DL
};
static inline int CC_CRC32_Init(CC_CRC32_CTX *c) {
c->crc = 0xFFFFFFFFL;
return 1;
}
static inline int CC_CRC32_Update(CC_CRC32_CTX *c, const uint8_t *data, uint32_t len) {
for(uint32_t i=0; i<len;i++)
c->crc = (c->crc>>8) ^ crc32_tbl[(c->crc&0xFF) ^ *data++];
return 1;
}
static inline int CC_CRC32_Final(unsigned char *md, CC_CRC32_CTX *c) {
c->crc = c->crc ^ 0xFFFFFFFFL;
md[0] = (c->crc & 0xff000000UL) >> 24;
md[1] = (c->crc & 0x00ff0000UL) >> 16;
md[2] = (c->crc & 0x0000ff00UL) >> 8;
md[3] = (c->crc & 0x000000ffUL) ;
return 1;
}
#endif
And an example to use it:
// Declare needed variables and buffers
CFStringRef result = NULL;
CFReadStreamRef readStream = NULL;
unsigned char digest[CC_CRC32_DIGEST_LENGTH];
char hash[2 * CC_CRC32_DIGEST_LENGTH + 1];
int chunkSizeForReadingData = 4096;
CC_CRC32_CTX ctx;
// Get the file URL
CFURLRef fileURL = //something...
// Create and open the read stream
readStream = CFReadStreamCreateWithFile(kCFAllocatorDefault,
(CFURLRef)fileURL);
bool didSucceed = (bool)CFReadStreamOpen(readStream);
// Initialize the hash object
CC_CRC32_Init(&ctx);
// Feed the data
bool hasMoreData = true;
while (hasMoreData) {
uint8_t buffer[chunkSizeForReadingData];
CFIndex readBytesCount = CFReadStreamRead(readStream,
(UInt8 *)buffer,
(CFIndex)sizeof(buffer));
if (readBytesCount == -1) break;
if (readBytesCount == 0) {
hasMoreData = false;
continue;
}
CC_CRC32_Update(&ctx, (const void*)buffer, (CC_LONG)readBytesCount);
}
// Compute the digest
CC_CRC32_Final(digest, &c);
// Compute the string result
for (size_t i = 0; i < chunkSizeForReadingData; ++i) {
snprintf(hash + (2 * i), 3, "%02x", (int)(digest[i]));
}
result = CFStringCreateWithCString(kCFAllocatorDefault,
(const char *)hash,
kCFStringEncodingUTF8);
But I would mostly recommend you used this Gist:
https://gist.github.com/paul-delange/6808278
Related
MLX90621 no Acknowlegde with the RAM
First off all I am a beginner regarding embedded programming. For an application I do want to use the MLX90621 thermo pixel array togehter with an STM32G431KB. On the Melexis Website is an example Code with some sort off abstraction. I moddified the lowlevel part of the code to work with the HAL library of the MCU. For some reason I can read the EEPROM and write to it, get an acknowledge... But when trying to read from the adress 0x60, the RAM, where the sensorvalues are stored i do not get an acknowledge. I do have checkt with an logic analyer and I am sending the correct messages. Just for refference I do have added the code part of the read function. Has anybody an idea regarding some very dump timing error or something like that. P.S. Allready tried an different sensor of my order with the exact same result. int MLX90621_I2CRead(uint8_t slaveAddr,uint8_t command, uint8_t startAddress, uint8_t addressStep, uint8_t nMemAddressRead, uint16_t *data) { uint8_t sa = slaveAddr << 1; int cnt = 0; int i = 0; uint8_t cmd[4] = {0,0,0,0}; uint8_t i2cData[132] = {0}; uint16_t *p; p = data; cmd[0] = command; cmd[1] = startAddress; cmd[2] = addressStep; cmd[3] = nMemAddressRead; if (HAL_I2C_Master_Transmit(&hi2c2, sa, cmd, 4, HAL_MAX_DELAY) != HAL_OK) return -1; HAL_Delay(1); //sa = sa | 0x01; //ack = i2c.read(sa, i2cData, 2*nMemAddressRead, 0); if (HAL_I2C_Master_Receive(&hi2c2, sa, i2cData, 2*nMemAddressRead, HAL_MAX_DELAY) != HAL_OK) return -1; for (cnt = 0; cnt < nMemAddressRead; cnt++) { i = cnt << 1; *p++ = (uint16_t)i2cData[i+1]*256 + (uint16_t)i2cData[i]; } return 0; }
How to link the libsvm library in google colab when executing CUDA? What is the proper linking flag for libsvm?
I am working on google colab and i want to use libsvm library in my project. I downloaded libsvm and installed it. Now when i use !nvcc -o command and run the code using CUDA i am getting errors like, undefined reference to `svm_get_nr_class undefined reference to 'svm_predict_probability' undefined reference to `svm_free_and_destroy_model I guess the problem is that libsvm is not properly linked, As i use -l with proper flags to compile with nvcc, but i don't know what to use with -l to properly link libsvm and use it. i downloaded libsvm using !git clone https://github.com/cjlin1/libsvm %cd libsvm/ !make && make install %cd /content/libsvm/python/ !make import sys sys.path.append('/content/libsvm/python') %cd /content now when i run this program %%cuda --name Blind_Deblurring_Cuda.cu #include <iostream> #include <fstream> #include <iostream> #include <fstream> #include "/content/brisque.h" #include "/content/libsvm/svm.h" #include <vector> #include <stdio.h> #include "fstream" #include "iostream" #include <algorithm> #include <iterator> #include <cmath> #include<stdlib.h> #include <math.h> #include <curand.h> #include <opencv2/core/cuda.hpp> #include <opencv2/core.hpp> #include "opencv2/imgproc.hpp" #include "opencv2/imgcodecs.hpp" #include <opencv2/core/core.hpp> #include <iostream> #include "opencv2/highgui.hpp" #include <opencv2/core/utility.hpp> //rescaling based on training data i libsvm float rescale_vector[36][2]; using namespace std; using namespace cv; float computescore(string imagename); void ComputeBrisqueFeature(Mat& orig, vector<double>& featurevector); int read_range_file() { //check if file exists char buff[100]; int i; string range_fname = "allrange"; FILE* range_file = fopen(range_fname.c_str(), "r"); if(range_file == NULL) return 1; //assume standard file format for this program fgets(buff, 100, range_file); fgets(buff, 100, range_file); //now we can fill the array for(i = 0; i < 36; ++i) { float a, b, c; fscanf(range_file, "%f %f %f", &a, &b, &c); rescale_vector[i][0] = b; rescale_vector[i][1] = c; } return 0; } int main(int argc, char** argv) { if(argc < 2) { cout << "Input Image argument not given." << endl; return -1; } //read in the allrange file to setup internal scaling array if(read_range_file()) { cerr<<"unable to open allrange file"<<endl; return -1; } float qualityscore; qualityscore = computescore(argv[1]); cout << "Quality Score: " << qualityscore << endl; } float computescore(string imagename) { // pre-loaded vectors from allrange file float min_[36] = {0.336999 ,0.019667 ,0.230000 ,-0.125959 ,0.000167 ,0.000616 ,0.231000 ,-0.125873 ,0.000165 ,0.000600 ,0.241000 ,-0.128814 ,0.000179 ,0.000386 ,0.243000 ,-0.133080 ,0.000182 ,0.000421 ,0.436998 ,0.016929 ,0.247000 ,-0.200231 ,0.000104 ,0.000834 ,0.257000 ,-0.200017 ,0.000112 ,0.000876 ,0.257000 ,-0.155072 ,0.000112 ,0.000356 ,0.258000 ,-0.154374 ,0.000117 ,0.000351}; float max_[36] = {9.999411, 0.807472, 1.644021, 0.202917, 0.712384, 0.468672, 1.644021, 0.169548, 0.713132, 0.467896, 1.553016, 0.101368, 0.687324, 0.533087, 1.554016, 0.101000, 0.689177, 0.533133, 3.639918, 0.800955, 1.096995, 0.175286, 0.755547, 0.399270, 1.095995, 0.155928, 0.751488, 0.402398, 1.041992, 0.093209, 0.623516, 0.532925, 1.042992, 0.093714, 0.621958, 0.534484}; double qualityscore; int i; struct svm_model* model; // create svm model object Mat orig = imread(imagename, 1); // read image (color mode) vector<double> brisqueFeatures; // feature vector initialization ComputeBrisqueFeature(orig, brisqueFeatures); // compute brisque features // use the pre-trained allmodel file string modelfile = "allmodel"; //if((model=svm_load_model(modelfile.c_str()))==0) { //fprintf(stderr,"can't open model file allmodel\n"); // exit(1); //} // float min_[37]; // float max_[37]; struct svm_node x[37]; // rescale the brisqueFeatures vector from -1 to 1 // also convert vector to svm node array object for(i = 0; i < 36; ++i) { float min = min_[i]; float max = max_[i]; x[i].value = -1 + (2.0/(max - min) * (brisqueFeatures[i] - min)); x[i].index = i + 1; } x[36].index = -1; int nr_class=svm_get_nr_class(model); double *prob_estimates = (double *) malloc(nr_class*sizeof(double)); // predict quality score using libsvm class qualityscore = svm_predict_probability(model,x,prob_estimates); free(prob_estimates); svm_free_and_destroy_model(&model); return qualityscore; } void ComputeBrisqueFeature(Mat& orig, vector<double>& featurevector) { Mat orig_bw_int(orig.size(), CV_64F, 1); // convert to grayscale cvtColor(orig, orig_bw_int, COLOR_BGR2GRAY); // create a copy of original image Mat orig_bw(orig_bw_int.size(), CV_64FC1, 1); orig_bw_int.convertTo(orig_bw, 1.0/255); orig_bw_int.release(); // orig_bw now contains the grayscale image normalized to the range 0,1 int scalenum = 2; // number of times to scale the image for (int itr_scale = 1; itr_scale<=scalenum; itr_scale++) { // resize image Size dst_size(orig_bw.cols/cv::pow((double)2, itr_scale-1), orig_bw.rows/pow((double)2, itr_scale-1)); Mat imdist_scaled; resize(orig_bw, imdist_scaled, dst_size, 0, 0, INTER_CUBIC); // INTER_CUBIC imdist_scaled.convertTo(imdist_scaled, CV_64FC1, 1.0/255.0); // calculating MSCN coefficients // compute mu (local mean) Mat mu(imdist_scaled.size(), CV_64FC1, 1); GaussianBlur(imdist_scaled, mu, Size(7, 7), 1.166); Mat mu_sq; cv::pow(mu, double(2.0), mu_sq); //compute sigma (local sigma) Mat sigma(imdist_scaled.size(), CV_64FC1, 1); cv::multiply(imdist_scaled, imdist_scaled, sigma); GaussianBlur(sigma, sigma, Size(7, 7), 1.166); cv::subtract(sigma, mu_sq, sigma); cv::pow(sigma, double(0.5), sigma); add(sigma, Scalar(1.0/255), sigma); // to avoid DivideByZero Error Mat structdis(imdist_scaled.size(), CV_64FC1, 1); subtract(imdist_scaled, mu, structdis); divide(structdis, sigma, structdis); // structdis is MSCN image // Compute AGGD fit to MSCN image double lsigma_best, rsigma_best, gamma_best; structdis = AGGDfit(structdis, lsigma_best, rsigma_best, gamma_best); featurevector.push_back(gamma_best); featurevector.push_back((lsigma_best*lsigma_best + rsigma_best*rsigma_best)/2); // Compute paired product images // indices for orientations (H, V, D1, D2) int shifts[4][2]={{0,1},{1,0},{1,1},{-1,1}}; for(int itr_shift=1; itr_shift<=4; itr_shift++) { // select the shifting index from the 2D array int* reqshift = shifts[itr_shift-1]; // declare shifted_structdis as pairwise image Mat shifted_structdis(imdist_scaled.size(), CV_64F, 1); // create copies of the images using BwImage constructor // utility constructor for better subscript access (for pixels) BwImage OrigArr(structdis); BwImage ShiftArr(shifted_structdis); // create pair-wise product for the given orientation (reqshift) for(int i=0; i<structdis.rows; i++) { for(int j=0; j<structdis.cols; j++) { if(i+reqshift[0]>=0 && i+reqshift[0]<structdis.rows && j+reqshift[1]>=0 && j+reqshift[1]<structdis.cols) { ShiftArr[i][j]=OrigArr[i + reqshift[0]][j + reqshift[1]]; } else { ShiftArr[i][j]=0; } } } // Mat structdis_pairwise; shifted_structdis = ShiftArr.equate(shifted_structdis); // calculate the products of the pairs multiply(structdis, shifted_structdis, shifted_structdis); // fit the pairwise product to AGGD shifted_structdis = AGGDfit(shifted_structdis, lsigma_best, rsigma_best, gamma_best); double constant = sqrt(tgamma(1/gamma_best))/sqrt(tgamma(3/gamma_best)); double meanparam = (rsigma_best-lsigma_best)*(tgamma(2/gamma_best)/tgamma(1/gamma_best))*constant; // push the calculated parameters from AGGD fit to pair-wise products featurevector.push_back(gamma_best); featurevector.push_back(meanparam); featurevector.push_back(cv::pow(lsigma_best,2)); featurevector.push_back(cv::pow(rsigma_best,2)); } } } // function to compute best fit parameters from AGGDfit Mat AGGDfit(Mat structdis, double& lsigma_best, double& rsigma_best, double& gamma_best) { // create a copy of an image using BwImage constructor (brisque.h - more info) BwImage ImArr(structdis); long int poscount=0, negcount=0; double possqsum=0, negsqsum=0, abssum=0; for(int i=0;i<structdis.rows;i++) { for (int j =0; j<structdis.cols; j++) { double pt = ImArr[i][j]; // BwImage provides [][] access if(pt>0) { poscount++; possqsum += pt*pt; abssum += pt; } else if(pt<0) { negcount++; negsqsum += pt*pt; abssum -= pt; } } } lsigma_best = cv::pow(negsqsum/negcount, 0.5); rsigma_best = cv::pow(possqsum/poscount, 0.5); double gammahat = lsigma_best/rsigma_best; long int totalcount = (structdis.cols)*(structdis.rows); double rhat = cv::pow(abssum/totalcount, static_cast<double>(2))/((negsqsum + possqsum)/totalcount); double rhatnorm = rhat*(cv::pow(gammahat,3) +1)*(gammahat+1)/pow(pow(gammahat,2)+1,2); double prevgamma = 0; double prevdiff = 1e10; float sampling = 0.001; for (float gam=0.2; gam<10; gam+=sampling) //possible to coarsen sampling to quicken the code, with some loss of accuracy { double r_gam = tgamma(2/gam)*tgamma(2/gam)/(tgamma(1/gam)*tgamma(3/gam)); double diff = abs(r_gam-rhatnorm); if(diff> prevdiff) break; prevdiff = diff; prevgamma = gam; } gamma_best = prevgamma; return structdis.clone(); } And then try to compile using !nvcc -o /content/src/Blind_Deblurring_Cuda /content/src/Blind_Deblurring_Cuda.cu -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_highgui -lopencv_ml It gives the following error /tmp/tmpxft_00003d8d_00000000-10_Blind_Deblurring_Cuda.o: In function `computescore(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)': tmpxft_00003d8d_00000000-5_Blind_Deblurring_Cuda.cudafe1.cpp:(.text+0x9bc): undefined reference to `svm_get_nr_class' tmpxft_00003d8d_00000000-5_Blind_Deblurring_Cuda.cudafe1.cpp:(.text+0x9fd): undefined reference to `svm_predict_probability' tmpxft_00003d8d_00000000-5_Blind_Deblurring_Cuda.cudafe1.cpp:(.text+0xa27): undefined reference to `svm_free_and_destroy_model' collect2: error: ld returned 1 exit status
Questions about this serial communication code? [Cortex-M4]
I'm looking at the following code from STMicroelectronics on implementing USART communication with interrupts #include <stm32f10x_lib.h> // STM32F10x Library Definitions #include <stdio.h> #include "STM32_Init.h" // STM32 Initialization /*---------------------------------------------------------------------------- Notes: The length of the receive and transmit buffers must be a power of 2. Each buffer has a next_in and a next_out index. If next_in = next_out, the buffer is empty. (next_in - next_out) % buffer_size = the number of characters in the buffer. *----------------------------------------------------------------------------*/ #define TBUF_SIZE 256 /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/ #define RBUF_SIZE 256 /*** Must be a power of 2 (2,4,8,16,32,64,128,256,512,...) ***/ /*---------------------------------------------------------------------------- *----------------------------------------------------------------------------*/ #if TBUF_SIZE < 2 #error TBUF_SIZE is too small. It must be larger than 1. #elif ((TBUF_SIZE & (TBUF_SIZE-1)) != 0) #error TBUF_SIZE must be a power of 2. #endif #if RBUF_SIZE < 2 #error RBUF_SIZE is too small. It must be larger than 1. #elif ((RBUF_SIZE & (RBUF_SIZE-1)) != 0) #error RBUF_SIZE must be a power of 2. #endif /*---------------------------------------------------------------------------- *----------------------------------------------------------------------------*/ struct buf_st { unsigned int in; // Next In Index unsigned int out; // Next Out Index char buf [RBUF_SIZE]; // Buffer }; static struct buf_st rbuf = { 0, 0, }; #define SIO_RBUFLEN ((unsigned short)(rbuf.in - rbuf.out)) static struct buf_st tbuf = { 0, 0, }; #define SIO_TBUFLEN ((unsigned short)(tbuf.in - tbuf.out)) static unsigned int tx_restart = 1; // NZ if TX restart is required /*---------------------------------------------------------------------------- USART1_IRQHandler Handles USART1 global interrupt request. *----------------------------------------------------------------------------*/ void USART1_IRQHandler (void) { volatile unsigned int IIR; struct buf_st *p; IIR = USART1->SR; if (IIR & USART_FLAG_RXNE) { // read interrupt USART1->SR &= ~USART_FLAG_RXNE; // clear interrupt p = &rbuf; if (((p->in - p->out) & ~(RBUF_SIZE-1)) == 0) { p->buf [p->in & (RBUF_SIZE-1)] = (USART1->DR & 0x1FF); p->in++; } } if (IIR & USART_FLAG_TXE) { USART1->SR &= ~USART_FLAG_TXE; // clear interrupt p = &tbuf; if (p->in != p->out) { USART1->DR = (p->buf [p->out & (TBUF_SIZE-1)] & 0x1FF); p->out++; tx_restart = 0; } else { tx_restart = 1; USART1->CR1 &= ~USART_FLAG_TXE; // disable TX interrupt if nothing to send } } } /*------------------------------------------------------------------------------ buffer_Init initialize the buffers *------------------------------------------------------------------------------*/ void buffer_Init (void) { tbuf.in = 0; // Clear com buffer indexes tbuf.out = 0; tx_restart = 1; rbuf.in = 0; rbuf.out = 0; } /*------------------------------------------------------------------------------ SenChar transmit a character *------------------------------------------------------------------------------*/ int SendChar (int c) { struct buf_st *p = &tbuf; // If the buffer is full, return an error value if (SIO_TBUFLEN >= TBUF_SIZE) return (-1); p->buf [p->in & (TBUF_SIZE - 1)] = c; // Add data to the transmit buffer. p->in++; if (tx_restart) { // If transmit interrupt is disabled, enable it tx_restart = 0; USART1->CR1 |= USART_FLAG_TXE; // enable TX interrupt } return (0); } /*------------------------------------------------------------------------------ GetKey receive a character *------------------------------------------------------------------------------*/ int GetKey (void) { struct buf_st *p = &rbuf; if (SIO_RBUFLEN == 0) return (-1); return (p->buf [(p->out++) & (RBUF_SIZE - 1)]); } /*---------------------------------------------------------------------------- MAIN function *----------------------------------------------------------------------------*/ int main (void) { buffer_Init(); // init RX / TX buffers stm32_Init (); // STM32 setup printf ("Interrupt driven Serial I/O Example\r\n\r\n"); while (1) { // Loop forever unsigned char c; printf ("Press a key. "); c = getchar (); printf ("\r\n"); printf ("You pressed '%c'.\r\n\r\n", c); } // end while } // end main My questions are the following: In the handler function, when does the statement ((p->in - p->out) & ~(RBUF_SIZE-1)) ever evaluate to a value other than zero? If RBUF_SIZE is a power of 2 as indicated, then ~(RBUF_SIZE-1) should always be zero. Is it checking if p->in > p->out? Even if this isn't true, the conditional should evaluate to zero anyway, right? In the line following, the statement p->buf [p->in & (RBUF_SIZE-1)] = (USART1->DR & 0x1FF); is made. Why does the code AND p->in with RBUF_SIZE-1? What kind of buffer are we using in this code? FIFO?
Not so. For example, assuming 32-bit arithmetic, if RBUF_SIZE == 0x00000100 then RBUF_SIZE-1 == 0x000000FF and ~(RBUF_SIZE-1) == 0xFFFFFF00 (it's a bitwise NOT, not a logical NOT). The check you refer to is therefore effectively the same as (p->in - p->out) < RBUF_SIZE, and it's not clear why it is superior. ARM GCC 7.2.1 produces identical length code for the two (-O1). p->in & (RBUF_SIZE-1) is the same as p->in % RBUF_SIZE when p->in is unsigned. Again, not sure why the former would be used when the latter is clearer; sure, it effectively forces the compiler to compute the modulo using an AND operation, but given that RBUF_SIZE is known at compile time to be a power of two my guess is that most compilers could figure this out (again, ARM GCC 7.2.1 certainly can, I've just tried it - it produces the same instructions either way). Looks like it. FIFO implemented as a circular buffer.
What does PKCS5_PBKDF2_HMAC_SHA1 return value mean?
I'm attempting to use OpenSSL's PKCS5_PBKDF2_HMAC_SHA1 method. I gather that it returns 0 if it succeeds, and some other value otherwise. My question is, what does a non-zero return value mean? Memory error? Usage error? How should my program handle it (retry, quit?)? Edit: A corollary question is, is there any way to figure this out besides reverse-engineering the method itself?
is there any way to figure this out besides reverse-engineering the method itself? PKCS5_PBKDF2_HMAC_SHA1 looks like one of those undocumented functions because I can't find it in the OpenSSL docs. OpenSSL has a lot of them, so you should be prepared to study the sources if you are going to use the library. I gather that it returns 0 if it succeeds, and some other value otherwise. Actually, its reversed. Here's how I know... $ grep -R PKCS5_PBKDF2_HMAC_SHA1 * crypto/evp/evp.h:int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen, crypto/evp/p5_crpt2.c:int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen, ... So, you find the function's implementation in crypto/evp/p5_crpt2.c: int PKCS5_PBKDF2_HMAC_SHA1(const char *pass, int passlen, const unsigned char *salt, int saltlen, int iter, int keylen, unsigned char *out) { return PKCS5_PBKDF2_HMAC(pass, passlen, salt, saltlen, iter, EVP_sha1(), keylen, out); } Following PKCS5_PBKDF2_HMAC: $ grep -R PKCS5_PBKDF2_HMAC * ... crypto/evp/evp.h:int PKCS5_PBKDF2_HMAC(const char *pass, int passlen, crypto/evp/p5_crpt2.c:int PKCS5_PBKDF2_HMAC(const char *pass, int passlen, ... And again, from crypto/evp/p5_crpt2.c: int PKCS5_PBKDF2_HMAC(const char *pass, int passlen, const unsigned char *salt, int saltlen, int iter, const EVP_MD *digest, int keylen, unsigned char *out) { unsigned char digtmp[EVP_MAX_MD_SIZE], *p, itmp[4]; int cplen, j, k, tkeylen, mdlen; unsigned long i = 1; HMAC_CTX hctx_tpl, hctx; mdlen = EVP_MD_size(digest); if (mdlen < 0) return 0; HMAC_CTX_init(&hctx_tpl); p = out; tkeylen = keylen; if(!pass) passlen = 0; else if(passlen == -1) passlen = strlen(pass); if (!HMAC_Init_ex(&hctx_tpl, pass, passlen, digest, NULL)) { HMAC_CTX_cleanup(&hctx_tpl); return 0; } while(tkeylen) { if(tkeylen > mdlen) cplen = mdlen; else cplen = tkeylen; /* We are unlikely to ever use more than 256 blocks (5120 bits!) * but just in case... */ itmp[0] = (unsigned char)((i >> 24) & 0xff); itmp[1] = (unsigned char)((i >> 16) & 0xff); itmp[2] = (unsigned char)((i >> 8) & 0xff); itmp[3] = (unsigned char)(i & 0xff); if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) { HMAC_CTX_cleanup(&hctx_tpl); return 0; } if (!HMAC_Update(&hctx, salt, saltlen) || !HMAC_Update(&hctx, itmp, 4) || !HMAC_Final(&hctx, digtmp, NULL)) { HMAC_CTX_cleanup(&hctx_tpl); HMAC_CTX_cleanup(&hctx); return 0; } HMAC_CTX_cleanup(&hctx); memcpy(p, digtmp, cplen); for(j = 1; j < iter; j++) { if (!HMAC_CTX_copy(&hctx, &hctx_tpl)) { HMAC_CTX_cleanup(&hctx_tpl); return 0; } if (!HMAC_Update(&hctx, digtmp, mdlen) || !HMAC_Final(&hctx, digtmp, NULL)) { HMAC_CTX_cleanup(&hctx_tpl); HMAC_CTX_cleanup(&hctx); return 0; } HMAC_CTX_cleanup(&hctx); for(k = 0; k < cplen; k++) p[k] ^= digtmp[k]; } tkeylen-= cplen; i++; p+= cplen; } HMAC_CTX_cleanup(&hctx_tpl); return 1; } So it looks like 0 on failure, and 1 on success. You should not see other values. And if you get a 0, then all the OUT parameters are junk. Memory error? Usage error? Well, sometimes you can call ERR_get_error. If you call it and it makes sense, then the error code is good. If the error code makes no sense, then its probably not good. Sadly, that's the way I handle it because the library is not consistent with setting error codes. For example, here's the library code to load the RDRAND engine. Notice the code clears the error code on failure if its a 3rd generation Ivy Bridge (that's the capability being tested), and does not clear or set an error otherwise!!! void ENGINE_load_rdrand (void) { extern unsigned int OPENSSL_ia32cap_P[]; if (OPENSSL_ia32cap_P[1] & (1<<(62-32))) { ENGINE *toadd = ENGINE_rdrand(); if(!toadd) return; ENGINE_add(toadd); ENGINE_free(toadd); ERR_clear_error(); } } How should my program handle it (retry, quit?)? It looks like a hard failure. Finally, that's exactly how I navigate the sources in this situation. If you don't like grep you can try ctags or another source code browser.
Getting ARP table on iPhone/iPad
I am trying to get the ARP entries on my iPad like here. When compiling the code to run on my iPad (so not the simulator) I am getting missing header error messages. You can resolve them by copying the header files into you project locally as mentioned in this post. The problem lies in the line sdl = (struct sockaddr_dl *)(sin + 1); in this piece of code: -(NSString*) ip2mac: (char*) ip { int expire_time, flags, export_only, doing_proxy, found_entry; NSString *mAddr = nil; u_long addr = inet_addr(ip); int mib[6]; size_t needed; char *host, *lim, *buf, *next; struct rt_msghdr *rtm; struct sockaddr_inarp *sin; struct sockaddr_dl *sdl; extern int h_errno; struct hostent *hp; mib[0] = CTL_NET; mib[1] = PF_ROUTE; mib[2] = 0; mib[3] = AF_INET; mib[4] = NET_RT_FLAGS; mib[5] = RTF_LLINFO; if (sysctl(mib, 6, NULL, &needed, NULL, 0) < 0) err(1, "route-sysctl-estimate"); if ((buf = malloc(needed)) == NULL) err(1, "malloc"); if (sysctl(mib, 6, buf, &needed, NULL, 0) < 0) err(1, "actual retrieval of routing table"); lim = buf + needed; for (next = buf; next < lim; next += rtm->rtm_msglen) { rtm = (struct rt_msghdr *)next; sin = (struct sockaddr_inarp *)(rtm + 1); sdl = (struct sockaddr_dl *)(sin + 1); if (addr) { if (addr != sin->sin_addr.s_addr) continue; found_entry = 1; } if (nflag == 0) hp = gethostbyaddr((caddr_t)&(sin->sin_addr), sizeof sin->sin_addr, AF_INET); else hp = 0; if (hp) host = hp->h_name; else { host = "?"; if (h_errno == TRY_AGAIN) nflag = 1; } if (sdl->sdl_alen) { u_char *cp = LLADDR(sdl); mAddr = [NSString stringWithFormat:#"%x:%x:%x:%x:%x:%x", cp[0], cp[1], cp[2], cp[3], cp[4], cp[5]]; // ether_print((u_char *)LLADDR(sdl)); } else mAddr = nil; } if (found_entry == 0) { return nil; } else { return mAddr; } } It gives the following error message: Arithmetic on pointer to incomplete type 'struct sockaddr_inarp*' When you compile the code for the iPad simulator everything runs fine. Does anyone have an idea how to solve this? A similar question (but not solved) is asked here.
After importing <netinet/if_ether.h>, you should edit it and change the line #include <net/if_arp.h> to #include "if_arp.h" and then import <net/if_arp.h> in your project as well. This should fix that error. Anyway the headers you need to import to compile the code you posted are: #include "if_ether.h" #include "route.h" #include "if_arp.h" #include "if_dl.h" Hope this helps =) EDIT: You need to "Add files to project", not simply importing it with #import or #include. You can find above files from following links: Files under "netinet" Files under "net"
#include <net/ethernet.h> instead of messing with the original headers. More info here: Implicit declaration of function 'ether_ntoa' is invalid in C99