aboutsummaryrefslogtreecommitdiff
path: root/Src/external_dependencies/openmpt-trunk/include/opusenc/src/picture.c
blob: ae40e5dbbea740c97e7301fdbaecc45e055b21ce (plain) (blame)
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/* Copyright (C)2007-2013 Xiph.Org Foundation
   File: picture.c

   Redistribution and use in source and binary forms, with or without
   modification, are permitted provided that the following conditions
   are met:

   - Redistributions of source code must retain the above copyright
   notice, this list of conditions and the following disclaimer.

   - Redistributions in binary form must reproduce the above copyright
   notice, this list of conditions and the following disclaimer in the
   documentation and/or other materials provided with the distribution.

   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
   ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
   A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR
   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
   PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
   LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
   SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "picture.h"
#include "unicode_support.h"

static const char BASE64_TABLE[64]={
  'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P',
  'Q','R','S','T','U','V','W','X','Y','Z','a','b','c','d','e','f',
  'g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v',
  'w','x','y','z','0','1','2','3','4','5','6','7','8','9','+','/'
};

/*Utility function for base64 encoding METADATA_BLOCK_PICTURE tags.
  Stores BASE64_LENGTH(len)+1 bytes in dst (including a terminating NUL).*/
static void base64_encode(char *dst, const char *src, int len){
  unsigned s0;
  unsigned s1;
  unsigned s2;
  int      ngroups;
  int      i;
  ngroups=len/3;
  for(i=0;i<ngroups;i++){
    s0=(unsigned char)src[3*i+0];
    s1=(unsigned char)src[3*i+1];
    s2=(unsigned char)src[3*i+2];
    dst[4*i+0]=BASE64_TABLE[s0>>2];
    dst[4*i+1]=BASE64_TABLE[(s0&3)<<4|s1>>4];
    dst[4*i+2]=BASE64_TABLE[(s1&15)<<2|s2>>6];
    dst[4*i+3]=BASE64_TABLE[s2&63];
  }
  len-=3*i;
  if(len==1){
    s0=(unsigned char)src[3*i+0];
    dst[4*i+0]=BASE64_TABLE[s0>>2];
    dst[4*i+1]=BASE64_TABLE[(s0&3)<<4];
    dst[4*i+2]='=';
    dst[4*i+3]='=';
    i++;
  }
  else if(len==2){
    s0=(unsigned char)src[3*i+0];
    s1=(unsigned char)src[3*i+1];
    dst[4*i+0]=BASE64_TABLE[s0>>2];
    dst[4*i+1]=BASE64_TABLE[(s0&3)<<4|s1>>4];
    dst[4*i+2]=BASE64_TABLE[(s1&15)<<2];
    dst[4*i+3]='=';
    i++;
  }
  dst[4*i]='\0';
}

/*A version of strncasecmp() that is guaranteed to only ignore the case of
   ASCII characters.*/
static int oi_strncasecmp(const char *a, const char *b, int n){
  int i;
  for(i=0;i<n;i++){
    int aval;
    int bval;
    int diff;
    aval=a[i];
    bval=b[i];
    if(aval>='a'&&aval<='z') {
      aval-='a'-'A';
    }
    if(bval>='a'&&bval<='z'){
      bval-='a'-'A';
    }
    diff=aval-bval;
    if(diff){
      return diff;
    }
  }
  return 0;
}

static int is_jpeg(const unsigned char *buf, size_t length){
  return length>=3&&memcmp(buf,"\xFF\xD8\xFF",3)==0;
}

static int is_png(const unsigned char *buf, size_t length){
  return length>=8&&memcmp(buf,"\x89PNG\x0D\x0A\x1A\x0A",8)==0;
}

static int is_gif(const unsigned char *buf, size_t length){
  return length>=6
   &&(memcmp(buf,"GIF87a",6)==0||memcmp(buf,"GIF89a",6)==0);
}

#define READ_U32_BE(buf) \
    (((buf)[0]<<24)|((buf)[1]<<16)|((buf)[2]<<8)|((buf)[3]&0xff))

