2 * Copyright (C) 2013 Thadeu Lima de Souza Cascardo <cascardo@minaslivre.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 3 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
28 static int add_field(char *buffer, size_t len, char *key, int klen, char *val, int vlen)
31 if (len < (klen + vlen + 3))
33 if (klen > 0x7f || klen < 0)
35 if (vlen > 0x7fff || vlen < 0)
37 buffer[0] = klen & 0x7f;
42 memcpy(buffer, key, klen);
46 buffer[0] = (vlen >> 8) & 0x7f;
47 buffer[1] = vlen & 0xff;
51 buffer[0] = vlen & 0x7f;
55 memcpy(buffer, val, vlen);
60 static int add_u32(char *buffer, int len, char *key, uint32_t val)
62 uint32_t nval = htonl(val);
63 return add_field(buffer, len, key, strlen(key), (char *) &nval, sizeof(val));
66 static int add_u8(char *buffer, int len, char *key, uint8_t val)
68 return add_field(buffer, len, key, strlen(key), (char *) &val, sizeof(val));
71 static int add_ascii(char *buffer, int len, char *key, char *val)
73 return add_field(buffer, len, key, strlen(key), val, strlen(val));
76 int resp(char **buffer)
85 //r = read(0, file, sizeof(file) - 1);
90 *buffer = malloc(slen);
91 if (*buffer == NULL) {
94 memset(*buffer, 0, slen);
99 n = add_field(p, end - p, "POS_INICIO", strlen("POS_INICIO"), (char *)&pos, 8);
102 n = add_u32(p, end - p, "POS_INICIO", 0);
104 n = add_u8(p, end - p, "SUBTIPO", 0);
106 n = add_ascii(p, end - p, "TEXTO", "0001.ERRO! Buahahahaha!");
108 n = add_ascii(p, end - p, "ARQUIVO", file);
115 int main(int argc, char **argv)