Skip to content
Permalink
Browse files

Add low level syscall, also update error handling

  • Loading branch information
heapsmash committed Oct 22, 2019
1 parent da002db commit d33a98b2f357f9df1929e71f94104580b8637119
Showing with 243 additions and 185 deletions.
  1. BIN BMPfileFormat.png
  2. 0 CMakeLists.txt
  3. BIN badguy.bmp
  4. +167 −82 bmp.c
  5. +9 −34 bmp.h
  6. +6 −0 make.sh
  7. +28 −0 sys.s
  8. +17 −36 syscalls.c
  9. +16 −33 syscalls.h
0 BMPfileFormat.png 100644 → 100755
No changes.
0 CMakeLists.txt 100644 → 100755
No changes.
BIN +126 Bytes badguy.bmp
Binary file not shown.
249 bmp.c 100644 → 100755
@@ -1,29 +1,9 @@
/* Copyright (c) 2019, Tofu von Helmholtz aka Michael S. Walker
* All Rights Reserved in all Federations, including Alpha Centauris.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are 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.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation are those
* of the authors and should not be interpreted as representing official policies,
* either expressed or implied, of the BITMAP ENDIAN project.
/* BitRip Copyleft !(c) 2019, Michael S. Walker <sigmatau@heapsmash.com>
* All Rights Unreserved in all Federations, including Alpha Centauris.
*
* chmod ugo+rwx make.sh
* sh make.sh
* ./bit_rip -n 2 -f badguy.bmp -a badguy -s badguy.c
*/

#pragma clang diagnostic push
@@ -35,15 +15,21 @@
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <zconf.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/syscall.h>

#include "syscalls.h"
#include "bmp.h"

#define _VERBOSE 0 /* For error output (1) is very verbose */

int main(int argc, char *argv[], char *envp[])
{
int opt;
void (*WriteDataToArray)(BitRipTools *) = NULL;
int opt,
return_status = 0, /* success */
(*WriteDataToArray)(BitRipTools *) = NULL;

BitRipTools bitmap;

bitmap.ncols_per_row = 1;
@@ -78,105 +64,169 @@ int main(int argc, char *argv[], char *envp[])
/* sanity check */
if ((errno == ERANGE && (bitmap.ncols_per_row == LONG_MAX || bitmap.ncols_per_row == LONG_MIN)) ||
(errno != 0 && bitmap.ncols_per_row == 0)) {
PRINT_ERRNO_AND_RETURN("-n value obfuscated")
PRINT_ERRNO_AND_RETURN("-n value spurious")
}

bitmap.bitmap_to_read_fd = Open(bitmap.file_name_to_read, O_RDONLY, 0);
bitmap.c_file_to_write_fd = Open(bitmap.file_name_to_save, O_RDWR | O_CREAT | O_TRUNC, UMASK);

ReadBitmapHeader(&bitmap);
if (ReadBitmapHeader(&bitmap) < 0 ) {
#if _VERBOSE == 1
PRINT_ERR("ReadBitmapHeader failed")
#endif // _VERBOSE
return_status = 1; /* fail */

goto close_fd;
}

switch (bitmap.header.width_px) {
case 16:
WriteDataToArray = ReadDataUint16;
WriteDataToArray = WriteDataUint16;
break;
case 8:
WriteDataToArray = ReadDataUint8;
WriteDataToArray = WriteDataUint8;
break;
default:
WriteDataToArray = ReadDataUint32;
WriteDataToArray = WriteDataUint32;
break;
}

WriteCommentToFile(bitmap);
WriteDataToArray(&bitmap);
if (WriteDataToArray(&bitmap) < 0) {
#if _VERBOSE == 1
PRINT_ERR("WriteDataToArray failed")
#endif //_VERBOSE == 1

return_status = 1; /* fail */
}

close_fd:
Close(bitmap.bitmap_to_read_fd);
Close(bitmap.c_file_to_write_fd);

return 0;
return return_status;
}


