File: utils.c

package info (click to toggle)
uxplay 1.72.2-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,196 kB
  • sloc: ansic: 22,822; cpp: 2,342; makefile: 7
file content (356 lines) | stat: -rw-r--r-- 10,351 bytes parent folder | download
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/**
 *  Copyright (C) 2011-2012  Juho Vähä-Herttua
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  Lesser General Public License for more details.
 *
 *=================================================================
 * modified by fduncanh 2021-2022
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <time.h>
#include <stdint.h>
#define SECOND_IN_NSECS 1000000000UL

char *
utils_strsep(char **stringp, const char *delim)
{
    char *original;
    char *strptr;

    if (*stringp == NULL) {
        return NULL;
    }

    original = *stringp;
    strptr = strstr(*stringp, delim);
    if (strptr == NULL) {
        *stringp = NULL;
        return original;
    }
    *strptr = '\0';
    *stringp = strptr+strlen(delim);
    return original;
}

int
utils_read_file(char **dst, const char *filename)
{
    FILE *stream;
    int filesize;
    char *buffer;
    int read_bytes;

    /* Open stream for reading */
    stream = fopen(filename, "rb");
    if (!stream) {
        return -1;
    }

    /* Find out file size */
    fseek(stream, 0, SEEK_END);
    filesize = ftell(stream);
    fseek(stream, 0, SEEK_SET);

    /* Allocate one extra byte for zero */
    buffer = malloc(filesize+1);
    if (!buffer) {
        fclose(stream);
        return -2;
    }

    /* Read data in a loop to buffer */
    read_bytes = 0;
    do {
        int ret = fread(buffer+read_bytes, 1,
                        filesize-read_bytes, stream);
        if (ret == 0) {
            break;
        }
        read_bytes += ret;
    } while (read_bytes < filesize);

    /* Add final null byte and close stream */
    buffer[read_bytes] = '\0';
    fclose(stream);

    /* If read didn't finish, return error */
    if (read_bytes != filesize) {
        free(buffer);
        return -3;
    }

    /* Return buffer */
    *dst = buffer;
    return filesize;
}

int
utils_hwaddr_raop(char *str, int strlen, const char *hwaddr, int hwaddrlen)
{
    int i,j;

    /* Check that our string is long enough */
    if (strlen == 0 || strlen < 2*hwaddrlen+1)
        return -1;

    /* Convert hardware address to hex string */
    for (i=0,j=0; i<hwaddrlen; i++) {
        int hi = (hwaddr[i]>>4) & 0x0f;
        int lo = hwaddr[i] & 0x0f;

        if (hi < 10) str[j++] = '0' + hi;
        else         str[j++] = 'A' + hi-10;
        if (lo < 10) str[j++] = '0' + lo;
        else         str[j++] = 'A' + lo-10;
    }

    /* Add string terminator */
    str[j++] = '\0';
    return j;
}

int
utils_hwaddr_airplay(char *str, int strlen, const char *hwaddr, int hwaddrlen)
{
    int i,j;

    /* Check that our string is long enough */
    if (strlen == 0 || strlen < 2*hwaddrlen+hwaddrlen)
        return -1;

    /* Convert hardware address to hex string */
    for (i=0,j=0; i<hwaddrlen; i++) {
        int hi = (hwaddr[i]>>4) & 0x0f;
        int lo = hwaddr[i] & 0x0f;

        if (hi < 10) str[j++] = '0' + hi;
        else         str[j++] = 'a' + hi-10;
        if (lo < 10) str[j++] = '0' + lo;
        else         str[j++] = 'a' + lo-10;

        str[j++] = ':';
    }

    /* Add string terminator */
    if (j != 0) j--;
    str[j++] = '\0';
    return j;
}

char *utils_parse_hex(const char *str, int str_len, int *data_len) {
    assert(str_len % 2 == 0);

    char *data = malloc(str_len / 2);

    for (int i = 0; i < (str_len / 2); i++) {
        char c_1 = str[i * 2];
        if (c_1 >= 97 && c_1 <= 102) {
            c_1 -= (97 - 10);
        } else if (c_1 >= 65 && c_1 <= 70) {
            c_1 -= (65 - 10);
        } else if (c_1 >= 48 && c_1 <= 57) {
            c_1 -= 48;
        } else {
            free(data);
            return NULL;
        }

        char c_2 = str[(i * 2) + 1];
        if (c_2 >= 97 && c_2 <= 102) {
            c_2 -= (97 - 10);
        } else if (c_2 >= 65 && c_2 <= 70) {
            c_2 -= (65 - 10);
        } else if (c_2 >= 48 && c_2 <= 57) {
            c_2 -= 48;
        } else {
            free(data);
            return NULL;
        }

        data[i] = (c_1 << 4) | c_2;
    }

    *data_len = (str_len / 2);
    return data;
}

char *utils_hex_to_string(const unsigned char *hex, int hex_len) {
    char *hex_str = (char *) malloc(2*hex_len + 1);
    char* pos = hex_str;
    for (int i = 0; i < hex_len; i++) {
        snprintf(pos, 3, "%2.2x", *(hex + i));
        pos +=2;
    }
    return hex_str;
}

char *utils_data_to_string(const unsigned char *data, int datalen, int chars_per_line) {
    assert(datalen >= 0);
    assert(chars_per_line > 0);
    int len = 3*datalen + 1;
    if (datalen > chars_per_line) {
        len += (datalen-1)/chars_per_line;
    }
    char *str = (char *) calloc(len + 1, sizeof(char));
    assert(str);
    char *p = str;
    int n = len + 1;
    for (int i = 0; i < datalen; i++) {
        if (i > 0 && i % chars_per_line == 0) {
            snprintf(p, n, "\n");
            n--;
            p++;
        }
        snprintf(p, n, "%2.2x ", (unsigned int) data[i]);
        n -= 3;
        p += 3;
    }
    snprintf(p, n, "\n");
    n--;
    p++;
    assert(p == &(str[len]));
    assert(len == strlen(str));
    return str;
}

