📄 wsbc.c
字号:
goto error; WS_UPDATE_DATA; decoded = ws_decode_buffer(data, data_len, WS_ENC_DATA, &ucp, (size_t) ui8, WS_ENC_END); if (decoded != ui8) goto error; WS_UPDATE_DATA; n->name = ws_memdup(ucp, ui8); if (n->name == NULL) goto error; /* Check the validity of the name. */ if (!ws_utf8_verify((unsigned char *) n->name, ui8, NULL)) goto error; /* Just check that the data contains only valid characters. */ for (l = 0; l < ui8; l++) { unsigned int ch = (unsigned char) n->name[l]; if (('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z') || ch == '_' || (l > 0 && ('0' <= ch && ch <= '9'))) /* Ok. */ continue; /* Invalid character in the function name. */ goto error; } /* Is the index valid? */ if (n->index >= num_functions) goto error; } } /* Functions. */ if (num_functions) { /* We have functions. */ bc->functions = ws_calloc(num_functions, sizeof(WsBcFunction)); if (bc->functions == NULL) goto error; bc->num_functions = num_functions; for (k = 0; k < bc->num_functions; k++) { WsBcFunction *f = &bc->functions[k]; decoded = ws_decode_buffer(data, data_len, WS_ENC_UINT8, &f->num_arguments, WS_ENC_UINT8, &f->num_locals, WS_ENC_MB_UINT32, &f->code_size, WS_ENC_END); if (!decoded) goto error; WS_UPDATE_DATA; decoded = ws_decode_buffer(data, data_len, WS_ENC_DATA, &ucp, f->code_size, WS_ENC_END); if (decoded != f->code_size) goto error; WS_UPDATE_DATA; if (f->code_size) { /* It is not an empty function. */ f->code = ws_memdup(ucp, f->code_size); if (f->code == NULL) goto error; } } } /* Did we process it all? */ if (data_len != 0) goto error; /* All done. */ return bc; /* * Error handling. */error: ws_bc_free(bc); return NULL;}/********************* Adding constant elements *************************/WsBool ws_bc_add_const_int(WsBc *bc, WsUInt16 *index_return, WsInt32 value){ WsUInt16 i; WsBcConstant *nc; /* Do we already have a suitable integer constant? */ for (i = 0; i < bc->num_constants; i++) { if (bc->constants[i].type == WS_BC_CONST_TYPE_INT && bc->constants[i].u.v_int == value) { *index_return = i; return WS_TRUE; } } /* Must add a new constant. */ nc = ws_realloc(bc->constants, (bc->num_constants + 1) * sizeof(WsBcConstant)); if (nc == NULL) return WS_FALSE; bc->constants = nc; bc->constants[bc->num_constants].type = WS_BC_CONST_TYPE_INT; bc->constants[bc->num_constants].u.v_int = value; *index_return = bc->num_constants++; return WS_TRUE;}WsBool ws_bc_add_const_float(WsBc *bc, WsUInt16 *index_return, WsFloat value){ WsUInt16 i; WsBcConstant *nc; /* Do we already have a suitable float32 constant? */ for (i = 0; i < bc->num_constants; i++) { if (bc->constants[i].type == WS_BC_CONST_TYPE_FLOAT32 && bc->constants[i].u.v_float == value) { *index_return = i; return WS_TRUE; } } /* Must add a new constant. */ nc = ws_realloc(bc->constants, (bc->num_constants + 1) * sizeof(WsBcConstant)); if (nc == NULL) return WS_FALSE; bc->constants = nc; bc->constants[bc->num_constants].type = WS_BC_CONST_TYPE_FLOAT32; bc->constants[bc->num_constants].u.v_float = value; *index_return = bc->num_constants++; return WS_TRUE;}WsBool ws_bc_add_const_utf8_string(WsBc *bc, WsUInt16 *index_return, const unsigned char *data, size_t len){ WsUInt16 i; WsBcConstant *nc; /* Do we already have a suitable utf8 constant? */ for (i = 0; i < bc->num_constants; i++) { if (bc->constants[i].type == WS_BC_CONST_TYPE_UTF8_STRING && bc->constants[i].u.v_string.len == len && memcmp(bc->constants[i].u.v_string.data, data, len) == 0) { *index_return = i; return WS_TRUE; } } /* Must add a new constant. */ nc = ws_realloc(bc->constants, (bc->num_constants + 1) * sizeof(WsBcConstant)); if (nc == NULL) return WS_FALSE; bc->constants = nc; bc->constants[bc->num_constants].type = WS_BC_CONST_TYPE_UTF8_STRING; bc->constants[bc->num_constants].u.v_string.len = len; bc->constants[bc->num_constants].u.v_string.data = ws_memdup(data, len); if (bc->constants[bc->num_constants].u.v_string.data == NULL) return WS_FALSE; *index_return = bc->num_constants++; return WS_TRUE;}WsBool ws_bc_add_const_empty_string(WsBc *bc, WsUInt16 *index_return){ WsUInt16 i; WsBcConstant *nc; /* Do we already have a suitable empty string constant? */ for (i = 0; i < bc->num_constants; i++) { if (bc->constants[i].type == WS_BC_CONST_TYPE_EMPTY_STRING) { *index_return = i; return WS_TRUE; } } /* Must add a new constant. */ nc = ws_realloc(bc->constants, (bc->num_constants + 1) * sizeof(WsBcConstant)); if (nc == NULL) return WS_FALSE; bc->constants = nc; bc->constants[bc->num_constants].type = WS_BC_CONST_TYPE_EMPTY_STRING; *index_return = bc->num_constants++; return WS_TRUE;}/********************* Adding pragmas ***********************************/WsBool ws_bc_add_pragma_access_domain(WsBc *bc, const unsigned char *domain, size_t domain_len){ WsBcPragma *p = add_pragma(bc, WS_BC_PRAGMA_TYPE_ACCESS_DOMAIN); if (p == NULL) return WS_FALSE; if (!ws_bc_add_const_utf8_string(bc, &p->index_1, domain, domain_len)) return WS_FALSE; return WS_TRUE;}WsBool ws_bc_add_pragma_access_path(WsBc *bc, const unsigned char *path, size_t path_len){ WsBcPragma *p = add_pragma(bc, WS_BC_PRAGMA_TYPE_ACCESS_PATH); if (p == NULL) return WS_FALSE; if (!ws_bc_add_const_utf8_string(bc, &p->index_1, path, path_len)) return WS_FALSE; return WS_TRUE;}WsBool ws_bc_add_pragma_user_agent_property(WsBc *bc, const unsigned char *name, size_t name_len, const unsigned char *property, size_t property_len){ WsBcPragma *p = add_pragma(bc, WS_BC_PRAGMA_TYPE_USER_AGENT_PROPERTY); if (p == NULL) return WS_FALSE; if (!ws_bc_add_const_utf8_string(bc, &p->index_1, name, name_len) || !ws_bc_add_const_utf8_string(bc, &p->index_2, property, property_len)) return WS_FALSE; return WS_TRUE;}WsBool ws_bc_add_pragma_user_agent_property_and_scheme( WsBc *bc, const unsigned char *name, size_t name_len, const unsigned char *property, size_t property_len, const unsigned char *scheme, size_t scheme_len){ WsBcPragma *p; p = add_pragma(bc, WS_BC_PRAGMA_TYPE_USER_AGENT_PROPERTY_AND_SCHEME); if (p == NULL) return WS_FALSE; if (!ws_bc_add_const_utf8_string(bc, &p->index_1, name, name_len) || !ws_bc_add_const_utf8_string(bc, &p->index_2, property, property_len) || !ws_bc_add_const_utf8_string(bc, &p->index_3, scheme, scheme_len)) return WS_FALSE; return WS_TRUE;}/********************* Adding functions *********************************/WsBool ws_bc_add_function(WsBc *bc, WsUInt8 *index_return, char *name, WsUInt8 num_arguments, WsUInt8 num_locals, WsUInt32 code_size, unsigned char *code){ WsBcFunction *nf; /* First, add the function to the function pool. */ nf = ws_realloc(bc->functions, (bc->num_functions + 1) * sizeof(WsBcFunction)); if (nf == NULL) return WS_FALSE; bc->functions = nf; bc->functions[bc->num_functions].num_arguments = num_arguments; bc->functions[bc->num_functions].num_locals = num_locals; bc->functions[bc->num_functions].code_size = code_size; bc->functions[bc->num_functions].code = ws_memdup(code, code_size); if (bc->functions[bc->num_functions].code == NULL) return WS_FALSE; /* Save the index of the function. */ *index_return = bc->num_functions++; /* For external functions (which have name), add a name entry to the function name pool. */ if (name) { WsBcFunctionName *nfn; nfn = ws_realloc(bc->function_names, ((bc->num_function_names + 1) * sizeof(WsBcFunctionName))); if (nfn == NULL) return WS_FALSE; bc->function_names = nfn; bc->function_names[bc->num_function_names].index = *index_return; bc->function_names[bc->num_function_names].name = ws_strdup(name); if (bc->function_names[bc->num_function_names].name == NULL) return WS_FALSE; bc->num_function_names++; } /* All done. */ return WS_TRUE;}/********************* Static functions *********************************/static WsBcPragma *add_pragma(WsBc *bc, WsBcPragmaType type){ WsBcPragma *np; /* Add a new pragma slot. */ np = ws_realloc(bc->pragmas, (bc->num_pragmas + 1) * sizeof(WsBcPragma)); if (np == NULL) return NULL; bc->pragmas = np; bc->pragmas[bc->num_pragmas].type = type; return &bc->pragmas[bc->num_pragmas++];}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -