cake
/
libg1m
Archived
1
0
Fork 0
This repository has been archived on 2024-03-16. You can view files and clone it, but cannot push or open issues or pull requests.
libg1m/src/encode/addin.c

86 lines
2.7 KiB
C

/* *****************************************************************************
* encode/addin.c -- encode an addin.
* Copyright (C) 2017 Thomas "Cakeisalie5" Touhey <thomas@touhey.fr>
*
* This file is part of libg1m.
* libg1m is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 3.0 of the License,
* or (at your option) any later version.
*
* libg1m 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with libg1m; if not, see <http://www.gnu.org/licenses/>.
* ************************************************************************** */
#include <libg1m/internals.h>
/**
* g1m_announce_addin:
* Announce an add-in's size.
*
* @arg handle the handle.
* @arg size the size to feed.
* @return the error code (0 if ok).
*/
int g1m_announce_addin(g1m_handle_t *handle, size_t *size)
{
*size += sizeof(struct standard_header) + sizeof(struct g1a_subheader);
*size += handle->g1m_handle_size;
return (0);
}
/**
* g1m_encode_addin:
* Encode an add-in.
*
* @arg handle the handle.
* @arg buffer the buffer to which to write to.
* @return the error code (0 if ok).
*/
int g1m_encode_addin(g1m_handle_t *handle, g1m_buffer_t *buffer)
{
size_t i; uint8_t *b;
/* make the StandardHeader up */
size_t filesize = 0; g1m_announce_addin(handle, &filesize);
struct standard_header std = {
.main_id = "USBPower",
.subtype = "\xF3\x00\x10\x00\x10\x00",
.filesize = htobe32((uint32_t)filesize),
.control = (filesize + 0x41) & 0xFF,
.control2 = (filesize + 0xB8) & 0xFF,
.number = 0xFFFF};
/* reverse it and output it */
b = (void*)&std;
for (i = 0; i < sizeof(struct standard_header); i++) b[i] = ~b[i];
DWRITE(std)
/* make the add-in subheader up and output it */
filesize -= sizeof(struct standard_header);
struct g1a_subheader sub = {
.estrips_count = 0,
.filesize = htobe32(filesize)};
strncpy((char*)sub.title, handle->g1m_handle_title, 8);
strncpy((char*)sub.internal_name, handle->g1m_handle_intname, 8);
g1m_encode_version(&handle->g1m_handle_version, (char*)sub.version);
g1m_encode_date(&handle->g1m_handle_creation_date,
(char*)sub.creation_date);
g1m_encode_picture((const uint32_t**)handle->g1m_handle_icon_unsel,
g1m_pictureformat_1bit, sub.icon,
G1A_ICON_WIDTH, G1A_ICON_HEIGHT);
DWRITE(sub)
/* output the content */
WRITE(handle->g1m_handle_content, handle->g1m_handle_size)
/* no error! */
return (0);
}