void ReadDataUint8(BitRipTools *data)
int WriteDataUint8(BitRipTools *data)
{
int nread;
int nread, write_status = -1;
uint32_t *temp_data = calloc((data->header.height_px * data->header.width_px) + 1, 1);

if (Lseek(data->bitmap_to_read_fd, data->header.offset, SEEK_SET) != data->header.offset) {
PRINT_ERR_AND_EXIT("Lseek failed")
#if _VERBOSE == 1
PRINT_ERRNO("Lseek failed")
#endif //_VERBOSE == 1

goto io_error;
}

nread = IoRead(data->bitmap_to_read_fd, temp_data, data->header.image_size_bytes);
if (nread < 0) {
PRINT_ERRNO_AND_EXIT("IoRead error")
#if _VERBOSE == 1
PRINT_ERRNO("IoRead error")
#endif //_VERBOSE == 1

goto io_error;
}

nread >>= 2;
Print(data->c_file_to_write_fd, "const unsigned int g_%s_height = %d; /* height in bits */ \n", data->array_name_to_store, data->header.height_px);
Print(data->c_file_to_write_fd, "const unsigned int g_%s_width = %d; /* width in bits */\n", data->array_name_to_store, data->header.width_px);
Print(data->c_file_to_write_fd, "const unsigned int g_%s_size = %d; /* total bytes */\n\n", data->array_name_to_store, data->header.image_size_bytes);

Print(data->c_file_to_write_fd, "const uint%d %s[] = {\n", data->header.width_px, data->array_name_to_store);
for (nread = nread - 1; nread >= 0; nread--) {
Print(data->c_file_to_write_fd, "const uint%d_t %s[] = {\n", data->header.width_px, data->array_name_to_store);
for (nread = nread - 1; nread > 0; nread--) {
temp_data[nread] = ~temp_data[nread];
temp_data[nread] = temp_data[nread] & 0x000000ff;
printf("0x%02x,\n", temp_data[nread]);
}
Print(data->c_file_to_write_fd, "\t0x%02x,", temp_data[nread]);
if (nread % data->ncols_per_row == 0)
Print(data->c_file_to_write_fd, "\n");
}

Print(data->c_file_to_write_fd, "\t0x%04x\n", temp_data[nread]);
Print(data->c_file_to_write_fd, "}; /* Generated with BitRip <sigmatau@heapsmash.com> */ \n\n");
write_status = 0;

io_error:
return write_status;
}


void ReadDataUint16(BitRipTools *data)
int WriteDataUint16(BitRipTools *data)
{
int nread;
int nread, write_status = -1;
uint32_t *temp_data = calloc((data->header.height_px * data->header.width_px) + 1, 1);

if (Lseek(data->bitmap_to_read_fd, data->header.offset, SEEK_SET) != data->header.offset) {
PRINT_ERR_AND_EXIT("Lseek failed")
#if _VERBOSE == 1
PRINT_ERRNO("Lseek failed")
#endif //_VERBOSE == 1

goto io_error;
}

nread = IoRead(data->bitmap_to_read_fd, temp_data, data->header.image_size_bytes);
if (nread < 0) {
PRINT_ERRNO_AND_EXIT("IoRead error")
#if _VERBOSE == 1
PRINT_ERRNO("IoRead error")
#endif //_VERBOSE == 1

goto io_error;
}

nread >>= 2;
Print(data->c_file_to_write_fd, "const unsigned int g_%s_height = %d; /* height in bits */ \n", data->array_name_to_store, data->header.height_px);
Print(data->c_file_to_write_fd, "const unsigned int g_%s_width = %d; /* width in bits */\n", data->array_name_to_store, data->header.width_px);
Print(data->c_file_to_write_fd, "const unsigned int g_%s_size = %d; /* total bytes */\n\n", data->array_name_to_store, data->header.image_size_bytes);

Print(data->c_file_to_write_fd, "const uint%d %s[] = {\n", data->header.width_px, data->array_name_to_store);
for (nread = nread - 1; nread >= 0; nread--) {
Print(data->c_file_to_write_fd, "const uint%d_t %s[] = {\n", data->header.width_px, data->array_name_to_store);
for (nread = nread - 1; nread > 0; nread--) {
temp_data[nread] = ENDIAN16(temp_data[nread]);
temp_data[nread] = ~temp_data[nread];
temp_data[nread] &= 0x0000ffff;
printf("0x%04x,\n", temp_data[nread]);
Print(data->c_file_to_write_fd, "\t0x%04x,", temp_data[nread]);
if (nread % data->ncols_per_row == 0)
Print(data->c_file_to_write_fd, "\n");
}

Print(data->c_file_to_write_fd, "\t0x%04x\n", temp_data[nread]);
Print(data->c_file_to_write_fd, "}; /* Generated with BitRip <sigmatau@heapsmash.com> */ \n\n");
write_status = 0;

io_error:
return write_status;
}


void ReadDataUint32(BitRipTools *data)
int WriteDataUint32(BitRipTools *data)
{
int nread;
int nread, write_status = -1;
uint32_t *temp_data = calloc((data->header.height_px * data->header.width_px) + 1, 1);

if (Lseek(data->bitmap_to_read_fd, data->header.offset, SEEK_SET) != data->header.offset) {
PRINT_ERR_AND_EXIT("Lseek failed")
#if _VERBOSE == 1
PRINT_ERRNO("Lseek failed")
#endif //_VERBOSE == 1

goto io_error;
}

nread = IoRead(data->bitmap_to_read_fd, temp_data, data->header.image_size_bytes);
if (nread < 0) {
PRINT_ERRNO_AND_EXIT("IoRead error")
#if _VERBOSE == 1
PRINT_ERRNO("IoRead error")
#endif //_VERBOSE == 1

goto io_error;
}

nread >>= 2;
Print(data->c_file_to_write_fd, "const unsigned int g_%s_height = %d;\t/* height in bits */ \n", data->array_name_to_store,
data->header.height_px);
Print(data->c_file_to_write_fd, "const unsigned int g_%s_width = %d;\t/* width in bits */\n", data->array_name_to_store, data->header.width_px);
Print(data->c_file_to_write_fd, "const unsigned int g_%s_size = %d;\t/* total bytes */\n\n", data->array_name_to_store,
data->header.image_size_bytes);
Print(data->c_file_to_write_fd, "const unsigned int g_%s_height = %d; /* height in bits */ \n", data->array_name_to_store, data->header.height_px);
Print(data->c_file_to_write_fd, "const unsigned int g_%s_width = %d; /* width in bits */\n", data->array_name_to_store, data->header.width_px);
Print(data->c_file_to_write_fd, "const unsigned int g_%s_size = %d; /* total bytes */\n\n", data->array_name_to_store, data->header.image_size_bytes);

Print(data->c_file_to_write_fd, "const uint32_t %s[] = {\n", data->array_name_to_store);
Print(data->c_file_to_write_fd, "const uint%d_t %s[] = {\n", data->header.width_px, data->array_name_to_store);
for (nread = nread - 1; nread > 0; nread--) {
temp_data[nread] = ENDIAN32(temp_data[nread]);
temp_data[nread] = ~temp_data[nread];
@@ -186,29 +236,64 @@ void ReadDataUint32(BitRipTools *data)
}

Print(data->c_file_to_write_fd, "\t0x%08x\n", temp_data[nread]);
Print(data->c_file_to_write_fd, "}; /* Generated with BitRip */ \n\n");
Print(data->c_file_to_write_fd, "}; /* Generated with BitRip <sigmatau@heapsmash.com> */ \n\n");
write_status = 0;

io_error:
return write_status;
}


void ReadBitmapHeader(BitRipTools *data)
int ReadBitmapHeader(BitRipTools *data)
{
/* (54 bytes) */
IoRead(data->bitmap_to_read_fd, &data->header.type, 2);
IoRead(data->bitmap_to_read_fd, &data->header.size, 4);
IoRead(data->bitmap_to_read_fd, &data->header.reserved1, 2);
IoRead(data->bitmap_to_read_fd, &data->header.reserved2, 2);
IoRead(data->bitmap_to_read_fd, &data->header.offset, 4);
IoRead(data->bitmap_to_read_fd, &data->header.dib_header_size, 4);
IoRead(data->bitmap_to_read_fd, &data->header.width_px, 4);
IoRead(data->bitmap_to_read_fd, &data->header.height_px, 4);
IoRead(data->bitmap_to_read_fd, &data->header.num_planes, 2);
IoRead(data->bitmap_to_read_fd, &data->header.bits_per_pixel, 2);
IoRead(data->bitmap_to_read_fd, &data->header.compression, 4);
IoRead(data->bitmap_to_read_fd, &data->header.image_size_bytes, 4);
IoRead(data->bitmap_to_read_fd, &data->header.x_resolution_ppm, 4);
IoRead(data->bitmap_to_read_fd, &data->header.y_resolution_ppm, 4);
IoRead(data->bitmap_to_read_fd, &data->header.num_colors, 4);
IoRead(data->bitmap_to_read_fd, &data->header.important_colors, 4);
if (IoRead(data->bitmap_to_read_fd, &data->header.type, 2) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.size, 4) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.reserved1, 2) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.reserved2, 2) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.offset, 4) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.dib_header_size, 4) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.width_px, 4) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.height_px, 4) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.num_planes, 2) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.bits_per_pixel, 2) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.compression, 4) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.image_size_bytes, 4) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.x_resolution_ppm, 4) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.y_resolution_ppm, 4) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.num_colors, 4) < 0)
return -1;

