Ejemplo n.º 1
0
void BencodeExact(ScriptValue &s, ScriptValue *args) {
	if (args[0].type != SCRIPT_LIST) return;
	ScriptValue *sv;
	ScriptValue sv2;
	if (args[0].listVal->numVals == 0) {
		CreateNullValue(sv2);
		sv = &sv2;
	}
	else sv = &args[0].listVal->vals[0];
	Bencode(sv, 0, s);
}
Ejemplo n.º 2
0
    bool ListValue::Set(size_t index, Value* in_value)
    {
        if(!in_value)
        {
            return false;
        }

        if(index >= list_.size())
        {
            // 中间的索引用空数据填充.
            while(index > list_.size())
            {
                Append(CreateNullValue());
            }
            Append(in_value);
        }
        else
        {
            DCHECK(list_[index] != in_value);
            delete list_[index];
            list_[index] = in_value;
        }
        return true;
    }
Ejemplo n.º 3
0
int JSONdecodeSub(ScriptValue &s, unsigned char * &b) {
	while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++;
	CreateNullValue(s);
	if (b[0] == '"') {
		return JSONParseString(s, b);
	}
	else if (b[0] == '[') {
		if (!CreateListValue(s, 4)) return 0;
		b++;
		while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++;
		if (*b == ']') {
			b++;
			return 1;
		}
		while (1) {
			ScriptValue sv;
			int res = JSONdecodeSub(sv, b);
			int res2;
			if (sv.type != SCRIPT_NULL) {
				res2 = s.listVal->PushBack(sv);
			}
			if (!res || !res2) return 0;
			while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++;
			if (*b == ']') {
				b++;
				return 1;
			}
			// ???
			if (*b != ',') return 0;
			b++;
		}
	}
	else if (b[0] == '{') {
		if (!CreateDictValue(s, 4)) return 0;
		b++;
		while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++;
		if (*b == '}') {
			b++;
			return 1;
		}
		while (1) {
			ScriptValue sv, sv2;
			if (!JSONParseString(sv, b)) {
				sv.Release();
				return 0;
			}
			while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++;
			if (*b != ':') {
				sv.Release();
				return 0;
			}
			b++;
			int res = JSONdecodeSub(sv2, b);
			int res2 = s.dictVal->Add(sv, sv2);
			if (!res || !res2) return 0;
			while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++;
			if (*b == '}') {
				b++;
				return 1;
			}
			// ???
			if (*b != ',') return 0;
			b++;
		}
	}
	else if ((b[0] >= '0' && b[0] <= '9') || b[0] == '-' || b[0] == '.') {
		unsigned char *t1, *t2;
		__int64 i = strtoi64((char*)b, (char**)&t1, 0);
		double d = strtod((char*)b, (char**)&t2);
		if (t1 >= t2) {
			if (t1) {
				b = t1;
				CreateIntValue(s, i);
				return 1;
			}
		}
		else {
			b = t2;
			CreateDoubleValue(s, d);
			return 1;
		}
	}
	else {
		if (LCASE(b[0]) == 'n' && LCASE(b[1]) == 'u' && LCASE(b[2]) == 'l' && LCASE(b[3]) == 'l') {
			b+=4;
			return 1;
		}
		if (LCASE(b[0]) == 'f' && LCASE(b[1]) == 'a' && LCASE(b[2]) == 'l' && LCASE(b[3]) == 's' && LCASE(b[4]) == 'e') {
			b+=5;
			return 1;
		}
		if (LCASE(b[0]) == 't' && LCASE(b[1]) == 'r' && LCASE(b[2]) == 'u' && LCASE(b[3]) == 'e') {
			CreateIntValue(s, 1);
			b+=4;
			return 1;
		}
	}
	return 0;
}
Ejemplo n.º 4
0
int JSONParseString(ScriptValue &s, unsigned char * &b) {
	while (*b == ' ' || *b == '\t' || *b == '\n' || *b == '\r') b++;
	CreateNullValue(s);
	if (b[0] == '"') {
		unsigned char *e = ++b;
		while (*e && *e != '"') {
			if (*e == '\\' && e[1]) e++;
			e++;
		}
		if (AllocateStringValue(s, (int)(e-b))) {
			int p = 0;
			unsigned char *w = s.stringVal->value;
			while(b<e) {
				if (b[0] != '\\') {
					w[p++] = b[0];
					b++;
					continue;
				}
				b++;
				if (b[0] == 'n') {
					w[p] = '\n';
				}
				else if (b[0] == 'r') {
					w[p] = '\r';
				}
				else if (b[0] == 't') {
					w[p] = '\t';
				}
				else if (b[0] == 'f') {
					w[p] = '\f';
				}
				else if (b[0] == 'b') {
					w[p] = '\b';
				}
				else if (b[0] == '\\') {
					w[p] = '\\';
				}
				else if (b[0] == '/') {
					w[p] = '/';
				}
				else if (b[0] == '\"') {
					w[p] = '/';
				}
				else if (b[0] == 'u') {
					b++;
					wchar_t c = 0;
					int i;
					for (i=0; i<4; i++) {
						if (*b>= '0' && *b<='9') {
							c = c*16 + b[0] - '0';
						}
						else if (*b>= 'A' && *b<='F') {
							c = c*16 + b[0] - 'A'+10;
						}
						else if (*b>= 'a' && *b<='f') {
							c = c*16 + b[0] - 'a'+10;
						}
						else break;
						b++;
					}
					p += UTF16toUTF8(w+p, &c, 1);
				}
				else if (b[0]) {
					// Who cares?
					b[0] = w[p];
				}
				else {
					b--;
					break;
				}
				p++;
				b++;
			}
			ResizeStringValue(s, p);
			if (b < e || !*b) return 0;
			b++;
			return 1;
		}
	}
	return 0;
}
Ejemplo n.º 5
0
void Bedecode(ScriptValue &s, ScriptValue *args) {
	int pos = 0;
	if (!BeDecode(s, args[0].stringVal, &pos)) {
		CreateNullValue(s);
	}
}
Ejemplo n.º 6
0
int BeDecode(ScriptValue &sv, StringValue *s, int *pos) {
	if (s->len < pos[0]) return 0;
	if (s->value[pos[0]] == 'i') {
		pos[0] ++;
		unsigned char *end = strchr(s->value + pos[0], 'e');
		__int64 val;
		if (!ReadInt(s->value + pos[0], end, val)) return 0;
		CreateIntValue(sv, val);
		pos[0] = 1+(int)(end-s->value);
	}
	else if (s->value[pos[0]] == 'l') {
		pos[0] ++;
		CreateListValue(sv, 3);
		while (s->len > pos[0]) {
			if (s->value[pos[0]] == 'e') {
				pos[0] ++;
				return 1;
			}
			ScriptValue sv2;
			if (!BeDecode(sv2, s, pos) || !sv.listVal->PushBack(sv2)) {
				sv.Release();
				return 0;
			}
		}
		sv.Release();
		return 0;
	}
	else if (s->value[pos[0]] == 'd') {
		pos[0] ++;
		CreateDictValue(sv, 3);
		while (s->len > pos[0]) {
			if (s->value[pos[0]] == 'e') {
				pos[0] ++;
				return 1;
			}
			ScriptValue sv2, sv3;
			if (!BeDecode(sv2, s, pos)) {
				sv.Release();
				return 0;
			}
			if (sv2.type != SCRIPT_STRING || !BeDecode(sv3, s, pos)) {
				sv2.Release();
				sv.Release();
				return 0;
			}
			if (!sv.dictVal->Add(sv, sv2)) {
				sv.Release();
				return 0;
			}
		}
		sv.Release();
		return 0;
	}
	else if (s->value[pos[0]] == 'n') {
		CreateNullValue(sv);
		pos[0]++;
	}
	else if (s->value[pos[0]] == 'f') {
		pos[0]++;
		if (pos[0] + 8 > s->len) return 0;
		double d;
		memcpy(&d, s->value+pos[0], 8);
		pos[0] += 8;
		CreateDoubleValue(sv, d);
		return 1;
	}
	else {
		unsigned char *end = strchr(s->value + pos[0], ':');
		__int64 val;
		if (!ReadInt(s->value + pos[0], end, val) || val < 0) return 0;
		pos[0] = 1+(int)(end-s->value);
		if (pos[0] + val > s->len) return 0;
		if (!CreateStringValue(sv, s->value+pos[0], (int) val)) return 0;
		pos[0] += (int) val;
	}
	return 1;
}
Ejemplo n.º 7
0
 Value* Value::DeepCopy() const
 {
     // 函数仅被null Values调用, 子类需提供自己的实现.
     DCHECK(IsType(TYPE_NULL));
     return CreateNullValue();
 }