/*Tries to extract the width, height, bits per pixel, and palette size of a
   PNG.
  On failure, simply leaves its outputs unmodified.*/
static void extract_png_params(const unsigned char *data, size_t data_length,
                        opus_uint32 *width, opus_uint32 *height,
                        opus_uint32 *depth, opus_uint32 *colors,
                        int *has_palette){
  if(is_png(data,data_length)){
    size_t offs;
    offs=8;
    while(data_length-offs>=12){
      opus_uint32 chunk_len;
      chunk_len=READ_U32_BE(data+offs);
      if(chunk_len>data_length-(offs+12))break;
      else if(chunk_len==13&&memcmp(data+offs+4,"IHDR",4)==0){
        int color_type;
        *width=READ_U32_BE(data+offs+8);
        *height=READ_U32_BE(data+offs+12);
        color_type=data[offs+17];
        if(color_type==3){
          *depth=24;
          *has_palette=1;
        }
        else{
          int sample_depth;
          sample_depth=data[offs+16];
          if(color_type==0)*depth=sample_depth;
          else if(color_type==2)*depth=sample_depth*3;
          else if(color_type==4)*depth=sample_depth*2;
          else if(color_type==6)*depth=sample_depth*4;
          *colors=0;
          *has_palette=0;
          break;
        }
      }
      else if(*has_palette>0&&memcmp(data+offs+4,"PLTE",4)==0){
        *colors=chunk_len/3;
        break;
      }
      offs+=12+chunk_len;
    }
  }
}

/*Tries to extract the width, height, bits per pixel, and palette size of a
   GIF.
  On failure, simply leaves its outputs unmodified.*/
static void extract_gif_params(const unsigned char *data, size_t data_length,
                        opus_uint32 *width, opus_uint32 *height,
                        opus_uint32 *depth, opus_uint32 *colors,
                        int *has_palette){
  if(is_gif(data,data_length)&&data_length>=14){
    *width=data[6]|data[7]<<8;
    *height=data[8]|data[9]<<8;
    /*libFLAC hard-codes the depth to 24.*/
    *depth=24;
    *colors=1<<((data[10]&7)+1);
    *has_palette=1;
  }
}


/*Tries to extract the width, height, bits per pixel, and palette size of a
   JPEG.
  On failure, simply leaves its outputs unmodified.*/
static void extract_jpeg_params(const unsigned char *data, size_t data_length,
                         opus_uint32 *width, opus_uint32 *height,
                         opus_uint32 *depth, opus_uint32 *colors,
                         int *has_palette){
  if(is_jpeg(data,data_length)){
    size_t offs;
    offs=2;
    for(;;){
      size_t segment_len;
      int    marker;
      while(offs<data_length&&data[offs]!=0xFF)offs++;
      while(offs<data_length&&data[offs]==0xFF)offs++;
      marker=data[offs];
      offs++;
      /*If we hit EOI* (end of image), or another SOI* (start of image),
         or SOS (start of scan), then stop now.*/
      if(offs>=data_length||(marker>=0xD8&&marker<=0xDA))break;
      /*RST* (restart markers): skip (no segment length).*/
      else if(marker>=0xD0&&marker<=0xD7)continue;
      /*Read the length of the marker segment.*/
      if(data_length-offs<2)break;
      segment_len=data[offs]<<8|data[offs+1];
      if(segment_len<2||data_length-offs<segment_len)break;
      if(marker==0xC0||(marker>0xC0&&marker<0xD0&&(marker&3)!=0)){
        /*Found a SOFn (start of frame) marker segment:*/
        if(segment_len>=8){
          *height=data[offs+3]<<8|data[offs+4];
          *width=data[offs+5]<<8|data[offs+6];
          *depth=data[offs+2]*data[offs+7];
          *colors=0;
          *has_palette=0;
        }
        break;
      }
      /*Other markers: skip the whole marker segment.*/
      offs+=segment_len;
    }
  }
}

