static int sync_ls(int fd, const char* path, sync_ls_cb func, void* cookie) {
    int len = strlen(path);
    if (len > 1024) goto fail;

    syncmsg msg;
    msg.req.id = ID_LIST;
    msg.req.namelen = htoll(len);

    if (!WriteFdExactly(fd, &msg.req, sizeof(msg.req)) || !WriteFdExactly(fd, path, len)) {
        goto fail;
    }

    for (;;) {
        if (!ReadFdExactly(fd, &msg.dent, sizeof(msg.dent))) break;
        if (msg.dent.id == ID_DONE) return 0;
        if (msg.dent.id != ID_DENT) break;

        len = ltohl(msg.dent.namelen);
        if (len > 256) break;

        char buf[257];
        if (!ReadFdExactly(fd, buf, len)) break;
        buf[len] = 0;

        func(ltohl(msg.dent.mode), ltohl(msg.dent.size), ltohl(msg.dent.time), buf, cookie);
    }

fail:
    adb_close(fd);
    return -1;
}
예제 #2
0
int sync_readtime(int fd, const char *path, unsigned int *timestamp,
                  unsigned int *mode)
{
    syncmsg msg;
    int len = strlen(path);

    msg.req.id = ID_STAT;
    msg.req.namelen = htoll(len);

    if(writex(fd, &msg.req, sizeof(msg.req)) ||
       writex(fd, path, len)) {
        return -1;
    }

    if(readx(fd, &msg.stat, sizeof(msg.stat))) {
        return -1;
    }

    if(msg.stat.id != ID_STAT) {
        return -1;
    }

    *timestamp = ltohl(msg.stat.time);
    *mode = ltohl(msg.stat.mode);
    return 0;
}
예제 #3
0
static int check_header(FILE *f)
{
    AU_HEADER(header);
    u_int32_t magic;
    u_int32_t hdr_size;
    u_int32_t data_size;
    u_int32_t encoding;
    u_int32_t sample_rate;
    u_int32_t channels;

    if (fread(header, 1, AU_HEADER_SIZE, f) != AU_HEADER_SIZE)
    {
        cw_log(LOG_WARNING, "Read failed (header)\n");
        return -1;
    }
    magic = ltohl(header[AU_HDR_MAGIC_OFF]);
    if (magic != (u_int32_t) AU_MAGIC)
    {
        cw_log(LOG_WARNING, "Bad magic: 0x%x\n", magic);
    }
/*    hdr_size = ltohl(header[AU_HDR_HDR_SIZE_OFF]);
    if (hdr_size < AU_HEADER_SIZE)*/
    hdr_size = AU_HEADER_SIZE;
/*    data_size = ltohl(header[AU_HDR_DATA_SIZE_OFF]); */
    encoding = ltohl(header[AU_HDR_ENCODING_OFF]);
    if (encoding != AU_ENC_8BIT_ULAW)
    {
        cw_log(LOG_WARNING, "Unexpected format: %d. Only 8bit ULAW allowed (%d)\n", encoding, AU_ENC_8BIT_ULAW);
        return -1;
    }
    sample_rate = ltohl(header[AU_HDR_SAMPLE_RATE_OFF]);
    if (sample_rate != 8000)
    {
        cw_log(LOG_WARNING, "Sample rate can only be 8000 not %d\n", sample_rate);
        return -1;
    }
    channels = ltohl(header[AU_HDR_CHANNELS_OFF]);
    if (channels != 1)
    {
        cw_log(LOG_WARNING, "Not in mono: channels=%d\n", channels);
        return -1;
    }
    /* Skip to data */
    fseek(f, 0, SEEK_END);
    data_size = ftell(f) - hdr_size;
    if (fseek(f, hdr_size, SEEK_SET) == -1)
    {
        cw_log(LOG_WARNING, "Failed to skip to data: %d\n", hdr_size);
        return -1;
    }
    return data_size;
}
예제 #4
0
파일: dce-rpc.c 프로젝트: wflk/lanmap2
static size_t parse_resp(const dcerpc_hdr *h, void *buf, size_t len)
{
    dcerpc_resp *r = buf;
    if (len < sizeof *r)
        return 0;
    /* adjust endianness */
    r->fraglen    = ltohs(r->fraglen);
    r->authlen    = ltohs(r->authlen);
    r->callid     = ltohl(r->callid);
    r->alloc_hint = ltohl(r->alloc_hint);
    r->contextid  = ltohl(r->contextid);
    r->opnum      = ltohl(r->opnum);
    return len;
}
예제 #5
0
 */EIP_INT16
createEncapsulationStructure(EIP_UINT8 * pa_buf, /* receive buffer*/
int pa_length, /* size of stuff in  buffer (might be more than one message)*/
struct S_Encapsulation_Data * pa_stData) /* the struct to be created*/

