1 /*
2 * Copyright 2008 The qDecoder Project. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE QDECODER PROJECT ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
17 * DISCLAIMED. IN NO EVENT SHALL THE QDECODER PROJECT BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
20 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
23 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <stdbool.h>
29 #include <stdarg.h>
30 #include <string.h>
31 #include <unistd.h>
32 #include <errno.h>
33 #include <sys/file.h>
34 #include "qDecoder.h"
35 #include "qInternal.h"
36
37 // Change two hex character to one hex value.
38 char _q_x2c(char hex_up, char hex_low) {
39 char digit;
40
41 digit = 16 * (hex_up >= 'A' ? ((hex_up & 0xdf) - 'A') + 10 : (hex_up - '0'));
42 digit += (hex_low >= 'A' ? ((hex_low & 0xdf) - 'A') + 10 : (hex_low - '0'));
43
44 return digit;
45 }
46
47 char *_q_makeword(char *str, char stop) {
48 char *word;
49 int len, i;
50
51 for (len = 0; ((str[len] != stop) && (str[len])); len++);
52 word = (char *)malloc(sizeof(char) * (len + 1));
53
54 for (i = 0; i < len; i++) word[i] = str[i];
55 word[i] = '\0';
56
57 if (str[len])len++;
58 for (i = len; str[i]; i++) str[i - len] = str[i];
59 str[i - len] = '\0';
60
61 return word;
62 }
63
64 // This function is perfectly same as fgets();
65 char *_q_fgets(char *str, int size, FILE *stream) {
66 int c;
67 char *ptr;
68
69 for (ptr = str; size > 1; size--) {
70 c = fgetc(stream);
71 if (c == EOF) break;
72 *ptr++ = (char)c;
73 if (c == '\n') break;
74 }
75
76 *ptr = '\0';
77 if (strlen(str) == 0) return NULL;
78
79 return str;
80 }
81
82 ssize_t _q_writef(int fd, char *format, ...) {
83 char buf[MAX_LINEBUF];
84 va_list arglist;
85
86 va_start(arglist, format);
87 vsnprintf(buf, sizeof(buf), format, arglist);
88 va_end(arglist);
89
90 while(true) {
91 int status = qSocketWaitWritable(fd, 1000);
92 if(status == 0) continue;
93 else if(status < 0) return -1;
94 break;
95 }
96
97 return write(fd, buf, strlen(buf));
98 }
99
100 ssize_t _q_write(int fd, const void *buf, size_t nbytes) {
101 if(nbytes == 0) return 0;
102
103 ssize_t sent = 0;
104
105 while(sent < nbytes) {
106 int status = qSocketWaitWritable(fd, 1000);
107 if(status == 0) continue;
108 else if(status < 0) break;
109
110 ssize_t wsize = write(fd, buf+sent, nbytes-sent);
111 if(wsize <= 0) break;
112 sent += wsize;
113 }
114
115 if(sent > 0) return sent;
116 return -1;
117 }
118
119 /* win32 compatible */
120 int _q_unlink(const char *pathname) {
121 #ifdef _WIN32
122 return _unlink(pathname);
123 #endif
124 return unlink(pathname);
125 }