#define IMAX(a,b) ((a) > (b) ? (a) : (b))

static unsigned char *opeint_read_picture_file(const char *filename, const char *description, int *error, size_t *size, size_t *offset) {
  FILE          *picture_file;
  size_t         cbuf;
  size_t         nbuf;
  size_t         data_offset;
  unsigned char *buf;
  picture_file=opeint_fopen(filename,"rb");
  /*Buffer size: 8 static 4-byte fields plus 2 dynamic fields, plus the
     file/URL data.
    We reserve at least 10 bytes for the media type, in case we still need to
     extract it from the file.*/
  data_offset=32+strlen(description)+10;
  buf=NULL;
  /*Complicated case: we have a real file.
    Read it in, attempt to parse the media type and image dimensions if
     necessary, and validate what the user passed in.*/
  if(picture_file==NULL){
    *error = OPE_CANNOT_OPEN;
    return NULL;
  }
  nbuf=data_offset;
  /*Add a reasonable starting image file size.*/
  cbuf=data_offset+65536;
  for(;;){
    unsigned char *new_buf;
    size_t         nread;
    new_buf=realloc(buf,cbuf);
    if(new_buf==NULL){
      fclose(picture_file);
      free(buf);
      *error = OPE_ALLOC_FAIL;
      return NULL;
    }
    buf=new_buf;
    nread=fread(buf+nbuf,1,cbuf-nbuf,picture_file);
    nbuf+=nread;
    if(nbuf<cbuf){
      int file_error;
      file_error=ferror(picture_file);
      fclose(picture_file);
      if(file_error){
        free(buf);
        *error = OPE_INVALID_PICTURE;
        return NULL;
      }
      break;
    }
    if(cbuf==0xFFFFFFFF){
      fclose(picture_file);
      free(buf);
      *error = OPE_INVALID_PICTURE;
      return NULL;
    }
    else if(cbuf>0x7FFFFFFFU)cbuf=0xFFFFFFFFU;
    else cbuf=cbuf<<1|1;
  }
  *size = nbuf;
  *offset = data_offset;
  return buf;
}

static int validate_picture_type(int picture_type, int seen_file_icons) {
  if (picture_type > 20) return 0;
  if(picture_type>=1&&picture_type<=2&&(seen_file_icons&picture_type)) return 0;
  return 1;
}

/*Parse a picture SPECIFICATION as given on the command-line.
  spec: The specification.
  error_message: Returns an error message on error.
  seen_file_icons: Bit flags used to track if any pictures of type 1 or type 2
   have already been added, to ensure only one is allowed.
  Return: A Base64-encoded string suitable for use in a METADATA_BLOCK_PICTURE
   tag.*/