{
  pa_stData->m_acCommBufferStart = pa_buf;
  pa_stData->nCommand_code = ltohs(&pa_buf);
  pa_stData->nData_length = ltohs(&pa_buf);
  pa_stData->nSession_handle = ltohl(&pa_buf);
  pa_stData->nStatus = ltohl(&pa_buf);
  /*memcpy(pa_stData->anSender_context, pa_buf, SENDER_CONTEXT_SIZE);*/
  pa_buf += SENDER_CONTEXT_SIZE;
  pa_stData->nOptions = ltohl(&pa_buf);
  pa_stData->m_acCurrentCommBufferPos = pa_buf;
  return (pa_length - ENCAPSULATION_HEADER_LENGTH - pa_stData->nData_length);
}
예제 #6
0
static int sync_finish_readtime(int fd, unsigned int *timestamp,
                                unsigned int *mode, unsigned int *size)
{
    syncmsg msg;

    if(readx(fd, &msg.stat, sizeof(msg.stat)))
        return -1;

    if(msg.stat.id != ID_STAT)
        return -1;

    *timestamp = ltohl(msg.stat.time);
    *mode = ltohl(msg.stat.mode);
    *size = ltohl(msg.stat.size);

    return 0;
}
void file_sync_service(int fd, void *cookie)
{
    syncmsg msg;
    char name[1025];
    unsigned namelen;

    char *buffer = malloc(SYNC_DATA_MAX);
    if(buffer == 0) goto fail;

    for(;;) {
        D("sync: waiting for command\n");

        if(readx(fd, &msg.req, sizeof(msg.req))) {
            fail_message(fd, "command read failure");
            break;
        }
        namelen = ltohl(msg.req.namelen);
        if(namelen > 1024) {
            fail_message(fd, "invalid namelen");
            break;
        }
        if(readx(fd, name, namelen)) {
            fail_message(fd, "filename read failure");
            break;
        }
        name[namelen] = 0;

        msg.req.namelen = 0;
        D("sync: '%s' '%s'\n", (char*) &msg.req, name);

        switch(msg.req.id) {
        case ID_STAT:
            if(do_stat(fd, name)) goto fail;
            break;
        case ID_LIST:
            if(do_list(fd, name)) goto fail;
            break;
        case ID_SEND:
            if(do_send(fd, name, buffer)) goto fail;
            break;
        case ID_RECV:
            if(do_recv(fd, name, buffer)) goto fail;
            break;
        case ID_QUIT:
            goto fail;
        default:
            fail_message(fd, "unknown command");
            goto fail;
        }
    }

fail:
    if(buffer != 0) free(buffer);
    D("sync: done\n");
    adb_close(fd);
}
예제 #8
0
static int handle_send_link(int s, char *path, char *buffer)
{
    syncmsg msg;
    unsigned int len;
    int ret;

    if(readx(s, &msg.data, sizeof(msg.data)))
        return -1;

    if(msg.data.id != ID_DATA) {
        fail_message(s, "invalid data message: expected ID_DATA");
        return -1;
    }

    len = ltohl(msg.data.size);
    if(len > SYNC_DATA_MAX) {
        fail_message(s, "oversize data message");
        return -1;
    }
    if(readx(s, buffer, len))
        return -1;

    ret = symlink(buffer, path);
    if(ret && errno == ENOENT) {
        if(mkdirs(path) != 0) {
            fail_errno(s);
            return -1;
        }
        ret = symlink(buffer, path);
    }
    if(ret) {
        fail_errno(s);
        return -1;
    }

    if(readx(s, &msg.data, sizeof(msg.data)))
        return -1;

    if(msg.data.id == ID_DONE) {
        msg.status.id = ID_OKAY;
        msg.status.msglen = 0;
        if(writex(s, &msg.status, sizeof(msg.status)))
            return -1;
    } else {
        fail_message(s, "invalid data message: expected ID_DONE");
        return -1;
    }

    return 0;
}
예제 #9
0
파일: browse.c 프로젝트: aNeutrino/lanmap2
static size_t parse_elect(const browse *b, char *buf, size_t len, const parse_status *st)
{
  struct electreq *e = (struct electreq *)buf;
  size_t bytes,
         namelen;
  if (len < sizeof *e)
    return 0;
  namelen = memcspn((char *)e->name, len - sizeof *e, "\x00", 1);
  bytes = sizeof *e + namelen;
  printf("%s %s (bytes=%u):", __FILE__, __func__, (unsigned)bytes);
  dump_chars((char *)e, bytes, stdout);
  fputc('\n', stdout);
  /* fix endianness */
  e->uptime = ltohl(e->uptime);
  assert(bytes == len);
  return bytes;
}
static int sync_readmode(int fd, const char* path, unsigned* mode) {
    syncmsg msg;
    int len = strlen(path);

    msg.req.id = ID_STAT;
    msg.req.namelen = htoll(len);

    if(!WriteFdExactly(fd, &msg.req, sizeof(msg.req)) ||
       !WriteFdExactly(fd, path, len)) {
        return -1;
    }

    if(!ReadFdExactly(fd, &msg.stat, sizeof(msg.stat))) {
        return -1;
    }

    if(msg.stat.id != ID_STAT) {
        return -1;
    }

    *mode = ltohl(msg.stat.mode);
    return 0;
}
예제 #11
0
/*   INT8 SendRRData(struct S_Encapsulation_Data *pa_stReceiveData)
 *   Call UCMM or Message Router if UCMM not implemented.
 *      pa_stReceiveData pointer to structure with data and header information.
 *  return status 	0 .. success.
 * 					-1 .. error
 */
