]> Pileus Git - aweather/blobdiff - src/wsr88ddec.c
Convert GtkBox and GtkScale to GTK 3 version
[aweather] / src / wsr88ddec.c
index 12adaa1256f663eb849d60a5b2c9656d49bc641d..9db0589c89dfa863f8d65fd8fdcd4091a0fa2c7b 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2009 Andy Spencer <spenceal@rose-hulman.edu>
+ * Copyright (C) 2009-2011 Andy Spencer <andy753421@gmail.com>
  * 
  * 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
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdint.h>
-#include <err.h>
-#include <arpa/inet.h>
+#include <glib.h>
 #include <bzlib.h>
 
-#define debug(...) fprintf(stderr, __VA_ARGS__);
+#define SANITY_MAX_SIZE 50*1024*1024 // 50 MB/bzip
 
 char *bunzip2(char *input, int input_len, int *output_len)
 {
-       bz_stream *stream = calloc(1, sizeof(bz_stream));
+       bz_stream *stream = g_new0(bz_stream, 1);
 
        switch (BZ2_bzDecompressInit(stream, 0, 0)) {
-       case BZ_CONFIG_ERROR: err(1, "the library has been mis-compiled");
-       case BZ_PARAM_ERROR:  err(1, "Parameter error");
-       case BZ_MEM_ERROR:    err(1, "insufficient memory is available");
-       //case BZ_OK:           debug("success\n"); break;
-       //default:              debug("unknown\n"); break;
+       case BZ_CONFIG_ERROR: g_error("the library has been mis-compiled");
+       case BZ_PARAM_ERROR:  g_error("Parameter error");
+       case BZ_MEM_ERROR:    g_error("insufficient memory is available");
+       //case BZ_OK:           g_debug("success"); break;
+       //default:              g_debug("unknown"); break;
        }
 
        int   status;
@@ -44,22 +40,22 @@ char *bunzip2(char *input, int input_len, int *output_len)
                stream->next_in   = input       + stream->total_in_lo32;
                stream->avail_in  = input_len   - stream->total_in_lo32;
                output_size *= 2;
-               output       = realloc(output, output_size);
-               //debug("alloc %d\n", output_size);
+               output       = g_realloc(output, output_size);
+               //g_debug("alloc %d", output_size);
                stream->next_out  = output      + stream->total_out_lo32;
                stream->avail_out = output_size - stream->total_out_lo32;
-               //debug("decompressing..\n"
+               //g_debug("decompressing..\n"
                //      "  next_in   = %p\n"
                //      "  avail_in  = %u\n"
                //      "  next_out  = %p\n"
-               //      "  avail_out = %u\n",
+               //      "  avail_out = %u",
                //      stream->next_in,
                //      stream->avail_in,
                //      stream->next_out,
                //      stream->avail_out);
-       } while ((status = BZ2_bzDecompress(stream)) == BZ_OK && output_size < 1024*1024);
+       } while ((status = BZ2_bzDecompress(stream)) == BZ_OK && output_size < SANITY_MAX_SIZE);
 
-       // debug("done with status %d = %d\n", status, BZ_STREAM_END);
+       //g_debug("done with status %d = %d", status, BZ_STREAM_END);
 
        *output_len = stream->total_out_lo32;
        BZ2_bzDecompressEnd(stream);
@@ -69,44 +65,49 @@ char *bunzip2(char *input, int input_len, int *output_len)
 int main(int argc, char **argv)
 {
        if (argc != 3) {
-               printf("usage: %s <input> <output>\n", argv[0]);
+               g_print("usage: %s <input> <output>\n", argv[0]);
                return 0;
        }
 
-       FILE *input  = fopen(argv[1], "r");
-       FILE *output = fopen(argv[2], "w+");
-       if (!input)  err(1, "error opening input");
-       if (!output) err(1, "error opening output");
+       FILE *input  = fopen(argv[1], "rb");
+       FILE *output = fopen(argv[2], "wb+");
+       if (!input)  g_error("error opening input");
+       if (!output) g_error("error opening output");
 
        int st;
        int size = 0;
-       char *buf = malloc(24);
+       char *buf = g_malloc(24);
 
        /* Clear header */
-       //debug("reading header\n");
-       fread (buf, 1, 24, input);
-       fwrite(buf, 1, 24, output);
+       //g_debug("reading header");
+       if (!fread (buf, 24, 1, input))
+               g_error("error reading header");
+       if (!fwrite(buf, 24, 1, output))
+               g_error("error writing header");
 
-       //debug("reading body\n");
+       //g_debug("reading body");
        while ((st = fread(&size, 1, 4, input))) {
-               //debug("size=%08x\n", size);
-               //debug("read %u bytes\n", st);
+               //g_debug("size=%08x", size);
+               //g_debug("read %u bytes", st);
                //fwrite(&size, 1, 4, output); // DEBUG
-               size = abs(ntohl(size));
+               size = ABS(g_ntohl(size));
                if (size < 0)
                        return 0;
-               //debug("size = %x\n", size);
-               if (size > 20*1024*1024)
-                       err(1, "sanity check failed, buf is to big: %d", size);
-               buf = realloc(buf, size);
-               fread (buf, 1, size, input);
+               //g_debug("size = %x", size);
+               if (size > SANITY_MAX_SIZE)
+                       g_error("sanity check failed, buf is to big: %d", size);
+               buf = g_realloc(buf, size);
+               if (fread(buf, 1, size, input) != size)
+                       g_error("error reading data");
                //fwrite(buf, 1, size, output); // DEBUG
 
                int dec_len;
                char *dec = bunzip2(buf, size, &dec_len);
-               // debug("decompressed %u bytes\n", dec_len);
-               fwrite(dec, 1, dec_len, output);
-               free(dec);
+               //g_debug("decompressed %u bytes", dec_len);
+               if (fwrite(dec, 1, dec_len, output) != dec_len)
+                       g_error("error writing data");
+               g_free(dec);
+               //g_debug("decompressed %-6x -> %x", size, dec_len);
        }
 
        return 0;