if (IoRead(data->bitmap_to_read_fd, &data->header.important_colors, 4) < 0)
return -1;
}


@@ -234,10 +319,10 @@ void WriteCommentToFile(BitRipTools data)
data.header.important_colors
};

Print(data.c_file_to_write_fd, "/*\tBitRip Copyright (c) 2019, Michael S. Walker <sigmatau@heapsmash.com>\n");
Print(data.c_file_to_write_fd, " *\tAll Rights Reserved in all Federations, including Alpha Centauris.\n");
Print(data.c_file_to_write_fd, "/*\tBitRip Copyleft !(c) 2019, Michael S. Walker <sigmatau@heapsmash.com>\n");
Print(data.c_file_to_write_fd, " *\tAll Rights Unreserved in all Federations, including Alpha Centauris.\n");
Print(data.c_file_to_write_fd, " *\n");
Print(data.c_file_to_write_fd, " *\t.--.https://github.com/heapsmash/Monochrome-Bitmap-Converter-----------.\n");
Print(data.c_file_to_write_fd, " *\t.--. https://github.com/heapsmash/Monochrome-Bitmap-Converter ---------.\n");
Print(data.c_file_to_write_fd, " *\t| | Header Name | Value | Description |\n");
Print(data.c_file_to_write_fd, " *\t:--+-------------------+-------+---------------------------------------:\n");
Print(data.c_file_to_write_fd, " *\t| | %-17s | %-5x | %-37s |\n", g_header_names[0], header_values[0], g_header_description[0]);

0 comments on commit d33a98b

Please sign in to comment.
You can’t perform that action at this time.