/* WebCamNow wcn and jpg decoder 0.1 by Luigi Auriemma e-mail: aluigi@autistici.org web: aluigi.org INTRODUCTION ------------ This file contains the two functions needed for decoding the .wcn files and the images located on the webcamnow.com server. Note that these functions are based on the free WebCamNow version. FUNCTIONS --------- uint8_t *webcamnow_wcn_unpack(uint8_t *data, int *size); this function decodes the wcn files usually available here: http://webcamnow.com/j11.wcn family webcams http://webcamnow.com/j22.wcn unmonitored webcams All you need to do is passing the buffer containing the data of the wcn file and its size, the function will return the buffer containing the plain-text content and will update the size value: buffer = webcamnow_wcn_unpack(wcn_buffer, &wcn_buffer_size); it returns NULL in case of errors, like wrong or non-ZIP data. the plain-text wcn file is very simple to handle, it's composed by some HTML comments you need to skip and has the following structure: number_of_webcams|webcam1|webcam2|webcam3|...|webcamN webcam1_ID|webcam2_ID|webcam3_ID|...|webcamN_ID void webcamnow_jpg_decoder(uint8_t *data, int size) when you want to see a webcam, all you need to do is connecting to webcamnow.com on port 4444 (or on another of those opened) and request the ID of the webcam you want to see using the command "look": look 12345 then you will receive a HTTP-like error code (200 if the webcam is up) followed by the amount of bytes to receive. The bytes you will receive are a JPG image you need to decode: webcamnow_jpg_decoder(jpg_buffer, jpg_buffer_size); note that the connection will stay up and you can send other requests and you will receive the next frame automatically. LICENSE ------- Copyright 2007 Luigi Auriemma This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version. This program 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 General Public License for more details. You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA http://www.gnu.org/licenses/gpl.txt */ #include #include #include #include #include /* unfortunately uncompress() supports only */ /* a windowsize of 15 while we need -15 */ uint8_t *webcamnow_unzip(uint8_t *in, int *insz) { z_stream z; int outsz, err; uint8_t *out; z.zalloc = (alloc_func)0; z.zfree = (free_func)0; z.opaque = (voidpf)0; if(inflateInit2(&z, -15)) return(NULL); out = NULL; outsz = *insz * 2; for(;;) { out = realloc(out, outsz); if(!out) return(NULL); z.next_in = in; z.avail_in = *insz; z.next_out = out; z.avail_out = outsz; err = inflate(&z, Z_FINISH); if((err == Z_OK) || (err == Z_STREAM_END)) { break; } else if(err == Z_BUF_ERROR) { inflateEnd(&z); if(inflateInit2(&z, -15)) return(NULL); outsz += *insz; } else { free(out); return(NULL); } } *insz = z.total_out; inflateEnd(&z); out = realloc(out, *insz); if(!out) return(NULL); return(out); } uint8_t *webcamnow_wcn_unpack(uint8_t *data, int *size) { int i, len; uint8_t j1, l1, j2, l2, i3, *p, *o; /* decoding */ p = o = data; j1 = *p++; l1 = *p++; j2 = *p++; l2 = *p++; len = *size - 5; for(i = 0; i < len; i++) { i3 = *p++; *o++ = i3 ^ j1 ^ l1 ^ j2 ^ l2; j1 = i3; } /* decompression */ *size = 0; if(memcmp(data, "\x50\x4b\x03\x04", 4)) return(NULL); len -= 31; data += 31; data = webcamnow_unzip(data, &len); if(!data) return(NULL); *size = len; return(data); } void webcamnow_jpg_decoder(uint8_t *data, int size) { for(; size > 0; size--) { data[size] ^= data[size - 1]; } data[0] ^= 0x7c; }