1 /* cvm/module_output.c - Response formatting
2 * Copyright (C) 2010 Bruce Guenter <bruce@untroubled.org>
3 *
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 2 of the License, or
7 * (at your option) any later version.
8 *
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.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include <sys/types.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <unistd.h>
22 #include "module.h"
23 #include "protocol.h"
24
25 unsigned char cvm_module_outbuffer[BUFSIZE];
26 unsigned cvm_module_outbuflen;
27 static unsigned char* outbufptr;
28
v1fact(unsigned number,const char * data,unsigned len)29 static int v1fact(unsigned number, const char* data, unsigned len)
30 {
31 /* Always leave room for a trailing NUL. */
32 if (cvm_module_outbuflen + len + 3 > BUFSIZE) {
33 cvm_module_outbuflen = BUFSIZE;
34 return 0;
35 }
36 cvm_module_outbuflen += len + 2;
37 *outbufptr++ = number;
38 memcpy(outbufptr, data, len);
39 outbufptr += len;
40 *outbufptr++ = 0;
41 return 1;
42 }
43
v2fact(unsigned number,const char * data,unsigned len)44 static int v2fact(unsigned number, const char* data, unsigned len)
45 {
46 /* Always leave room for a trailing zero type byte. */
47 if (cvm_module_outbuflen + len + 3 > BUFSIZE) {
48 cvm_module_outbuflen = BUFSIZE;
49 return 0;
50 }
51 cvm_module_outbuflen += len + 2;
52 *outbufptr++ = number;
53 *outbufptr++ = len;
54 memcpy(outbufptr, data, len);
55 outbufptr += len;
56 return 1;
57 }
58
59 static int (*fact)(unsigned,const char*,unsigned);
60
cvm1_fact_start(void)61 static void cvm1_fact_start(void)
62 {
63 fact = v1fact;
64 cvm_module_outbuflen = 1;
65 outbufptr = cvm_module_outbuffer + 1;
66 }
67
cvm2_fact_start(void)68 static void cvm2_fact_start(void)
69 {
70 fact = v2fact;
71 cvm_module_outbuflen = 0;
72 outbufptr = cvm_module_outbuffer;
73 v2fact(0, (char*)cvm_module_inbuffer+2, cvm_module_inbuffer[1]);
74 }
75
cvm_module_fact_start(void)76 void cvm_module_fact_start(void)
77 {
78 if (cvm_module_inbuffer[0] == CVM2_PROTOCOL)
79 cvm2_fact_start();
80 else
81 cvm1_fact_start();
82 }
83
cvm_module_fact_str(unsigned number,const char * data)84 int cvm_module_fact_str(unsigned number, const char* data)
85 {
86 if (!data) return 0;
87 return fact(number, data, strlen(data));
88 }
89
cvm_module_fact_end(unsigned code)90 void cvm_module_fact_end(unsigned code)
91 {
92 if (cvm_module_outbuflen >= BUFSIZE)
93 code = CVME_BAD_MODDATA;
94 cvm_module_outbuffer[0] = code;
95 *outbufptr++ = 0;
96 ++cvm_module_outbuflen;
97 }
98
cvm_module_fact_uint(unsigned number,unsigned long data)99 int cvm_module_fact_uint(unsigned number, unsigned long data)
100 {
101 char buf[64];
102 char* ptr;
103
104 if (!data)
105 return fact(number, "0", 1);
106 ptr = buf + 63;
107 *ptr-- = 0;
108 while (data) {
109 *ptr-- = (data % 10) + '0';
110 data /= 10;
111 }
112 ++ptr;
113 return fact(number, ptr, buf+63-ptr);
114 }
115