Example #1
0
int
oggz_comments_copy (OGGZ * src, long src_serialno,
                    OGGZ * dest, long dest_serialno)
{
    const OggzComment * comment;

    if (src == NULL || dest == NULL) return OGGZ_ERR_BAD_OGGZ;

    if (dest->flags & OGGZ_WRITE) {
        if (OGGZ_CONFIG_WRITE) {
            oggz_comment_set_vendor (dest, dest_serialno,
                                     oggz_comment_get_vendor (src, src_serialno));

            for (comment = oggz_comment_first (src, src_serialno); comment;
                    comment = oggz_comment_next (src, src_serialno, comment)) {
                oggz_comment_add (dest, dest_serialno, comment);
            }
        } else {
            return OGGZ_ERR_DISABLED;
        }
    } else {
        return OGGZ_ERR_INVALID;
    }

    return 0;
}
Example #2
0
long
oggz_comments_encode (OGGZ * oggz, long serialno,
                      unsigned char * buf, long length)
{
    oggz_stream_t * stream;
    char * c = (char *)buf;
    const OggzComment * comment;
    int nb_fields = 0, vendor_length = 0, field_length;
    long actual_length, remaining = length;

    stream = oggz_get_stream (oggz, serialno);
    if (stream == NULL) return OGGZ_ERR_BAD_SERIALNO;

    /* Vendor string */
    if (stream->vendor)
        vendor_length = strlen (stream->vendor);
    actual_length = 4 + vendor_length;
#ifdef DEBUG
    printf ("oggz_comments_encode: vendor = %s\n", stream->vendor);
#endif

    /* user comment list length */
    actual_length += 4;

    for (comment = oggz_comment_first (oggz, serialno); comment;
            comment = oggz_comment_next (oggz, serialno, comment)) {
        actual_length += 4 + strlen (comment->name);    /* [size]"name" */
        if (comment->value)
            actual_length += 1 + strlen (comment->value); /* "=value" */

#ifdef DEBUG
        printf ("oggz_comments_encode: %s = %s\n",
                comment->name, comment->value);
#endif

        nb_fields++;
    }

    actual_length++; /* framing bit */

    if (buf == NULL) return actual_length;

    remaining -= 4;
    if (remaining <= 0) return actual_length;
    writeint (c, 0, vendor_length);
    c += 4;

    if (stream->vendor) {
        field_length = strlen (stream->vendor);
        memcpy (c, stream->vendor, MIN (field_length, remaining));
        c += field_length;
        remaining -= field_length;
        if (remaining <= 0 ) return actual_length;
    }

    remaining -= 4;
    if (remaining <= 0) return actual_length;
    writeint (c, 0, nb_fields);
    c += 4;

    for (comment = oggz_comment_first (oggz, serialno); comment;
            comment = oggz_comment_next (oggz, serialno, comment)) {

        field_length = strlen (comment->name);     /* [size]"name" */
        if (comment->value)
            field_length += 1 + strlen (comment->value); /* "=value" */

        remaining -= 4;
        if (remaining <= 0) return actual_length;
        writeint (c, 0, field_length);
        c += 4;

        field_length = strlen (comment->name);
        memcpy (c, comment->name, MIN (field_length, remaining));
        c += field_length;
        remaining -= field_length;
        if (remaining <= 0) return actual_length;

        if (comment->value) {
            remaining --;
            if (remaining <= 0) return actual_length;
            *c = '=';
            c++;

            field_length = strlen (comment->value);
            memcpy (c, comment->value, MIN (field_length, remaining));
            c += field_length;
            remaining -= field_length;
            if (remaining <= 0) return actual_length;
        }
    }

    if (remaining <= 0) return actual_length;
    *c = 0x01;

    return actual_length;
}
long
oggz_comments_encode (OGGZ * oggz, long serialno,
                      unsigned char * buf, long length)
{
  oggz_stream_t * stream;
  char * c = (char *)buf;
  const OggzComment * comment;
  int nb_fields = 0, vendor_length = 0;
  unsigned long actual_length = 0, remaining = length, field_length;

  /* Deal with sign of length first */
  if (length < 0) return 0;

  stream = oggz_get_stream (oggz, serialno);
  if (stream == NULL) return OGGZ_ERR_BAD_SERIALNO;

  /* Vendor string */
  if (stream->vendor)
    vendor_length = oggz_comment_len (stream->vendor);
  if (accum_length (&actual_length, 4 + vendor_length) == 0)
    return 0;
#ifdef DEBUG
  printf ("oggz_comments_encode: vendor = %s\n", stream->vendor);
#endif

  /* user comment list length */
  if (accum_length (&actual_length, 4) == 0)
    return 0;


  for (comment = oggz_comment_first (oggz, serialno); comment;
       comment = oggz_comment_next (oggz, serialno, comment)) {
    /* [size]"name" */
    if (accum_length (&actual_length, 4 + oggz_comment_len (comment->name)) == 0)
      return 0;
    if (comment->value) {
      /* "=value" */
      if (accum_length (&actual_length, 1 + oggz_comment_len (comment->value)) == 0)
        return 0;
    }

#ifdef DEBUG
    printf ("oggz_comments_encode: %s = %s\n",
	    comment->name, comment->value);
#endif

    nb_fields++;
  }

  /* framing bit */
  if (accum_length (&actual_length, 1) == 0)
    return 0;

  /* NB. actual_length is not modified from here onwards */

  if (buf == NULL) return actual_length;

  remaining -= 4;
  if (remaining <= 0) return actual_length;
  writeint (c, 0, vendor_length);
  c += 4;

  if (stream->vendor) {
    field_length = oggz_comment_len (stream->vendor);
    memcpy (c, stream->vendor, MIN (field_length, remaining));
    c += field_length; remaining -= field_length;
    if (remaining <= 0) return actual_length;
  }

  remaining -= 4;
  if (remaining <= 0) return actual_length;
  writeint (c, 0, nb_fields);
  c += 4;

  for (comment = oggz_comment_first (oggz, serialno); comment;
       comment = oggz_comment_next (oggz, serialno, comment)) {

    field_length = oggz_comment_len (comment->name);     /* [size]"name" */
    if (comment->value)
      field_length += 1 + oggz_comment_len (comment->value); /* "=value" */

    remaining -= 4;
    if (remaining <= 0) return actual_length;
    writeint (c, 0, field_length);
    c += 4;

    field_length = oggz_comment_len (comment->name);
    memcpy (c, comment->name, MIN (field_length, remaining));
    c += field_length; remaining -= field_length;
    if (remaining <= 0) return actual_length;

    if (comment->value) {
      remaining --;
      if (remaining <= 0) return actual_length;
      *c = '=';
      c++;

      field_length = oggz_comment_len (comment->value);
      memcpy (c, comment->value, MIN (field_length, remaining));
      c += field_length; remaining -= field_length;
      if (remaining <= 0) return actual_length;
    }
  }

  if (remaining <= 0) return actual_length;
  *c = 0x01;

  return actual_length;
}