static int urlcmp(register const char* p, register const char* s, int d) { int pc; int sc; for (;;) { if ((pc = *p++) == d) { if (*s != d) return -1; break; } if ((sc = *s++) == d) return 1; if (pc == '%') { pc = (int)strntol(p, 2, NiL, 16); p += 2; } if (sc == '%') { sc = (int)strntol(s, 2, NiL, 16); s += 2; } if (pc < sc) return -1; if (pc > sc) return 1; } return 0; }
int InsertUser(Table* table, const char* nick, const char* password, const char* status) { int user_id; ByteArray<1024> key; ByteArray<1024> value; int nread; // this should be an atomic Incr operation table->Get(NULL, "user:id", value); user_id = strntol(value.buffer, value.length, &nread); user_id++; value.Printf("%d", user_id); table->Set(NULL, "user:id", value); key.Printf("user:%d", user_id); value.Printf("nick:%s:password:%s:status:%s", nick, password, status); table->Set(NULL, key, value); // nick is indexed, we have to make a reverse reference key.Printf("user:nick:%s", nick); value.Printf("%d", user_id); table->Set(NULL, key, value); return user_id; }
static void load_long(struct json_token* tokens, const char* path, long* into) { const struct json_token* token; token = find_json_token(tokens, path); if (token) { *into = strntol(token->ptr, token->len, 10); } }
/** * parse_number() - parses numbers * @beg - string input * @out - output buffer where parsed number should be put * @out_len - length of the output buffer * @filled - pointer where number of bytes successfully * parsed will be put * * Supports decimals in the range [INT_MIN, INT_MAX] and * hexidecimals of any size, which should be started with * prefix 0x or 0X. * * Returns the end pointer where parsing has been stopped. * In case of parsing error or lack of bytes in output buffer * NULL will be returned. */ static const char *parse_number(const char *beg, char *out, unsigned int out_len, unsigned int *filled) { const char *end; unsigned int val; long lval; int num, i; if (!out_len) return NULL; num = 0; sscanf(beg, "0%*[xX]%*[0-9a-fA-F]%n", &num); if (num == 0) { /* Here we are trying to parse decimal */ char *_end; /* Looking ahead */ _end = strcasestr(beg, "0x"); if (_end) num = _end - beg; if (num) lval = strntol(beg, num, &_end, 10); else lval = strtol(beg, &_end, 10); if (beg == _end || lval > INT_MAX || lval < INT_MIN) return NULL; end = _end; i = 0; if (!lval) { num = 0; out[i] = 0x00; i = 1; } else { val = (unsigned int)lval; for (; val && out_len; out_len--, i++, val >>= 8) out[i] = val & 0xff; if (val) return NULL; } } else {
static void effects_post(struct mg_connection* conn) { int len1; int len2; char kind[256]; char arg1[256]; char arg2[256]; struct channel* channel; settings_load(); len1 = mg_get_var(conn, KIND, kind, sizeof(kind)); if (len1 < 0) { mg_printf_data(conn, "Missing field: " KIND); mg_send_status(conn, 400); return; } if(!strncmp(kind, "treads", sizeof(kind))) { channel = &channel_treads; } else if(!strncmp(kind, "barrel", sizeof(kind))) { channel = &channel_barrel; } else if(!strncmp(kind, "panels", sizeof(kind))) { channel = &channel_panels; } else { mg_printf_data(conn, "Invalid kind"); mg_send_status(conn, 400); return; } len1 = mg_get_var(conn, ACTIVE, arg1, sizeof(arg1)); if (len1 > 0) { long idx; LOG(("selectEffect: kind=%s active=%s\n", kind, arg1)); idx = strntol(arg1, len1, 10); if (idx >= 0 && idx < channel->num_effects) { channel->active = idx; effects_post_reply(conn); return; } mg_printf_data(conn, "Invalid effect"); mg_send_status(conn, 400); return; } len1 = mg_get_var(conn, IS_SSAVER, arg1, sizeof(arg1)); if (len1 > 0) { LOG(("setEffectScreenSaver: %s\n", arg1)); if (!strncmp(arg1, "true", sizeof(arg1))) { channel->effects[channel->active]->screen_saver = 1; } else { channel->effects[channel->active]->screen_saver = 0; } effects_post_reply(conn); return; } len1 = mg_get_var(conn, COLOR, arg1, sizeof(arg1)); len2 = mg_get_var(conn, ARGUMENT, arg2, sizeof(arg2)); if (len1 > 0 && len2 > 0) { long idx = strntol(arg2, len2, 10); long color = strntol(arg1+1, len1-1, 16); LOG(("setEffectParameters: %ld, %lx\n", idx, color)); if (idx < NUM_PANELS/3) { channel->effects[channel->active]->color_arg.colors[idx].value = color; } effects_post_reply(conn); return; } else if (len1 > 0) { long color = strntol(arg1+1, len1-1, 16); LOG(("setEffectColor: %lx\n", color)); channel->effects[channel->active]->color_arg.color.value = color; effects_post_reply(conn); return; } else if (len2 > 0) { long value = strntol(arg2, len2, 10); LOG(("setEffectArgument: %ld\n", value)); channel->effects[channel->active]->argument = value; effects_post_reply(conn); return; } mg_printf_data(conn, "Invalid data"); mg_send_status(conn, 400); }