/****************************************************************************** * Function: void tmtoa(rtccTimeDate *td,char *str) * * PreCondition: None * * Input: str - pointer to the zero terminated string * dt - TimeDate struct storing time value * * Output: None * * Side Effects: None * * Overview: Convert rtcc Date to ANSI time (24 hour) format HH-MM-SS * to the given zero terminated string * * Note: None *****************************************************************************/ void tmtoa(rtccTimeDate *td,char *str) { btoh(td->f.hour,str); str[2]=':'; btoh(td->f.min,str+3); str[5]=':'; btoh(td->f.sec,str+6); }
/****************************************************************************** * Function: void dttoa(rtccTimeDate *td,char *str) * * PreCondition: None * * Input: str - pointer to the zero terminated string * dt - TimeDate struct storing date value * * Output: None * * Side Effects: None * * Overview: Convert rtcc Date to ANSI date (european) format YYYY-MM-DD * to the given zero terminated string * * Note: None *****************************************************************************/ void dttoa(rtccTimeDate *td,char *str) { btoh(0x20,str); btoh(td->f.year,str+2); str[4]='-'; btoh(td->f.mon,str+5); str[7]='-'; btoh(td->f.mday,str+8); }
void binToHex ( const unsigned char *src , int32_t srcLen , char *dst ) { const unsigned char *srcEnd = src + srcLen; for ( ; src && src < srcEnd ; ) { *dst++ = btoh(*src>>4); *dst++ = btoh(*src&15); src++; } // always null term! *dst = '\0'; // sanity check if ( src != srcEnd ) { g_process.shutdownAbort(true); } }
/* * create a hex record in ASCII and write into object file */ void flush_hex(void) { char *p; register int i; if (!hex_cnt) return; p = hex_out; *p++ = ':'; btoh((unsigned char) hex_cnt, &p); btoh((unsigned char) (hex_adr >> 8), &p); btoh((unsigned char) (hex_adr & 0xff), &p); *p++ = '0'; *p++ = '0'; for (i = 0; i < hex_cnt; i++) btoh(hex_buf[i], &p); btoh((unsigned char) chksum(), &p); *p++ = '\n'; *p = '\0'; fwrite(hex_out, 1, strlen(hex_out), objfp); hex_adr += hex_cnt; hex_cnt = 0; }
// used to verify the validity of NMEA sentences int verify_nmea(char *string){ if (string[0] != '$') return -1; int length = strlen(string); if (length > 90) { printf("ER string %s too long\n",string); return -1; } if (string[length-1]!= '\n') { printf("ER no newline\n"); return -1; } if (string[length-2] != '\r') { printf("ER no return\n"); return -1; } int p = 1; // start after the $, stop before the * char temp[3]; char cs[3]; cs[2] = '\0'; cs[0] = string[length-4]; cs[1] = string[length-3]; temp[2]='\0'; temp[1] = '\0'; char c = '\0'; while (string[p] != '*') { c = c ^ string[p++]; } // compare the checksums btoh(c,temp); p = strcmp(cs,temp); if (p != 0) { printf("ER CHECKSUM ERROR in %s, %s != %s\n",string, cs, temp); return -1; } return 0; }
char *SHA256(const char *string) { static int n = 0; static char ret[5][SHA256_HASH_LENGTH + 1]; //Cleanse the current buffer if (!string) { OPENSSL_cleanse(ret[n], SHA256_HASH_LENGTH + 1); return NULL; } char* sha256string = ret[n++]; unsigned char sha256out[SHA256_HASH_LENGTH + 1] = ""; SHA256_CTX ctx; SHA256_Init(&ctx); SHA256_Update(&ctx, string, strlen(string)); SHA256_Final(sha256out, &ctx); btoh(sha256out, SHA256_DIGEST_LENGTH, sha256string, SHA256_HASH_LENGTH + 1); OPENSSL_cleanse(&ctx, sizeof(ctx)); if (n == 5) n = 0; return sha256string; }
char *MD5(const char *string) { static int n = 0; static char ret[5][MD5_HASH_LENGTH + 1]; //Cleanse the current buffer if (!string) { OPENSSL_cleanse(ret[n], MD5_HASH_LENGTH + 1); return NULL; } char* md5string = ret[n++]; unsigned char md5out[MD5_HASH_LENGTH + 1] = ""; MD5_CTX ctx; MD5_Init(&ctx); MD5_Update(&ctx, string, strlen(string)); MD5_Final(md5out, &ctx); btoh(md5out, MD5_DIGEST_LENGTH, md5string, MD5_HASH_LENGTH + 1); OPENSSL_cleanse(&ctx, sizeof(ctx)); if (n == 5) n = 0; return md5string; }