00001
00016 #include "includes.h"
00017
00018 #include "common.h"
00019
00020
00021 static int hex2num(char c)
00022 {
00023 if (c >= '0' && c <= '9')
00024 return c - '0';
00025 if (c >= 'a' && c <= 'f')
00026 return c - 'a' + 10;
00027 if (c >= 'A' && c <= 'F')
00028 return c - 'A' + 10;
00029 return -1;
00030 }
00031
00032
00033 static int hex2byte(const char *hex)
00034 {
00035 int a, b;
00036 a = hex2num(*hex++);
00037 if (a < 0)
00038 return -1;
00039 b = hex2num(*hex++);
00040 if (b < 0)
00041 return -1;
00042 return (a << 4) | b;
00043 }
00044
00045
00053 int hwaddr_aton(const char *txt, u8 *addr)
00054 {
00055 int i;
00056
00057 for (i = 0; i < 6; i++) {
00058 int a, b;
00059
00060 a = hex2num(*txt++);
00061 if (a < 0)
00062 return -1;
00063 b = hex2num(*txt++);
00064 if (b < 0)
00065 return -1;
00066 *addr++ = (a << 4) | b;
00067 if (i < 5 && *txt++ != ':')
00068 return -1;
00069 }
00070
00071 return 0;
00072 }
00073
00074
00084 int hexstr2bin(const char *hex, u8 *buf, size_t len)
00085 {
00086 size_t i;
00087 int a;
00088 const char *ipos = hex;
00089 u8 *opos = buf;
00090
00091 for (i = 0; i < len; i++) {
00092 a = hex2byte(ipos);
00093 if (a < 0)
00094 return -1;
00095 *opos++ = a;
00096 ipos += 2;
00097 }
00098 return 0;
00099 }
00100
00101
00112 void inc_byte_array(u8 *counter, size_t len)
00113 {
00114 int pos = len - 1;
00115 while (pos >= 0) {
00116 counter[pos]++;
00117 if (counter[pos] != 0)
00118 break;
00119 pos--;
00120 }
00121 }
00122
00123
00124 void wpa_get_ntp_timestamp(u8 *buf)
00125 {
00126 struct os_time now;
00127 u32 sec, usec;
00128 be32 tmp;
00129
00130
00131 os_get_time(&now);
00132 sec = now.sec + 2208988800U;
00133
00134 usec = now.usec;
00135 usec = 4295 * usec - (usec >> 5) - (usec >> 9);
00136 tmp = host_to_be32(sec);
00137 os_memcpy(buf, (u8 *) &tmp, 4);
00138 tmp = host_to_be32(usec);
00139 os_memcpy(buf + 4, (u8 *) &tmp, 4);
00140 }
00141
00142
00143 static inline int _wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data,
00144 size_t len, int uppercase)
00145 {
00146 size_t i;
00147 char *pos = buf, *end = buf + buf_size;
00148 int ret;
00149 if (buf_size == 0)
00150 return 0;
00151 for (i = 0; i < len; i++) {
00152 ret = os_snprintf(pos, end - pos, uppercase ? "%02X" : "%02x",
00153 data[i]);
00154 if (ret < 0 || ret >= end - pos) {
00155 end[-1] = '\0';
00156 return pos - buf;
00157 }
00158 pos += ret;
00159 }
00160 end[-1] = '\0';
00161 return pos - buf;
00162 }
00163
00173 int wpa_snprintf_hex(char *buf, size_t buf_size, const u8 *data, size_t len)
00174 {
00175 return _wpa_snprintf_hex(buf, buf_size, data, len, 0);
00176 }
00177
00178
00188 int wpa_snprintf_hex_uppercase(char *buf, size_t buf_size, const u8 *data,
00189 size_t len)
00190 {
00191 return _wpa_snprintf_hex(buf, buf_size, data, len, 1);
00192 }
00193
00194
00195 #ifdef CONFIG_ANSI_C_EXTRA
00196
00197 #ifdef _WIN32_WCE
00198 void perror(const char *s)
00199 {
00200 wpa_printf(MSG_ERROR, "%s: GetLastError: %d",
00201 s, (int) GetLastError());
00202 }
00203 #endif
00204
00205
00206 int optind = 1;
00207 int optopt;
00208 char *optarg;
00209
00210 int getopt(int argc, char *const argv[], const char *optstring)
00211 {
00212 static int optchr = 1;
00213 char *cp;
00214
00215 if (optchr == 1) {
00216 if (optind >= argc) {
00217
00218 return EOF;
00219 }
00220
00221 if (argv[optind][0] != '-' || argv[optind][1] == '\0') {
00222
00223 return EOF;
00224 }
00225 }
00226
00227 if (os_strcmp(argv[optind], "--") == 0) {
00228
00229 optind++;
00230 return EOF;
00231 }
00232
00233 optopt = argv[optind][optchr];
00234 cp = os_strchr(optstring, optopt);
00235 if (cp == NULL || optopt == ':') {
00236 if (argv[optind][++optchr] == '\0') {
00237 optchr = 1;
00238 optind++;
00239 }
00240 return '?';
00241 }
00242
00243 if (cp[1] == ':') {
00244
00245 optchr = 1;
00246 if (argv[optind][optchr + 1]) {
00247
00248 optarg = &argv[optind++][optchr + 1];
00249 } else if (++optind >= argc) {
00250
00251 return '?';
00252 } else {
00253
00254 optarg = argv[optind++];
00255 }
00256 } else {
00257
00258 if (argv[optind][++optchr] == '\0') {
00259 optchr = 1;
00260 optind++;
00261 }
00262 optarg = NULL;
00263 }
00264 return *cp;
00265 }
00266 #endif
00267
00268
00269 #ifdef CONFIG_NATIVE_WINDOWS
00270
00279 void wpa_unicode2ascii_inplace(TCHAR *str)
00280 {
00281 #ifdef UNICODE
00282 char *dst = (char *) str;
00283 while (*str)
00284 *dst++ = (char) *str++;
00285 *dst = '\0';
00286 #endif
00287 }
00288
00289
00290 TCHAR * wpa_strdup_tchar(const char *str)
00291 {
00292 #ifdef UNICODE
00293 TCHAR *buf;
00294 buf = os_malloc((strlen(str) + 1) * sizeof(TCHAR));
00295 if (buf == NULL)
00296 return NULL;
00297 wsprintf(buf, L"%S", str);
00298 return buf;
00299 #else
00300 return os_strdup(str);
00301 #endif
00302 }
00303 #endif
00304
00305
00321 const char * wpa_ssid_txt(const u8 *ssid, size_t ssid_len)
00322 {
00323 static char ssid_txt[33];
00324 char *pos;
00325
00326 if (ssid_len > 32)
00327 ssid_len = 32;
00328 os_memcpy(ssid_txt, ssid, ssid_len);
00329 ssid_txt[ssid_len] = '\0';
00330 for (pos = ssid_txt; *pos != '\0'; pos++) {
00331 if ((u8) *pos < 32 || (u8) *pos >= 127)
00332 *pos = '_';
00333 }
00334 return ssid_txt;
00335 }
00336
00337
00338 void * __hide_aliasing_typecast(void *foo)
00339 {
00340 return foo;
00341 }
00342