lsquic_util.c revision 50aadb33
150aadb33SDmitri Tikhonov/* Copyright (c) 2017 LiteSpeed Technologies Inc. See LICENSE. */ 250aadb33SDmitri Tikhonov/* 350aadb33SDmitri Tikhonov * Utility functions 450aadb33SDmitri Tikhonov */ 550aadb33SDmitri Tikhonov 650aadb33SDmitri Tikhonov#include <ctype.h> 750aadb33SDmitri Tikhonov#include <stdint.h> 850aadb33SDmitri Tikhonov#include <stdio.h> 950aadb33SDmitri Tikhonov#include <string.h> 1050aadb33SDmitri Tikhonov#include <sys/time.h> 1150aadb33SDmitri Tikhonov 1250aadb33SDmitri Tikhonov#include "lsquic_int_types.h" 1350aadb33SDmitri Tikhonov#include "lsquic_util.h" 1450aadb33SDmitri Tikhonov 1550aadb33SDmitri Tikhonov 1650aadb33SDmitri Tikhonovlsquic_time_t 1750aadb33SDmitri Tikhonovlsquic_time_now (void) 1850aadb33SDmitri Tikhonov{ 1950aadb33SDmitri Tikhonov struct timeval tv; 2050aadb33SDmitri Tikhonov (void) gettimeofday(&tv, NULL); 2150aadb33SDmitri Tikhonov return (lsquic_time_t) tv.tv_sec * 1000000 + tv.tv_usec; 2250aadb33SDmitri Tikhonov} 2350aadb33SDmitri Tikhonov 2450aadb33SDmitri Tikhonov 2550aadb33SDmitri Tikhonovint 2650aadb33SDmitri Tikhonovlsquic_is_zero (const void *pbuf, size_t bufsz) 2750aadb33SDmitri Tikhonov{ 2850aadb33SDmitri Tikhonov const unsigned char *buf, *end; 2950aadb33SDmitri Tikhonov const unsigned long *buf_ul; 3050aadb33SDmitri Tikhonov unsigned n_ul; 3150aadb33SDmitri Tikhonov unsigned long n_non_zero; 3250aadb33SDmitri Tikhonov 3350aadb33SDmitri Tikhonov buf = pbuf; 3450aadb33SDmitri Tikhonov end = buf + bufsz; 3550aadb33SDmitri Tikhonov buf_ul = (unsigned long *) buf; 3650aadb33SDmitri Tikhonov n_ul = bufsz / sizeof(buf_ul[0]); 3750aadb33SDmitri Tikhonov buf += n_ul * sizeof(buf_ul[0]); 3850aadb33SDmitri Tikhonov n_non_zero = 0; 3950aadb33SDmitri Tikhonov 4050aadb33SDmitri Tikhonov while (n_ul--) 4150aadb33SDmitri Tikhonov n_non_zero |= buf_ul[n_ul]; 4250aadb33SDmitri Tikhonov 4350aadb33SDmitri Tikhonov while (buf < end) 4450aadb33SDmitri Tikhonov n_non_zero |= *buf++; 4550aadb33SDmitri Tikhonov 4650aadb33SDmitri Tikhonov return n_non_zero == 0; 4750aadb33SDmitri Tikhonov} 4850aadb33SDmitri Tikhonov 4950aadb33SDmitri Tikhonov 5050aadb33SDmitri Tikhonov/* XXX this function uses static buffer. Replace it with hexdump() if possible */ 5150aadb33SDmitri Tikhonovchar *get_bin_str(const void *s, size_t len, size_t max_display_len) 5250aadb33SDmitri Tikhonov{ 5350aadb33SDmitri Tikhonov const unsigned char *p, *pEnd; 5450aadb33SDmitri Tikhonov char *pOutput; 5550aadb33SDmitri Tikhonov size_t lenOrg = len; 5650aadb33SDmitri Tikhonov static char str[512 * 2 + 40] = {0}; 5750aadb33SDmitri Tikhonov 5850aadb33SDmitri Tikhonov /** 5950aadb33SDmitri Tikhonov * We alloc fixed size buffer, at most max_display_len is 512 6050aadb33SDmitri Tikhonov */ 6150aadb33SDmitri Tikhonov size_t fit_display_len = (max_display_len > 512 ? 512 : max_display_len); 6250aadb33SDmitri Tikhonov if (len > fit_display_len) 6350aadb33SDmitri Tikhonov len = fit_display_len; 6450aadb33SDmitri Tikhonov 6550aadb33SDmitri Tikhonov pOutput = &str[0] + sprintf(str, "(%zd/%zd)=0x", len, lenOrg); 6650aadb33SDmitri Tikhonov 6750aadb33SDmitri Tikhonov for(p = s, pEnd = s + len; p < pEnd; ++p) 6850aadb33SDmitri Tikhonov { 6950aadb33SDmitri Tikhonov sprintf(pOutput, "%02X", *p); 7050aadb33SDmitri Tikhonov pOutput += 2; 7150aadb33SDmitri Tikhonov } 7250aadb33SDmitri Tikhonov if (lenOrg > len) 7350aadb33SDmitri Tikhonov { 7450aadb33SDmitri Tikhonov sprintf(pOutput, "..."); 7550aadb33SDmitri Tikhonov pOutput += 3; 7650aadb33SDmitri Tikhonov } 7750aadb33SDmitri Tikhonov return str; 7850aadb33SDmitri Tikhonov} 7950aadb33SDmitri Tikhonov 8050aadb33SDmitri Tikhonov 8150aadb33SDmitri Tikhonovsize_t 8250aadb33SDmitri Tikhonovhexdump (const void *src_void, size_t src_sz, char *out, size_t out_sz) 8350aadb33SDmitri Tikhonov{ 8450aadb33SDmitri Tikhonov/* Ruler: 8550aadb33SDmitri Tikhonov * 8650aadb33SDmitri Tikhonov 6 31 57 73 8750aadb33SDmitri Tikhonov | | | | 8850aadb33SDmitri Tikhonov0000 00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F |................| 8950aadb33SDmitri Tikhonov * 9050aadb33SDmitri Tikhonov */ 9150aadb33SDmitri Tikhonov#define LINE_SIZE (74 + 1 /* newline */) 9250aadb33SDmitri Tikhonov const unsigned char *src = src_void; 9350aadb33SDmitri Tikhonov const unsigned char *const src_end = src + src_sz; 9450aadb33SDmitri Tikhonov char *const out_end = out + out_sz; 9550aadb33SDmitri Tikhonov unsigned line = 0; 9650aadb33SDmitri Tikhonov 9750aadb33SDmitri Tikhonov while (src < src_end && out_end - out >= LINE_SIZE) 9850aadb33SDmitri Tikhonov { 9950aadb33SDmitri Tikhonov const unsigned char *limit = src + 16; 10050aadb33SDmitri Tikhonov if (limit > src_end) 10150aadb33SDmitri Tikhonov limit = src_end; 10250aadb33SDmitri Tikhonov unsigned hex_off = 6; 10350aadb33SDmitri Tikhonov unsigned alpha_off = 57; 10450aadb33SDmitri Tikhonov sprintf(out, "%04x", line++); 10550aadb33SDmitri Tikhonov out[4] = ' '; 10650aadb33SDmitri Tikhonov out[5] = ' '; 10750aadb33SDmitri Tikhonov while (src < limit) 10850aadb33SDmitri Tikhonov { 10950aadb33SDmitri Tikhonov sprintf(out + hex_off, "%02X ", *src); 11050aadb33SDmitri Tikhonov sprintf(out + alpha_off, "%c", isprint(*src) ? *src : '.'); 11150aadb33SDmitri Tikhonov hex_off += 3; 11250aadb33SDmitri Tikhonov out[hex_off] = ' '; 11350aadb33SDmitri Tikhonov hex_off += 30 == hex_off; 11450aadb33SDmitri Tikhonov out[hex_off] = ' '; 11550aadb33SDmitri Tikhonov ++alpha_off; 11650aadb33SDmitri Tikhonov out[alpha_off] = ' '; 11750aadb33SDmitri Tikhonov ++src; 11850aadb33SDmitri Tikhonov } 11950aadb33SDmitri Tikhonov memset(out + hex_off, ' ', 56 - hex_off); 12050aadb33SDmitri Tikhonov memset(out + alpha_off, '.', 73 - alpha_off); 12150aadb33SDmitri Tikhonov out[56] = '|'; 12250aadb33SDmitri Tikhonov out[73] = '|'; 12350aadb33SDmitri Tikhonov out[74] = '\n'; 12450aadb33SDmitri Tikhonov out += LINE_SIZE; 12550aadb33SDmitri Tikhonov } 12650aadb33SDmitri Tikhonov 12750aadb33SDmitri Tikhonov if (out < out_end) 12850aadb33SDmitri Tikhonov *out = '\0'; 12950aadb33SDmitri Tikhonov else 13050aadb33SDmitri Tikhonov out_end[-1] = '\0'; 13150aadb33SDmitri Tikhonov 13250aadb33SDmitri Tikhonov return out + out_sz - out_end; 13350aadb33SDmitri Tikhonov} 134