EIP_STATUS
handleReceivedSendRRDataCmd(struct S_Encapsulation_Data * pa_stReceiveData)
{
  EIP_INT16 nSendSize;
  EIP_STATUS eRetVal = EIP_OK_SEND;

  if (pa_stReceiveData->nData_length >= 6)
    {
      /* Commandspecific data UDINT .. Interface Handle, UINT .. Timeout, CPF packets */
      /* don't use the data yet */
      ltohl(&pa_stReceiveData->m_acCurrentCommBufferPos); /* skip over null interface handle*/
      ltohs(&pa_stReceiveData->m_acCurrentCommBufferPos); /* skip over unused timeout value*/
      pa_stReceiveData->nData_length -= 6; /* the rest is in CPF format*/

      if (EIP_ERROR != checkRegisteredSessions(pa_stReceiveData)) /* see if the EIP session is registered*/
        {
          nSendSize =
              notifyCPF(pa_stReceiveData,
                  &pa_stReceiveData->m_acCommBufferStart[ENCAPSULATION_HEADER_LENGTH]);

          if (nSendSize >= 0)
            { /* need to send reply */
              pa_stReceiveData->nData_length = nSendSize;
            }
          else
            {
              eRetVal = EIP_ERROR;
            }
        }
      else
        { /* received a package with non registered session handle */
          pa_stReceiveData->nData_length = 0;
          pa_stReceiveData->nStatus = OPENER_ENCAP_STATUS_INVALID_SESSION_HANDLE;
        }
    }
  return eRetVal;
}
예제 #12
0
int			readWavData(FILE* file, char *data,
				int numSamples, int width)
{
  // read data
  int temp = fread(data, 1, width * numSamples, file);
  if (temp != width * numSamples) {
    fprintf(stderr, "Failed to read sound data\n");
    return -1;
  }

  // byte swap
  if (width == 2) {
    int16_t* sample = (int16_t*)data;
    for (int i = 0; i < numSamples; ++i)
      ltohs(sample + i);
  }
  else if (width == 4) {
    int32_t* sample = (int32_t*)data;
    for (int i = 0; i < numSamples; ++i)
      ltohl(sample + i);
  }

  return 0;
}
예제 #13
0
int sync_recv(int fd, const char *rpath, const char *lpath, int show_progress)
{
    syncmsg msg;
    int len;
    int lfd = -1;
    char *buffer = send_buffer.data;
    unsigned id;
    unsigned long long size = 0;

    len = strlen(rpath);
    if(len > 1024) return -1;

    if (show_progress) {
        // Determine remote file size.
        syncmsg stat_msg;
        stat_msg.req.id = ID_STAT;
        stat_msg.req.namelen = htoll(len);

        if (writex(fd, &stat_msg.req, sizeof(stat_msg.req)) ||
            writex(fd, rpath, len)) {
            return -1;
        }

        if (readx(fd, &stat_msg.stat, sizeof(stat_msg.stat))) {
            return -1;
        }

        if (stat_msg.stat.id != ID_STAT) return -1;

        size = ltohl(stat_msg.stat.size);
    }

    msg.req.id = ID_RECV;
    msg.req.namelen = htoll(len);
    if(writex(fd, &msg.req, sizeof(msg.req)) ||
       writex(fd, rpath, len)) {
        return -1;
    }

    if(readx(fd, &msg.data, sizeof(msg.data))) {
        return -1;
    }
    id = msg.data.id;

    if((id == ID_DATA) || (id == ID_DONE)) {
        adb_unlink(lpath);
        mkdirs(lpath);
        lfd = adb_creat(lpath, 0644);
        if(lfd < 0) {
            fprintf(stderr,"cannot create '%s': %s\n", lpath, strerror(errno));
            return -1;
        }
        goto handle_data;
    } else {
        goto remote_error;
    }

    for(;;) {
        if(readx(fd, &msg.data, sizeof(msg.data))) {
            return -1;
        }
        id = msg.data.id;

    handle_data:
        len = ltohl(msg.data.size);
        if(id == ID_DONE) break;
        if(id != ID_DATA) goto remote_error;
        if(len > SYNC_DATA_MAX) {
            fprintf(stderr,"data overrun\n");
            adb_close(lfd);
            return -1;
        }

        if(readx(fd, buffer, len)) {
            adb_close(lfd);
            return -1;
        }

        if(writex(lfd, buffer, len)) {
            fprintf(stderr,"cannot write '%s': %s\n", rpath, strerror(errno));
            adb_close(lfd);
            return -1;
        }

        total_bytes += len;

        if (show_progress) {
            print_transfer_progress(total_bytes, size);
        }
    }

    adb_close(lfd);
    return 0;

remote_error:
    adb_close(lfd);
    adb_unlink(lpath);

    if(id == ID_FAIL) {
        len = ltohl(msg.data.size);
        if(len > 256) len = 256;
        if(readx(fd, buffer, len)) {
            return -1;
        }
        buffer[len] = 0;
    } else {
        memcpy(buffer, &id, 4);
        buffer[4] = 0;
//        strcpy(buffer,"unknown reason");
    }
    fprintf(stderr,"failed to copy '%s' to '%s': %s\n", rpath, lpath, buffer);
    return 0;
}
static int handle_send_file(int s, char *path, mode_t mode, char *buffer)
{
    syncmsg msg;
    unsigned int timestamp = 0;
    int fd;

    fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
    if(fd < 0 && errno == ENOENT) {
        mkdirs(path);
        fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL, mode);
    }
    if(fd < 0 && errno == EEXIST) {
        fd = adb_open_mode(path, O_WRONLY, mode);
    }
    if(fd < 0) {
        if(fail_errno(s))
            return -1;
        fd = -1;
    }

    for(;;) {
        unsigned int len;

        if(readx(s, &msg.data, sizeof(msg.data)))
            goto fail;

        if(msg.data.id != ID_DATA) {
            if(msg.data.id == ID_DONE) {
                timestamp = ltohl(msg.data.size);
                break;
            }
            fail_message(s, "invalid data message");
            goto fail;
        }
        len = ltohl(msg.data.size);
        if(len > SYNC_DATA_MAX) {
            fail_message(s, "oversize data message");
            goto fail;
        }
        if(readx(s, buffer, len))
            goto fail;

        if(fd < 0)
            continue;
        if(writex(fd, buffer, len)) {
            int saved_errno = errno;
            adb_close(fd);
            adb_unlink(path);
            fd = -1;
            errno = saved_errno;
            if(fail_errno(s)) return -1;
        }
    }

    if(fd >= 0) {
        struct utimbuf u;
        adb_close(fd);
        u.actime = timestamp;
        u.modtime = timestamp;
        utime(path, &u);

        msg.status.id = ID_OKAY;
        msg.status.msglen = 0;
        if(writex(s, &msg.status, sizeof(msg.status)))
            return -1;
    }
    return 0;

