📄 utils.c
字号:
/*Copyright (c) 2002-2003 Nicolas Jombart - HSCRedistribution and use in source and binary forms, with or withoutmodification, are permitted provided that the following conditionsare met:1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.2. 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.3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission.THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS ORIMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIESOF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUTNOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANYTHEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OFTHIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*//* $Id: utils.c,v 1.14 2004/09/09 11:25:21 jombart Exp $ */#include <stdlib.h>#include <stdio.h>#include <string.h>#include <time.h>#include "wspp.h"/* * Display usage */void usage(char *name) { printf("%s version %s: HTTP to WSP proxy.\n", name, WSPP_VERSION); printf("(c) 2003-2004 HSC\n"); printf("\n"); printf("Usage : %s <GW ADDR> <GW PORT> <LISTEN PORT>\n", name); printf("Default GW port is %d\n", DEFAULT_GW_PORT); printf("Default Listen port is %d\n", DEFAULT_LISTEN_PORT); printf("\n");}/* * Returns unsigned variable length integer * (null terminate the string) */int uintvar(char *octets, int value) {int i, j, c = 0;char enc[6]; /* No continue bit on the last one */ enc[5] = value & 0x7f; value >>= 7; for(i = 4; i >= 0; i--) { enc[i] = 0x80 | (value & 0x7f); value >>= 7; } /* cancel 0x80 (==0) octets */ for(i = 0; i < 6; i++) { if(enc[i] == '\x80') c++; } for(i = c, j = 0; i < 6; i++) { octets[j++] = enc[i]; } octets[j] = '\0'; /* null terminate */ return(j);}int uintvar_len(int value) {int i, j, c = 0;char enc[6];char octets[6]; /* No continue bit on the last one */ enc[5] = value & 0x7f; value >>= 7; for(i = 4; i >= 0; i--) { enc[i] = 0x80 | (value & 0x7f); value >>= 7; } /* cancel 0x80 (==0) octets */ for(i = 0; i < 6; i++) { if(enc[i] == '\x80') c++; } for(i = c, j = 0; i < 6; i++) { octets[j++] = enc[i]; } octets[j] = '\0'; /* null terminate */ return(strlen(octets));}/* * Returns value from "Unsigned variable length integers" */int from_uintvar (char *data, int offset, int max_offset){ int value = 0; int octet; do { octet = data[offset]; offset++; value <<= 7; value += octet & 0x7f; } while ((max_offset > offset) && (octet & 0x80)); return value;}/* * strcat with realloc and make sure end by only one \n */char * restrcat(char *s, const char *append){ char *p; char *tmp; if(s == NULL) { if(append[strlen(append)-1] != '\n') { tmp = (char *) malloc( (strlen(append) + 1) * sizeof(char)); sprintf(tmp, "%s\n", append); s = strdup(tmp); free(tmp); } else { s = strdup(append); } } else { p = (char *) realloc(s, (strlen(append) + strlen(s) + 2) * sizeof(char)); if(p == NULL) return(0); tmp = strdup(append); if(append[strlen(append)-1] == '\n') { tmp[strlen(append) -1] = '\0'; } strcat(p, tmp); free(tmp); strcat(p, "\n"); s = p; } return(s);}/* * Fill an "Error 500" page */void error500(http *body, int error_code) { char tmp[64]; body->code = 500; body->headers = NULL; body->headers = restrcat(body->headers, HTTP_INTERNAL_ERROR); body->headers = restrcat(body->headers, "Content-type: text/html"); body->headers = restrcat(body->headers, HEADER_VIA); body->data_length = strlen(INTERNAL_ERROR_BODY) + 16; body->data = malloc(body->data_length * sizeof(char)); snprintf(body->data, body->data_length, INTERNAL_ERROR_BODY, error_code); /* correct length */ body->data_length = strlen(body->data); sprintf(tmp, "Content-Length: %d", strlen(INTERNAL_ERROR_BODY)); body->headers = restrcat(body->headers, tmp);}/* * Return encoded date as string */char * extime(char *buffer, int size) {int i;size_t *epoch;char *date; date = (char *) malloc(64); epoch = (int *) malloc(sizeof(size_t)); *epoch = 0; for(i = 0; i < size; i++) { *epoch += buffer[i] & 0xFF; if(i < (size -1)) { *epoch <<= 8; } } date = ctime((const time_t *)epoch); return date;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -