Commits

FennecFox  committed 1c9c49d

Fucked up version which doesn't output anything

  • Participants
  • Parent commits cfebe4e

Comments (0)

Files changed (3)

 #include <stdio.h>
 #include <string.h>
 #include <assert.h>
-#include <string.h>
 
 #include "flag.h"
 
 
 void serveLibya (int socket) {
     
-    bmp libyanFlag;
-    
-    libyanFlag.width = 64 * ENLARGMENT_FACTOR;
-    libyanFlag.height = 32 * ENLARGEMENT_FACTOR;
-    libyanFlag.colorsPerPixel = 3;
-    libyanFlag.pixelArraySize = libyanFlag.width * libyanFlag.height *
-    libyanFlag.colorsPerPixel;
-    libyanFlag.fileSize = HEADER_SIZE * libyanFlag.pixelArraySize;
-    
+    imageHeader libyanFlagHeader = createImageHeader 
+                                   (64 * ENLARGEMENT_FACTOR,
+                                    32 * ENLARGEMENT_FACTOR);
+    int i = 0, j;
+    int curPixel = 0;
+    char pixelArray[libyanFlagHeader.dib.pixelArraySize];                 
+    pixelColor greenPixel;
     
     printf ("Old Libyan flag requested...\n");
-    serveHTTPHeader (socket, "200 OK", libyanFlag.fileSize,
+    serveHTTPHeader (socket, "200 OK", libyanFlagHeader.bmp.fileSize,
                      "image/bmp");
-     
-    int i = 0, j;
-    int curPixel = 0;
-    char pixelArray[libyanFlag.pixelArraySize];                 
-    pixelColor greenPixel;
-    
+                     
     greenPixel.red = 51;
     greenPixel.green = 153;
     greenPixel.blue = 51;
   
-    while (i < libyanFlag.height) {
+    while (i < libyanFlagHeader.dib.width) {
         j = 0;
-                         
-        while (j < libyanFlag.width) {
-            memcpy(pixelArray[curPixel], &greenPixel, 
+        
+        while (j < libyanFlagHeader.dib.height) {
+            memcpy(&pixelArray[curPixel], &greenPixel, 
                    sizeof (greenPixel)); 
             j++;
-            curPixel += libyanFlag.colorsPerPixel;
+            curPixel += 3;
         }
                          
         i++;
     }
                      
-    serveBitmapHeader (libyanFlag, socket);
-    write (socket, pixels, sizeof (pixels));                 
+    serveBitmapHeader (libyanFlagHeader, socket);
+    write (socket, pixelArray, sizeof (pixelArray));                 
     printf ("Old Libyan flag sent!\n");
    
 }
 
 void serveDenmark (int socket) {
+    
+    imageHeader danishFlagHeader = createImageHeader 
+                                   (37 * ENLARGEMENT_FACTOR,
+                                    28 * ENLARGEMENT_FACTOR);
+    pixelColor redPixel;
+    pixelColor whitePixel; 
+    
+    char pixelArray[danishFlagHeader.dib.pixelArraySize]; 
+    int x, y;
+    int curPixel = 0;
+                
+    
     printf("Danish flag requested...\n");
-    serveHTTPHeader (socket, "200 OK", FILE_SIZE, "image/bmp");
-    
-    pixelColor redPixel;
-    pixelColor whitePixel;
-    
+    serveHTTPHeader (socket, "200 OK", danishFlagHeader.bmp.fileSize,
+                     "image/bmp");
+   
     redPixel.red = 255;
     redPixel.green = 0;
     redPixel.blue = 0;
     whitePixel.red = 255;
     whitePixel.green = 255;
     whitePixel.blue = 255;
-    
-    char pixelArraylibyanFlag.pixelArraySize];  
-    
-    int width = 37 * ENLARGEMENT_FACTOR;
-    int height = 28 * ENLARGEMENT_FACTOR;
-    
-    int x, y;
+   
     x = 0;
     y = 0;
-    while (y <= height) {
     
-        while (x <= width) {    
-            if (x > 12 && x < 17) || (y >12 && y < 17) {
+    while (y < danishFlagHeader.dib.height) {
+    
+        while (x < danishFlagHeader.dib.width) {    
+            if ((x > 12 && x < 17) || (y >12 && y < 17)) {
                      
-                memcpy(pixelArray[curPixel], &whitePixel, 
+                memcpy(&pixelArray[curPixel], &whitePixel, 
                        sizeof (whitePixel)); 
             } else {
-                memcpy(pixelArray[curPixel], &redPixel, 
+                memcpy(&pixelArray[curPixel], &redPixel, 
                        sizeof (redPixel));
 
             }
-            curPixel+= COLORS PER PIXEL;
-            x+=;
+            curPixel += 3;
+            x++;
         }
     y++;
     }
+    serveBitmapHeader (danishFlagHeader, socket);
+    write (socket, pixelArray, sizeof (pixelArray));
+    printf ("Danish flag sent!\n");
 }
 
-void serveBitmapHeader (bmp imageInfo, int socket) {
-    char header[HEADER_SIZE] = {
-        
-        // BMP header
-        0x42, 0x4D,                     // file magic
-        0x00, 0x00, 0x00, 0x00,         // file size
-        0x00, 0x00, 0x00, 0x00,         // unused
-        
-        // pixel array offset
-        HEADER_SIZE, 0x00, 0x00, 0x00,
-        
-        // DIB header
-        0x28, 0x00, 0x00, 0x00,         // number of bytes in DIB
-        0x00, 0x00, 0x00, 0x00,         // width
-        0x00, 0x00, 0x00, 0x00,         // height
-        0x01, 0x00,                     // num color planes
-        0x18, 0x00,                     // bits per pixel
-        0x00, 0x00, 0x00, 0x00,         // no compression
-        0x02, 0x10, 0x00, 0x00,         // size of pixel array
-        0x12, 0x0B, 0x00, 0x00,         // horizontal res
-        0x12, 0x0B, 0x00, 0x00,         // vertical res
-        0x00, 0x00, 0x00, 0x00,         // palette size (not used)
-        0x00, 0x00, 0x00, 0x00          // all colors important
-    };
+// creates a basic BMP image header, filling in default values
+// for resolution, color planes and so on.
+imageHeader createImageHeader (int width, int height) {
+    imageHeader header;
+    bmpHeader bmpInfo;
+    dibHeader dibInfo;
     
-    // fill in data
+    int headerSize, fileSize;
+    int pixelArraySize = width * height * 3;
     
-    memcpy (header + 0x02, &imageInfo.fileSize, 4);
-    memcpy (header + 0x12, &imageInfo.width, 4);
-    memcpy (header + 0x16, &imageInfo.height, 4);
-    memcpy (header + 0x22, &imageInfo.pixelArraySize, 4);
+    headerSize = sizeof(imageHeader);
+    fileSize = headerSize + pixelArraySize;
     
-    write (socket, header, HEADER_SIZE);
+    /*bmpInfo.fileMagicA = 'B';
+    bmpInfo.fileMagicB = 'M';*/
+    bmpInfo.fileMagicFiller = 16973;
+    bmpInfo.fileSize = fileSize;
+    
+    // pixel array always lies after the headers
+    bmpInfo.pixelArrayOffset = headerSize;
+    
+    dibInfo.width = width;
+    dibInfo.height = height;
+    dibInfo.pixelArraySize = pixelArraySize;
+    
+    // and setup our defaults
+    dibInfo.colorPlanes = 1;
+    dibInfo.bitsPerPixel = 24;
+    dibInfo.horizResolution = 2835; // pixels / meter
+    dibInfo.vertResolution = 2835;
+    
+    header.bmp = bmpInfo;
+    header.dib = dibInfo;
+    
+    return header;
 }
+
+void serveBitmapHeader (imageHeader imageInfo, int socket) {
+    char *ptr = (char*)&imageInfo;
+    write (socket, ptr, sizeof (imageInfo));
+}
+
 #define FALSE 0
 #define TRUE 1
 
+#include <sys/types.h>
+
 // server data
 #define MAX_PATH_LENGTH         2048
 #define SIMPLE_SERVER_VERSION   1.0
 #define DIB_HEADER_SIZE     40
 #define HEADER_SIZE         (BMP_HEADER_SIZE + DIB_HEADER_SIZE)
 
+typedef struct _bmpHeader {
+    /*unsigned char fileMagicA;
+    unsigned char fileMagicB;*/
+    unsigned short fileMagicFiller;
+    
+    unsigned int fileSize;
+    unsigned int applicationSpecific;
+    unsigned int pixelArrayOffset;
+} bmpHeader;
+
+typedef struct _dibHeader {
+    unsigned int dibHeaderLength;
+    int width;
+    int height;
+    
+    unsigned short colorPlanes;
+    unsigned short bitsPerPixel;
+    unsigned int compressionType;
+    unsigned int pixelArraySize;
+    
+    unsigned int horizResolution;
+    unsigned int vertResolution;
+    
+    unsigned int numColors;
+    unsigned int importantColors;
+} dibHeader;
+
 typedef struct _pixelColor {
     int blue;
     int green;
     int red;
 } pixelColor;
 
-typedef struct _bmp {
-    int width;
-    int height;
-    int colorsPerPixel;
-    int pixelArraySize;
-    int fileSize;
-} bmp;
+typedef struct _imageHeader {
+    bmpHeader bmp;
+    dibHeader dib;
+} imageHeader;
 
 // server functions 
 int waitForConnection  (int serverSocket);
 void serveHTTPHeader   (int socket, char *statusMessage, int length,
                         char *contentType);
 void serveImage        (int serverSocket, char *path, int pathLen);
-void serveBitmapHeader (bmp imageInfo, int socket);
+void serveBitmapHeader (imageHeader imageInfo, int socket);
+imageHeader createImageHeader (int width, int height);
 void serveHTML         (int serverSocket, char *path, int pathLen);
 void serveLibya        (int socket);
 void serveDenmark      (int socket);
 #include <netinet/in.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
 #include <assert.h>
-#include <unistd.h>
-#include <string.h>
 
 #include "flag.h"
 
     char message[MAX_HEADER_SIZE];
     char *format = "HTTP/1.0 %s\n"
         "Content-Type: %s\n"
-        "Content-Length: %o\n" //length in octets
+        "Content-Length: %d\n" //length in octets
         "\n";
   
     snprintf (message, sizeof (message), format, statusMessage,