File: image.c

package info (click to toggle)
normalize 0.7.2-1
  • links: PTS
  • area: main
  • in suites: woody
  • size: 1,804 kB
  • ctags: 719
  • sloc: ansic: 8,244; sh: 7,534; perl: 454; makefile: 177; sed: 16
file content (137 lines) | stat: -rw-r--r-- 3,226 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/* Copyright (C) 2001 Chris Vaill
   This file is part of nid3lib.

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */

/*
 * Image frame (APIC) manipulation routines
 */

#define _POSIX_C_SOURCE 2

#include "config.h"

#if STDC_HEADERS
# include <stdlib.h>
# include <string.h>
#else
# if HAVE_STDLIB_H
#  include <stdlib.h>
# endif
# if HAVE_STRING_H
#  include <string.h>
# endif
#endif
#if HAVE_ERRNO_H
# include <errno.h>
#endif

#include "nid3P.h"


/*
 * For a PIC or APIC frame, save the image in file fname, with the
 * proper extension added (e.g. fname.jpg if the mime type is
 * image/jpeg).
 */
int
id3_frame_save_image(id3_frame_t f, const char *fname)
{
  id3_t tag = f->id3;
  unsigned char *data;
  FILE *out;
  char *fullname = NULL, *mimetype;
  int i, err_save;

  data = id3_frame_get_raw(f);
  if (data == NULL)
    return -1;

  switch (id3_get_version(tag)) {
  case ID3_VERSION_2_2:
    if (strcmp(f->id, "PIC") != 0)
      return -1;
    if (f->sz < 4)
      return -1;
    fullname = (char *)malloc(strlen(fname) + 4 + 1);
    if (fullname == NULL)
      return -1;
    sprintf(fullname, "%s.%c%c%c", fname, data[1], data[2], data[3]);
    data = data + 5;
    break;

  case ID3_VERSION_2_3:
  case ID3_VERSION_2_4:
    if (strcmp(f->id, "APIC") != 0)
      return -1;
    /* make sure the mime type is terminated */
    for (i = 1; i < f->sz; i++)
      if (data[i] == '\0')
	break;
    if (data[i] != '\0' || i < 8)
      return -1;
    mimetype = data + 7;
    fullname = (char *)malloc(strlen(fname) + strlen(mimetype) + 2);
    if (fullname == NULL)
      return -1;
    sprintf(fullname, "%s.%s", fname, mimetype);
    data = data + strlen(mimetype) + 6 + 3;
  }

  /* skip description string */
  switch (f->data[0]) {
  case ID3_TEXT_UTF16:
  case ID3_TEXT_UTF16BE:
    while (!(data[0] == '\0' && data[1] == '\0') && data - f->data < f->sz)
      data += 2;
    if (!(data[0] == '\0' && data[1] == '\0'))
      goto error_free;
    data += 2;
    break;
  case ID3_TEXT_UTF8:
  case ID3_TEXT_ISO:
    while (data[0] != '\0' && data - f->data < f->sz)
      data++;
    if (data[0] != '\0')
      goto error_free;
    data++;
    break;
  }

  if (data - f->data >= f->sz)
    goto error_free;

  out = fopen(fullname, "wb");
  if (out == NULL)
    goto error_close;
  if (fwrite(data, f->sz - (data - f->data), 1, out) < 1)
    goto error_close;
  fclose(out);
  free(fullname);

  return 0;

 error_close:
  err_save = errno;
  fclose(out);
  free(fullname);
  errno = err_save;
  return -1;

 error_free:
  free(fullname);
  errno = EINVAL;
  return -1;
}