static char *opeint_parse_picture_specification_impl(unsigned char *buf, size_t nbuf, size_t data_offset, int picture_type, const char *description,
                                  int *error, int *seen_file_icons){
  opus_uint32  width;
  opus_uint32  height;
  opus_uint32  depth;
  opus_uint32  colors;
  const char    *mime_type;
  char          *out;
  size_t         data_length;
  size_t         b64_length;
  int          has_palette;
  *error = OPE_OK;
  if (picture_type < 0) picture_type=3;
  if (!validate_picture_type(picture_type, *seen_file_icons)) {
    *error = OPE_INVALID_PICTURE;
    return NULL;
  }
  if (buf == NULL) return NULL;
  data_length=nbuf-data_offset;
  /*Try to extract the image dimensions/color information from the file.*/
  width=height=depth=colors=0;
  has_palette=-1;
  {
    if(is_jpeg(buf+data_offset,data_length)){
      mime_type="image/jpeg";
      extract_jpeg_params(buf+data_offset,data_length,
       &width,&height,&depth,&colors,&has_palette);
    }
    else if(is_png(buf+data_offset,data_length)){
      mime_type="image/png";
      extract_png_params(buf+data_offset,data_length,
       &width,&height,&depth,&colors,&has_palette);
    }
    else if(is_gif(buf+data_offset,data_length)){
      mime_type="image/gif";
      extract_gif_params(buf+data_offset,data_length,
       &width,&height,&depth,&colors,&has_palette);
    }
    else{
      *error = OPE_INVALID_PICTURE;
      return NULL;
    }
  }
  /*These fields MUST be set correctly OR all set to zero.
    So if any of them (except colors, for which 0 is a valid value) are still
     zero, clear the rest to zero.*/
  if(width==0||height==0||depth==0)width=height=depth=colors=0;
  if(picture_type==1&&(width!=32||height!=32
   ||strlen(mime_type)!=9
   ||oi_strncasecmp("image/png",mime_type,9)!=0)){
    *error = OPE_INVALID_ICON;
    return NULL;
  }
  /*Build the METADATA_BLOCK_PICTURE buffer.
    We do this backwards from data_offset, because we didn't necessarily know
     how big the media type string was before we read the data in.*/
  data_offset-=4;
  WRITE_U32_BE(buf+data_offset,(unsigned long)data_length);
  data_offset-=4;
  WRITE_U32_BE(buf+data_offset,colors);
  data_offset-=4;
  WRITE_U32_BE(buf+data_offset,depth);
  data_offset-=4;
  WRITE_U32_BE(buf+data_offset,height);
  data_offset-=4;
  WRITE_U32_BE(buf+data_offset,width);
  data_offset-=strlen(description);
  memcpy(buf+data_offset,description,strlen(description));
  data_offset-=4;
  WRITE_U32_BE(buf+data_offset,strlen(description));
  data_offset-=strlen(mime_type);
  memcpy(buf+data_offset,mime_type,strlen(mime_type));
  data_offset-=4;
  WRITE_U32_BE(buf+data_offset,strlen(mime_type));
  data_offset-=4;
  WRITE_U32_BE(buf+data_offset,picture_type);
  data_length=nbuf-data_offset;
  b64_length=BASE64_LENGTH(data_length);
  out=(char *)malloc(b64_length+1);
  if(out!=NULL){
    base64_encode(out,(char *)buf+data_offset,data_length);
    if(picture_type>=1&&picture_type<=2)*seen_file_icons|=picture_type;
  } else {
    *error = OPE_ALLOC_FAIL;
  }
  return out;
}

char *opeint_parse_picture_specification(const char *filename, int picture_type, const char *description,
                                  int *error, int *seen_file_icons){
  size_t nbuf;
  size_t data_offset;
  unsigned char *buf;
  char *ret;
  if (picture_type < 0) picture_type=3;
  if (!validate_picture_type(picture_type, *seen_file_icons)) {
    *error = OPE_INVALID_PICTURE;
    return NULL;
  }
  if (description == NULL) description = "";
  buf = opeint_read_picture_file(filename, description, error, &nbuf, &data_offset);
  if (buf == NULL) return NULL;
  ret = opeint_parse_picture_specification_impl(buf, nbuf, data_offset, picture_type, description, error, seen_file_icons);
  free(buf);
  return ret;
}

char *opeint_parse_picture_specification_from_memory(const char *mem, size_t size, int picture_type, const char *description,
                                  int *error, int *seen_file_icons){
  size_t nbuf;
  size_t data_offset;
  unsigned char *buf;
  char *ret;
  if (picture_type < 0) picture_type=3;
  if (!validate_picture_type(picture_type, *seen_file_icons)) {
    *error = OPE_INVALID_PICTURE;
    return NULL;
  }
  if (description == NULL) description = "";
  data_offset=32+strlen(description)+10;
  nbuf = data_offset + size;
  buf = (unsigned char *)malloc(nbuf);
  if (buf == NULL) {
    *error = OPE_ALLOC_FAIL;
    return NULL;
  }
  memcpy(buf+data_offset, mem, size);
  ret = opeint_parse_picture_specification_impl(buf, nbuf, data_offset, picture_type, description, error, seen_file_icons);
  free(buf);
  return ret;
}