summaryrefslogtreecommitdiff
path: root/src/quic_util.c
blob: e00b3e258892ceee61357cb7e0407afe2802eccc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
/*
 * quic_util.c
 *
 *  Created on: 2019��4��4��
 *      Author: root
 */
#include "quic_analysis.h"
#include<stdio.h>
extern struct quic_param_t g_quic_param;


bool a_readUInt64(UINT64* buf, char* quic_data, UINT32 quic_data_len, UINT32* offset){
	return a_readBytes(buf, sizeof(*buf), quic_data, quic_data_len, offset);
}

bool a_readUInt32(UINT32* buf, char* quic_data, UINT32 quic_data_len, UINT32* offset){
	return a_readBytes(buf, sizeof(*buf), quic_data, quic_data_len, offset);
}

bool a_readUInt8(UINT8* buf, char* quic_data, UINT32 quic_data_len, UINT32* offset){
	return a_readBytes(buf, sizeof(*buf), quic_data, quic_data_len, offset);
}

bool a_readUInt16(UINT16* buf, char* quic_data, UINT32 quic_data_len, UINT32* offset){
	return a_readBytes(buf, sizeof(*buf), quic_data, quic_data_len, offset);
}

bool a_readBytes(void* buf, UINT32 len, char * quic_data, UINT32 quic_data_len, UINT32* quic_offset) {
	UINT32 offset = *quic_offset;
	if (!a_canRead(len, quic_data_len, offset)) {
		return false;
	}
	memcpy(buf, quic_data + offset, len);
	offset += len;
	*quic_offset = offset;
	return true;
}

bool a_canRead(size_t bytes, UINT32 g_len_t, UINT32 g_pos_t) {
	if(g_pos_t >= g_len_t){
		return false;
	}
    return bytes <= (g_len_t - g_pos_t);
}

UINT64 a_pletoh64(const void *p, UINT32 offset)
{
    return (UINT64)*((const UINT8 *)(p)+7+offset)<<56|
           (UINT64)*((const UINT8 *)(p)+6+offset)<<48|
           (UINT64)*((const UINT8 *)(p)+5+offset)<<40|
           (UINT64)*((const UINT8 *)(p)+4+offset)<<32|
           (UINT64)*((const UINT8 *)(p)+3+offset)<<24|
           (UINT64)*((const UINT8 *)(p)+2+offset)<<16|
           (UINT64)*((const UINT8 *)(p)+1+offset)<<8|
           (UINT64)*((const UINT8 *)(p)+0+offset)<<0;
}
UINT64 a_pletoh48(const void *p, UINT32 offset)
{
    return (UINT64)*((const UINT8 *)(p)+5+offset)<<40|
           (UINT64)*((const UINT8 *)(p)+4+offset)<<32|
		   (UINT64)*((const UINT8 *)(p)+3+offset)<<24|
		   (UINT64)*((const UINT8 *)(p)+2+offset)<<16|
           (UINT64)*((const UINT8 *)(p)+1+offset)<<8|
           (UINT64)*((const UINT8 *)(p)+0+offset)<<0;
}

UINT32 a_pletoh32(const void *p, UINT32 offset)
{
    return (UINT32)*((const UINT8 *)(p)+3+offset)<<24|
           (UINT32)*((const UINT8 *)(p)+2+offset)<<16|
           (UINT32)*((const UINT8 *)(p)+1+offset)<<8|
           (UINT32)*((const UINT8 *)(p)+0+offset)<<0;
}

UINT32 a_pletoh24(const void *p, UINT32 offset)
{
    return (UINT32)*((const UINT8 *)(p)+2+offset)<<16|
           (UINT32)*((const UINT8 *)(p)+1+offset)<<8|
           (UINT32)*((const UINT8 *)(p)+0+offset)<<0;
}

//UINT16 a_pletoh16(const void *p)
//{
//	UINT32 offset = g_pos_t;
//    return (UINT16)*((const UINT8 *)(p)+0+offset)<<0|
//           (UINT16)*((const UINT8 *)(p)+1+offset)<<8;
//}

UINT16 a_pletoh16(const void *p, UINT32 offset){
	return (UINT16)*((const UINT8 *)(p)+0+offset)<<0|
	           (UINT16)*((const UINT8 *)(p)+1+offset)<<8;
}

UINT16 a_pntoh16(const void *p, UINT32 offset)
{
    return (UINT16)*((const UINT8 *)(p)+1+offset)<<0|
           (UINT16)*((const UINT8 *)(p)+0+offset)<<8;
}


UINT32 a_pntoh24(const void *p, UINT32 offset)
{
    return (UINT32)*((const UINT8 *)(p)+0+offset)<<16|
           (UINT32)*((const UINT8 *)(p)+1+offset)<<8|
           (UINT32)*((const UINT8 *)(p)+2+offset)<<0;
}

UINT32 a_pntoh32(const void *p, UINT32 offset)
{
    return (UINT32)*((const UINT8 *)(p)+0+offset)<<24|
           (UINT32)*((const UINT8 *)(p)+1+offset)<<16|
           (UINT32)*((const UINT8 *)(p)+2+offset)<<8|
           (UINT32)*((const UINT8 *)(p)+3+offset)<<0;
}

UINT64 a_pntoh48(const void *p, UINT32 offset)
{
    return (UINT64)*((const UINT8 *)(p)+0+offset)<<40|
           (UINT64)*((const UINT8 *)(p)+1+offset)<<32|
           (UINT64)*((const UINT8 *)(p)+2+offset)<<24|
           (UINT64)*((const UINT8 *)(p)+3+offset)<<16|
           (UINT64)*((const UINT8 *)(p)+4+offset)<<8|
           (UINT64)*((const UINT8 *)(p)+5+offset)<<0;
}

UINT64 a_pntoh64(const void *p, UINT32 offset)
{
    return (UINT64)*((const UINT8 *)(p)+0+offset)<<56|
           (UINT64)*((const UINT8 *)(p)+1+offset)<<48|
           (UINT64)*((const UINT8 *)(p)+2+offset)<<40|
           (UINT64)*((const UINT8 *)(p)+3+offset)<<32|
           (UINT64)*((const UINT8 *)(p)+4+offset)<<24|
           (UINT64)*((const UINT8 *)(p)+5+offset)<<16|
           (UINT64)*((const UINT8 *)(p)+6+offset)<<8|
           (UINT64)*((const UINT8 *)(p)+7+offset)<<0;
}

void a_phton64(UINT8 *p, UINT64 v) {
    p[0] = (UINT8)(v >> 56);
    p[1] = (UINT8)(v >> 48);
    p[2] = (UINT8)(v >> 40);
    p[3] = (UINT8)(v >> 32);
    p[4] = (UINT8)(v >> 24);
    p[5] = (UINT8)(v >> 16);
    p[6] = (UINT8)(v >> 8);
    p[7] = (UINT8)(v >> 0);
}

int get_remaining_len(UINT32 g_len_t, UINT32 offset){
	return g_len_t - offset;
}

void a_ntoa( unsigned int in, char *buffer)
{
    unsigned char *bytes = (unsigned char *) &in;
    snprintf( buffer, 15, "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
}

//char* a_ntoa( unsigned int in, char * str)
//{
//
//	char buffer[64];
//    unsigned char *bytes = (unsigned char *) &in;
//    printf("%s  %d.%d.%d.%d\t", str, bytes[0], bytes[1], bytes[2], bytes[3] );
//    snprintf( buffer, sizeof (buffer), "%d.%d.%d.%d", bytes[0], bytes[1], bytes[2], bytes[3] );
//    return buffer;
//}