Exemplo n.º 1
0
static VALUE Buffer_read(int argc, VALUE* argv, VALUE self)
{
    VALUE out = Qnil;
    unsigned long n = -1;
    bool all = false;

    switch(argc) {
    case 2:
        out = argv[1];
        /* pass through */
    case 1:
        n = FIX2ULONG(argv[0]);
        break;
    case 0:
        all = true;
        break;
    default:
        rb_raise(rb_eArgError, "wrong number of arguments (%d for 0..2)", argc);
    }

    BUFFER(self, b);

    if(out != Qnil) {
        CHECK_STRING_TYPE(out);
    }

    if(all) {
        return read_all(b, out);
    }

    if(n == 0) {
        /* do nothing */
        MAKE_EMPTY_STRING(out);
        return out;
    }

#ifndef DISABLE_BUFFER_READ_TO_S_OPTIMIZE
    if(!msgpack_buffer_has_io(b) && out == Qnil &&
            msgpack_buffer_all_readable_size(b) <= n) {
        /* same as to_s && clear; optimize */
        VALUE str = msgpack_buffer_all_as_string(b);
        msgpack_buffer_clear(b);

        if(RSTRING_LEN(str) == 0) {
            return Qnil;
        } else {
            return str;
        }
    }
#endif

    MAKE_EMPTY_STRING(out);
    read_until_eof(b, out, n);

    if(RSTRING_LEN(out) == 0) {
        return Qnil;
    } else {
        return out;
    }
}
Exemplo n.º 2
0
int main()
{
    uint32_t dummy;
    uint32_t len;
    char* buff;
    js_image_t* image;
    uint32_t i, j, op;
    double number;
    js_gc_init(&dummy);
    buff = read_until_eof(stdin, &len);
    image = js_image_parse(buff, len);
    printf("read %d sections\n", image->section_count);
    for(i = 0; i < image->section_count; i++) {
        printf("\nsection %d (flags %d, var count: %d):\n", i, image->sections[i].flags, image->sections[i].var_count);
        for(j = 0; j < image->sections[i].instruction_count; j++) {
            op = image->sections[i].instructions[j];
            printf("    %04d  %-12s", j, js_instruction(op)->name);
            switch(js_instruction(op)->operand) {
            case OPERAND_NONE:
                printf("\n");
                break;
            case OPERAND_NUMBER:
                number = *(double*)&image->sections[i].instructions[++j];
                printf("%lf\n", number);
                j++;
                break;
            case OPERAND_UINT32:
                op = image->sections[i].instructions[++j];
                printf("%u\n", op);
                break;
            case OPERAND_UINT32_UINT32:
                op = image->sections[i].instructions[++j];
                printf("%u, ", op);
                op = image->sections[i].instructions[++j];
                printf("%u\n", op);
                break;
            case OPERAND_STRING:
                op = image->sections[i].instructions[++j];
                printf("\"%s\" (%d)\n", image->strings[op]->buff, op);
                break;
            case OPERAND_UINT32_STRING:
                op = image->sections[i].instructions[++j];
                printf("%u, ", op);
                op = image->sections[i].instructions[++j];
                printf("\"%s\" (%d)\n", image->strings[op]->buff, op);
                break;
            }
        }
    }
    printf("\nstrings:\n");
    for(i = 0; i < image->string_count; i++) {
        printf("    %04d  \"%s\"\n", i, image->strings[i]->buff);
    }

    return 0;
}
Exemplo n.º 3
0
static inline VALUE read_all(msgpack_buffer_t* b, VALUE out)
{
#ifndef DISABLE_BUFFER_READ_TO_S_OPTIMIZE
    if(out == Qnil && !msgpack_buffer_has_io(b)) {
        /* same as to_s && clear; optimize */
        VALUE str = msgpack_buffer_all_as_string(b);
        msgpack_buffer_clear(b);
        return str;
    }
#endif
    MAKE_EMPTY_STRING(out);
    read_until_eof(b, out, 0);
    return out;
}
Exemplo n.º 4
0
static VALUE Buffer_skip(VALUE self, VALUE sn)
{
    BUFFER(self, b);

    unsigned long n = FIX2ULONG(sn);

    /* do nothing */
    if(n == 0) {
        return ULONG2NUM(0);
    }

    size_t sz = read_until_eof(b, Qnil, n);
    return ULONG2NUM(sz);
}
Exemplo n.º 5
0
static int
mock_reauthorize (const char *mode,
                  const char *user,
                  const char *argument,
                  char **output)
{
  int fds[2];
  pid_t pid;
  int status;

  const char *argv[] = {
      BUILDDIR "/mock-reauthorize",
      mode,
      user,
      argument,
      NULL
  };

  if (output)
    {
      if (pipe (fds) < 0)
        assert_not_reached ();
    }

  pid = fork ();
  if (pid == 0)
    {
      if (output)
        dup2 (fds[1], 1);
      execv (argv[0], (char **)argv);
      fprintf (stderr, "exec failed: %s: %m\n", argv[0]);
      _exit (127);
    }

  if (output)
    {
      close (fds[1]);
      *output = read_until_eof (fds[0]);
      close (fds[0]);
    }

  assert_num_eq (waitpid (pid, &status, 0), pid);

  assert (WIFEXITED (status));
  if (WEXITSTATUS (status) == 77)
    re_test_skip ("need to 'make enable-root-tests'");

  return WEXITSTATUS (status);
}