bool TestExtFile::test_fread() {
  Variant f = f_fopen("test/test_ext_file.tmp", "w");
  f_fputs(f, "testing fread");
  f_fclose(f);

  f = f_fopen("test/test_ext_file.tmp", "r");
  VS(f_fread(f, 7), "testing");
  VS(f_fread(f, 100), " fread");
  return Count(true);
}
bool TestExtFile::test_fseek() {
  Variant f = f_fopen("test/test_ext_file.tmp", "w");
  f_fputs(f, "testing fseek");
  f_fclose(f);

  f = f_fopen("test/test_ext_file.tmp", "r");
  f_fseek(f, -5, k_SEEK_END);
  VS(f_fread(f, 7), "fseek");

  f_fseek(f, 7);
  VS(f_fread(f, 7), " fseek");
  return Count(true);
}
Exemple #3
0
bool f_hash_update_file(CObjRef init_context, CStrRef filename,
                        CObjRef stream_context /* = null */) {
  Variant f = f_fopen(filename, "rb");
  if (same(f, false)) {
    return false;
  }

  HashContext *hash = init_context.getTyped<HashContext>();
  for (Variant chunk = f_fread(f, 1024); !is_empty_string(chunk);
       chunk = f_fread(f, 1024)) {
    String schunk = chunk.toString();
    hash->ops->hash_update(hash->context, (unsigned char *)schunk.data(),
                           schunk.size());
  }
  return true;
}
bool TestExtNetwork::test_pfsockopen() {
  Variant f = f_pfsockopen("facebook.com", 80);
  VERIFY(!same(f, false));
  f_fputs(f, "GET / HTTP/1.0\n\n");
  VERIFY(!f_fread(f, 15).toString().empty());
  return Count(true);
}
bool TestExtFile::test_popen() {
  Variant f = f_fopen("test/test_ext_file.tmp", "w");
  f_fputs(f, "testing popen");
  f_fclose(f);

  f = f_popen("cat test/test_ext_file.tmp", "r");
  VS(f_fread(f, 20), "testing popen");
  return Count(true);
}
Exemple #6
0
static Variant php_hash_do_hash_hmac(CStrRef algo, CStrRef data,
                                     bool isfilename, CStrRef key,
                                     bool raw_output /* = false */) {
  HashEnginePtr ops = php_hash_fetch_ops(algo);
  if (!ops) {
    raise_warning("Unknown hashing algorithm: %s", algo.data());
    return false;
  }
  Variant f;
  if (isfilename) {
    f = f_fopen(data, "rb");
    if (same(f, false)) {
      return false;
    }
  }

  void *context = malloc(ops->context_size);
  ops->hash_init(context);

  char *K = prepare_hmac_key(ops, context, key);

  if (isfilename) {
    for (Variant chunk = f_fread(f, 1024); !is_empty_string(chunk);
         chunk = f_fread(f, 1024)) {
      String schunk = chunk.toString();
      ops->hash_update(context, (unsigned char *)schunk.data(), schunk.size());
    }
  } else {
    ops->hash_update(context, (unsigned char *)data.data(), data.size());
  }

  String raw = String(ops->digest_size, ReserveString);
  char *digest = raw.mutableSlice().ptr;
  ops->hash_final((unsigned char *)digest, context);
  finalize_hmac_key(K, ops, context, digest);
  free(context);

  raw.setSize(ops->digest_size);
  if (raw_output) {
    return raw;
  }
  return StringUtil::HexEncode(raw);
}
bool TestExtFile::test_feof() {
  Variant f = f_fopen("test/test_ext_file.tmp", "w");
  f_fputs(f, "testing feof");
  f_fclose(f);

  f = f_fopen("test/test_ext_file.tmp", "r");
  VERIFY(!f_feof(f));
  VS(f_fread(f, 20), "testing feof");
  VERIFY(f_feof(f));
  return Count(true);
}
bool TestExtFile::test_read_write() {
  Variant f = f_fopen("test/test_ext_file.tmp", "w");
  f_fwrite(f, "testing read/write");
  f_fclose(f);

  f = f_fopen("test/test_ext_file.tmp", "r+");
  f_fseek(f, 8);
  f_fwrite(f, "succeeds");
  f_fseek(f, 8);
  VS(f_fread(f, 8), "succeeds");
  return Count(true);
}
bool TestExtFile::test_ftruncate() {
  Variant f = f_fopen("test/ext/test_ext_file.tmp", "w");
  f_fputs(f, "testing ftruncate");
  f_fclose(f);

  f = f_fopen("test/ext/test_ext_file.tmp", "r+");
  f_ftruncate(f, 7);
  f_fclose(f);

  f = f_fopen("test/ext/test_ext_file.tmp", "r");
  VS(f_fread(f, 20), "testing");
  return Count(true);
}
bool TestExtMailparse::test_mailparse_stream_encode() {
    {
        String text = "hello, this is some text=hello.";
        Variant fp = f_tmpfile();
        f_fwrite(fp, text);
        f_rewind(fp);
        Variant dest = f_tmpfile();
        f_mailparse_stream_encode(fp, dest, "quoted-printable");
        f_rewind(dest);
        Variant data = f_fread(dest, 2048);
        VS(data, "hello, this is some text=3Dhello.");
    }
    {
        String text =
            "To: [email protected]\n"
            "\n"
            "blah blah blah From blah $ \" & £ blah blah blah blah blah\n"
            "From the first of the month, things will be different!\n"
            "blah blah blah From blah\n"
            "Frome is a town in Somerset.";

        f_ob_start();

        Variant fp = f_tmpfile();
        f_fwrite(fp, text);
        f_rewind(fp);

        Variant fpdest = f_tmpfile();
        f_mailparse_stream_encode(fp, fpdest, "quoted-printable");
        f_rewind(fpdest);
        f_fpassthru(fpdest);

        f_fclose(fp);
        f_fclose(fpdest);

        String output = f_ob_get_contents();
        f_ob_end_clean();
        VS(output,
           "To: [email protected]\r\n"
           "\r\n"
           "blah blah blah From blah $ \" & =C2=A3 blah blah blah blah blah\r\n"
           "=46rom the first of the month, things will be different!\r\n"
           "blah blah blah From blah\r\n"
           "Frome is a town in Somerset.");
    }

    return Count(true);
}
Exemple #11
0
int64_t f_hash_update_stream(CObjRef context, CObjRef handle,
                         int length /* = -1 */) {
  HashContext *hash = context.getTyped<HashContext>();
  int didread = 0;
  while (length) {
    Variant chunk = f_fread(handle, length > 0 ? length : 1024);
    if (is_empty_string(chunk)) {
      return didread;
    }
    String schunk = chunk.toString();
    hash->ops->hash_update(hash->context, (unsigned char *)schunk.data(),
                           schunk.size());
    didread += schunk.size();
    length -= schunk.size();
  }
  return didread;
}
Exemple #12
0
int libxml_streams_IO_read(void* context, char* buffer, int len) {
    ITRACE(1, "libxml_IO_read({}, {}, {})\n", context, (void*)buffer, len);
    Trace::Indent _i;

    Resource stream(static_cast<ResourceData*>(context));
    assert(len >= 0);
    Variant ret = f_fread(stream, len);
    if (ret.isString()) {
        const String& str = ret.asCStrRef();
        if (str.size() <= len) {
            std::memcpy(buffer, str.data(), str.size());
            return str.size();
        }
    }

    return -1;
}
bool TestExtNetwork::test_fsockopen() {
  {
    Variant f = f_fsockopen("facebook.com", 80);
    VERIFY(!same(f, false));
    f_fputs(f, "GET / HTTP/1.0\n\n");
    VERIFY(!f_fread(f, 15).toString().empty());
  }
  {
    Variant f = f_fsockopen("ssl://www.facebook.com", 443);
    VERIFY(!same(f, false));
    f_fwrite(f,
             "GET / HTTP/1.1\r\n"
             "Host: www.facebook.com\r\n"
             "Connection: Close\r\n"
             "\r\n");
    StringBuffer response;
    while (!same(f_feof(f), true)) {
      Variant line = f_fgets(f, 128);
      response.append(line.toString());
    }
    VERIFY(!response.detach().empty());
  }
  return Count(true);
}
Exemple #14
0
Variant HHVM_FUNCTION(bzread, const Resource& bz, int length /* = 1024 */) {
  return f_fread(bz, length);
}
Exemple #15
0
Variant f_bzread(CObjRef bz, int length /* = 1024 */) {
  return f_fread(bz, length);
}