bool ParsedStr::parse(const char *str) { assert(NULL == _str); /* don't call me twice */ _str = strdup(str); int str_count = 0; char *s = (char*)_str; while (NULL != delim_str_iter(&s)) { ++str_count; } if (str_count % 2 != 0) { /* malformed string */ return false; } _count = str_count / 2; _names = (const char**)malloc(_count * sizeof(_names[0])); _values = (const char**)malloc(_count * sizeof(_values[0])); s = (char*)_str; int idx = 0; while (*s) { _names[idx] = s; str_skip(&s); _values[idx] = s; str_skip(&s); ++idx; } assert(idx == _count); return true; }
/* * Returns the Authorization type from the headers * Does not modify the header hashtable. */ gchar * cockpit_auth_parse_authorization_type (GHashTable *headers) { gchar *line; gchar *type; gchar *next; gpointer key; gsize i; /* Avoid copying as it can contain passwords */ if (!g_hash_table_lookup_extended (headers, "Authorization", &key, (gpointer *)&line)) return NULL; line = str_skip (line, ' '); next = strchr (line, ' '); if (!next) return NULL; type = g_strndup (line, next - line); for (i = 0; type[i] != '\0'; i++) type[i] = g_ascii_tolower (type[i]); return type; }
GBytes * cockpit_auth_parse_authorization (GHashTable *headers, gchar **type) { gchar *line; gchar *next; gchar *contents; gsize length; gpointer key; gsize i; /* Avoid copying as it can contain passwords */ if (!g_hash_table_lookup_extended (headers, "Authorization", &key, (gpointer *)&line)) return NULL; g_hash_table_steal (headers, "Authorization"); g_free (key); line = str_skip (line, ' '); next = strchr (line, ' '); if (!next) { g_free (line); return NULL; } contents = str_skip (next, ' '); if (g_base64_decode_inplace (contents, &length) == NULL) { g_free (line); return NULL; } /* Null terminate for convenience, but null count not included in GBytes */ contents[length] = '\0'; if (type) { *type = g_strndup (line, next - line); for (i = 0; (*type)[i] != '\0'; i++) (*type)[i] = g_ascii_tolower ((*type)[i]); } /* Avoid copying by using the line directly */ return g_bytes_new_with_free_func (contents, length, clear_free_authorization, line); }
void radc_compiler::compile(int options) { if (_cache_program != -1) { varlist = programs[_cache_program].value->varlist; statements = programs[_cache_program].value->statements; return ; } char * str = buffer.c_ptr(); int line = 1; int depth = 0; _options = options; while (*str && !is_error()) { str = str_skip(str, ' '); if (*str == '#') // comment { while (*str && *str != '\n') ++str; if (*str == '\n') ++str; continue; } char * line_str = str; int line_length = 0; while (*str && *str != '\n') { ++str; ++line_length; } if (*str == '\n') { *str = 0; ++str; } str_trim(line_str, line_length); if (*line_str) { const char * matched_str = NULL; radc_stat * stat = NULL; bool insert_empty = false; do { matched_str = str_match(line_str, "function"); if (matched_str && (*matched_str == ' ' || *matched_str == '\0')) { matched_str = str_skip(matched_str, ' '); matched_str = str_match(matched_str, "main"); if (matched_str) { stat = new rstat_entry; stat->str = str_skip(matched_str, ' '); } else { set_error("Error: [%d] - main.", line); } depth += 1; break; } matched_str = str_match(line_str, "if"); if (matched_str && (*matched_str == ' ' || *matched_str == '\0')) { stat = new rstat_if; stat->str = str_skip(matched_str, ' '); insert_empty = true; depth += 1; break; } matched_str = str_match(line_str, "else"); if (matched_str && (*matched_str == ' ' || *matched_str == '\0')) { matched_str = str_skip(matched_str, ' '); const char * matched_str2 = str_match(matched_str, "if"); if (matched_str2 && (*matched_str2 == ' ' || *matched_str2 == '\0')) { stat = new rstat_elseif; stat->str = str_skip(matched_str2, ' '); insert_empty = true; } else { stat = new rstat_else; stat->str = str_skip(matched_str, ' '); insert_empty = true; } break; } matched_str = str_match(line_str, "end"); if (matched_str && (*matched_str == ' ' || *matched_str == '\0')) { stat = new rstat_end; stat->str = str_skip(matched_str, ' '); depth -= 1; break; } matched_str = str_match(line_str, "while"); if (matched_str && (*matched_str == ' ' || *matched_str == '\0')) { stat = new rstat_while; stat->str = str_skip(matched_str, ' '); insert_empty = true; depth += 1; break; } matched_str = str_match(line_str, "return"); if (matched_str && (*matched_str == ' ' || *matched_str == '\0')) { stat = new rstat_return; stat->str = str_skip(matched_str, ' '); break; } stat = new rstat_exp; stat->str = str_skip(line_str, ' '); } while (0); if (stat != NULL) { stat->line = line; statements.PushBack(stat); if (insert_empty) { statements.PushBack(new rstat_empty); } } } ++line; } if (!is_error() && depth != 0) { set_error("Error: end don't macthed."); } // if Ìøת for (int i = 0; i < statements.Size(); ++i) { if (TYPE_OF(rstat_if, statements[i]) || TYPE_OF(rstat_elseif, statements[i]) || TYPE_OF(rstat_else, statements[i])) { int in_if = 0, end_if = 0; for (int j = i + 1; j < statements.Size(); ++j) { if (TYPE_OF(rstat_elseif, statements[j]) || TYPE_OF(rstat_else, statements[j]) || TYPE_OF(rstat_end, statements[j])) { if (in_if == 0) { end_if = j - 1; statements[i]->jump = j; break; } } if (TYPE_OF(rstat_if, statements[j]) || TYPE_OF(rstat_while, statements[j])) { in_if += 1; } if (TYPE_OF(rstat_end, statements[j])) { in_if -= 1; } } d_assert (end_if > i); in_if = 0; for (int j = end_if + 1; j < statements.Size(); ++j) { if (TYPE_OF(rstat_end, statements[j])) { if (in_if == 0) { statements[end_if]->jump = j; break; } } if (TYPE_OF(rstat_if, statements[j]) || TYPE_OF(rstat_while, statements[j])) { in_if += 1; } if (TYPE_OF(rstat_end, statements[j])) { in_if -= 1; } } } } // while Ìøת for (int i = 0; i < statements.Size(); ++i) { if (TYPE_OF(rstat_while, statements[i])) { int in_while = 0, end_while = 0; for (int j = i + 1; j < statements.Size(); ++j) { if (TYPE_OF(rstat_end, statements[j])) { if (in_while == 0) { end_while = j - 1; statements[i]->jump = j; break; } } if (TYPE_OF(rstat_if, statements[j]) || TYPE_OF(rstat_while, statements[j])) { in_while += 1; } if (TYPE_OF(rstat_end, statements[j])) { in_while -= 1; } } d_assert (end_while > i); statements[end_while]->jump = i; } } for (int i = 0; i < statements.Size(); ++i) { statements[i]->build(); } }