コード例 #1
0
TEST(Base64Tests, FromBase64)
{
    EXPECT_EQ("", from_base64(""));
    EXPECT_EQ("a", from_base64("YQ=="));
    EXPECT_EQ("ab", from_base64("YWI="));
    EXPECT_EQ("abc", from_base64("YWJj"));
    EXPECT_EQ("abcd", from_base64("YWJjZA=="));
}
コード例 #2
0
ファイル: attr.c プロジェクト: halgandd/bacula
int unpack_attributes_record(JCR *jcr, int32_t stream, char *rec, ATTR *attr)
{
   char *p;
   /*
    * An Attributes record consists of:
    *    File_index
    *    Type   (FT_types)
    *    Filename
    *    Attributes
    *    Link name (if file linked i.e. FT_LNK)
    *    Extended attributes (Win32)
    *  plus optional values determined by AR_ flags in upper bits of Type
    *    Data_stream
    *
    */
   attr->stream = stream;
   Dmsg1(400, "Attr: %s\n", rec);
   if (sscanf(rec, "%d %d", &attr->file_index, &attr->type) != 2) {
      Jmsg(jcr, M_FATAL, 0, _("Error scanning attributes: %s\n"), rec);
      Dmsg1(100, "\nError scanning attributes. %s\n", rec);
      return 0;
   }
   Dmsg2(400, "Got Attr: FilInx=%d type=%d\n", attr->file_index, attr->type);
   if (attr->type & AR_DATA_STREAM) {
      attr->data_stream = 1;
   } else {
      attr->data_stream = 0;
   }
   attr->type &= FT_MASK;             /* keep only type bits */
   p = rec;
   while (*p++ != ' ')               /* skip record file index */
      { }
   while (*p++ != ' ')               /* skip type */
      { }

   attr->fname = p;                   /* set filname position */
   while (*p++ != 0)                  /* skip filename */
      { }
   attr->attr = p;                    /* set attributes position */
   while (*p++ != 0)                  /* skip attributes */
      { }
   attr->lname = p;                   /* set link position */
   while (*p++ != 0)                  /* skip link */
      { }
   pm_strcpy(attr->attrEx, p);        /* copy extended attributes, if any */

   if (attr->data_stream) {
      int64_t val;
      while (*p++ != 0)               /* skip extended attributes */
         { }
      from_base64(&val, p);
      attr->data_stream = (int32_t)val;
   }
   Dmsg7(400, "unpack_attr FI=%d Type=%d fname=%s attr=%s lname=%s attrEx=%s ds=%d\n",
      attr->file_index, attr->type, attr->fname, attr->attr, attr->lname,
      attr->attrEx, attr->data_stream);
   *attr->ofname = 0;
   *attr->olname = 0;
   return 1;
}
コード例 #3
0
ファイル: backup_phase2.c プロジェクト: Sherlock221B/burp
static uint64_t decode_req(const char *buf)
{
	int64_t val;
	const char *p=buf;
	p+=from_base64(&val, p);
	return (uint64_t)val;
}
コード例 #4
0
ファイル: base64.c プロジェクト: Ecordonnier/OpenSC
int sc_base64_decode(const char *in, u8 *out, size_t outlen)
{
	int len = 0, r, skip;
	unsigned int i;

	while ((r = from_base64(in, &i, &skip)) > 0) {
		int finished = 0, s = 16;

		if (r < 3)
			finished = 1;
		while (r--) {
			if (outlen <= 0)
				return SC_ERROR_BUFFER_TOO_SMALL;
			*out++ = i >> s;
			s -= 8;
			outlen--;
			len++;
		}
		in += skip;
		if (finished || *in == 0)
			return len;
	}
	if (r == 0)
		return len;
	return SC_ERROR_INVALID_ARGUMENTS;
}
コード例 #5
0
ファイル: attribs.c プロジェクト: anarexia/bacula
/** Decode a LinkFI field of encoded stat packet */
int32_t decode_LinkFI(char *buf, struct stat *statp, int stat_size)
{
   char *p = buf;
   int64_t val;
   /*
    * We store into the stat packet so make sure the caller's conception
    *  is the same as ours.  They can be different if LARGEFILE is not
    *  the same when compiling this library and the calling program.
    */
   ASSERT(stat_size == (int)sizeof(struct stat));

   skip_nonspaces(&p);                /* st_dev */
   p++;                               /* skip space */
   skip_nonspaces(&p);                /* st_ino */
   p++;
   p += from_base64(&val, p);
   plug(statp->st_mode, val);         /* st_mode */
   p++;
   skip_nonspaces(&p);                /* st_nlink */
   p++;
   skip_nonspaces(&p);                /* st_uid */
   p++;
   skip_nonspaces(&p);                /* st_gid */
   p++;
   skip_nonspaces(&p);                /* st_rdev */
   p++;
   skip_nonspaces(&p);                /* st_size */
   p++;
   skip_nonspaces(&p);                /* st_blksize */
   p++;
   skip_nonspaces(&p);                /* st_blocks */
   p++;
   skip_nonspaces(&p);                /* st_atime */
   p++;
   skip_nonspaces(&p);                /* st_mtime */
   p++;
   skip_nonspaces(&p);                /* st_ctime */

   /* Optional FileIndex of hard linked file data */
   if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
      p++;
      p += from_base64(&val, p);
      return (int32_t)val;
   }
   return 0;
}
コード例 #6
0
ファイル: attribs.c プロジェクト: Sherlock221B/burp
uint64_t decode_file_no_and_save_path(struct iobuf *iobuf, char **save_path)
{
	int64_t val;
	char *p=iobuf->buf;
	p+=from_base64(&val, iobuf->buf);
	*save_path=p+1;
	return (uint64_t)val;
}
コード例 #7
0
ファイル: mcp56.c プロジェクト: ctz/cryptopals
int main(int argc, char **argv)
{
  pool p[1] = { pool_create() };  
  random_init();

  /* biases at 16 and 32, so we prefix our 30 byte secret with
   * two bytes to start with, up to 17 */
  byteblock prefix = { (uint8_t *) "AAAAAAAAAAAAAAAAAA", 2 };
  
  assert(argc == 2);
  byteblock secret = from_base64(p, argv[1]);
  
  assert(secret.len == SECRET_LEN);
  
  unsigned counts[SECRET_LEN][256];
  memset(counts, 0, sizeof(counts));
  
  while (prefix.len < 18)
  {
    uint8_t buf[64];
    byteblock bb = { buf, 0 };
    
    for (size_t i = 0; i < ROUNDS; i++)
    {
      oracle(&prefix, &secret, &bb);
      
      if (prefix.len <= 15)
      {
        /* bias at 16 towards 240 (full) 0 (half) 16 (half) */
        uint8_t b16 = buf[15];
        counts[15 - prefix.len][b16 ^ 240] += FULL_WEIGHT;
        counts[15 - prefix.len][b16 ^ 0] += HALF_WEIGHT;
        counts[15 - prefix.len][b16 ^ 16] += HALF_WEIGHT;
      }
        
      /* bias at 32 towards 224 (full) 0 (half) 32 (half) */
      uint8_t b32 = buf[31];
      counts[31 - prefix.len][b32 ^ 224] += FULL_WEIGHT;
      counts[31 - prefix.len][b32 ^ 0] += HALF_WEIGHT;
      counts[31 - prefix.len][b32 ^ 32] += HALF_WEIGHT;
    }
    
    prefix.len++;
    
    byteblock plaintext = recover(p, counts);
    printf("guess: %s\n", to_ascii(p, &plaintext));
  }
  
  byteblock recovered = recover(p, counts);
  printf("message: %s\n", to_ascii(p, &recovered));
  
  p->finish(p);
  return 0;
}
コード例 #8
0
ファイル: xmppipe.c プロジェクト: chmduquesne/xmppipe
int message_handler(xmpp_conn_t * const conn, xmpp_stanza_t * const stanza, void * const userdata)
{
    char *intext, *decoded_intext;
    if(!xmpp_stanza_get_child_by_name(stanza, "body")) return 1;
    if(!strcmp(xmpp_stanza_get_attribute(stanza, "type"), "error")) return 1;
    intext = xmpp_stanza_get_text(xmpp_stanza_get_child_by_name(stanza, "body"));

    decoded_intext = from_base64(intext, strlen(intext));
    fwrite((const void*)decoded_intext, 1, strlen(intext), stdout);
    free(decoded_intext);
    return 1;
}
コード例 #9
0
ファイル: main.c プロジェクト: gstraube/c_exercises
int main(int argc, char *argv[])
{
    /* You can modify this file as needed, for example to test different
     * files. If you run on command line, this function optionally reads
     * the filename from command line, if one is given. */
    
    char *filename = "testi.txt";
    if (argc > 1) {
        filename = argv[1];
    }
    
    // Write encoded file to 'testi.enc'
    int n = to_base64("testi.enc", filename);
    printf("to_base64 returned: %d\n", n);
    
    // Follow with reverse operation -- 'testi.ver' should be same as
    // your original source file
    n = from_base64("testi.ver", "testi.enc");
    printf("from_base64 returned: %d\n", n);
}
コード例 #10
0
ファイル: db.c プロジェクト: InvLim/heimdal
static void
db_replay_log_table_set_keys_iter(heim_object_t key, heim_object_t value,
				  void *arg)
{
    heim_db_t db = arg;
    heim_data_t k, v;

    if (db->ret)
	return;

    k = from_base64((heim_string_t)key, &db->error);
    if (k == NULL) {
	db->ret = ENOMEM;
	return;
    }
    v = (heim_data_t)value;

    db->ret = db->plug->setf(db->db_data, db->current_table, k, v, &db->error);
    heim_release(k);
}
コード例 #11
0
ファイル: mcp6.c プロジェクト: ctz/cryptopals
int main(int argc, char **argv)
{
  pool p[1] = { pool_create() };
  
  assert(argc == 2);
  byteblock cipher = from_base64(p, argv[1]);
  
  int best_score = INT_MAX;
  byteblock best_key;
  byteblock best_plaintext;
  
  // this is actually quick enough and easier to bruteforce: 40 * 256 * 256 tries
  
  for (size_t k = 2; k < MAX_KEYLEN; k++)
  {
    byteblock key = { p->alloc(p, k), k };
    for (size_t i = 0; i < key.len; i++)
    {
      key.buf[i] = find_key_byte(p, &cipher, i, key.len);
    }
    
    byteblock plain = byteblock_xor(p, &cipher, &key);
    int score = score_english(&plain);
    
    if (score < best_score)
    {
      best_score = score;
      best_key = key;
      best_plaintext = plain;
    }
  }
  
  printf("key: %s, plain: %s\n", to_hex(p, &best_key), to_ascii(p, &best_plaintext));
  
  p->finish(p);
  return 0;
}
コード例 #12
0
ファイル: attribs.c プロジェクト: Sherlock221B/burp
// Decode a stat packet from base64 characters.
void attribs_decode(struct sbuf *sb)
{
	static const char *p;
	static int64_t val;
	static struct stat *statp;

	if(!(p=sb->attr.buf)) return;
	statp=&sb->statp;

	if(sb->burp2)
	{
		// Burp1 does not have this field.
		p += from_base64(&val, p);
		sb->burp2->index=val;
		p++;
		// Compression for burp2.
		p += from_base64(&val, p);
		sb->compression=val;
		p++;
		// Burp1 does not have this field.
		p += from_base64(&val, p);
		sb->burp2->encryption=val;
		p++;
	}
	p += from_base64(&val, p);
	plug(statp->st_dev, val);
	p++;
	p += from_base64(&val, p);
	plug(statp->st_ino, val);
	p++;
	p += from_base64(&val, p);
	plug(statp->st_mode, val);
	p++;
	p += from_base64(&val, p);
	plug(statp->st_nlink, val);
	p++;
	p += from_base64(&val, p);
	plug(statp->st_uid, val);
	p++;
	p += from_base64(&val, p);
	plug(statp->st_gid, val);
	p++;
	p += from_base64(&val, p);
	plug(statp->st_rdev, val);
	p++;
	p += from_base64(&val, p);
	plug(statp->st_size, val);
	p++;
	p += from_base64(&val, p);
#ifdef HAVE_WIN32
	//   plug(statp->st_blksize, val);
	p++;
	p += from_base64(&val, p);
	//   plug(statp->st_blocks, val);
#else
	plug(statp->st_blksize, val);
	p++;
	p += from_base64(&val, p);
	plug(statp->st_blocks, val);
#endif
	p++;
	p += from_base64(&val, p);
	plug(statp->st_atime, val);
	p++;
	p += from_base64(&val, p);
	plug(statp->st_mtime, val);
	p++;
	p += from_base64(&val, p);
	plug(statp->st_ctime, val);

	// FreeBSD user flags.
	if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
	{
		p++;
		if(!*p) return;
		p += from_base64(&val, p);
#ifdef HAVE_CHFLAGS
		plug(statp->st_flags, val);
	}
	else
	{
		statp->st_flags  = 0;
#endif
	}

	// Look for winattr.
	if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
	{
		p++;
		p += from_base64(&val, p);
	}
	else
		val = 0;
	sb->winattr=val;

	// Compression for burp1.
	if(sb->burp1)
	{
		if(*p == ' ' || (*p != 0 && *(p+1) == ' '))
		{
			p++;
			if(!*p) return;
			p += from_base64(&val, p);
			sb->compression=val;
		}
		else
			sb->compression=-1;
	}
}
コード例 #13
0
ファイル: attribs.c プロジェクト: viniciusingracio/bareos
/**
 * Set Extended File Attributes for Win32
 *
 * fname is the original filename
 * ofile is the output filename (may be in a different directory)
 *
 * Returns:  true  on success
 *           false on failure
 */
