-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.c
168 lines (135 loc) · 4.46 KB
/
utils.c
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
168
/*
Copyright (C) 2012 Daniel Hazelbaker
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <sasl/saslutil.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "common.h"
#include "utils.h"
//
// Allows us to do sprintf style code with a buffer in a safe way.
// Appends to the buffer.
//
void buffercatf(char *buffer, const char *format, ...) {
va_list args;
va_start(args, format);
vsnprintf(buffer + strlen(buffer), BUFFER_SIZE - strlen(buffer), format,
args);
va_end(args);
}
//
// A merged implementation of snprintf and strncat.
//
size_t snprintfcat(char *buf, size_t bufSize, char const *fmt, ...) {
size_t result;
va_list args;
size_t len = strnlen(buf, bufSize);
va_start(args, fmt);
result = vsnprintf(buf + len, bufSize - len - 1, fmt, args);
va_end(args);
buf[len + result] = '\0';
return result + len;
}
//
// Convert the given raw binary data into a hex-string format. The hex
// string buffer must have enough room for the stored data.
//
void binaryToHex(const unsigned char *data, int len, char *hexStr) {
int i;
char h, l;
for (i = 0; i < len; i++) {
h = (data[i] & 0xF0) >> 4;
l = (data[i] & 0x0F);
if (h >= 0x0A)
*hexStr++ = ((h - 0x0A) + 'A');
else
*hexStr++ = (h + '0');
if (l >= 0x0A)
*hexStr++ = ((l - 0x0A) + 'A');
else
*hexStr++ = (l + '0');
}
*hexStr = '\0';
}
//
// Convert the hex-string into a raw binary data stream. The length of the
// data buffer is stored in the 'len' parameter. The output data buffer must
// have enough room to store the raw data-stream.
//
void hexToBinary(const char *hexStr, unsigned char *data, int *len) {
unsigned char *d = data;
unsigned char val;
while (*hexStr != '\0' && *(hexStr + 1) != '\0') {
if (*hexStr >= 'A')
val = ((*hexStr - 'A' + 0x0A) << 4);
else
val = ((*hexStr - '0') << 4);
hexStr++;
if (*hexStr >= 'A')
val += (*hexStr - 'A' + 0x0A);
else
val += (*hexStr - '0');
hexStr++;
*d++ = val;
}
*len = (d - data);
}
//
// Convert a binary data stream into a base-64 encoded string. Also prepend
// the original binary data length to the output string.
//
int binaryToBase64(const char *data, int len, char *str) {
int result;
unsigned int outLen;
char *tempBuf;
tempBuf = (char *)malloc(len * 2);
if (tempBuf == NULL)
return -1;
result = sasl_encode64((char *)data, len, tempBuf, (len * 2), &outLen);
tempBuf[outLen] = '\0';
sprintf(str, "{%d}%s", len, tempBuf);
free(tempBuf);
return result;
}
//
// Convert a base-64 encoded string into a binary stream. Do a check if the
// string includes the original length at the beginning.
//
int base64ToBinary(const char *hexStr, char *data, int *dataLen) {
int result;
unsigned int sasl_outlen;
unsigned long attached_outlen = 0;
//
// Get the original length if they provided it.
//
if (*hexStr == '{') {
sscanf(hexStr + 1, "%lu", &attached_outlen);
hexStr = strchr(hexStr, '}');
if (hexStr == NULL)
return -1;
hexStr++;
}
result = sasl_decode64(hexStr, strlen(hexStr), (char *)data, BUFFER_SIZE,
&sasl_outlen);
if (attached_outlen > 0 && attached_outlen != sasl_outlen)
return -1;
*dataLen = sasl_outlen;
return result;
}