vfs: check userland buffers before reading them.
[haiku.git] / src / apps / icon-o-matic / import_export / flat_icon / RDefExporter.cpp
blobc1db24bf802450bc289bf2e104b7ee0e83500569
1 /*
2 * Copyright 2006, Haiku. All rights reserved.
3 * Distributed under the terms of the MIT License.
5 * Authors:
6 * Stephan Aßmus <superstippi@gmx.de>
7 */
9 #include "RDefExporter.h"
11 #include <stdio.h>
12 #include <string.h>
14 #include <DataIO.h>
16 // constructor
17 RDefExporter::RDefExporter()
18 : FlatIconExporter()
22 // destructor
23 RDefExporter::~RDefExporter()
27 // Export
28 status_t
29 RDefExporter::Export(const Icon* icon, BPositionIO* stream)
31 BMallocIO buffer;
32 status_t ret = FlatIconExporter::Export(icon, &buffer);
33 if (ret < B_OK)
34 return ret;
36 return _Export((const uint8*)buffer.Buffer(), buffer.BufferLength(), stream);
39 // MIMEType
40 const char*
41 RDefExporter::MIMEType()
43 return "text/x-vnd.Be.ResourceDef";
46 // #pragma mark -
48 // _Export
49 status_t
50 RDefExporter::_Export(const uint8* source, size_t sourceSize, BPositionIO* stream) const
52 char buffer[2048];
53 // write header
54 sprintf(buffer, "\nresource(<your resource id here>) #'VICN' array {\n");
55 size_t size = strlen(buffer);
57 ssize_t written = stream->Write(buffer, size);
58 if (written < 0)
59 return (status_t)written;
60 if (written < (ssize_t)size)
61 return B_ERROR;
63 status_t ret = B_OK;
64 const uint8* b = source;
66 // print one line (32 values)
67 while (sourceSize >= 32) {
68 sprintf(buffer, " $\"%.2X%.2X%.2X%.2X"
69 "%.2X%.2X%.2X%.2X"
70 "%.2X%.2X%.2X%.2X"
71 "%.2X%.2X%.2X%.2X"
72 "%.2X%.2X%.2X%.2X"
73 "%.2X%.2X%.2X%.2X"
74 "%.2X%.2X%.2X%.2X"
75 "%.2X%.2X%.2X%.2X\"\n",
76 b[0], b[1], b[2], b[3],
77 b[4], b[5], b[6], b[7],
78 b[8], b[9], b[10], b[11],
79 b[12], b[13], b[14], b[15],
80 b[16], b[17], b[18], b[19],
81 b[20], b[21], b[22], b[23],
82 b[24], b[25], b[26], b[27],
83 b[28], b[29], b[30], b[31]);
85 size = strlen(buffer);
86 written = stream->Write(buffer, size);
87 if (written != (ssize_t)size) {
88 if (written >= 0)
89 ret = B_ERROR;
90 else
91 ret = (status_t)written;
92 break;
95 sourceSize -= 32;
96 b += 32;
98 // beginning of last line
99 if (ret >= B_OK && sourceSize > 0) {
100 sprintf(buffer, " $\"");
101 size = strlen(buffer);
102 written = stream->Write(buffer, size);
103 if (written != (ssize_t)size) {
104 if (written >= 0)
105 ret = B_ERROR;
106 else
107 ret = (status_t)written;
110 // last line (up to 32 values)
111 bool endQuotes = sourceSize > 0;
112 if (ret >= B_OK && sourceSize > 0) {
113 for (size_t i = 0; i < sourceSize; i++) {
114 sprintf(buffer, "%.2X", b[i]);
115 size = strlen(buffer);
116 written = stream->Write(buffer, size);
117 if (written != (ssize_t)size) {
118 if (written >= 0)
119 ret = B_ERROR;
120 else
121 ret = (status_t)written;
122 break;
126 if (ret >= B_OK) {
127 // finish
128 sprintf(buffer, endQuotes ? "\"\n};\n" : "};\n");
129 size = strlen(buffer);
130 written = stream->Write(buffer, size);
131 if (written != (ssize_t)size) {
132 if (written >= 0)
133 ret = B_ERROR;
134 else
135 ret = (status_t)written;
139 return ret;