void renderHex(unsigned char *buf, int len, const char* s) { for (int i = 0; i < len; i++) { unsigned char msb = hexToDec(s[i * 2 + 0]); unsigned char lsb = hexToDec(s[i * 2 + 1]); buf[i] = (msb << 4) | lsb; } }
char getOctetFromEscaped(char hex1, char hex2) { if (isxdigit((int)hex1) && isxdigit((int)hex2)) { return (char)((hexToDec(hex1) << 4) + hexToDec(hex2)); } return 0; }
void Sha1Hash::setHash(std::string hash) { uint8 counter = 0; char ch1, ch2; uint8 b1, b2; for (uint8 i = 0; i < hash.length(); i = i + 2) { ch1 = hash.at(i); ch2 = hash.at(i + 1); b1 = hexToDec(ch1); b2 = hexToDec(ch2); mDigest[counter++] = (b1 << 4) | b2; } }
char DataReader_::handleNextCharWithChunked(char &c) { switch (chunkedState) { case READ_CHUNKED_LEN: if (c == '\n') { chunked_data_len_not_parse.trim(); // delete \r\n characters chunked_data_len = hexToDec(chunked_data_len_not_parse); // convert lenght to int chunked_data_len_not_parse = ""; chunkedState = READ_CHUNKED_DATA; if (chunked_data_len == 0) { return END_OF_DATA_CHAR; } } else { chunked_data_len_not_parse += c; } break; case READ_CHUNKED_DATA: chunked_data_len -= 1; if (-1 < chunked_data_len) { // first tine data_len == 0 -> go to read len return c; // raw data without chuncked } else { char_count += 1; // escape \r\n last chars in chuncked data if (char_count > 1) { char_count = 0; chunkedState = READ_CHUNKED_LEN; } } break; } return SKIP_CHAR; }
/* delete a message from the device's database */ void handleDeleteMessage() { unsigned int index = hexToDec(dequeue(&requestBuffer))*16 + hexToDec(dequeue(&requestBuffer)); unsigned int result = deleteSmsWithIndex(index); /*if success, refresh screen*/ if(result == 0) { setRefreshDisplay(); refreshDisplay(); } /*enqueue reply*/ enqueueString(&replyBuffer, "DE", 2); enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, decToHex(result)); }
int main(void) { char *input = (char *) malloc(512); loadHex(input); printf("Decimal: %d\n", (int) hexToDec(input)); return 0; }
/* sends an SMS */ void handleSendMessage() { enqueueString(&replyBuffer, "SM", 2); enqueue(&replyBuffer, '0'); /*check destination length then fetch destination*/ unsigned int destLen = hexToDec(dequeue(&requestBuffer))*16 + hexToDec(dequeue(&requestBuffer)); if(destLen < 1 || destLen > 12) { enqueue(&replyBuffer, '1'); return; } SMS_SUBMIT smsToSend; for(int i=0; i<destLen; i++) { dequeue(&requestBuffer); smsToSend.recipient_id[i] = dequeue(&requestBuffer); } /*check data length then fetch message content*/ smsToSend.data_length = hexToDec(dequeue(&requestBuffer))*16 + hexToDec(dequeue(&requestBuffer)); if(smsToSend.data_length > DATA_MAX_LENGTH) { enqueue(&replyBuffer, '1'); return; } for(i=0; i<smsToSend.data_length; i++) smsToSend.data[i] = (char)(hexToDec(dequeue(&requestBuffer))*16 + hexToDec(dequeue(&requestBuffer))); /*send SMS and enqueue success indicator*/ sendSmsAndRefresh(&smsToSend); enqueue(&replyBuffer, '0'); }
/*----------------------------------------------------------------------* * Unescapes "%"-escaped characters in a query: */ static void unescapeUrl(char *url) { int x, y, len; len = strlen(url); for (x = 0, y = 0; url[y]; x++, y++) { if ((url[x] = url[y]) == '%' && y < (len - 2)) { url[x] = hexToDec(&url[y+1]); y += 2; } } url[x] = '\0'; }
void ClientAJAKumoTCP::handleCmd(char *cmd, char* parameter, char* data) { uint16_t command = (cmd[0] << 8) | cmd[1]; switch(command) { case 'QI': { // Output routing info uint8_t output = hexToDec(parameter, 4); uint8_t input = hexToDec(data + strlen(data) - 4, 4); _receivedRouting = output-1; if(output <= ClientAJAKumoTCP_NUMOUTPUTS) { if(routingState[output-1] != input-1) { if(_serialOutput > 0) Serial << "Route changed: " << input << " --> " << output << "\n"; routingState[output-1] = input-1; } } break; } default: if(_serialOutput > 1) Serial << "Unhandled incoming " << cmd << "," << parameter << " = " << data << "\n"; } }
int htoi(char s[]) { int result = 0; int i = 0; if (s[1] == 'x' || s[1] == 'X') i = 2; for (; s[i] != '\0'; i++) { result = result * 16 + hexToDec(s[i]); } return result; }
/* updates a message's data field */ void handleUpdateMessage() { enqueueString(&replyBuffer, "UM", 2); unsigned int index = hexToDec(dequeue(&requestBuffer))*16 + hexToDec(dequeue(&requestBuffer)); /* fetch data length and check if allowed */ unsigned int dataLen = hexToDec(dequeue(&requestBuffer))*16 + hexToDec(dequeue(&requestBuffer)); if(dataLen > DATA_MAX_LENGTH) { enqueue(&replyBuffer, '1'); return; } /* fetch data */ char data[DATA_MAX_LENGTH]; for(int i=0; i<dataLen; i++) data[i] = (char)(hexToDec(dequeue(&requestBuffer))*16 + hexToDec(dequeue(&requestBuffer))); /* process the request and enqueue the result */ enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, updateSmsDataWithIndex(index, data, dataLen)); }
int main(int argc, char *argv[]) { int i, selectIndex, tintValue, optionSwitch, negativeIndex, yiq, terse, enableRed, enableBlue, enableGreen, ret, exitStatus; i = yiq = terse = tintValue = optionSwitch = negativeIndex = enableRed = enableBlue = enableGreen = ret = exitStatus = 0; selectIndex = -1; long red, blue, green; red = blue = green = 0; char *colorString, *inputString; char negativeTintInput[10] = ""; /* -l (limit), -i (invert), -s (select) */ while ((i = getopt (argc, argv, ":rghbtlis:c")) != -1) switch(i) { case 'l': optionSwitch = 1; break; case 'i': optionSwitch = -1; break; case 'c': optionSwitch = 2; break; case 'r': enableRed = 1; break; case 'g': enableGreen = 1; break; case 'b': enableBlue = 1; break; case 's': selectIndex = atoi(optarg); break; case 'h': usage(); case 't': terse = 1; break; case '?': /* here we hand number arguments to a string to parse */ /* because we want to do negative args without escaping (--) */ switch(optopt) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': case '0': negativeTintInput[negativeIndex++] = optopt; break; default: usage(); } } /* always gonna need an input string. */ if (argv[optind] == NULL) usage(); /* if not inverting, need tint value. */ if (optionSwitch != -1 && optionSwitch != 2 && !negativeIndex) if (argv[optind] == NULL) usage(); /* default to all colors. */ if (!enableRed && !enableGreen && !enableBlue) enableRed = enableGreen = enableBlue = 1; /* set input and color strings, default to last 6 characters. */ inputString = argv[argc - 1]; selectIndex = selectIndex == -1 ? strlen(inputString) - 6 : selectIndex; colorString = &inputString[selectIndex]; /* set color handles. */ red = hexToDec(colorString, 0); green = hexToDec(colorString, 2); blue = hexToDec(colorString, 4); /* set and apply the tint value if we're using it */ if (optionSwitch != -1 && optionSwitch != 2) { tintValue = negativeIndex ? -1 * atoi(negativeTintInput) : atoi(argv[optind]); red += tintValue; green += tintValue; blue += tintValue; } /* do the thing */ switch(optionSwitch) { case -1: /* invert */ red = 255 - red; green = 255 - green; blue = 255 - blue; break; case 0: /* normal */ case 1: /* limit */ ret = makeValid(&red, optionSwitch); ret += makeValid(&green, optionSwitch); ret += makeValid(&blue, optionSwitch); exitStatus = ret >=2 ? 1 : 0; break; case 2: /* contrast - ref: https://24ways.org/2010/calculating-color-contrast/ */ yiq = ((red * 299) + (green * 587) + (blue * 114)) / 1000; exit(yiq >= 128 ? 1 : 0); } /* insert result if enabled */ if (enableRed) decToHex(red, &colorString[0]); if (enableGreen) decToHex(green, &colorString[2]); if (enableBlue) decToHex(blue, &colorString[4]); /* print color only */ if (terse) { colorString[6] = '\0'; inputString = colorString; } printf("%s", inputString); exit(exitStatus); }
/* reads the data of a message into the send buffer */ void handleReadMessage() { char upperNibble = dequeue(&requestBuffer), lowerNibble = dequeue(&requestBuffer); unsigned int index = hexToDec(upperNibble)*16 + hexToDec(lowerNibble); pSmsNode_t pSelectedSms = getSmsWithIndex(index); if(pSelectedSms == NULL) { sendInvalidRequestReply(); return; } enqueueString(&replyBuffer, "MC", 2); enqueue(&replyBuffer, upperNibble); enqueue(&replyBuffer, lowerNibble); if (pSelectedSms->type == INCOMING_MESSAGE) { unsigned int srcLen, i; SMS_DELIVER* pMsg = (SMS_DELIVER*)pSelectedSms->pSMS; /* fetch timestamp time */ enqueue(&replyBuffer, pMsg->timestamp[7]); enqueue(&replyBuffer, pMsg->timestamp[6]); enqueue(&replyBuffer, pMsg->timestamp[9]); enqueue(&replyBuffer, pMsg->timestamp[8]); enqueue(&replyBuffer, pMsg->timestamp[11]); enqueue(&replyBuffer, pMsg->timestamp[10]); /* fetch destination */ enqueue(&replyBuffer, decToHex(DEVICE_ID_LENGTH/16)); enqueue(&replyBuffer, decToHex(DEVICE_ID_LENGTH%16)); char dest[] = DEVICE_ID; for(i=0 ; i<DEVICE_ID_LENGTH ; ++i) { enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, dest[i]); } /* fetch source */ for(srcLen=0; (pMsg->sender_id[srcLen] != (char)NULL) && (srcLen < ID_MAX_LENGTH); ++srcLen); enqueue(&replyBuffer, decToHex(srcLen/16)); enqueue(&replyBuffer, decToHex(srcLen%16)); for(i=0 ; i<srcLen ; ++i) { enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, pMsg->sender_id[i]); } /* fetch SMS message data */ unsigned int dataLength =pMsg->data_length; enqueue(&replyBuffer, decToHex(dataLength/16)); enqueue(&replyBuffer, decToHex(dataLength%16)); for(i=0 ; i<dataLength ; ++i) { enqueue(&replyBuffer, decToHex(pMsg->data[i]/16)); enqueue(&replyBuffer, decToHex(pMsg->data[i]%16)); } } else /* OUTGOING_MESSAGE */ { unsigned int destLen, i; SMS_SUBMIT* pMsg = (SMS_SUBMIT*)pSelectedSms->pSMS; /* no timestamp, send zeros instead (see README) */ enqueueString(&replyBuffer, "000000", 6); /* fetch destination */ for(destLen=0; (pMsg->recipient_id[destLen] != (char)NULL) && (destLen < ID_MAX_LENGTH); ++destLen); enqueue(&replyBuffer, decToHex(destLen/16)); enqueue(&replyBuffer, decToHex(destLen%16)); for(i=0 ; i<destLen ; ++i) { enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, pMsg->recipient_id[i]); } /* fetch source */ enqueue(&replyBuffer, decToHex(DEVICE_ID_LENGTH/16)); enqueue(&replyBuffer, decToHex(DEVICE_ID_LENGTH%16)); char src[] = DEVICE_ID; for(i=0 ; i<DEVICE_ID_LENGTH ; ++i) { enqueue(&replyBuffer, '0'); enqueue(&replyBuffer, src[i]); } /* fetch SMS message data */ unsigned int dataLength = pMsg->data_length; enqueue(&replyBuffer, decToHex(dataLength/16)); enqueue(&replyBuffer, decToHex(dataLength%16)); for(i=0 ; i<dataLength ; ++i) { enqueue(&replyBuffer, decToHex(pMsg->data[i]/16)); enqueue(&replyBuffer, decToHex(pMsg->data[i]%16)); } } }
// strURL: URL to decode. CString CURLEncode::Decode(CString strURL) { int i=strURL.Find(_T('%')); TCHAR tc1=0, tc2=0; BYTE b=0; BOOL bFound=FALSE; while (i>-1) { tc1=strURL.GetAt(i+1); tc2=strURL.GetAt(i+2); if (isHex(tc1) && isHex(tc2)) { b=hexToDec(tc1, tc2); // first deal with 1-byte unprintable characters if (b<0x1F || b==0x7F) { strURL.SetAt(i, b); strURL.Delete(i+1, 2); } else { // Then deal with 1-byte unsafe/reserved characters // We are reading for those static LPCTSTR strings, // so we have nice support for Unicode bFound=FALSE; for (int ii=0; ii<m_iUnsafeLen && !bFound; ii++) { if (__toascii(m_lpszUnsafeString[ii])==b) { strURL.SetAt(i, m_lpszUnsafeString[ii]); strURL.Delete(i+1, 2); bFound=TRUE; } } for (int ii=0; ii<m_iReservedLen && !bFound; ii++) { if (__toascii(m_lpszReservedString[ii])==b) { strURL.SetAt(i, m_lpszReservedString[ii]); strURL.Delete(i+1, 2); bFound=TRUE; } } // Then deal with UTF-8 (2-bytes) characters if (!bFound) { // We need to have 2 bytes for decoding if (strURL.GetAt(i+3)==_T('%')) { tc1=strURL.GetAt(i+4); tc2=strURL.GetAt(i+5); if (isHex(tc1) && isHex(tc2)) { BYTE b2=hexToDec(tc1, tc2); strURL.SetAt(i, fromUTF8(MAKEWORD(b2, b))); strURL.Delete(i+1, 5); } } } } } i=strURL.Find(_T('%'), i+1); } return strURL; }
void Gprmc::stringInterpretation(String gpsString) { gprmcIndex = 0; firstCommaIndex = 0; secondCommaIndex = 0; thirdCommaIndex = 0; fourthCommaIndex = 0; fifthCommaIndex = 0; sixthCommaIndex = 0; seventhCommaIndex = 0; eighthCommaIndex = 0; ninthCommaIndex = 0; tenthCommaIndex = 0; eleventhCommaIndex = 0; twelfthCommaIndex = 0; time = 0; status = "V"; latitude = 0; longitude = 0; speedOverGround = 0; date = 0; gprmcFound = false; checksumFound = false; checksum = 0; character = '0'; if(gpsString.indexOf("GPRMC") != -1) { gprmcFound = true; } if(gprmcFound) { gprmcIndex = gpsString.indexOf("GPRMC"); firstCommaIndex = gpsString.indexOf (',', gprmcIndex + 1); secondCommaIndex = gpsString.indexOf (',', firstCommaIndex + 1); thirdCommaIndex = gpsString.indexOf (',', secondCommaIndex + 1); fourthCommaIndex = gpsString.indexOf (',', thirdCommaIndex + 1); fifthCommaIndex = gpsString.indexOf (',', fourthCommaIndex + 1); sixthCommaIndex = gpsString.indexOf (',', fifthCommaIndex + 1); seventhCommaIndex = gpsString.indexOf (',', sixthCommaIndex + 1); eighthCommaIndex = gpsString.indexOf (',', seventhCommaIndex + 1); ninthCommaIndex = gpsString.indexOf (',', eighthCommaIndex + 1); tenthCommaIndex = gpsString.indexOf (',', ninthCommaIndex + 1); eleventhCommaIndex = gpsString.indexOf (',', tenthCommaIndex + 1); twelfthCommaIndex = gpsString.indexOf (',', eleventhCommaIndex + 1); /* * substring() * der Startindex ist inklusive, der Endindex ist exkluiv! */ if (gpsString.substring(twelfthCommaIndex + 2, twelfthCommaIndex + 3).equals("*")) { gprmcSentence = gpsString.substring (gprmcIndex, twelfthCommaIndex + 5); } timeString = gpsString.substring (firstCommaIndex + 1, secondCommaIndex ); statusString = gpsString.substring (secondCommaIndex + 1, thirdCommaIndex ); latitudeString = gpsString.substring (thirdCommaIndex + 1, fourthCommaIndex ); // northSouthString = gpsString.substring (fourthCommaIndex + 1, fifthCommaIndex ); longitudeString = gpsString.substring (fifthCommaIndex + 1, sixthCommaIndex ); // westEastString = gpsString.substring (sixthCommaIndex + 1, seventhCommaIndex ); speedOverGroundString = gpsString.substring (seventhCommaIndex + 1, eighthCommaIndex ); // trackAngleString = gpsString.substring (eighthCommaIndex + 1, ninthCommaIndex ); dateString = gpsString.substring (ninthCommaIndex + 1, tenthCommaIndex ); // magneticVariationString = gpsString.substring (tenthCommaIndex + 1, eleventhCommaIndex); // magneticVariationSignString = gpsString.substring (eleventhCommaIndex + 1, twelfthCommaIndex ); if (gpsString.substring(twelfthCommaIndex + 2, twelfthCommaIndex + 3).equals("*")) { checksumString = gpsString.substring (twelfthCommaIndex + 3, twelfthCommaIndex + 5); checksumFound = true; } /* * Checksumme überprüfen: * Die Checksumme wird berechnet, indem alle Zeichen zwischen dem $ und dem * * duch ein (bitweise) exklusiv-oder berechnet werden. * Angegeben wird die Checksumme durch eine Hexadezimalzahl, die hiner dem * steht. */ for (int i = 0; i < gprmcSentence.length() - 3; i++) { checksum = checksum ^ byte(gprmcSentence[i]); } /* * Die Daten werden nur gesetzt, falls die empfangenen Daten * valide sind und die Checksumme korrekt ist. */ if(statusString.equals("A") && hexToDec(checksumString) == checksum) { char floatBufferTime[15]; timeString.toCharArray(floatBufferTime, sizeof(floatBufferTime)); setTime(atof(floatBufferTime)); setStatus(statusString); char floatBufferLat[15]; latitudeString.toCharArray(floatBufferLat, sizeof(floatBufferLat)); setLatitude(atof(floatBufferLat)); char floatBufferLon[15]; longitudeString.toCharArray(floatBufferLon, sizeof(floatBufferLon)); setLongitude(atof(floatBufferLon)); char floatBufferSpeed[15]; speedOverGroundString.toCharArray(floatBufferSpeed, sizeof(floatBufferSpeed)); setSpeedOverGround(atof(floatBufferSpeed)); char floatBufferDate[15]; dateString.toCharArray(floatBufferDate, sizeof(floatBufferDate)); setDate(atol(floatBufferDate)); } } }