/* Copyright (c) 2004, Noah Sorscher
 * All rights reserved.
 *
 * 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.
 * 	¥ 	Neither the name of the Noah Sorscher nor the names of its
 * 	contributors may be used to endorse or promote products derived from
 * 	this software without specific prior written permission.
 * 
 * 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 COPYRIGHT OWNER 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.
 */

/*
 *  APIC.c
 *  
 *
 *  Created by Noah on 11/5/04.
 *  Copyright 2004 __MyCompanyName__. All rights reserved.
 *  metahoot@3ivx.com
 *
 */

#include "APIC.h"


/*  
	http://www.id3.org/id3v2.4.0-frames.txt
	
    V2.3-2.4 encoding
    Text encoding      $xx
    MIME type          <text string> $00
    Picture type       $xx
    Description        <text string according to encoding> $00 (00)
    Picture data       <binary data> */


pictureResource parseTag(Handle h) {
	pictureResource pr;
	pr.textEncoding = FALSE;
	pr.mimeType = NULL;
	pr.picType = Other;
	pr.description = NULL;
	pr.pictureData = NULL;
	
	if (!h) return pr;
	long size = GetHandleSize(h);
	if (!size) return pr;
	char *bytes = (char *)*h;
	pr.textEncoding = bytes[0];
	pr.mimeType = bytes+1;
	int index = 1;
        index += strlen(pr.mimeType) + 1; // plus 1 for the closing '\0'
        if (index >= size) // found no '\0'!
		return pr;
	pr.picType = bytes[index++];
	pr.description = bytes+index;
        index += strlen(pr.description) + 1;
	if (index == size)
		return pr;
	if (pr.textEncoding) // unicode, ends in two 0's, not one
		index++;
	// picture data
        pr.pictureData = NewHandleClear(size);
	BlockMoveData(bytes+index, *(pr.pictureData), size-index);
	return pr;
}

Handle createTag(pictureResource r) {
	int mimeLength = strlen(r.mimeType), descLength = strlen(r.description), pictureLength = 0, totalLength = 0, i=0;
	
	if (r.pictureData) pictureLength = GetHandleSize(r.pictureData);
	totalLength = 4;
	if (r.textEncoding) totalLength++;
	totalLength += mimeLength;
	totalLength += descLength;
	totalLength += pictureLength;
	
	char *bytes = malloc(totalLength * sizeof(char));
	bytes[i++] = r.textEncoding;
	strncpy(bytes+i, r.mimeType, mimeLength); i += mimeLength;
	bytes[i++] = 0;
        bytes[i++] = r.picType;
	strncpy(bytes+i, r.description, descLength); i+= descLength;
	bytes[i++] = 0;
	if (r.textEncoding)
		bytes[i++] = 0;
	if (pictureLength)
		//BlockMoveData(*(r.pictureData), bytes, pictureLength);
                memcpy(bytes+i, *(r.pictureData), pictureLength * sizeof(char));
	
	Handle h = NewHandleClear(totalLength);
	BlockMoveData(bytes, *h, totalLength);
        free(bytes);
	return h;
}

void disposeTag(pictureResource r) {
    if (r.pictureData) DisposeHandle(r.pictureData);
}