fail:
    if(fd >= 0)
        adb_close(fd);
    adb_unlink(path);
    return -1;
}
예제 #15
0
static int sync_send(int fd, const char *lpath, const char *rpath,
                     unsigned mtime, mode_t mode, int show_progress)
{
    syncmsg msg;
    int len, r;
    syncsendbuf *sbuf = &send_buffer;
    char* file_buffer = NULL;
    int size = 0;
    char tmp[64];

    len = strlen(rpath);
    if(len > 1024) goto fail;

    snprintf(tmp, sizeof(tmp), ",%d", mode);
    r = strlen(tmp);

    msg.req.id = ID_SEND;
    msg.req.namelen = htoll(len + r);

    if(writex(fd, &msg.req, sizeof(msg.req)) ||
       writex(fd, rpath, len) || writex(fd, tmp, r)) {
        free(file_buffer);
        goto fail;
    }

    if (file_buffer) {
        write_data_buffer(fd, file_buffer, size, sbuf, show_progress);
        free(file_buffer);
    } else if (S_ISREG(mode))
        write_data_file(fd, lpath, sbuf, show_progress);
#ifdef HAVE_SYMLINKS
    else if (S_ISLNK(mode))
        write_data_link(fd, lpath, sbuf);
#endif
    else
        goto fail;

    msg.data.id = ID_DONE;
    msg.data.size = htoll(mtime);
    if(writex(fd, &msg.data, sizeof(msg.data)))
        goto fail;

    if(readx(fd, &msg.status, sizeof(msg.status)))
        return -1;

    if(msg.status.id != ID_OKAY) {
        if(msg.status.id == ID_FAIL) {
            len = ltohl(msg.status.msglen);
            if(len > 256) len = 256;
            if(readx(fd, sbuf->data, len)) {
                return -1;
            }
            sbuf->data[len] = 0;
        } else
            strcpy(sbuf->data, "unknown reason");

        fprintf(stderr,"failed to copy '%s' to '%s': %s\n", lpath, rpath, sbuf->data);
        return -1;
    }

    return 0;

fail:
    fprintf(stderr,"protocol failure\n");
    adb_close(fd);
    return -1;
}
int sync_recv(int fd, const char *rpath, const char *lpath)
{
    syncmsg msg;
    int len;
    int lfd = -1;
    char *buffer = send_buffer.data;
    unsigned id;

    len = strlen(rpath);
    if(len > 1024) return -1;

    msg.req.id = ID_RECV;
    msg.req.namelen = htoll(len);
    if(writex(fd, &msg.req, sizeof(msg.req)) ||
       writex(fd, rpath, len)) {
        return -1;
    }

    if(readx(fd, &msg.data, sizeof(msg.data))) {
        return -1;
    }
    id = msg.data.id;

    if((id == ID_DATA) || (id == ID_DONE)) {
        sdb_unlink(lpath);
        mkdirs((char *)lpath);
        lfd = sdb_creat(lpath, 0644);
        if(lfd < 0) {
            fprintf(stderr,"cannot create '%s': %s\n", lpath, strerror(errno));
            return -1;
        }
        goto handle_data;
    } else {
        goto remote_error;
    }

    for(;;) {
        if(readx(fd, &msg.data, sizeof(msg.data))) {
            return -1;
        }
        id = msg.data.id;

    handle_data:
        len = ltohl(msg.data.size);
        if(id == ID_DONE) break;
        if(id != ID_DATA) goto remote_error;
        if(len > SYNC_DATA_MAX) {
            fprintf(stderr,"data overrun\n");
            sdb_close(lfd);
            return -1;
        }

        if(readx(fd, buffer, len)) {
            sdb_close(lfd);
            return -1;
        }

        if(writex(lfd, buffer, len)) {
            fprintf(stderr,"cannot write '%s': %s\n", rpath, strerror(errno));
            sdb_close(lfd);
            return -1;
        }

        total_bytes += len;
    }

    sdb_close(lfd);
    return 0;

remote_error:
    sdb_close(lfd);
    sdb_unlink(lpath);

    if(id == ID_FAIL) {
        len = ltohl(msg.data.size);
        if(len > 256) len = 256;
        if(readx(fd, buffer, len)) {
            return -1;
        }
        buffer[len] = 0;
    } else {
        memcpy(buffer, &id, 4);
        buffer[4] = 0;
//        strcpy(buffer,"unknown reason");
    }
    fprintf(stderr,"failed to copy '%s' to '%s': %s\n", rpath, lpath, buffer);
    return 0;
}
static int check_header(FILE *f)
{
	int type, size, formtype;
	int fmt, hsize, fact;
	short format, chans;
	int freq;
	int data;
	if (fread(&type, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (type)\n");
		return -1;
	}
	if (fread(&size, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (size)\n");
		return -1;
	}
	size = ltohl(size);
	if (fread(&formtype, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (formtype)\n");
		return -1;
	}
	if (memcmp(&type, "RIFF", 4)) {
		ast_log(LOG_WARNING, "Does not begin with RIFF\n");
		return -1;
	}
	if (memcmp(&formtype, "WAVE", 4)) {
		ast_log(LOG_WARNING, "Does not contain WAVE\n");
		return -1;
	}
	if (fread(&fmt, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (fmt)\n");
		return -1;
	}
	if (memcmp(&fmt, "fmt ", 4)) {
		ast_log(LOG_WARNING, "Does not say fmt\n");
		return -1;
	}
	if (fread(&hsize, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (formtype)\n");
		return -1;
	}
	if (ltohl(hsize) != 20) {
		ast_log(LOG_WARNING, "Unexpected header size %d\n", ltohl(hsize));
		return -1;
	}
	if (fread(&format, 1, 2, f) != 2) {
		ast_log(LOG_WARNING, "Read failed (format)\n");
		return -1;
	}
	if (ltohs(format) != 49) {
		ast_log(LOG_WARNING, "Not a GSM file %d\n", ltohs(format));
		return -1;
	}
	if (fread(&chans, 1, 2, f) != 2) {
		ast_log(LOG_WARNING, "Read failed (format)\n");
		return -1;
	}
	if (ltohs(chans) != 1) {
		ast_log(LOG_WARNING, "Not in mono %d\n", ltohs(chans));
		return -1;
	}
	if (fread(&freq, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (freq)\n");
		return -1;
	}
	if (ltohl(freq) != DEFAULT_SAMPLE_RATE) {
		ast_log(LOG_WARNING, "Unexpected frequency %d\n", ltohl(freq));
		return -1;
	}
	/* Ignore the byte frequency */
	if (fread(&freq, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (X_1)\n");
		return -1;
	}
	/* Ignore the two weird fields */
	if (fread(&freq, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (X_2/X_3)\n");
		return -1;
	}
	/* Ignore the byte frequency */
	if (fread(&freq, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (Y_1)\n");
		return -1;
	}
	/* Check for the word fact */
	if (fread(&fact, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (fact)\n");
		return -1;
	}
	if (memcmp(&fact, "fact", 4)) {
		ast_log(LOG_WARNING, "Does not say fact\n");
		return -1;
	}
	/* Ignore the "fact value" */
	if (fread(&fact, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (fact header)\n");
		return -1;
	}
	if (fread(&fact, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (fact value)\n");
		return -1;
	}
	/* Check for the word data */
	if (fread(&data, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (data)\n");
		return -1;
	}
	if (memcmp(&data, "data", 4)) {
		ast_log(LOG_WARNING, "Does not say data\n");
		return -1;
	}
	/* Ignore the data length */
	if (fread(&data, 1, 4, f) != 4) {
		ast_log(LOG_WARNING, "Read failed (data)\n");
		return -1;
	}
	return 0;
}
예제 #18
0
int dvslib_readbmpfile(char * filename, sv_storageinfo * pstorage, char * buffer, int buffersize, int * pxsize, int * pysize)
{
  bmppreheader  preheader;
  bmpheader     header;
  FILE *        fp;
  uint32        clut[256];
  uint8 * p;
  int x,y,i;
  size_t tmp;
  
  fp = fopen(filename, "r");
  if(fp == NULL) {
    printf("dvslib_readbmpfile: Could not open file '%s'\n", filename);
    return SV_ERROR_FILEOPEN;
  }
  
  tmp = fread(&preheader.magic, 1, sizeof(preheader) - 2, fp); 
  if(tmp != sizeof(bmppreheader)-2) {
    fclose(fp);
    return SV_ERROR_FILEREAD;
  }
  preheader.magic      = ltohs(&preheader.magic);
  preheader.bfsize     = ltohl(&preheader.bfsize);
  preheader.reserved1  = ltohs(&preheader.reserved1);
  preheader.reserved2  = ltohs(&preheader.reserved2);
  preheader.bfoffset   = ltohl(&preheader.bfoffset);

 
  tmp = fread(&header, 1, sizeof(bmpheader), fp); 
  if(tmp != sizeof(bmpheader)) {
    fclose(fp);
    return SV_ERROR_FILEREAD;
  }

  header.size          = ltohl (&header.size);
  if(header.size >= 40) {
    header.width         = ltohl(&header.width);
    header.height        = ltohl(&header.height);
    header.planes        = ltohs(&header.planes);  
    header.bitcount      = ltohs(&header.bitcount);
    header.compression   = ltohl(&header.compression);
    header.imagesize     = ltohl(&header.imagesize);
    header.xpelspermeter = ltohl(&header.xpelspermeter);
    header.ypelspermeter = ltohl(&header.ypelspermeter);
    header.clrused       = ltohl(&header.clrused);
    header.clrimportant  = ltohl(&header.clrimportant);
  } else if(header.size == 12) {
    bmpheaderold * h     = (bmpheaderold *) &header;
    header.bitcount      = ltohs(&h->bitcount);
    header.planes        = ltohs(&h->planes);  
    header.height        = ltohs(&h->height);
    header.width         = ltohs(&h->width);
    header.compression   = BI_RGB;
    header.imagesize     = header.width * header.bitcount * header.planes * header.height / 8;
    header.xpelspermeter = 0;
    header.ypelspermeter = 0;
    header.clrused       = 0;
    header.clrimportant  = 0;    
  } else {
    fclose(fp);
    return SV_ERROR_FILESEEK;
  }

#ifdef DEBUG_BMP
  printf("BMP header\n");
  printf("size:             %d\n", header.size);
  printf("height:           %d\n", header.height);
  printf("width:            %d\n", header.width);
  printf("planes:           %d\n", header.planes);
  printf("bitcount:         %d\n", header.bitcount);
  printf("compression:      %d\n", header.compression);
  printf("imagesize:        %d\n", header.imagesize);
  printf("xpels:            %d\n", header.xpelspermeter);
  printf("ypels:            %d\n", header.ypelspermeter);
  printf("clrused:          %d\n", header.clrused);
  printf("clrimportant:     %d\n", header.clrimportant);
#endif

  if(header.compression != BI_RGB) {
    return SV_ERROR_FILEFORMAT; 
  }

  if(!((header.bitcount == 24) ||
       (header.bitcount == 8)  ||
       (header.bitcount == 4)  ||
       (header.bitcount == 1))) {
    return SV_ERROR_FILEFORMAT; 
  }
   
  if(pxsize) {
    *pxsize = (header.width + 1) & ~1;
  }
  if(pysize) {
    *pysize = header.height;
  }

  if(!buffer) {
    fclose(fp);
    return SV_OK;
  }
    
  if(header.bitcount <= 8) {
    //int clut_start;
    size_t clut_size;

    if(header.clrused == 0) {
      header.clrused = 1 << header.bitcount;
    }

    //clut_start = sizeof(bmppreheader) - 2 + header.size;
    clut_size  = header.clrused * sizeof(uint32);
    
    tmp = fread(&clut, 1, clut_size, fp); 
    if(tmp != clut_size) {
      fclose(fp);
      return SV_ERROR_FILEREAD;
    } 

    if(header.size == 12) {
      uint8 * ptmp = (uint8 *) &clut[0];
      int  from = (int)clut_size * 3 / 4 - 3 ;
      int  to   = (int)clut_size - 4;
      for(; (to >= 0); from-=3, to-=4) {
        ptmp[to + 3] = 0;
        ptmp[to + 2] = ptmp[from + 2];
        ptmp[to + 1] = ptmp[from + 1];
        ptmp[to    ] = ptmp[from    ];
      }
    } 
  }

  for(y = 0; y < pstorage->storageysize; y++) {
    int r1,g1,b1,r2,g2,b2;
    uint32 temp, bit;
    p = (uint8*)&buffer[pstorage->fieldoffset[0] + (pstorage->storageysize - y - 1) * pstorage->lineoffset[0]];
    for(temp = bit = x = 0; x < pstorage->storagexsize; x+=2) {
      if(x < (int)header.width) {
        if(header.bitcount != 24) {
          switch(header.bitcount) {
          case 1:
            if(bit == 0) {
              temp = fgetc(fp) & 0xff;
            }
            b1 = clut[temp&(1<<bit)?1:0];
            g1 = clut[temp&(1<<bit)?1:0]>>8;
            r1 = clut[temp&(1<<bit)?1:0]>>16;
            bit++;
            b2 = clut[temp&(1<<bit)?1:0];
            g2 = clut[temp&(1<<bit)?1:0]>>8;
            r2 = clut[temp&(1<<bit)?1:0]>>16;
            bit++;
            if(bit >= 8) {
              bit = 0;
            }
            break;
          case 4:
            temp = fgetc(fp) & 0xff;
            b1 = clut[temp>>4];
            g1 = clut[temp>>4]>>8;
            r1 = clut[temp>>4]>>16;
            b2 = clut[temp&15];
            g2 = clut[temp&15]>>8;
            r2 = clut[temp&15]>>16;
	    break;
          default:
            temp = fgetc(fp) & 0xff;
            b1 = clut[temp];
            g1 = clut[temp]>>8;
            r1 = clut[temp]>>16;
            temp = fgetc(fp) & 0xff;
            b2 = clut[temp];
            g2 = clut[temp]>>8;
            r2 = clut[temp]>>16;
          }
        } else {
          b1 = fgetc(fp); g1 = fgetc(fp); r1 = fgetc(fp);
          if(x + 1 < (int)header.width) {
            b2 = fgetc(fp); g2 = fgetc(fp); r2 = fgetc(fp);
          } else {
            b2 = 0; g2 = 0; r2 = 0;
          }
        }
      } else {
        b1 = 0; g1 = 0; r1 = 0;
        b2 = 0; g2 = 0; r2 = 0;
      }

      switch(pstorage->colormode) {
      case SV_COLORMODE_MONO:
        *p++ = 0x10 + ( 67 * r1      + 131 * g1      +  25 * b1) / 256;
        *p++ = 0x10 + ( 67 * r2      + 131 * g2      + 112 * b2) / 256;
        break;
      case SV_COLORMODE_CHROMA:
        *p++ = 0x80 + (112 * (r1+r2) -  93 * (g1+g2) -  18 * (b1+b2)) / 512;
        *p++ = 0x80 + (-38 * (r1+r2) -  73 * (g1+g2) + 131 * (b1+b2)) / 512;
        break;
      case SV_COLORMODE_YUV422:
        /*
        // lum =>  16 + 224 * (0.299 * R + 0.587 * G + 0.114 * B);
	// rmy => 128 + 224 * (0.500 * R - 0.419 * G - 0.081 * B);
	// bmy => 128 + 224 * (-.169 * R - 0.331 * G + 0.500 * B);
        */
        *p++ = 0x80 + (112 * (r1+r2) -  93 * (g1+g2) -  18 * (b1+b2)) / 512;
        *p++ = 0x10 + ( 67 * r1      + 131 * g1      +  25 * b1) / 256;
        *p++ = 0x80 + (-38 * (r1+r2) -  73 * (g1+g2) + 131 * (b1+b2)) / 512;
        *p++ = 0x10 + ( 67 * r2      + 131 * g2      + 112 * b2) / 256;
        break;
      case SV_COLORMODE_YUV2QT:
        *p++ = 0x10 + ( 67 * r1      + 131 * g1      +  25 * b1) / 256;
        *p++ =        (112 * (r1+r2) -  93 * (g1+g2) -  18 * (b1+b2)) / 512;
        *p++ = 0x10 + ( 67 * r2      + 131 * g2      + 112 * b2) / 256;
        *p++ =        (-38 * (r1+r2) -  73 * (g1+g2) + 131 * (b1+b2)) / 512;
       break;
      case SV_COLORMODE_YUV422A:
        *p++ = 0x80 + (112 * (r1+r2) -  93 * (g1+g2) -  18 * (b1+b2)) / 512;
        *p++ = 0x10 + ( 67 * r1      + 131 * g1      +  25 * b1) / 256;
        *p++ = (r1 + g1 + b1) / 3;
        *p++ = 0x80 + (-38 * (r1+r2) -  73 * (g1+g2) + 131 * (b1+b2)) / 512;
        *p++ = 0x10 + ( 67 * r2      + 131 * g2      + 112 * b2) / 256;
        *p++ = (r2 + g2 + b2) / 3;
        break;
      case SV_COLORMODE_RGB_BGR:
        *p++ = b1;
        *p++ = g1;
        *p++ = r1;
        *p++ = b2;
        *p++ = g2;
        *p++ = r2;
        break;
      case SV_COLORMODE_ABGR:
      case SV_COLORMODE_ARGB:
        *p++ = (r1 + g1 + b1) / 3;
        *p++ = r1;
        *p++ = g1;
        *p++ = b1;
        *p++ = (r2 + g2 + b2) / 3;
        *p++ = r2;
        *p++ = g2;
        *p++ = b2;
        break;
      case SV_COLORMODE_BGRA:
        *p++ = b1;
        *p++ = g1;
        *p++ = r1;
        *p++ = (r1 + g1 + b1) / 3;
        *p++ = b2;
        *p++ = g2;
        *p++ = r2;
        *p++ = (r2 + g2 + b2) / 3;
        break;
      case SV_COLORMODE_RGB_RGB:
        *p++ = r1;
        *p++ = g1;
        *p++ = b1;
        *p++ = r2;
        *p++ = g2;
        *p++ = b2;
        break;
      case SV_COLORMODE_RGBA:
        *p++ = r1;
        *p++ = g1;
        *p++ = b1;
        *p++ = (r1 + g1 + b1) / 3;
        *p++ = r2;
        *p++ = g2;
        *p++ = b2;
        *p++ = (r2 + g2 + b2) / 3;
        break;
      case SV_COLORMODE_YUV444:
        *p++ = 0x80;
        *p++ = (r1 + g1 + b1) / 3;
        *p++ = 0x80;
        *p++ = 0x80;
        *p++ = (r2 + g2 + b2) / 3; 
        *p++ = 0x80;
        break;
      case SV_COLORMODE_YUV444A:
        *p++ = 0x80;
        *p++ = (r1 + g1 + b1) / 3;
        *p++ = 0x80;
        *p++ = (r1 + g1 + b1) / 3;
        *p++ = 0x80;
        *p++ = (r2 + g2 + b2) / 3; 
        *p++ = 0x80;
        *p++ = (r2 + g2 + b2) / 3;
        break;
      default:
        return SV_ERROR_PROGRAM;
      }
    }
예제 #19
0
static int sync_send(int fd, const char *lpath, const char *rpath,
                     unsigned mtime, mode_t mode, int verifyApk)
{
    syncmsg msg;
    int len, r;
    syncsendbuf *sbuf = &send_buffer;
    char* file_buffer = NULL;
    int size = 0;
    char tmp[64];

    len = strlen(rpath);
    if(len > 1024) goto fail;

    snprintf(tmp, sizeof(tmp), ",%d", mode);
    r = strlen(tmp);

    if (verifyApk) {
        int lfd;
        zipfile_t zip;
        zipentry_t entry;
        int amt;

        // if we are transferring an APK file, then sanity check to make sure
        // we have a real zip file that contains an AndroidManifest.xml
        // this requires that we read the entire file into memory.
        lfd = adb_open(lpath, O_RDONLY);
        if(lfd < 0) {
            fprintf(stderr,"cannot open '%s': %s\n", lpath, strerror(errno));
            return -1;
        }

        size = adb_lseek(lfd, 0, SEEK_END);
        if (size == -1 || -1 == adb_lseek(lfd, 0, SEEK_SET)) {
            fprintf(stderr, "error seeking in file '%s'\n", lpath);
            adb_close(lfd);
            return 1;
        }

        file_buffer = (char *)malloc(size);
        if (file_buffer == NULL) {
            fprintf(stderr, "could not allocate buffer for '%s'\n",
                    lpath);
            adb_close(lfd);
            return 1;
        }
        amt = adb_read(lfd, file_buffer, size);
        if (amt != size) {
            fprintf(stderr, "error reading from file: '%s'\n", lpath);
            adb_close(lfd);
            free(file_buffer);
            return 1;
        }

        adb_close(lfd);

        zip = init_zipfile(file_buffer, size);
        if (zip == NULL) {
            fprintf(stderr, "file '%s' is not a valid zip file\n",
                    lpath);
            free(file_buffer);
            return 1;
        }

        entry = lookup_zipentry(zip, "AndroidManifest.xml");
        release_zipfile(zip);
        if (entry == NULL) {
            fprintf(stderr, "file '%s' does not contain AndroidManifest.xml\n",
                    lpath);
            free(file_buffer);
            return 1;
        }
    }

    msg.req.id = ID_SEND;
    msg.req.namelen = htoll(len + r);

    if(writex(fd, &msg.req, sizeof(msg.req)) ||
       writex(fd, rpath, len) || writex(fd, tmp, r)) {
        free(file_buffer);
        goto fail;
    }

    if (file_buffer) {
        write_data_buffer(fd, file_buffer, size, sbuf);
        free(file_buffer);
    } else if (S_ISREG(mode))
        write_data_file(fd, lpath, sbuf);
#ifdef HAVE_SYMLINKS
    else if (S_ISLNK(mode))
        write_data_link(fd, lpath, sbuf);
#endif
    else
        goto fail;

    msg.data.id = ID_DONE;
    msg.data.size = htoll(mtime);
    if(writex(fd, &msg.data, sizeof(msg.data)))
        goto fail;

    if(readx(fd, &msg.status, sizeof(msg.status)))
        return -1;

    if(msg.status.id != ID_OKAY) {
        if(msg.status.id == ID_FAIL) {
            len = ltohl(msg.status.msglen);
            if(len > 256) len = 256;
            if(readx(fd, sbuf->data, len)) {
                return -1;
            }
            sbuf->data[len] = 0;
        } else
            strcpy(sbuf->data, "unknown reason");

        fprintf(stderr,"failed to copy '%s' to '%s': %s\n", lpath, rpath, sbuf->data);
        return -1;
    }

    return 0;

fail:
    fprintf(stderr,"protocol failure\n");
    adb_close(fd);
    return -1;
}
예제 #20
0
static int handle_send_file(int s, char *path, uid_t uid,
        gid_t gid, mode_t mode, char *buffer, bool do_unlink)
{
    syncmsg msg;
    unsigned int timestamp = 0;
    int fd;

    fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
    if(fd < 0 && errno == ENOENT) {
        if(mkdirs(path) != 0) {
            if(fail_errno(s))
                return -1;
            fd = -1;
        } else {
            fd = adb_open_mode(path, O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, mode);
        }
    }
    if(fd < 0 && errno == EEXIST) {
        fd = adb_open_mode(path, O_WRONLY | O_CLOEXEC, mode);
    }
    if(fd < 0) {
        if(fail_errno(s))
            return -1;
        fd = -1;
    } else {
        if(fchown(fd, uid, gid) != 0) {
            fail_errno(s);
            errno = 0;
        }

        /*
         * fchown clears the setuid bit - restore it if present.
         * Ignore the result of calling fchmod. It's not supported
         * by all filesystems. b/12441485
         */
        fchmod(fd, mode);
    }

    for(;;) {
        unsigned int len;

        if(readx(s, &msg.data, sizeof(msg.data)))
            goto fail;

        if(msg.data.id != ID_DATA) {
            if(msg.data.id == ID_DONE) {
                timestamp = ltohl(msg.data.size);
                break;
            }
            fail_message(s, "invalid data message");
            goto fail;
        }
        len = ltohl(msg.data.size);

        if(syc_size_enabled == 1) {
            if(len > SYNC_DATA_MAX) {
                fail_message(s, "oversize data message");
                goto fail;
            } else {
                unsigned int total = 0;
                while (total < len) {
                    int count = len - total;
                    if (count > SYNC_DATA_MAX_CUSTOMIZE) {
                        count = SYNC_DATA_MAX_CUSTOMIZE;
                    }

                    if(readx(s, buffer, count))
                        goto fail;

                    if(fd < 0)
                        continue;
                    if(writex(fd, buffer, count)) {
                        int saved_errno = errno;
                        adb_close(fd);
                        if (do_unlink) adb_unlink(path);
                        fd = -1;
                        errno = saved_errno;
                        if(fail_errno(s)) return -1;
                    }

                    total += count;
                }
            }
        }else {
        if(len > SYNC_DATA_MAX) {
            fail_message(s, "oversize data message");
            goto fail;
        }
        if(readx(s, buffer, len))
            goto fail;

        if(fd < 0)
            continue;
        if(writex(fd, buffer, len)) {
            int saved_errno = errno;
            adb_close(fd);
            if (do_unlink) adb_unlink(path);
            fd = -1;
            errno = saved_errno;
            if(fail_errno(s)) return -1;
        }
    }
    }

    if(fd >= 0) {
        struct utimbuf u;
        adb_close(fd);
        selinux_android_restorecon(path, 0);
        u.actime = timestamp;
        u.modtime = timestamp;
        utime(path, &u);

        msg.status.id = ID_OKAY;
        msg.status.msglen = 0;
        if(writex(s, &msg.status, sizeof(msg.status)))
            return -1;
    }
    return 0;

fail:
    if(fd >= 0)
        adb_close(fd);
    if (do_unlink) adb_unlink(path);
    return -1;
}