Ejemplo n.º 1
0
SRE_NOAPI sre_regex_range_t *
sre_regex_turn_char_class_caseless(sre_pool_t *pool, sre_regex_range_t *range)
{
    sre_char              from, to;
    sre_regex_range_t    *r, *nr;

    for (r = range; r; r = r->next) {
        from = r->from;
        to   = r->to;

        if (to >= 'A' && from <= 'Z') {
            /* overlap with A-Z */

            nr = sre_palloc(pool, sizeof(sre_regex_range_t));
            if (nr == NULL) {
                return NULL;
            }

            nr->from = sre_max(from, 'A') + 32;
            nr->to = sre_min(to, 'Z') + 32;
            nr->next = r->next;

            r->next = nr;
            r = nr;
        }

        if (to >= 'a' && from <= 'z') {
            /* overlap with a-z */

            nr = sre_palloc(pool, sizeof(sre_regex_range_t));
            if (nr == NULL) {
                return NULL;
            }

            nr->from = sre_max(from, 'a') - 32;
            nr->to = sre_min(to, 'z') - 32;
            nr->next = r->next;

            r->next = nr;
            r = nr;
        }
    }

    return range;
}
Ejemplo n.º 2
0
SRE_API sre_vm_thompson_ctx_t *
sre_vm_thompson_jit_create_ctx(sre_pool_t *pool, sre_program_t *prog)
{
    sre_uint_t                       size, len;
    sre_vm_thompson_ctx_t           *ctx;
    sre_vm_thompson_thread_list_t   *clist, *nlist;

    size = sre_vm_thompson_jit_get_threads_added_size(prog);

    dd("threads_added size: %d", (int) size);

    ctx = sre_palloc(pool, sizeof(sre_vm_thompson_ctx_t) - 1 + size);
    if (ctx == NULL) {
        return NULL;
    }

    dd("ctx->threads_added: %x",
       (int) offsetof(sre_vm_thompson_ctx_t, threads_added));

    ctx->pool = pool;
    ctx->program = prog;

    len = prog->uniq_threads;

    clist = sre_vm_thompson_create_thread_list(pool, len);
    if (clist == NULL) {
        return NULL;
    }

    ctx->current_threads = clist;

    nlist = sre_vm_thompson_create_thread_list(pool, len);
    if (nlist == NULL) {
        return NULL;
    }

    ctx->next_threads = nlist;

    ctx->tag = prog->tag + 1;
    ctx->first_buf = 1;

    return ctx;
}
Ejemplo n.º 3
0
sre_vm_thompson_ctx_t *
sre_vm_thompson_init(sre_pool_t *pool, sre_program_t *prog)
{
    unsigned                         len;
    sre_vm_thompson_ctx_t           *ctx;
    sre_vm_thompson_thread_list_t   *clist, *nlist;

    ctx = sre_palloc(pool, sizeof(sre_vm_thompson_ctx_t));
    if (ctx == NULL) {
        return NULL;
    }

    ctx->pool = pool;
    ctx->program = prog;

    len = prog->len;

    clist = sre_vm_thompson_thread_list_create(pool, len);
    if (clist == NULL) {
        return NULL;
    }

    ctx->current_threads = clist;

    nlist = sre_vm_thompson_thread_list_create(pool, len);
    if (nlist == NULL) {
        return NULL;
    }

    ctx->next_threads = nlist;

    ctx->tag = prog->tag + 1;
    ctx->first_buf = 1;

    return ctx;
}
Ejemplo n.º 4
0
static sre_int_t
sre_program_get_leading_bytes_helper(sre_pool_t *pool, sre_instruction_t *pc,
                                     sre_program_t *prog, sre_chain_t **res, unsigned tag)
{
    sre_int_t            rc;
    sre_chain_t         *cl, *ncl;
    sre_instruction_t   *bc;

    if (pc->tag == tag) {
        return SRE_OK;
    }

    if (pc == prog->start + 1) {
        /* skip the dot (.) in the initial boilerplate ".*?" */
        return SRE_OK;
    }

    pc->tag = tag;

    switch (pc->opcode) {
    case SRE_OPCODE_SPLIT:
        rc = sre_program_get_leading_bytes_helper(pool, pc->x, prog, res,
                tag);
        if (rc != SRE_OK) {
            return rc;
        }

        return sre_program_get_leading_bytes_helper(pool, pc->y, prog, res,
                tag);

    case SRE_OPCODE_JMP:
        return sre_program_get_leading_bytes_helper(pool, pc->x, prog, res,
                tag);

    case SRE_OPCODE_SAVE:
        if (++pc == prog->start + prog->len) {
            return SRE_OK;
        }

        return sre_program_get_leading_bytes_helper(pool, pc, prog, res,
                tag);

    case SRE_OPCODE_MATCH:
        prog->nullable = 1;
        return SRE_DONE;

    case SRE_OPCODE_ASSERT:
        if (++pc == prog->start + prog->len) {
            return SRE_OK;
        }

        return sre_program_get_leading_bytes_helper(pool, pc, prog, res, tag);

    case SRE_OPCODE_ANY:
        return SRE_DECLINED;

    default:
        /* CHAR, ANY, IN, NOTIN */

        ncl = sre_palloc(pool, sizeof(sre_chain_t));
        if (ncl == NULL) {
            return SRE_ERROR;
        }

        ncl->data = pc;
        ncl->next = NULL;

        if (*res) {
            for (cl = *res; /* void */; cl = cl->next) {
                bc = cl->data;
                if (bc->opcode == pc->opcode) {
                    if (bc->opcode == SRE_OPCODE_CHAR) {
                        if (bc->v.ch == pc->v.ch) {
                            return SRE_OK;
                        }
                    }
                }

                if (cl->next == NULL) {
                    cl->next = ncl;
                    return SRE_OK;
                }
            }

        } else {
            *res = ncl;
        }

        return SRE_OK;
    }

    /* impossible to reach here */
}