char *utils_data_to_text(const char *data, int datalen) {
    char *ptr = (char *) calloc(datalen + 1, sizeof(char));
    assert(ptr);
    strncpy(ptr, data, datalen);
    char *p = ptr;
    while (p) {
        p  = strchr(p, '\r');  /* replace occurences of '\r' by ' ' */
	if (p) *p = ' ';
    }
    return ptr;
}

void ntp_timestamp_to_time(uint64_t ntp_timestamp, char *timestamp, size_t maxsize) {
    time_t rawtime = (time_t) (ntp_timestamp / SECOND_IN_NSECS);
    struct tm ts = *localtime(&rawtime);
    assert(maxsize > 29);
#ifdef _WIN32  /*modification for compiling for Windows */
    strftime(timestamp, 20, "%Y-%m-%d %H:%M:%S", &ts);
#else
    strftime(timestamp, 20, "%F %T", &ts);
#endif
    snprintf(timestamp + 19, 11,".%9.9lu", (unsigned long) ntp_timestamp % SECOND_IN_NSECS);
}

void ntp_timestamp_to_seconds(uint64_t ntp_timestamp, char *timestamp, size_t maxsize) {
    time_t rawtime = (time_t) (ntp_timestamp / SECOND_IN_NSECS);
    struct tm ts = *localtime(&rawtime);
    assert(maxsize > 12);
    strftime(timestamp, 3, "%S", &ts);
    snprintf(timestamp + 2, 11,".%9.9lu", (unsigned long) ntp_timestamp % SECOND_IN_NSECS);
}

int utils_ipaddress_to_string(int addresslen, const unsigned char *address, unsigned int zone_id, char *string, int sizeof_string) {
    int ret = 0;
    unsigned char ipv6_link_local_prefix[] = { 0xfe, 0x80, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
    assert(sizeof_string > 0);
    assert(string);
    if (addresslen != 4 && addresslen != 16) { //invalid address length   (only ipv4 and ipv6 allowed)
        string[0] = '\0';
    }
    if (addresslen == 4) {          /* IPV4 */
        ret = snprintf(string, sizeof_string, "%d.%d.%d.%d", address[0], address[1], address[2], address[3]);
    } else if (zone_id) {           /* IPV6 link-local  */
        if (memcmp(address, ipv6_link_local_prefix, 8)) { 
            string[0] = '\0';     //only link-local ipv6 addresses can have a zone_id
        } else {
	    ret = snprintf(string, sizeof_string, "fe80::%02x%02x:%02x%02x:%02x%02x:%02x%02x%%%u",
                           address[8], address[9], address[10], address[11],
                           address[12], address[13], address[14], address[15], zone_id);
        }
    } else {          /* IPV6 standard*/
        ret = snprintf(string, sizeof_string, "%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x:%02x%02x",
                       address[0], address[1], address[2], address[3], address[4], address[5], address[6], address[7],
                       address[8], address[9], address[10], address[11], address[12], address[13], address[14], address[15]);
    }
    return ret;
}

char *utils_strip_data_from_plist_xml(char *plist_xml) {
    /* returns NULL   if no data needs to be stripped out.
     * returns pointer to newly-allocated stripped text char *xml 
     * WHICH (like plist_xml) MUST BE FREED AFTER USE*/
    assert(plist_xml);
    int len = (int) strlen(plist_xml);
    char *last =  plist_xml + len * sizeof(char); // position of null termination of plist_xml
 
    char *eol;
    char *eol_data;
    char *xml = NULL;
    int nchars;
    char line[81];
    int count;
    char *begin = strstr(plist_xml, "<data>");
    char *end;
    if (!begin) {
        /* there are no data lines, nothing to do */
        return NULL;
    } else {
        xml = (char *) calloc((len + 1), sizeof(char));
    }
    char *ptr1 = plist_xml;
    char *ptr2 = xml;
    do {
        eol = strchr(begin,'\n');
        nchars = eol + 1 - ptr1;
        memcpy(ptr2, ptr1, nchars);
        ptr2 += nchars;
	ptr1 += nchars;
        end = strstr(ptr1, "</data>");
	assert(end);
        count = 0;
        do {
            eol_data = eol;
            eol = strchr(eol + 1, '\n');
            count++;
        } while (eol < end);
	count--;   // last '\n' counted ends the first non-data line (contains "</data>")
	if (count > 1) {
             snprintf(line, sizeof(line), "        (%d lines data omitted, 64 chars/line)\n", count);
             nchars = strlen(line);
	     memcpy(ptr2, line, nchars);
             ptr2 += nchars;
             ptr1 = eol_data + 1;
        } else {
             nchars = eol_data + 1 - ptr1;
             memcpy(ptr2, ptr1, nchars);
             ptr2 += nchars;
             ptr1 += nchars; 
	}
	begin = strstr(ptr1, "<data>");
	if (begin == NULL) {
            nchars = (int) (last + 1 - ptr1);
            memcpy(ptr2, ptr1, nchars);   //includes the  null terminator
            break;
        }
    } while (ptr1 <= last);
    return xml;
}

const char *gmt_time_string() {
  static char date_buf[64];
  memset(date_buf, 0, 64);

  time_t now = time(0);
  if (strftime(date_buf, 63, "%c GMT", gmtime(&now)))
    return date_buf;
  else
    return "";
}