static bool set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
{
    char *p = attr->attrEx;
    int64_t val;
    WIN32_FILE_ATTRIBUTE_DATA atts;
    ULARGE_INTEGER li;

    /** if we have neither Win ansi nor wchar API, get out */
    if (!(p_SetFileAttributesW || p_SetFileAttributesA)) {
        return false;
    }

    if (!p || !*p) {                   /* we should have attributes */
        Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", attr->ofname, ofd->fid);
        if (is_bopen(ofd)) {
            bclose(ofd);
        }
        return false;
    } else {
        Dmsg2(100, "Attribs %s = %s\n", attr->ofname, attr->attrEx);
    }

    p += from_base64(&val, p);
    plug(atts.dwFileAttributes, val);
    p++;                               /* skip space */
    p += from_base64(&val, p);
    li.QuadPart = val;
    atts.ftCreationTime.dwLowDateTime = li.LowPart;
    atts.ftCreationTime.dwHighDateTime = li.HighPart;
    p++;                               /* skip space */
    p += from_base64(&val, p);
    li.QuadPart = val;
    atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
    atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
    p++;                               /* skip space */
    p += from_base64(&val, p);
    li.QuadPart = val;
    atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
    atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
    p++;
    p += from_base64(&val, p);
    plug(atts.nFileSizeHigh, val);
    p++;
    p += from_base64(&val, p);
    plug(atts.nFileSizeLow, val);

    /** At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */

    if (!is_bopen(ofd)) {
        Dmsg1(100, "File not open: %s\n", attr->ofname);
        bopen(ofd, attr->ofname, O_WRONLY | O_BINARY, 0, 0); /* attempt to open the file */
    }

    /*
     * Restore file attributes and times on the restored file.
     */
    if (!win32_restore_file_attributes(attr->ofname, bget_handle(ofd), &atts)) {
        win_error(jcr, "win32_restore_file_attributes:", attr->ofname);
    }

    if (is_bopen(ofd)) {
        bclose(ofd);
    }

    return true;
}
コード例 #14
0
ファイル: direct.c プロジェクト: Vaysman/cntlm
int www_authenticate(int sd, rr_data_t request, rr_data_t response, struct auth_s *creds) {
	char *tmp, *buf, *challenge;
	rr_data_t auth;
	int len;
	
	int rc = 0;

	buf = new(BUFSIZE);

	strcpy(buf, "NTLM ");
	len = ntlm_request(&tmp, creds);
	if (len) {
		to_base64(MEM(buf, unsigned char, 5), MEM(tmp, unsigned char, 0), len, BUFSIZE-5);
		free(tmp);
	}

	auth = dup_rr_data(request);
	auth->headers = hlist_mod(auth->headers, "Connection", "keep-alive", 1);
	auth->headers = hlist_mod(auth->headers, "Authorization", buf, 1);
	auth->headers = hlist_mod(auth->headers, "Content-Length", "0", 1);
	auth->headers = hlist_del(auth->headers, "Transfer-Encoding");

	/*
	 * Drop whatever error page server returned
	 */
	if (!http_body_drop(sd, response))
		goto bailout;

	if (debug) {
		printf("\nSending WWW auth request...\n");
		hlist_dump(auth->headers);
	}

	if (!headers_send(sd, auth))
		goto bailout;

	if (debug)
		printf("\nReading WWW auth response...\n");

	/*
	 * Get NTLM challenge
	 */
	reset_rr_data(auth);
	if (!headers_recv(sd, auth)) {
		goto bailout;
	}

	if (debug)
		hlist_dump(auth->headers);

	/*
	 * Auth required?
	 */
	if (auth->code == 401) {
		if (!http_body_drop(sd, auth))
			goto bailout;

		tmp = hlist_get(auth->headers, "WWW-Authenticate");
		if (tmp && strlen(tmp) > 6 + 8) {
			challenge = new(strlen(tmp) + 5 + 1);
			len = from_base64(challenge, tmp + 5);
			if (len > NTLM_CHALLENGE_MIN) {
				len = ntlm_response(&tmp, challenge, len, creds);
				if (len > 0) {
					strcpy(buf, "NTLM ");
					to_base64(MEM(buf, unsigned char, 5), MEM(tmp, unsigned char, 0), len, BUFSIZE-5);
					request->headers = hlist_mod(request->headers, "Authorization", buf, 1);
					free(tmp);
				} else {
コード例 #15
0
ファイル: backup_phase2.c プロジェクト: Sherlock221B/burp
static int deal_with_read(struct iobuf *rbuf, struct slist *slist, struct blist  *blist, struct conf *conf, int *backup_end, int *requests_end, int *blk_requests_end)
{
	int ret=0;
	switch(rbuf->cmd)
	{
		/* Incoming file request. */
		case CMD_FILE:
			if(add_to_file_requests(slist, rbuf, conf)) goto error;
			return 0;

		/* Incoming data block request. */
		case CMD_DATA_REQ:
			if(add_to_data_requests(blist, rbuf)) goto error;
			goto end;

		/* Incoming control/message stuff. */
		case CMD_WRAP_UP:
		{
			int64_t wrap_up;
			struct blk *blk;
			from_base64(&wrap_up, rbuf->buf);
			for(blk=blist->head; blk; blk=blk->next)
			{
				if(blk->index==(uint64_t)wrap_up)
				{
					blist->last_requested=blk;
					blist->last_sent=blk;
					break;
				}
			}
			if(!blk)
			{
#ifdef HAVE_WIN32
				logp("Could not find wrap up index: %016I64X\n",
#else
				logp("Could not find wrap up index: %016lX\n",
#endif
					wrap_up);
				logp("Could not find wrap up index: %d\n",
					wrap_up);
//				goto error;
			}
			goto end;
		}
		case CMD_WARNING:
			logp("WARNING: %s\n", rbuf->cmd);
			cntr_add(conf->cntr, rbuf->cmd, 0);
			goto end;
		case CMD_GEN:
			if(!strcmp(rbuf->buf, "requests_end"))
			{
				*requests_end=1;
				goto end;
			}
			else if(!strcmp(rbuf->buf, "blk_requests_end"))
			{
				*blk_requests_end=1;
				goto end;
			}
			else if(!strcmp(rbuf->buf, "backup_end"))
			{
				*backup_end=1;
				goto end;
			}
			break;
	}
コード例 #16
0
ファイル: attribs.c プロジェクト: Sherlock221B/burp
uint64_t decode_file_no(struct iobuf *iobuf)
{
	int64_t val;
	from_base64(&val, iobuf->buf);
	return (uint64_t)val;
}
コード例 #17
0
ファイル: attribs.c プロジェクト: tcheneau/burp
/* Decode a stat packet from base64 characters */
void decode_stat(const char *buf, struct stat *statp, int64_t *winattr, int *compression)
{
   const char *p = buf;
   int64_t val;

   p += from_base64(&val, p);
   plug(statp->st_dev, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_ino, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_mode, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_nlink, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_uid, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_gid, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_rdev, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_size, val);
   p++;
   p += from_base64(&val, p);
#ifdef HAVE_WIN32
//   plug(statp->st_blksize, val);
   p++;
   p += from_base64(&val, p);
//   plug(statp->st_blocks, val);
#else
   plug(statp->st_blksize, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_blocks, val);
#endif
   p++;
   p += from_base64(&val, p);
   plug(statp->st_atime, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_mtime, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_ctime, val);

   /* FreeBSD user flags */
   if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
      p++;
      if(!*p) return;
      p += from_base64(&val, p);
#ifdef HAVE_CHFLAGS
      plug(statp->st_flags, val);
   } else {
      statp->st_flags  = 0;
#endif
   }

   /* Look for winattr */
   if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
      p++;
      p += from_base64(&val, p);
   } else {
      val = 0;
   }
   *winattr=val;

   /* Compression */
   if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
      p++;
      if(!*p) return;
      p += from_base64(&val, p);
      *compression=val;
   } else {
      *compression=-1;
   }
}
コード例 #18
0
ファイル: backup_phase2.c プロジェクト: pablodav/burp
static int deal_with_read(struct iobuf *rbuf, struct slist *slist,
	struct cntr *cntr, uint8_t *end_flags)
{
	int ret=0;
	switch(rbuf->cmd)
	{
		/* Incoming file request. */
		case CMD_FILE:
		case CMD_METADATA:
			if(add_to_file_requests(slist, rbuf)) goto error;
			return 0;

		/* Incoming data block request. */
		case CMD_DATA_REQ:
			if(add_to_data_requests(slist->blist, rbuf)) goto error;
			goto end;

		/* Incoming control/message stuff. */
		case CMD_WRAP_UP:
		{
			int64_t wrap_up;
			struct blk *blk;
			struct blist *blist=slist->blist;
			from_base64(&wrap_up, rbuf->buf);
			for(blk=blist->head; blk; blk=blk->next)
			{
				if(blk->index==(uint64_t)wrap_up)
				{
					blist->last_requested=blk;
					blist->last_sent=blk;
					break;
				}
			}
			if(!blk)
			{
				logp("Could not find wrap up index: %016" PRIX64 "\n",
					wrap_up);
//				goto error;
			}
			goto end;
		}
		case CMD_MESSAGE:
		case CMD_WARNING:
		{
			log_recvd(rbuf, cntr, 0);
			goto end;
		}
		case CMD_GEN:
			if(!strcmp(rbuf->buf, "requests_end"))
			{
				(*end_flags)|=END_REQUESTS;
				goto end;
			}
			else if(!strcmp(rbuf->buf, "blk_requests_end"))
			{
				(*end_flags)|=END_BLK_REQUESTS;
				goto end;
			}
			else if(!strcmp(rbuf->buf, "backup_end"))
			{
				(*end_flags)|=END_BACKUP;
				goto end;
			}
			break;
		default:
			break;
	}

	iobuf_log_unexpected(rbuf, __func__);
error:
	ret=-1;
end:
	iobuf_free_content(rbuf);
	return ret;
}
コード例 #19
0
ファイル: attribs.c プロジェクト: anarexia/bacula
/** Decode a stat packet from base64 characters */
int decode_stat(char *buf, struct stat *statp, int stat_size, int32_t *LinkFI)
{
   char *p = buf;
   int64_t val;

   /*
    * We store into the stat packet so make sure the caller's conception
    *  is the same as ours.  They can be different if LARGEFILE is not
    *  the same when compiling this library and the calling program.
    */
   ASSERT(stat_size == (int)sizeof(struct stat));

   p += from_base64(&val, p);
   plug(statp->st_dev, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_ino, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_mode, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_nlink, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_uid, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_gid, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_rdev, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_size, val);
   p++;
#ifndef HAVE_MINGW
   p += from_base64(&val, p);
   plug(statp->st_blksize, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_blocks, val);
   p++;
#else
   p += from_base64(&val, p);
//   plug(statp->st_blksize, val);
   p++;
   p += from_base64(&val, p);
//   plug(statp->st_blocks, val);
   p++;
#endif
   p += from_base64(&val, p);
   plug(statp->st_atime, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_mtime, val);
   p++;
   p += from_base64(&val, p);
   plug(statp->st_ctime, val);

   /* Optional FileIndex of hard linked file data */
   if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
      p++;
      p += from_base64(&val, p);
      *LinkFI = (uint32_t)val;
   } else {
      *LinkFI = 0;
      return 0;
   }

   /* FreeBSD user flags */
   if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
      p++;
      p += from_base64(&val, p);
#ifdef HAVE_CHFLAGS
      plug(statp->st_flags, val);
   } else {
      statp->st_flags  = 0;
#endif
   }

   /* Look for data stream id */
   if (*p == ' ' || (*p != 0 && *(p+1) == ' ')) {
      p++;
      p += from_base64(&val, p);
   } else {
      val = 0;
   }
   return (int)val;
}
コード例 #20
0
ファイル: attr.c プロジェクト: rkorzeniewski/bacula
int unpack_attributes_record(JCR *jcr, int32_t stream, char *rec, int32_t reclen, ATTR *attr)
{
   char *p;
   int object_len;
   /*
    * An Attributes record consists of:
    *    File_index
    *    Type   (FT_types)
    *    Filename
    *    Attributes
    *    Link name (if file linked i.e. FT_LNK)
    *    Extended attributes (Win32)
    *  plus optional values determined by AR_ flags in upper bits of Type
    *    Data_stream
    *
    */
   attr->stream = stream;
   Dmsg1(dbglvl, "Attr: %s\n", rec);
   if (sscanf(rec, "%d %d", &attr->file_index, &attr->type) != 2) {
      Jmsg(jcr, M_FATAL, 0, _("Error scanning attributes: %s\n"), rec);
      Dmsg1(dbglvl, "\nError scanning attributes. %s\n", rec);
      return 0;
   }
   Dmsg2(dbglvl, "Got Attr: FilInx=%d type=%d\n", attr->file_index, attr->type);
   /*
    * Note AR_DATA_STREAM should never be set since it is encoded
    *  at the end of the attributes.
    */
   if (attr->type & AR_DATA_STREAM) {
      attr->data_stream = 1;
   } else {
      attr->data_stream = 0;
   }
   attr->type &= FT_MASK;             /* keep only type bits */
   p = rec;
   while (*p++ != ' ')               /* skip record file index */
      { }
   while (*p++ != ' ')               /* skip type */
      { }

   attr->fname = p;                   /* set filname position */
   while (*p++ != 0)                  /* skip filename */
      { }
   attr->attr = p;                    /* set attributes position */
   while (*p++ != 0)                  /* skip attributes */
      { }
   attr->lname = p;                   /* set link position */
   while (*p++ != 0)                  /* skip link */
      { }
   attr->delta_seq = 0;
   if (attr->type == FT_RESTORE_FIRST) {
      /* We have an object, so do a binary copy */
      object_len = reclen + rec - p;
      attr->attrEx = check_pool_memory_size(attr->attrEx, object_len + 1);
      memcpy(attr->attrEx, p, object_len);
      /* Add a EOS for those who attempt to print the object */
      p = attr->attrEx + object_len;
      *p = 0;
   } else {
      pm_strcpy(attr->attrEx, p);     /* copy extended attributes, if any */
      if (attr->data_stream) {
         int64_t val;
         while (*p++ != 0)            /* skip extended attributes */
            { }
         from_base64(&val, p);
         attr->data_stream = (int32_t)val;
      } else {
         while (*p++ != 0)            /* skip extended attributes */
            { }
         if (p - rec < reclen) {
            attr->delta_seq = str_to_int32(p); /* delta_seq */
         }
      }
   }
   Dmsg8(dbglvl, "unpack_attr FI=%d Type=%d fname=%s attr=%s lname=%s attrEx=%s datastr=%d delta_seq=%d\n",
      attr->file_index, attr->type, attr->fname, attr->attr, attr->lname,
      attr->attrEx, attr->data_stream, attr->delta_seq);
   *attr->ofname = 0;
   *attr->olname = 0;
   return 1;
}
コード例 #21
0
ファイル: attribs.c プロジェクト: anarexia/bacula
/**
 * Set Extended File Attributes for Win32
 *
 *  fname is the original filename
 *  ofile is the output filename (may be in a different directory)
 *
 * Returns:  true  on success
 *           false on failure
 */
static bool set_win32_attributes(JCR *jcr, ATTR *attr, BFILE *ofd)
{
   char *p = attr->attrEx;
   int64_t val;
   WIN32_FILE_ATTRIBUTE_DATA atts;
   ULARGE_INTEGER li;
   POOLMEM *win32_ofile;

   /** if we have neither Win ansi nor wchar API, get out */
   if (!(p_SetFileAttributesW || p_SetFileAttributesA)) {
      return false;
   }

   if (!p || !*p) {                   /* we should have attributes */
      Dmsg2(100, "Attributes missing. of=%s ofd=%d\n", attr->ofname, ofd->fid);
      if (is_bopen(ofd)) {
         bclose(ofd);
      }
      return false;
   } else {
      Dmsg2(100, "Attribs %s = %s\n", attr->ofname, attr->attrEx);
   }

   p += from_base64(&val, p);
   plug(atts.dwFileAttributes, val);
   p++;                               /* skip space */
   p += from_base64(&val, p);
   li.QuadPart = val;
   atts.ftCreationTime.dwLowDateTime = li.LowPart;
   atts.ftCreationTime.dwHighDateTime = li.HighPart;
   p++;                               /* skip space */
   p += from_base64(&val, p);
   li.QuadPart = val;
   atts.ftLastAccessTime.dwLowDateTime = li.LowPart;
   atts.ftLastAccessTime.dwHighDateTime = li.HighPart;
   p++;                               /* skip space */
   p += from_base64(&val, p);
   li.QuadPart = val;
   atts.ftLastWriteTime.dwLowDateTime = li.LowPart;
   atts.ftLastWriteTime.dwHighDateTime = li.HighPart;
   p++;
   p += from_base64(&val, p);
   plug(atts.nFileSizeHigh, val);
   p++;
   p += from_base64(&val, p);
   plug(atts.nFileSizeLow, val);

   /** Convert to Windows path format */
   win32_ofile = get_pool_memory(PM_FNAME);
   unix_name_to_win32(&win32_ofile, attr->ofname);

   /** At this point, we have reconstructed the WIN32_FILE_ATTRIBUTE_DATA pkt */

   if (!is_bopen(ofd)) {
      Dmsg1(100, "File not open: %s\n", attr->ofname);
      bopen(ofd, attr->ofname, O_WRONLY|O_BINARY, 0);   /* attempt to open the file */
   }

   if (is_bopen(ofd)) {
      Dmsg1(100, "SetFileTime %s\n", attr->ofname);
      if (!SetFileTime(bget_handle(ofd),
                         &atts.ftCreationTime,
                         &atts.ftLastAccessTime,
                         &atts.ftLastWriteTime)) {
         win_error(jcr, "SetFileTime:", win32_ofile);
      }
      bclose(ofd);
   }

   Dmsg1(100, "SetFileAtts %s\n", attr->ofname);
   if (!(atts.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
      if (p_SetFileAttributesW) {
         POOLMEM* pwszBuf = get_pool_memory(PM_FNAME);   
         make_win32_path_UTF8_2_wchar(&pwszBuf, attr->ofname);

         BOOL b=p_SetFileAttributesW((LPCWSTR)pwszBuf, atts.dwFileAttributes & SET_ATTRS);
         free_pool_memory(pwszBuf);
      
         if (!b) 
            win_error(jcr, "SetFileAttributesW:", win32_ofile); 
      }
      else {
         if (!p_SetFileAttributesA(win32_ofile, atts.dwFileAttributes & SET_ATTRS)) {
            win_error(jcr, "SetFileAttributesA:", win32_ofile);
         }
      }
   }
   free_pool_memory(win32_ofile);
   return true;
}
コード例 #22
0
ファイル: mcp19.c プロジェクト: ctz/cryptopals
int main(int argc, char **argv)
{
  pool p[1] = { pool_create() };
  assert(argc == CIPHERS + 1);

  byteblock cts[CIPHERS];

  argc--;
  argv++;

  for (int i = 0; i < argc; i++)
  {
    byteblock plain = from_base64(p, argv[i]);
    cts[i] = rijndael_ctr_process(p, &key, &nonce, &plain);
  }

  /* build a table of probablities of key values */
  int guesses[GUESSLEN][256] = { { 0 } };
  for (size_t i = 0; i < GUESSLEN; i++)
  {
    for (int guess = 0; guess < 256; guess++)
    {
      for (size_t sample = 0; sample < CIPHERS; sample++)
      {
        if (cts[sample].len >= i)
        {
          int result = guess ^ cts[sample].buf[i];
          guesses[i][guess] += score_english_char(result);
        }
      }
    }
  }

  /* construct the key from the highest probability values */
  uint8_t key[GUESSLEN];

  for (size_t i = 0; i < GUESSLEN; i++)
  {
    uint8_t best;
    int best_score = 0xffff;

    for (int guess = 0; guess < 256; guess++)
    {
      if (guesses[i][guess] < best_score)
      {
        best = guess;
        best_score = guesses[i][guess];
      }
    }

    key[i] = best;
  }

  /* now decrypt and print */
  for (size_t i = 0; i < GUESSLEN; i++)
  {
    if (i >= cts[ATTACK].len)
      break;
    printf("%c", cts[ATTACK].buf[i] ^ key[i]);
  }
  printf("\n");

  p->finish(p);
  return 0;
}